(function ($){
	//jQuery plugin
	//sets an equal height of a floating collection of elements per line
	$.fn.syncHeightPerLine = function(syncList){
		syncList = syncList? $(syncList) : $(this);
		var toSync = [];
		syncList.each(function(i,val){
			toSync.push(this);
			if( ($(this).next().length && $(this).position().top != $(this).next().position().top) || 
				!$(this).next().length
				){
				$(toSync).height($(toSync).maxHeight());			
				toSync = [];
			}
		});
	}

	//jQuery plugin
	//computes the maximal height of an array of elements
	$.fn.maxHeight = function(){
		var max = 0;
		$(this).each(function(){
			var height = $(this).height();
			max = height>max? height : max;
		});
		return max;
	}

	//jQuery plugin
	//gives tab-functionality to a list with its tab-container
	$.fn.makeTabs = function(){
		if(this.hasTabs){ return; }
		this.hasTabs = true;

		var containers = $(this).nextAll().hide();
		var tabs = $(this).children().find("a");

		var foundActive = 0;
		tabs
			.each(function(i,val){
				this.container = containers[i]? containers[i] : $("<div/>");
				this.show = function(){
					tabs.each(function(){
						this.hide();
					});
					$(this.container).show();
					$(this).parents("li").addClass("active");
				}
				this.hide = function(){
					$(this.container).hide();
					$(this).parents("li").removeClass("active");
				}
				if(!foundActive && $(this).parents("li").hasClass("active")){
					foundActive = i;
				}
				else{
					this.hide();
				}
			});
			
		this.containers = containers;
		this.tabs = tabs;
		
		if(tabs[foundActive]){
			tabs[0].show();
		}
		
		return $(this);
	}

	//use this to activate a tab
	$.fn.showTab = function(i){
		if(this.hasTabs && this.tabs[i]){
			this.tabs[i].show();
		}
		
		return $(this);
	}
	
	//decides if the user uses a shitbrowser or not
	$.shitbrowser = ($.browser.msie && parseInt($.browser.version)<7);

	//expander is like an accordeon but can has more than one item opened
	$.fn.expander = function(){
		if(this.isExpander){ return; }
		this.isExpander = true;
		
		$(this).children(".expander-header").find("a")
			.click(function(e){
				e.preventDefault();
				if($(this).hasClass("closed")){
					$(this).parent().next().slideDown("fast",function(){
						$(this).prev().children().removeClass("closed");
					});
				}
				else{
					$(this).parent().next().slideUp("fast",function(){
						$(this).prev().children().addClass("closed");
					});
				}
			});
		$(this).children(".expander-header:first").find("a").click();		
	}
	
	//use to display hints in inputfields, via title-attribute
	$.fn.inputField = function(){
		if(!$(this).attr("title") || this.isMarked){
			return $(this);
		}
		this.isMarked = true;
		$(this)
			.focus(function(){
				if(!this.changed){
					$(this).val("").removeClass("hint");
				}
			})
			.blur(function(){
				if(!this.changed){
					$(this).val($(this).attr("title")).addClass("hint");
				}
			})
			.keyup(function(){
				this.changed = ($(this).val()? true : false);
			})
			.keyup()
			.blur()
			.each(function(){
				if($(this).val() == $(this).attr("title")){
					this.changed = false;
				}
			});
		return $(this);
	}
	
	$.dropdowns = [];
	$.fn.dropdown = function(){
		if(this.isDropdown) return $(this);
		this.isDropdown = true;
		var dd = this;
		var options = [];
		var dropTitle = $("<div class='drop-title'/>");
		var dropTitleText = $("<span/>")
		dropTitle.append(dropTitleText);
		var dropList = $("<ul class='drop-list'/>").hide();
		$("option", this).each(function(){
			var opt = this;	
			dropList
				.append(					
					$("<li/>")
						.append(
							$("<a href='#'/>")
								.each(function(){
									this.partner = opt;
									this.partner.partner = this;
									options.push(opt);
								})
								.click(function(e){
									e.preventDefault();
									$("option",dd).removeAttr("selected");
									$(opt).attr("selected", "selected");
									dd.close();
									dd.updateDropList();
								})
						)
				);
		});
		this.updateDropList = function(){
			$(this.options).each(function(){
				$(this.partner).text($(this).text());			
			});
			var selected = $(this).find(":selected")
			dropTitleText.text(selected.text());
			if(selected.attr("value") == ""){
				dropTitleText.addClass("hint");
			}
			else{
				dropTitleText.removeClass("hint");
			}
			$(dd).change();
		}
		this.options = options;
		this.dropList = dropList;
		this.dropTitle = dropTitle;
		$(this.dropTitle).each(function(){
			this.dropdown = dd;
		});
		this.dropTitleText = dropTitleText;
		var dropWrap = $("<div class='drop-wrap'/>");
		var dropMenu = $("<div class='drop-menu'/>");
		$(this).wrap(dropWrap).parent().after(dropMenu);
		dropMenu.append(dropTitle).append(dropList);
		this.updateDropList();
		
		this.open = function(){
			$.closeAllDrops();
			this.dropList.show().parents().css("z-index", 100);
			this.dropTitle.addClass("open")
		}
		
		this.close = function(){
			this.dropList.hide().parents().css("z-index", "");
			this.dropTitle.removeClass("open")
		}
		
		this.dropTitle.click(function(){
			if(!$(this).hasClass("open")){
				this.dropdown.open();
			}
			else{
				this.dropdown.close();
			}
		});
		
		
		//adjust IE6
		if($.shitbrowser){
			var dropWidth = parseInt(dropTitleText.css("width")) + parseInt(dropTitleText.css("padding-right"));
			dropTitleText.width(dropWidth);
			$(this.dropList).show().width($(this.dropList).outerWidth()).hide();
			$("li", this.dropList)
				.mouseover(function(){ $(this).children().addClass("hover"); })
				.mouseout(function(){ $(this).children().removeClass("hover"); })
				.each(function(){
					$(this).css("margin-bottom", -1)
					$("a",this).css("height", 1);
				});
		}
		
		$.dropdowns.push(this);
		
		//prevent bubbeling click events
		$(dropMenu).click(function(e){
			e.stopPropagation();
		});
	}
	
	$.closeAllDrops = function(){
		$($.dropdowns).each(function(){
			this.close();
		});
	}
	$(document).click(function(){
		$.closeAllDrops();
	});
	
})(jQuery);


