// css for mac
if(navigator.userAgent.indexOf('Mac') != -1){
	document.write('<link rel="stylesheet" type="text/css" media="screen" href="fileadmin/templates/css/mac.css" />');
}

// js for ie 6
if(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1){
	document.write('<script type="text/javascript" src="fileadmin/templates/js/function_ie6.min.js"></script>');
}
// write label into field als value
this.label2value = function(){	

	var inactive = "inactive";
	var active = "active";
	var focused = "focused";
	
	$(".label2value label").each(function(){		
		obj = document.getElementById($(this).attr("for"));
		if(($(obj).attr("type") == "text") || ($(obj).attr("type") == "password") || (obj.tagName.toLowerCase() == "textarea")){			
			$(obj).addClass(inactive);			
			var text = $(this).text();
			$(this).css("display","none");			
			
			//only set label into field if the field has no predefined text (used for birdfinder)
			if($(obj).val() == '') {
				$(obj).val(text);
			}
			
			if($(obj).attr("type") == 'password') {
				$(obj).replaceWith('<input id="pass" class="field_text" type="text" value="Passwort" name="pass"/>');
			}
			
			$(obj).focus(function(){	
				$(this).addClass(focused);
				$(this).removeClass(inactive);
				$(this).removeClass(active);								  
				if($(this).val() == text) $(this).val("");
			});	
			$(obj).blur(function(){	
				$(this).removeClass(focused);													 
				if($(this).val() == "") {
					$(this).val(text);
					$(this).addClass(inactive);
				} else {
					$(this).addClass(active);		
				};				
			});				
		};	
	});		
};

// Auto-adjust I-frame Height 
function doIframe(){
	o = document.getElementsByTagName('iframe');
	for(i=0;i<o.length;i++){
		if (/\bautoHeight\b/.test(o[i].className)){
			setHeight(o[i]);
			addEvent(o[i],'load', doIframe);
		}
	}
}
function setHeight(e){
	if(e.contentDocument){
		e.height = e.contentDocument.body.offsetHeight + 35;
	} else {
		e.height = e.contentWindow.document.body.scrollHeight;
	}
}
function addEvent(obj, evType, fn){
	if(obj.addEventListener)
	{
	obj.addEventListener(evType, fn,false);
	return true;
	} else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
	return r;
	} else {
	return false;
	}
}

if (document.getElementById && document.createTextNode){
 addEvent(window,'load', doIframe);	
}

// Shop total Calculation 
function recalc(){
	$("[id^=total_item]").calc(
		// the equation to use for the calculation
		"qty * price",
		// define the variables used in the equation, these can be a jQuery object
		{
			qty: $("input[name^=qty_item_]"),
			price: $("[id^=price_item_]")
		},
		// define the formatting callback, the results of the calculation are passed to this function
		function (s){
			// return the number as a dollar amount
			return "Fr" + s.toFixed(2);
		},
		// define the finish callback, this runs after the calculation has been complete
		function ($this){
			// sum the total of the $("[id^=total_item]") selector
			var sum = $this.sum();
			
			$("#grandTotal").text(
				// round the results to 2 digits
				"Fr. " + sum.toFixed(2)
			);
		}
	);
}

$(document).ready(function(){
						   
	label2value();
	
	$("#pass").focus(function() { 
			$("#pass").after('<input id="pass" class="field_text" type="password" value="" name="pass"/>').remove();
			$("#pass").focus();
	});
	
	//folding effect for footer area
	$("a.folding_link").click(function () {
	  $(this).toggleClass("opened");
	  $(this).siblings('.folding_content').toggle();
	  $(this).parent().parent().toggleClass("bg_opened");
	});

	//folding effect for content area
	if($("div.folding_title").length > 0) {
		$("div.folding_title").click(function () {
		  $(this).parent().toggleClass("opened");
		  $(this).siblings('.folding_content').toggle();
		});
	}

	// eyekon: ronny (02.10.2009)
	// add parser through the tablesorter addParser method
	$.tablesorter.addParser({
		// set a unique id
		id: 'date_de',
		is: function(s) {
			// return false so this parser is not auto detected
			return false;
		},
		format: function(s) {
			// format your data for normalization
			// zb: ab 12. November 2009 in der Schweiz
			var regexp = /(\d\d?)\. (januar|februar|märz|april|mai|juni|juli|august|september|oktober|november|dezember) (\d\d\d\d)/i;
			var result = regexp.exec(s);
			var day = parseInt(result[1],10);
			var month = {januar:1,februar:2,märz:3,april:4,mai:5,juni:6,juli:7,august:8,september:9,oktober:10,november:11,dezember:12}[result[2].toLowerCase()];
			var year = parseInt(result[3],10);
			return year*10000 + month*100 + day;
		},
		// set type, either numeric or text
		type: 'numeric'
	});
	
	//table sorting standard
	if ($(".tablesorter.standard").length > 0) {
		$(".tablesorter.standard").tablesorter({
			headers: {
				1: {sorter: 'date_de'},
				2: {sorter: false}
			},
			textExtraction: 'complex',
			sortList: [[1,0]]
		});
	}
	
	//table sorting in Medienmitteilung
	if($(".tablesorter.custom").length > 0) {
		$(".tablesorter.custom").tablesorter({ 
			// pass the headers argument and assing a object 
			headers: { 
				// assign the third column (we start counting zero) 
				2: { 
					// disable it by setting the property sorter to false 
					sorter: false 
				} 
			},
			sortList: [[0,1]],
			widgets: ['LastRowCSS']
		}).find('tr:last').addClass("lastRow");
	}
	
	//shop 
	//onload
	if($("input[name^=qty_item_]").length > 0) {
		recalc();
	}
	//key up 
	$("input[name^=qty_item_]").bind("keyup", recalc);
	
	//portrait appearance
	if($("#appearance_nav")) {
		links = $("#appearance_nav a");
		$("#appearance_nav a").click(function () {
			itemId = $(this).attr("rel");
			$(this).attr("class","current");
			links.each(function(){
				if($(this).attr("rel") != itemId) {
					$($(this).attr("rel")).attr("class","appearance");
					$(this).attr("class","");
				} 
			});
			$(itemId).attr("class","appearance_open");
		});
		
	}

	//vogel geschlecht
	
	if($("#bird_sex_nav")) {
		links_bird = $("#bird_sex_nav a");
		$("#bird_sex_nav a").click(function () {
			itemId_bird = $(this).attr("rel");
			$(this).attr("class","current");
			links_bird.each(function(){
				if($(this).attr("rel") != itemId_bird) {
					$($(this).attr("rel")).attr("class","bird_sex_img");
					$(this).attr("class","");
				} 
			});
			$(itemId_bird).attr("class","bird_sex_img_open");
		});
	}
});

