/* JavaScript for MAM Baby */

$(document).ready(function() {
	
	/***********
	 * sIFR
	 ***********/
/*	if(typeof sIFR == "function"){
		// headings of subnavigation in #mainleft
		sIFR.replaceElement(named({sSelector:"#mainleft h3", sFlashSrc:"sifr/ectoplasmn.swf", sColor:"#ba86b6", sBgColor:"#FFFFFF", sFlashVars:"textalign=left&offsetTop=0"}));
	}
*/	
	
	
	/***********
	 * Dropdowns
	 ***********/
	// show dropdowns
	$("a.dropdownheader").click(function() {
		// hide currently open dropdowns
		$("div.dropdown").slideUp();
		// calculate position and width of dropdown
		var offset = $(this).offset();
		var header_width = $(this).width();
		var href = $(this).attr("href");
		// show dropdown
		if (href.length != 0 && $("div" + href + ".dropdown").css("display") != "block") {
			$("div" + href + ".dropdown").slideDown();
		}
		return false;
	});
	// hide all dropdowns if user clicks in the viewport
	$().click(function() {
		$("div.dropdown").slideUp();
	});
	
	
	/***********
	 * Plugin: Colorbox
	 ***********/
	$.fn.colorbox.settings.initialWidth = 100;
	$.fn.colorbox.settings.initialHeight = 100;
	$.fn.colorbox.settings.opacity = 0.6;
	$.fn.colorbox.settings.rel = 'imagebox';
	
	$("a[rel='imagebox']").colorbox();
	
	$("a[rel^='iframebox']").map(function(index) {
		var rel = $(this).attr("rel");
		var dim = rel.match(/^.+\\|(\d+)x(\d+)$/);
		var dim_width = (dim[1] > 0 ? dim[1] : false);
		var dim_height = (dim[2] > 0 ? dim[2] : false);
		$(this).colorbox({iframe:true, width:dim_width, height:dim_height, rel:false});
	});
	
	$("a[rel^='inlinebox']").map(function(index) {
		var rel = $(this).attr("rel");
		var dim = rel.match(/^.+\\|(\d+)x(\d+)(\|(.*))?$/);
		var dim_width = (dim[1] > 0 ? dim[1] : false);
		var dim_height = (dim[2] > 0 ? dim[2] : false);
		var dim_href = dim[4];
		$(this).colorbox({inline:true, href:'#'+dim_href, width:dim_width, height:dim_height, rel:false});
	});
	
	/***********
	 * Formular fields
	 ***********/
	 
	// clear search field on click
	$("#top form input.searchfield").click(function() {
		$("#top form input.searchfield").val("");
	});
	// write "Suchen" back again if user leaves the search
	// $("#top form input.searchfield").blur(function() {
	//	$("#top form input.searchfield").val("Suchen");
	// });
	
	// clear "Geburstag Tag" field on click
	$("#content form input.birthday.dayfield").livequery('click', function() {
		$(this).val("");
	});
	// write "TT" back again if user leaves field
	$("#content form input.birthday.dayfield").livequery('blur', function() {
		$(this).val("TT");
	});
	
	// clear "Geburstag Monat" field on click
	$("#content form input.birthday.monthfield").livequery('click', function() {
		$(this).val("");
	});
	// write "MM" back again if user leaves field
	$("#content form input.birthday.monthfield").livequery('blur', function() {
		$(this).val("MM");
	});
	
	// clear "Geburstag Jahr" field on click
	$("#content form input.birthday.yearfield").livequery('click', function() {
		$(this).val("");
	});
	// write "JJJJ" back again if user leaves field
	$("#content form input.birthday.yearfield").livequery('blur', function() {
		$(this).val("JJJJ");
	});
	
	
	
	/***********
	 * Navigation
	 ***********/
	 
	// toggle menu level 2
	$("#mainnav > li > a").mouseover(function() {
		// clear / close other dropdowns
		$("ul.level2").css("display", "none");
		$("#mainnav>li").removeClass("active");
		// mark current dropdown as active
		$(this).parent().addClass("active");
		// move and show subnav
		var list_level2 = $(this).siblings("ul.level2");
		list_level2.css("display", "block").css("left", "5px").css("top", "23px");	
	});
	
	// toggle menu level 3
	$("ul#mainnav > li > ul.level2 > li").mouseover(function() {
		// clear / close other dropdowns
		$("ul.level3").css("display", "none");
		$("#mainnav > li > ul.level2 > li").removeClass("active2").removeClass("plus3");
		// highlight current item
		if ($(this).hasClass("showplus3"))
			$(this).addClass("plus3");
		else
			$(this).addClass("active2");
		// move and show subnav
		var list_level3 = $(this).children("div.level3container");
		list_level3.css("display", "block").css("left", "156px").css("top", "0px");
		list_level3.children("ul.level3").css("display", "block");		
	});
	
	// hide the submenus if user moves the mouse over possibly conflicting elements
	// $("#headerbox, #main, #flash").mouseover(function() {
	$("#main").mouseover(function() {
		$("ul.level2").css("display", "none");
		$("ul.level3").css("display", "none");
		$("#mainnav>li").removeClass("active");
		$("#mainnav > li > ul.level2 > li").removeClass("active2").removeClass("plus3");
	});
	
	
	
	/***********
	 * Headerbox
	 ***********/
	
	$("#showmamclub").click(function() {
		$("#headerbox #mamclubinfo").attr("class", "container");
		$("#headerbox #mamclub").attr("class", "containeractive");
		$("#headerbox #opinionleaderinfo").attr("class", "container");
		$("#headerbox #opinionleaderlogin").attr("class", "container");
		
		$("#headerbox #showparents a").attr("class", "active");
		$("#headerbox #showdoctors a").removeAttr("class", "active");
		
		return false;
	});
	
	
	
	/***********
	 * Registrierung: Kind hinzufügen und entfernen
	 ***********/
	
	var child_count = 2; 
	var max_count = 2; 
	
	if (document.getElementById("AmountBabys") != null)
		child_count = document.getElementById("AmountBabys").innerHTML;
	max_count = child_count;
	// alert(child_count);
	
	$("form a.add_child").livequery('click', function() {
		
		if (child_count < 11) {
			
			str_replacement = unescape(document.getElementById("NextBaby").innerHTML);
			str_replacement = str_replacement.replace(/\+/g, " ");
			str_replacement = str_replacement.replace(/\#1/g, "#"+child_count);
			str_replacement = str_replacement.replace(/BabyName1/g, "BabyName"+max_count);
			str_replacement = str_replacement.replace(/BabyBirthday1/g, "BabyBirthday"+max_count);
			str_replacement = str_replacement.replace(/BabyGender1/g, "BabyGender"+max_count);
			str_replacement = str_replacement.replace(/Baby_1/g, "Baby_"+max_count);
			str_replacement = str_replacement.replace(/BabyNewsletter1/g, "BabyNewsletter"+max_count);
			str_replacement = str_replacement.replace(/Number_1/g, max_count);
			str_replacement = str_replacement.replace(/HeadlineBaby1/g, "HeadlineBaby"+max_count);
			// alert(str_replacement);
			
			// counter erhöhen
			child_count++;
			max_count++;
			
			// zeilen hinzufügen
			$(this).parent().parent().replaceWith(str_replacement);
			
			return false;
		} else {
			alert(document.getElementById("TextNoMoreBabies").innerHTML);
		}
	
	return true;
	});
	
	$("form a.remove-child").livequery('click', function() {
		
		// alert('tr.Baby_' + $(this).attr("href"));
		str_temp = $(this).attr("href");
		array_temp = str_temp.split("/");
		if (array_temp.length > 1)
			str_temp = array_temp[array_temp.length-1];
		
		// ResortOrderNumber
		y = 1;
		for(i=1; i<max_count; i++)
			{
			if (i != str_temp)
				{
				if (document.getElementById("HeadlineBaby" + i) != null)
					{
					str_text = '<b>'+document.getElementById("TextHeadlineBabies").innerHTML+' #'+y+'</b>';
					document.getElementById("HeadlineBaby" + i).innerHTML = str_text;
					y++;
					}
				}
			}
		
		// counter darf nicht zurückgesetzt werden!
		// zeilen entfernen
		$("tr.Baby_" + str_temp).each(function() {
			$(this).remove();
		});
		child_count--;
		
		return false;
	});



	// Quu - Sequence
	$('.quusequence').change(function () {
	
		// Bei Null muss vorher der entsprechende Wert herausgesucht werden.
		z = -1;
		for(i=0; i<array_options.length; i++) {
			
			if ($(this).attr('id') == array_options[i][1]) {
				
				z = array_options[i][2];
			}
		}
		
		for(i=0; i<array_options.length; i++) {
			
			// Bei allen anderen Select-Feldern den Wert löschen.
			if ($(this).attr('id') != array_options[i][1]) {
			
				if ($(this).val() > 0) {
				
					$('#'+array_options[i][1]).children("option[value='"+$(this).val()+"']").remove();
				}
				
				// Sobald das Feld einen Wert hatte, stellen wir diesen in allen anderen Boxen wieder her.
				if (z > 0) {
					
					y = 0;
					for (y=z; y>0; y--) {
						if ($("#"+array_options[i][1]+" > option[value='"+y+"']").length)
							break;
					}
					
					$("#"+array_options[i][1]+" > option[value='"+y+"']").after('<option value="'+z+'">'+z+'</option>');
				}
			}
			
			// Matrix setzen
			if ($(this).attr('id') == array_options[i][1]) {
				
				array_options[i][2] = $(this).val();
			}
		}
	});



	// Quu - Sequence - Beim ersten laden sollen die nicht benötigten gelöscht werden
	for(y=0; y<array_options.length; y++) {
		
		for(i=0; i<array_options.length; i++) {
			
			// Bei allen anderen Select-Feldern den Wert löschen.
			if (array_options[y][1] != array_options[i][1]) {
			
				if (array_options[y][2] > 0) {
				
					$('#'+array_options[i][1]).children("option[value='"+array_options[y][2]+"']").remove();
				}
			}
		}
	}



});



/***********
 * Zeigt mehrer Bilder mit Thumbnails an.
 ***********/

function show_multiple_image(int_number) {

	document.getElementById("multiple-image-container").innerHTML = document.getElementById("multiple-image-"+int_number).innerHTML

}



/***********
 * DropDownSelector
 ***********/

function dropdownselector(c) {

	// Hide all boxes
	int_amount = document.getElementById("dropdownselector-max").innerHTML
	for (i=0; i<int_amount; i++) {
		document.getElementById("dropdownselector-container"+i).style.display = 'none';
	}
	
	// Show desired container
	document.getElementById("dropdownselector-container"+c.value).style.display = 'block';
}



/***********
 *  Quu - Sequence
 ***********/
 
var array_options = new Array;

 
/** Shop: Order tracking */

function trackOrder() {
	if (typeof(order) != undefined) {
		pageTracker._addTrans(
		      order.orderId,
		      order.shopName,
		      order.price,
		      order.tax,
		      order.shipping,
		      order.city,
		      order.state,
		      order.country
		); 
		
		if (typeof(items) != undefined) {
			for (var i = 0; i < items.length; i++) {
				pageTracker._addItem(
				      items[i].orderId, // required
				      items[i].SKU,
				      items[i].productName,
				      items[i].productCategory,
				      items[i].price,
				      items[i].amount
					);
			}
		} 
		
		pageTracker._trackTrans();
	}
}