// Image replacement

jQuery.noConflict();

function roll(img_name, img_src) {
	if (document[img_name]){
		document[img_name].src = img_src;
	}
	else{
		document.getElementById(img_name).src = img_src;
	}
}

function setBackgroundImage(id, img_src) {
	jQuery(id).css("background-image","url(" + img_src + ")");
}


function clickHandlerTabNav() {
	//init
	jQuery('#tabnav li').each(function() {
		if(!jQuery(this).hasClass("current")) {
			jQuery('#tab-' + jQuery(this).children('span').attr('id')).addClass('hidden');
		}
	});

	
	jQuery('#tabnav li span').bind('click', function(e) {
		var clicked = jQuery(e.target);
		
		if (!clicked.parents().hasClass("current")) {
			//close others before opening this item
			jQuery('#tabnav').children('.current').each(function() {
				jQuery(this).removeClass();
				jQuery('#tab-' + jQuery(this).children('span').attr('id')).addClass('hidden');
			});
		}
		
		jQuery('#tab-' + jQuery(this).attr('id')).removeClass('hidden');
		//set/remove class expanded to parent tr
		jQuery(this).parent('li').addClass('current');
		
	});
}

function checkCartHeight() {
	
	if(jQuery('#cartcontent').height() >= 366) {
		jQuery('#cartcontent').css('height', '366px');
		
		jQuery('#cartcontent').jScrollPane({
			scrollbarWidth: 9,
			scrollbarMargin: 0,
			showArrows: true,
			arrowSize: 12,
			maintainPosition: false
		});
		
	} else {
		if(jQuery('.jScrollPaneContainer').length > 0) {
			removeScrollPane();
			jQuery('#cartcontent').removeAttr('style');
		}
		
		if(jQuery('#cartcontent table tr').length > 0) {
			jQuery(this).remove();
		} else {
			jQuery('.cart_empty').removeClass('hidden');
		}
	}
}


function removeScrollPane() {
	jQuery('#cartcontent').insertAfter('#midicart > .header');
	jQuery('.jScrollPaneContainer').remove();
}


function setLastRow() {
	var tablerows = new Array();
	if(jQuery('#cartcontent tr').length > 0) {
		jQuery('#cartcontent tr').each(function() {
			tablerows.push(jQuery(this)); 
		});
		
		tablerows[tablerows.length - 1].addClass('last');
		tablerows[0].addClass('first');
	}
	
}


function displayPaymentPanel() {
			
	if(jQuery('#order input:radio:checked').parent().parent().children("label[for='radio_invoice']").attr("for") == 'radio_invoice'){
		jQuery("#paymentInfos").addClass("hidden");
	} else if (jQuery('#order input:radio:checked').parent().parent().children("label[for='radio_postcard']").attr("for") == 'radio_postcard'){
		jQuery("#paymentInfos").addClass("hidden");
	} else {
		jQuery("#paymentInfos").removeClass("hidden");
	}
}

function displayDeliveryAddress() {	
	if(jQuery('input[name=formular:differentDelivery]').is(':checked')){	
		jQuery('#formular\\:deliverycheck').removeClass("hidden");
	} else{
		jQuery('#formular\\:deliverycheck').addClass("hidden");
	}	
}

//init dynamic elements after document loaded
jQuery(document).ready(function(){
	
	//init search field
	jQuery("#search-form\\:keywordsearch").keyup(function(e) { 
		if(e.keyCode == 13) { 
			jQuery("#search-form\\:searchformsubmit").trigger('click');
		} 
	}); 
	
	jQuery("#suche\\:keyword").keyup(function(e) { 
		if(e.keyCode == 13) { 
			jQuery("#suche\\:searchformsearchsubmit").trigger('click');
		} 
	});  
	
	// init mainnav dropdowns
	jQuery(".tabteaser").hide();
	jQuery(".tabcontent:not(:first)").hide();
	jQuery("#tabTeaser").html(jQuery(".tabteaser:first").html());
	jQuery(".yui-nav li.toggle").click(function(){
		var myClass = jQuery(this).attr("class");
		var myId = jQuery(this).attr("id");
		var myIndex = myId.replace("tabLink", "");
		if (myClass.indexOf("selected") == -1) {
			var highlightId = "#tabContent" + myIndex;
			jQuery(highlightId).show();
			jQuery(highlightId).siblings().hide();
			jQuery(this).siblings().removeClass("selected");
			jQuery(this).addClass("selected");
			
			var highlightTeaser = "#tabTeaser" + myIndex;
			jQuery("#tabTeaser").html(jQuery(highlightTeaser).html());
		}
	});
	
	
	//behavior tabnav
	if(jQuery('#tabnav').length > 0) {
		clickHandlerTabNav();
	}
		
	
	//init midicart
	if(jQuery('#cartcontent').length > 0) {
		//last row needs class "last" to remove dotted bottom border
		setLastRow();
		checkCartHeight();
		//check height if scrollPane needs to be instantiate
		jQuery('#cartcontent').jScrollPane({
			scrollbarWidth: 9,
			scrollbarMargin: 0,
			showArrows: true,
			arrowSize: 12,
			maintainPosition: false
		});	
	}
	
	//init tooltip on product detail
	if(jQuery('.tt').length > 0) {
		
		jQuery('.tt').children('img').tooltip({
			bodyHandler: function() {
				return jQuery(this).next('.hidden').html();
			}, 
			showURL: false,
			delay: 0,
			fade: 250,
			top: -25
		});
		jQuery('.tt').children('a').children('img').tooltip({
			bodyHandler: function() {
				return jQuery(this).parent().next('.hidden').html();
			}, 
			showURL: false,
			delay: 0,
			fade: 250,
			top: -25
		});
	}
	
		
	if(jQuery('#prod_availability').length > 0) {
		jQuery('#TB_content').hide();
	}
	
	
	if(jQuery('input[name=formular:differentDelivery]').is(':checked')){	
		jQuery('#formular\\:deliverycheck').removeClass("hidden");
	} else{
		jQuery('#formular\\:deliverycheck').addClass("hidden");
	}
	
	if(jQuery('#order input:radio:checked').parent().parent().children("label[for='radio_invoice']").attr("for") == 'radio_invoice'){
		jQuery("#paymentInfos").addClass("hidden");
	} else if (jQuery('#order input:radio:checked').parent().parent().children("label[for='radio_postcard']").attr("for") == 'radio_postcard'){
		jQuery("#paymentInfos").addClass("hidden");
	} else {
		jQuery("#paymentInfos").removeClass("hidden");
	}	
	
});
