// JavaScript Document
$(document).ready(function(){   
	changeProductImageFinder();
	changeOrderImage();
	
	if (jQuery.browser.msie){
		removeLoder();
	} else {
		changeProductImageMenu();
	}
});


function changeProductImageFinder () {
	$("#produktfinder .productlist li a").hover (function(e) {
				
				var element = $("#produktfinder .image img");
				var id = $(this).attr("id");
								
				e.stopPropagation();
				element.stop(true, true);
				doFadeIn(element, id);
			}, function() {
				doFadeOut($("#produktfinder .image img"));
			}
	);

}

//function changeProductImageMenu () {
//	$("#subnavigation a").hover(
//			function(e) {
//				if ($(this).parent().find(".productpic").length == 1){
//					var element = $(this).parent().find("img");
//					var id = $(this).attr("id");
//									
//					e.stopPropagation();
//					element.stop(true, true);
//			
//					doFadeIn(element, id);
//				}
//			},
//			function(){
//				doFadeOut( $(this).parent().find("img"));
//			}
//	);
//}
function changeProductImageMenu () {
	$("#subnavigation a").bind("mouseenter",
		function(e) {
			if ($(this).parent().find(".productpic").length == 1){
				var element = $(this).parent().find("img");
				var id = $(this).attr("id");
								
				e.stopPropagation();
				element.stop(true, true);
		
				doFadeIn(element, id);
			}
		}
	);
	$("#subnavigation a").bind("mouseleave",
		function(e){
			var img = $(this).parent().find('img');
			if(!$(e.relatedTarget).hasClass("productpic")){
			doFadeOut( $(this).parent().find("img"));
			}
		}
	);
}

function removeLoder() {
	$("#subnavigation a").hover(
			function(e) {
				var liArray = $(this).parent().find(".productpic").find("li");
				if (liArray.length == 1){
					$(liArray[0]).css("background-image","none");
				}
				
			},
			function(){
			}
	);
}

function changeOrderImage () {
	$(".ce-order table tr").bind("mouseenter",
			function(e) {
				var element = $(".ce-order .content .image img"); 
				var id = $(this).attr("id");
								
				e.stopPropagation();
				element.stop(true, true);
		
				doFadeIn(element, id);
			}
	);
}

function getImagePath(id) {
	if (id != null)  {
		id = id.substring(2);
		
		if (id.match("_")) {
			idArr = id.split("_");
			id = idArr[0];
		}
		
		if(arrProducts[id] == null || arrProducts[id] == "undefined"){
			var img = arrProducts["idEmpty"];
			
		}else{
			var img = arrProducts[id];
		}
		
		return img;
	}
	return;
}


var _loaderGif = null;
function doFadeIn (element, id) {
		var img = getImagePath(id);
		
		if (img != null)  {
			if (jQuery.browser.msie && jQuery.browser.version < 7){		// if IE6
				element.attr("src", img);
				
			}else{
//				element.hide(1, function() {
//					element.attr("src", img);
//					element.fadeIn(800);
//					
//				});
				if(!_loaderGif){
					_loaderGif = element.parent().parent().css("background-image");
				}
				element.parent().parent().css("background-image",_loaderGif);
				element.parent().hide();
				element.attr("src",img);
				element.parent().fadeIn(800,function(){
					_loaderGif = element.parent().parent().css("background-image");
					element.parent().parent().css("background-image","none");
				});
				
			}
//			
		}
}

function doFadeOut (element) {
		element.parent().hide();
		element.attr("src", getImagePath("p-id0")).parent().show();
}


/* ########################################## */
/* #####   TOOGLE			   			##### */
/* ########################################## */

/* toogle foldable tab */
function toogleFoldableTab(element) {
	var tabdiv = jQuery(element).parent();
	
	if(tabdiv.parent().attr("id") == "order_foldable"){
		$(".ce-order .content .image img").attr("src", getImagePath("default"));
	}
	
	if(jQuery(tabdiv).hasClass('tab-active')) {
		jQuery(tabdiv).find(".content").slideUp();
		jQuery(tabdiv).removeClass('tab-active');
	} else {
		jQuery(tabdiv).parent().find(".content").parent().removeClass('tab-active');
		jQuery(tabdiv).parent().find(".content").slideUp();
		
		jQuery(tabdiv).find(".content").slideDown();
		jQuery(tabdiv).addClass('tab-active');
	}
}

