// JavaScript Document
// execute your scripts when the DOM is ready. this is mostly a good habit
$(function() {

	$('#mainNav ul ul li.current').parents('li').addClass('current')
	
	$('#mainNav ul li').first().addClass('first');
	$('#mainNav ul li').last().addClass('last');
			
	// select one or more elements to be overlay triggers
	$("#siteLinks a.overlayTrigger").overlay({
		target: '#login',		 
		color: '#ccc',
		top: 200,
		left: 200
	});

	// select one or more elements to be overlay triggers
	if ($('#productBody div.image').width()) {
		$("#productBody div.image a").overlay({
			target: '#gallery',		 
			color: '#ccc',
			closeOnClick: true,
			template: '<strong>${title}</strong> <span>${index} / ${total}</span>'
		}).gallery();
	}
	
	if ($('#contentPage #content div.images').width()) {
		// select the thumbnails and make them trigger our overlay 
		$("#content div.images a").overlay({ 
			// each trigger uses the same overlay with the id "gallery" 
			target: '#gallery',		 
			top: 200,
			left: 200
		// let the gallery plugin do its magic! 
		}).gallery();
	}

	// Load twitterfeeds from Masfriesland
	if ($('#twitterFeed').height()) {
		$('#twitterFeed p').remove();
		$("#feeds").getTwitter({
			userName: "masfriesland",
			numTweets: 3,
			loaderText: "Tweets laden...",
			slideIn: true,
			showHeading: false,
			headingText: "Laatste tweets",
			showProfileLink: false
		});
	}
	
	// Add odd class to odd li's
	$('#feeds li:odd').livequery(function(){
		$(this).addClass('odd');
	});
	
	// Remove default text from search field and put it back on blur
	$('#rightColWrapper input.zoeken').focus(function(){
		if($(this).val() == 'Doorzoek website') {
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val() == '') {
			$(this).val('Doorzoek website');
		}
	});
	
	
	$("#wrapper div.blokje").each(function() {
		$(this).click(function() {
			var mylinks = $(this).find('a');
            if (mylinks.size() > 0) {
            	document.location = mylinks[mylinks.size() - 1].href;
            }
		});
		this.style.cursor = 'pointer';
	});
	
	$('#headerFader').innerfade({
				speed: 'slow',
				timeout: 4000,
				type: 'sequence',
				containerheight: '229px'
			});

});

function wlGetWishlist() {
	jQuery.getJSON('/wishlist/getshortlist/?rnd=' + new Date().getTime(), function(data) {
		$('#wishlistbox').html(data.wishlist);
	});
}

function wlAddProduct(productid) {
	jQuery.getJSON('/wishlist/addproduct/?prdid=' + productid + '&rnd=' + new Date().getTime(), function(data) {
		alert(data.message);
		wlGetWishlist();
	});
	
	return false;
}

function wlDelProduct(productid) {
	jQuery.getJSON('/wishlist/deleteproduct/?prdid=' + productid + '&rnd=' + new Date().getTime(), function(data) {
		if (data.ok) {
			wlGetWishlist();
		}
		else {
			alert(data.message);
		}
	});
	
	return false;
}
