$(document).ready(function () {

	$("a.iframe").fancybox({
		'width'				: 660,
		'height'			: 560,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'   : 'no'

	});


	/**
	 * Fancybox
	 */
	$("a.fancybox").fancybox({
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			if(title) {
				// Split the title into two strings, one for description and one for download url
				var titleText 		= title.substring(0,title.indexOf("|"));
				var highresImage 	= title.substring(title.indexOf("|")+1);
				
				if(title.indexOf("|") <= 0) {
					titleText = title;
					highresImage = "";
				}
				
				return '<span id="fancybox-title-over"><span class="fancybox-descr">' + imageText + ' ' + (currentIndex + 1) + 
				' / ' + currentArray.length + (titleText.length ? ' &nbsp; ' + titleText : '') + '</span>' + (highresImage.length ? 
				' <span class="fancybox-download"><a href="' + highresImage + '">' + downloadText + '</a></span> ' : '') + '</span>';
			}
			else {
				return '<span id="fancybox-title-over"><span class="fancybox-descr">' + imageText + ' ' + (currentIndex + 1) + 
				' / ' + currentArray.length + '</span></span>';
			}
		}
	});
	
	
	/**
	 * Jcarousel lite
	 */
	var current_image = 1;
	var slideshow_length = $("#slideshow ul li").length;
	if(slideshow_length > 1) {
		if(slideshow_length == 2){
			$('#slideshow ul li').clone().insertAfter('#slideshow ul li:last');
		}

		$("#slideshow").jCarouselLite({
			btnNext: "#slideshow-holder a.next",
			btnPrev: "#slideshow-holder a.prev",
			speed: 400,
			afterEnd: function(items) {
				items.each(function(i) {
					if(i == 0) {
						var id = $(this).attr("id").split("-");
						$("ul#slideshow-thumbs li#thumb-"+thumb_image).hide();
						current_image = id[1];
						$("#slideshow-holder a.button.hover").mouseover();
						return false;
					}
				});
			}
		});
	} else {
		$("#slideshow-holder a.button").hide();
	}
	
	// Jcarousel buttons
	$("#slideshow-holder a.button").hover(function() {
		$(this).addClass("hover");
		if($(this).hasClass("prev")) {
			$("ul#slideshow-thumbs").removeClass("next").addClass("prev");
			thumb_image = parseInt(current_image)-1 > 0 ? parseInt(current_image)-1 : slideshow_length;
		} else {
			current_image_add = parseInt(current_image)+1;
			$("ul#slideshow-thumbs").removeClass("prev").addClass("next");
			thumb_image = parseInt(current_image)+1 <= slideshow_length ? parseInt(current_image)+1 : 1;
		}
		$("ul#slideshow-thumbs li#thumb-"+thumb_image).show();
	}, function() {
		$("ul#slideshow-thumbs li#thumb-"+thumb_image).hide();
		$(this).removeClass("hover");
	});
	
	
	/**
	 * Product information
	 */
	$("ul.more-info-menu li a, .product-group .popup ul li a").click(function() {
		var group = $(this).closest(".product-group");
		var contentClass = $(this).attr("class");
		
		if(!$(group).children(".more-information").is(":visible")) {
			$(group).find(".trigger").click();
		}
		
		$(group).find("ul.more-info-menu li.active").removeClass("active");
		$(group).find("ul.more-info-menu li a."+contentClass).closest("li").addClass("active");
		
		$(group).find(".more-info-content .content:visible").hide();
		$(group).find(".more-info-content ."+contentClass).show();
		//$(group).children(".more-information .more-info-content ."+$(this).attr("class")).show();
	});
	$(".product-group .trigger").click(function() {
		var group = $(this).closest(".product-group");
		var moreInfoLink = $(this).children("a.more-info");
		$(moreInfoLink).hasClass("collapse") ? $(moreInfoLink).removeClass("collapse") : $(moreInfoLink).addClass("collapse");
		
		// Skip slideToggle id IE6/IE7
		if($("body").hasClass("ie6") || $("body").hasClass("ie7")) {
			$(group).children(".more-information").toggle();
		} else {
			$(group).children(".more-information").slideToggle();
		}		
	});
	
	
	/**
	 * IE6 - Megadrop
	 */
	if($.browser.msie && $.browser.version=="6.0") {
		$("#primary-nav li:not(#primary-nav li li)").hover(function() {
			$(this).addClass("hover");
		}, function() {
			$(this).removeClass("hover");
		});
	}
	
	
	/**
	 * Tooltip
	 */
	$('.bubble-info').each(function () {
		var distance = 10;
		var time = 150;
		var hideDelay = 0;
	
		var hideDelayTimer = null;
	
		var beingShown = false;
		var shown = false;
		var trigger = $('.trigger', this);
		//var info = $('.popup', this).css('opacity', 0);
		var info = $('.popup', this);
		var bubbleInfo = $(this);
		
		$([trigger.get(0), info.get(0)]).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				// don't trigger the animation again
				return;
			} else {
				// reset position of info box
				beingShown = true;
				var leftPos = bubbleInfo.hasClass("side") ? trigger.width() : (trigger.width() /2);
				if($("body").hasClass("ie6") || $("body").hasClass("ie7")) {
					bubbleInfo.css("z-index", 1000);
					bubbleInfo.css("direction", 'ltr');
				}
				info.css({
					top: 40,
					left: leftPos,
					display: 'block'
				}).animate({
					top: '-=' + distance + 'px'//,
					//opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
			}
	
			return false;
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				info.animate({
					top: '-=' + distance + 'px'//,
					//opacity: 0
				}, time, 'swing', function () {
					shown = false;
					info.css('display', 'none');
					if($("body").hasClass("ie6") || $("body").hasClass("ie7")) {
						bubbleInfo.css("z-index", 200);
						bubbleInfo.css("direction", 'ltr');
					}
				});
			}, hideDelay);
	
			return false;
		});
	});

		
	// All input fields assigned with the class 'default' will
	// get their default values hidden when clicked, and reset again
	// if the user leaves the field empty or hasnt changed anything
	// The searchfields needs an accompanying hidden input with the
	// same id but suffixed "-default" that contains the default value.
	$("input.default").each(function() {
		var defaultVal = $(this).next(".defaultvalue").val();
		if (!$(this).val())
			$(this).val(defaultVal);
		$(this).focus(function() {
			if ($(this).val() == defaultVal)
				$(this).val("");
		});
		$(this).blur(function() {
			if ($(this).val() == "")
				$(this).val(defaultVal);
		});
	});

	// Hook up the searchbox
	$("#search .button a").click(function() {
		var url = $(this).attr("href");
		url += "?query=" + encodeURIComponent($("#search .searchfield").val());
		document.location = url;
		return false;
	});
	$("#search .searchfield").keydown(function(e) {
		if (e.keyCode == 13) {
			$("#search .button a").click();
			return false;
		}
	});
	
	
});


/**
 * Jcarousel change callback.
 */
function jcarousel_change(items) {
	var id  = 0;
	items.each(function(i) {
		if(i == 0) {
			id = $(this).attr("id").split("_");
			return false;
		}
	});
	
	$("#slideshow-images ul li:visible").css({zIndex:99}).fadeOut(500);
	$("#slideimage_"+id[1]).css({zIndex:90}).fadeIn(500);
}
