// #######################################################################################
// WINDOW LOG FUNCTION ###################################################################
// #######################################################################################

window.log = function(){
  log.history = log.history || [];
  log.history.push(arguments);
  arguments.callee = arguments.callee.caller;  
  if(this.console) console.log( Array.prototype.slice.call(arguments) );
};
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)b[a]=b[a]||c})(window.console=window.console||{});





// #######################################################################################
// PAJINATE PLUGIN #######################################################################
// #######################################################################################

(function($){
/*******************************************************************************************/
// Customized! Based on
// jquery.pajinate.js - version 0.2
// A jQuery plugin for paginating through any number of DOM elements
// 
// Copyright (c) 2010, Wes Nolte (http://wesnolte.com)
// Liscensed under the MIT License (MIT-LICENSE.txt)
// http://www.opensource.org/licenses/mit-license.php
// Created: 2010-04-16 | Updated: 2010-04-26
/*******************************************************************************************/

	$.fn.pajinate = function(options){
		// Set some state information
		var current_page = 'current_page';
		var items_per_page = 'items_per_page';
		var last_page = 'last_page';
		
		var meta;
	
		// Setup default option values
		var defaults = {
			item_container_id : '.pajcontent',
			items_per_page : 5,			
			nav_panel_id : '.page_navigation',		
			start_page : 0,
			nav_label_prev : '&laquo; Vorherige',
			nav_label_next : 'N&auml;chste &raquo;',
			nav_label_page : 'Seite'
		};
		var options = $.extend(defaults,options);
		var $item_container;
		var $page_container;
		var $items;
		var $nav_panels;
	
		return this.each(function(){
			$page_container = $(this);
			$item_container = $(this).find(options.item_container_id);
			$items = $page_container.find(options.item_container_id).children();
			meta = $page_container;
			
			// Initialise meta data
			meta.data(current_page,0);
			meta.data(items_per_page, options.items_per_page);
					
			// Get the total number of items
			var total_items = $item_container.children().size();
			
			// Calculate the number of pages needed
			var number_of_pages = Math.ceil(total_items/options.items_per_page);
			meta.data(last_page,number_of_pages-1);
			
			// Construct the nav bar
			var navigation_html = '<a class="previous_link" href="">'+ options.nav_label_prev +'</a>';
			navigation_html += '<select name="span-'+ $page_container.attr('id') +'" class="styled-grau">';
			var current_link = 0;
			while(number_of_pages > current_link){
				navigation_html += '<option value="' + current_link +'">'+ options.nav_label_page +' '+ (current_link + 1) +'</option>';
				current_link++;
			}
			navigation_html += '</select>';
			navigation_html += '<a class="next_link" href="">'+ options.nav_label_next +'</a>';
						
			// And add it to the appropriate area of the DOM	
			$nav_panels = $page_container.find(options.nav_panel_id);			
			$nav_panels.html(navigation_html);
			
			/* Setup Page Display */
			// And hide all pages
			$items.hide();
			// Show the first page			
			$items.slice(0, meta.data(items_per_page)).show();

			/* Bind the actions to their respective links */
			 
			// Event handler for 'Prev' link
			$page_container.find('.previous_link').click(function(e){
				e.preventDefault();
				showPrevPage($(this));
				selectOption();
			});
			
			// Event handler for 'Next' link
			$page_container.find('.next_link').click(function(e){
				e.preventDefault();				
				showNextPage($(this));
				selectOption();
			});
			
			// Event handler for each 'Page' link
			$page_container.find('select').change(function(e){
				e.preventDefault();
				goto($(this).val());
			});			
			
			// Goto the required page
			goto(parseInt(options.start_page));
		});
		
		function showPrevPage(e){
			new_page = parseInt(meta.data(current_page)) - 1;
			
			// Check that we aren't on a boundary link
			if(meta.data(current_page)>0){
				goto(new_page);
			}
		};
			
		function showNextPage(e){
			new_page = parseInt(meta.data(current_page)) + 1;
			
			// Check that we aren't on a boundary link
			if(meta.data(current_page)<meta.data(last_page)){
				goto(new_page);
			}
		};
		
		function selectOption(){
			var selectBox = $page_container.find(options.nav_panel_id + ' select option[value=' + meta.data(current_page) + ']');
			selectBox.attr('selected', 'selected');
			$page_container.find(options.nav_panel_id + ' #selectspan-'+ $page_container.attr('id')).html(selectBox.text());
		}
			
		function goto(page_num){
			
			var ipp = meta.data(items_per_page);
			
			var isLastPage = false;
			
			// Find the start of the next slice
			start_from = page_num * ipp;
			
			// Find the end of the next slice
			end_on = start_from + ipp;
			// Hide the current page	
			$items.hide()
					.slice(start_from, end_on)
					.show();										 
			
			// Set the current page meta data							
			meta.data(current_page,page_num);
		};	
		
	};
	
})(jQuery);