/* toogleTab */
function toogleTab(element) {
	var aTag = $(element);
	var content = jQuery(element).parent().parent().parent().find(".content");

	if(content.hasClass('open')) {
		content.slideUp(500);
		content.removeClass('open');
		aTag.parent().find(".open").removeClass('open');
	} else {
		content.slideDown(500);
		content.addClass('open');
		aTag.addClass("open");
	}
}

/* ########################################## */
/* #####   TOOLTIP			   			##### */
/* ########################################## */

//Show Tooltip if exist
jQuery(document).ready(function() {
	tooltip();
});	
this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 30;	//horizontal
		yOffset = 40;	//vertical

		xOffset = 0;	//horizontal
		yOffset = -5;	//vertical
	/* END CONFIG */ 
	
	$('.ce a').hover(function(e){

		var html = $(this).find('.tooltip').html();

		if (html){
			$("body").append("<p id='tooltip'>"+ html +"</p>");

			//	var posY = parseInt ($(this).find("img").offset().top) + yOffset - $("#tooltip").height();
			//	var posX = parseInt ($(this).find("img").offset().left) + xOffset;
			var posY = e.pageY + yOffset - $("#tooltip").height();
			var posX = e.pageX + xOffset;
	
			$("#tooltip")
				.css("top",(posY) + "px")
				.css("left",(posX) + "px")
				//.fadeIn("fast");
				.show();
		}
    },
	function(){
//		this.title = this.t;		
		$("#tooltip").remove();
    });
    
    $('.ce a').mousemove(function(e){
    	var posY = e.pageY + yOffset - $("#tooltip").height();
		var posX = e.pageX + xOffset;
		$("#tooltip")
			.css("top",(posY) + "px")
			.css("left",(posX) + "px");
	});	
};

/* ########################################## */
/* #####   ORDER CALCULATOR   			##### */
/* ########################################## */

// order step 1 - calculate price
$(document).ready(function(){
	var value = 0; 
	
    $(".ce-order .content .total-price").each(function(){
    	var price = $(this).parent().find(".single-price").text();
		var ammount = parseInt($(this).parent().find(".ammount input").attr("value"));
		if(!ammount || ammount<0){
			// $(this).parent().find(".ammount input").attr("value",0)
			ammount = 0;
		}
		var singlePrice = orderHelper(price,ammount,$(this));
		
		value += singlePrice;
	});
  	$(".ce-order .order-result span.result-value").text(floatToPrice(value)+" Euro");
  	$(".ce-order .order-result span.result-mwst-value").text(floatToMwSt(value)+" Euro");

	// on change
	$(".ce-order .content .ammount input").change(function(e){
    	var newValue = 0;
    	
    	var price = $(this).parent().parent().find(".single-price").text();
		var ammount = parseInt($(this).attr("value"));
		if( ammount<0){
			// $(this).attr("value",0)
			ammount = 0;
		}
		orderHelper(price,ammount, $(this).parent().parent().find(".total-price"), true);
    	
		$(".ce-order .content .ammount input").each(function(){
			var price = $(this).parent().parent().find(".single-price").text();
			var ammount = parseInt($(this).attr("value"));
			if(isNaN(ammount) || ammount<0){
				// $(this).attr("value",0)
				ammount = 0;
    		}
			var singlePrice = orderHelper(price,ammount, $(this).parent().parent().find(".total-price"));
			
			newValue += singlePrice;
    	});
		$(".ce-order .order-result span.result-value").text((floatToPrice(newValue))+" Euro");
	  	$(".ce-order .order-result span.result-mwst-value").text((floatToMwSt(newValue))+" Euro");
	});
});

// Order Step 2 - calculate price with payment method
$(document).ready(function(){
//	setPriceWithMethod (0);
	
	$("#paymentMethod_fieldset input").click(function(e){
		var query = $(this).parent().find("input:checked").attr("value");
		var methodValue = (parseFloat(query.replace(/\,/,'.'))).toFixed(2);
		setPriceWithMethod (methodValue);
	});
	
	$("#paymentMethod_fieldset input:checked").trigger('click');

});

