$(document).ready(function() {
	// Setup startpage main-image gallery
	initGallery();
	
	// Sets the correct width/height for the product entrances div
	// needed for IE so we can hide the div on page load
	var cssObj = {
		'width' : '922px',
		'height' : '248px'
	}
	
	// Fix PNG-transparency in IE
	$(document).pngFix();
	
	// Topmenu: show products if hovering the products tab, else hide
	$("#product-entrances").hide();
	
	 // Apply the css to the #product-entrances div
 	$("#product-entrances").css(cssObj);
	
	$("#primary-nav ul li").mouseover(function() {
		if($(this).attr("id") == "products") {
			$("#hover-wrapper").show();
			
			jQuery.each(jQuery.browser, function(i) {
 				 if($.browser.msie) {
					$("#product-entrances").show();
	 			 } else {
		 			$("#product-entrances").fadeIn(400);
	  			}
			});
		}
		else {
			$("#hover-wrapper").hide();
			$("#product-entrances").hide();
		}
	});

	// Topmenu: hide the products if you hover outside of the div
	$("#template").mouseover(function() {
		$("#product-entrances").hide();
	});

	// Topmenu: change backgroundcolor of the product div on hover
	$("#product-entrances .product").hover(function () {
		$(this).addClass("p-hover");
	}, function () {
		$(this).removeClass("p-hover");
	});

	// Toggle "tip a friend"-form
	$(".tip-toggle").click(function() {
		toggleTipAFriend();
		return false;
	});

	// Print-button
	$("#print a").click(function() {
		window.print();
		return false;
	});

	// Maintain default value for searchboxes
	var searchDefault = $("#searchfield_initialvalue");
	var searchInput = $("#searchfield");
	if (searchDefault.length == 1) {
		// Set default value when page loads
		if (!searchInput.val())
			searchInput.val(searchDefault.val()).addClass("initalvalue");
		searchInput.focus(function() {
			// Empty box when focused
			if ($(this).val() == searchDefault.val())
				$(this).val("").removeClass("initalvalue");
		}).blur(function() {
			// Re-set default value when leaving the box empty
			if ($(this).val() == "")
				$(this).val(searchDefault.val()).addClass("initalvalue");
		});
	}

	// Submit top search
	$("#header_search").keydown(function(e) {
		if (e.keyCode == 13) {
			submitTopSearch();
			return false;
		}
	});
	$("#header-search-btn").click(function() {
		submitTopSearch();
		return false;
	});

	// Submit page search
	$("#searchfield").keydown(function(e) {
		if (e.keyCode == 13) {
			submitPageSearch();
			return false;
		}
	});
	$(".search-button a").click(function() {
		if (searchInput.val() == searchDefault.val())
			searchInput.val("").removeClass("initalvalue");
		submitPageSearch();
		return false;
	});

	// Open typecode-links in new window
	$("a.typecode").click(function() {
        window.open($(this).attr("href"), "newwindow", "width=550,height=550,toolbar=no,location=no,resizable=yes,scrollbars");
		return false;
	});
});

function toggleTipAFriend() {
	$("p#tip a").toggleClass("active");
	$("#tip-a-friend").slideToggle(250);
}

function initGallery() {
	$('.gallery').addClass('gallery_demo'); // adds new class name to maintain degradability

	$('ul.gallery_demo').galleria({
		history   : false, // activates the history object for bookmarking, back-button etc.
		clickNext : true, // helper for making the image clickable
		insert    : '#main_image', // the containing selector for our main image
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

			// fade in the image & caption
			image.css('display','none').fadeIn(1000);
			caption.css('display','none').fadeIn(1000);

			// fetch the thumbnail container
			var _li = thumb.parents('li');

			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.3);

			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');

			// add a title for the clickable image
			image.attr('title','Next image >>');
		},
		onThumb : function(thumb) { // thumbnail effects goes here

			// fetch the thumbnail container
			var _li = thumb.parents('li');

			// if thumbnail is active, fade all the way.
			var _fadeTo = _li.is('.active') ? '1' : '0.3';

			// fade in the thumbnail when finnished loading
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);

			// hover effects
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
			)
		}
	});
}

// Slideshow on the startpage-gallery
var userClickedThumbnail = false;
$(document).ready(function() {
	slideshowSwitch(1);

	$("#template.startpage .gallery li").click(function() {
		userClickedThumbnail = true;
	});
});

function slideshowSwitch(currentImage) {
	// Stop iterating if user has clicked
	if (userClickedThumbnail)
		return;

	// Simulate click
	$("#template.startpage .gallery li:nth-child(" + currentImage + ") img").click();

	// Start next iteration
	var maxCount = $("#template.startpage .gallery li").length;
	var nextImage = currentImage >= maxCount ? 1 : currentImage + 1;
	setTimeout(function() { slideshowSwitch(nextImage); }, 5000);
}

function submitTopSearch() {
	var url = $("#header-search-btn").attr("href");
	var query = $("#header_search").val();
	if (query)
		url += '?query=' + encodeURIComponent(query);
	document.location = url;
}

function submitPageSearch() {
	var url = $(".search-button a").attr("href");
	var query = $("#searchfield").val();
	if (query)
		url += '?query=' + encodeURIComponent(query);
	document.location = url;
}