// #######################################################################################
// CHARACTER COUNTER PLUGIN ##############################################################
// #######################################################################################


jQuery.fn.setCounter = function(maxLength){

  return this.each(function(){

    var jqthis = jQuery(this);

    if (jqthis.is('textarea')){

      var counterId = jqthis.attr('id') + "_counter";

      var htmlCounter = '<div style="position:absolute;bottom:0;left:710px; width:100px;color:#5f5f5f" id="' + counterId + '">';
      htmlCounter += '<span class="length">' + jqthis.val().length + '</span>';

      if (maxLength){
        htmlCounter += '<span class="sep">/</span>';
        htmlCounter += '<span class="maxLength">' + maxLength + '</span>';
      }

      htmlCounter += '</div>';

      jqthis.after(htmlCounter);

      jQuery('#'+counterId).css({
        'text-align':'left',
        'margin-top':'0'
      });

      jqthis.bind("keyup",function(){

        var content = jqthis.val();

        if (maxLength && content.length > maxLength){
          content = content.substring(0,maxLength)
          jqthis.val( content );
        }

        jQuery('#' + counterId + " > span.length").text(content.length);        

      });

    } else {
      alert("Not applicable to element: " + jqthis);
    }
  });
}






// #######################################################################################
// COOKIE PLUGIN #########################################################################
// #######################################################################################

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    
};





// #######################################################################################
// CUSTOM FORM ELEMENTS PLUGIN ###########################################################
// #######################################################################################

/*

## MODIFIED VERSION OF
CUSTOM FORM ELEMENTS

Created by Ryan Fait
www.ryanfait.com

The only things you may need to change in this file are the following
variables: checkboxHeight, radioHeight and selectWidth (lines 24, 25, 26)

The numbers you set for checkboxHeight and radioHeight should be one quarter
of the total height of the image want to use for checkboxes and radio
buttons. Both images should contain the four stages of both inputs stacked
on top of each other in this order: unchecked, unchecked-clicked, checked,
checked-clicked.

You may need to adjust your images a bit if there is a slight vertical
movement during the different stages of the button activation.

The value of selectWidth should be the width of your select list image.

Visit http://ryanfait.com/ for more information.

*/

/* Für eine feste Breite: Diese beiden Zeilen freigeben und die darauf folgende auskommentieren.
   Außerdem im CSS in .select-weiss, .select-grau die Breite freigeben. */
// var selectWidth = "190";
// document.write('<style type="text/css">select.styled-weiss,select.styled-grau { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');

document.write('<style type="text/css">select.styled-weiss,select.styled-grau, select.styled-dunkel { position: relative; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');

function HasClassName(objElement, strClass) {
   // if there is a class
   if ( objElement.className ) {
      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');
      // get uppercase class for comparison purposes
      var strClassUpper = strClass.toUpperCase();
      // find all instances and remove them
      for ( var i = 0; i < arrList.length; i++ ) {
         // if class found
         if ( arrList[i].toUpperCase() == strClassUpper ) {
            // we found it
            return true;
            }
         }
      }
   // if we got here then the class name is not there
   return false;
}

var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("select"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if(HasClassName(inputs[a], "styled-weiss")) {
				option = inputs[a].getElementsByTagName("option");
				active = option.length > 0 ? option[0].childNodes[0].nodeValue : null;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select-weiss";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if(!inputs[a].getAttribute("disabled")) {
					inputs[a].onchange = Custom.choose;
				} else {
					inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
				}
			}
			if(HasClassName(inputs[a], "styled-grau")) {
				option = inputs[a].getElementsByTagName("option");
				active = option.length > 0 ? option[0].childNodes[0].nodeValue : null;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select-grau";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if(!inputs[a].getAttribute("disabled")) {
					inputs[a].onchange = Custom.choose;
				} else {
					inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
				}
			}
			if(HasClassName(inputs[a], "styled-dunkel")) {
				option = inputs[a].getElementsByTagName("option");
				active = option.length > 0 ? option[0].childNodes[0].nodeValue : null;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select-dunkel";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if(!inputs[a].getAttribute("disabled")) {
					inputs[a].onchange = Custom.choose;
				} else {
					inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
				}
			}
		}
		document.onMouseUp = Custom.clear;
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}
window.onload = Custom.init;