function setPriceWithMethod (methodValue){
	var value = parseFloat(($(".ce-order .order-result dl dd.order-value").text()).replace(/\,/,'.'));
	value += parseFloat(methodValue);
	
	$(".ce-order .order-result dl dd.payment-costs").text((floatToPrice(methodValue))+" EUR");
	$(".ce-order .order-result dl dd.result-value").text((floatToPrice(value))+" EUR");
	
}
function orderHelper(price,ammount,jQueryTarget, isChange){
	var value = 0;
	price = parseFloat(price.replace(/\,/,'.'));
	value = price * ammount;
	value = value.toFixed(2);
	var returnVal = value*1;
	
	if (!isNaN(ammount)){
		if (value != 0 || isChange){
			jQueryTarget.text(floatToPrice(value));
		}
	} else {
		jQueryTarget.text("");
	}
	
	return returnVal;
}


// N = Netto, B = Brutto, p = Prozentsatz			
// N = B / (p / 100 + 1)
// Mwst. = N * (p / 100)

function floatToMwSt(val){	
	n = val / (19 / 100 +1);
	mwst = n * (19 / 100);
	val = floatToPrice(mwst);
	return val;
}

function floatToPrice(val){
	val = val*1;
	val = val.toFixed(2);
	val = val.toString();
	val = val.replace(/\./,',');
	
	return val;
}




/**
 * 
 * @return
 */
function jumpToURL() {
  dropdown = document.getElementById('yearSelect');  
  for(i=0; i < dropdown.length; i++)
  {
  	if(document.getElementById('yearSelect').options[i].selected == true && document.getElementById('yearSelect').options[i].value!='')
  	{
  		window.location.href = document.getElementById('yearSelect').options[i].value;
  	}
  }    
}



function fadeTab(element, cId) {
	var ele = $(element);
	var content = $("#"+cId);
	var box = content.parent();
	var current = box.find(".current");

	
	if (cId != current.attr("id")){
		ele.parent().find(".current").removeClass("current");
		//current.removeClass("current").animate({ left: parseInt(-box.width())});
		
		var currentHeight = current.height();

		content.css("left",-10000).show();
		var contentHeight = content.height();
		content.css("left",0).hide();

		var newheight = (currentHeight > contentHeight) ? currentHeight : contentHeight;

		
		//box.css("height", newheight);
		box.animate({height: newheight},500);
		current.removeClass("current").fadeOut(500, function(){
			content.addClass("current").fadeIn(500, function(){
				//box.removeAttr('style');
			});
		});
		box.animate({height: contentHeight},500);
		
		//content.css("left",box.width());
		//content.addClass('current').animate( { left: 0});
		ele.addClass('current');
	}
}

function hideTabs(){
	// move Tab content  
	//$(".ce-tab .content-box").find(".content:not(.current)").css("left",$(".ce-tab .content-box").width());
	$(".ce-tab .content-box").find(".tab-content:not(.current)").css("display","none");
	
}

//autoscroll control
function initCallback(carousel){
	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
};

//	circular scroll
var carousel_itemList = [];
var carousel_itemHTML = [];

function carousel_itemVisibleInCallback(carousel, item, i, state, evt){
    var idx = carousel.index(i, carousel_itemHTML.length);
    carousel.add(i, carousel_itemHTML[idx - 1]);
    carousel.startAuto();
};

function carousel_itemVisibleOutCallback(carousel, item, i, state, evt){
    carousel.remove(i);
}; 


jQuery(document).ready(function() {
	$('#carousel1 li').each(function(i){
		  carousel_itemHTML.push($(this).html());
		});
	
    jQuery('#carousel1').jcarousel({
	    auto: 3,
	    animation: 500,
    	scroll: 1,
        wrap: 'circular',
        initCallback: initCallback,
        itemVisibleInCallback: {onBeforeAnimation: carousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: carousel_itemVisibleOutCallback} 
    });
});