(function ($){
	// document onDomReady init
	$(function(){

		//sync teaser-lists line height
		$(".teaser-list").each(function(){
			$(this).children("ul").children().syncHeightPerLine();
		});
		
		//make tabs
		var foundTabs = false;
		$("ul.tabs").each(function(){
			$(this).makeTabs();
			foundTabs = true;
		});	
		//history safe tabs
		if(foundTabs){
			$(".tabs a").history(function(){
				this.show();
			});
			$.ajaxHistory.initialize();
		}
		
		//make expanders
		$(".expander").each(function(){
			$(this).expander();
		});
		
		//initialize form fields
		$(".form-textline input.text").each(function(){
			$(this).inputField();
		});
		
		//initialize dropdowns
		$(".dropdown select").each(function(){
			$(this).dropdown();
		});
		
		//initialize checkboxes
		var updateCbLabel = function(){
			if(this.checked){
				$(this.label).addClass("perfectcheckbox-checked");
			}
			else{
				$(this.label).removeClass("perfectcheckbox-checked");
			}
		}	
		$("label.perfectcheckbox").each(function(){
			var chbx = $("#"+$(this).attr("for")).get(0);
			chbx.label = this;
			this.checkbox = chbx;
			//image preloading
			if($(this.checkbox).is(":disabled")){
				$(this).addClass("perfectcheckbox-disabled");
			}
			else{				
				$(chbx)
				.change(updateCbLabel)
				.click(updateCbLabel)				//IE6 hack -> wrong event firing
				.focus(function(){
					$(this.label).addClass("perfectcheckbox-focus");
				})
				.blur(function(){
					$(this.label).removeClass("perfectcheckbox-focus");
				})
				.each(updateCbLabel);				//initialize
			}				
		});
		
		//initialize radios		
		var radioGroupNames = [];//get all radio groupnames of perfect radios
		$("input.perfectradio").each(function(){
			if((","+radioGroupNames.toString()).indexOf(this.name)<1){
				radioGroupNames.push(this.name);
			}
		});
		var updateLabels = function(){ //this function updates the label-class according the checked-state of the relative 
			$(this.group).each(function(){
				if(this.checked){
					$(this.label).addClass("perfectradio-checked");
				}
				else{
					$(this.label).removeClass("perfectradio-checked");
				}
			});
		}
		for(var i=0; i<radioGroupNames.length; i++){ //create radio groups and init the states
			var grp = $("input[name='"+radioGroupNames[i]+"']").filter("input[type='radio']");
			grp.each(function(){
				this.group = grp;
				this.label = $("label[for='"+this.id+"']").get(0);
				this.label.radio = this;				
				if($(this).is(":disabled")){
					$(this.label).addClass("perfectradio-disabled");
				}
				else{
					$(this)
					.change(updateLabels)
					.click(updateLabels)	//IE6 hack -> wrong event firing
					.focus(function(){
						$(this.label).addClass("perfectradio-focus");
					})
					.blur(function(){
						$(this.label).removeClass("perfectradio-focus");
					});
				}
			})
			.each(updateLabels);	//initialize 
		}
		
		
	});
})(jQuery);
