$(document).ready(function () {

	/**
	 * Variables
	 */

	slideTimeOut = 10000;
	fadeTimeOut = 1000;
	headerFade = 750;

	
	/**
	 * Carousel
	 */
	$('#carousel').jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
		visible: 4
	});

	/**
	 * Page block images
	 */

	$('.pci-nav li').mouseenter(function (){

		// Get id
		id = $(this).attr('id');

		$(this).parents('.product-item').find('.pci-nav li').removeClass('active');
		$(this).addClass('active');
	
		// Do something with it (hide all tabs / show current tab based on id)
		id = id.replace ('block', 'img');

		$(this).parents('.product-item').find('.image').css('display', 'none');

		$('#'+id).css('display', 'block');

	});

	/**
	 * Header
	 */

	var items = $('ul#header-images li');

	// Custom animation
	$("ul#header-blocks li").mouseenter(function(){

		var title = $(this).attr('id').replace('block_', '');

		active_img = $('ul#header-images li.active');
		active_block = $('ul#header-blocks li.active');

		slide_img = $('ul#header-images li#' + title);

		// Remove active
		active_img.removeClass('active');
		active_block.removeClass('active');

		slide_img.addClass('active');
		$(this).addClass('active');

		var next_item = title.replace ('nr', '');

		$(active_img).fadeOut(fadeTimeOut);
		$(slide_img).fadeIn (fadeTimeOut);

		clearInterval(loader);

		loader = setInterval (function () {

		console.log ($(active_img).attr('id'));
		
			// Remove active
			active_img = $('ul#header-images li.active');
			active_block = $('ul#header-blocks li#block_' + $(active_img).attr('id') + '.active');

			// Calculate next
			next = items.index(active_img) + 1;

			// Check for offset
			if (next == items.length) {
				next = 0;
			}

			// Fetch next
			slide_img = items.get(next);
			slide_block = $('ul#header-blocks li#block_' + $(slide_img).attr('id'));

			// Remove active
			$(active_img).removeClass('active');
			$(active_block).removeClass('active');

			$(slide_img).addClass('active');
			$(slide_block).addClass('active');

			$(active_img).fadeOut(fadeTimeOut);
			$(slide_img).fadeIn (fadeTimeOut);

		}, slideTimeOut);

	});

	if (items.length > 1) {

		$('ul#header-images li').css ("display", "none");
		$('ul#header-images li.active').css ("display", "block");

		loader = setInterval (function () {

			// Remove active
			active_img = $('ul#header-images li.active');
			active_block = $('ul#header-blocks li#block_' + $(active_img).attr('id') + '.active');

			// Calculate next
			next = items.index(active_img) + 1;

			// Check for offset
			if (next == items.length) {
				next = 0;
			}

			// Fetch next
			slide_img = items.get(next);
			slide_block = $('ul#header-blocks li#block_' + $(slide_img).attr('id'));

			// Remove active
			$(active_img).removeClass('active');
			$(active_block).removeClass('active');

			$(slide_img).addClass('active');
			$(slide_block).addClass('active');

			$(active_img).fadeOut(fadeTimeOut);
			$(slide_img).fadeIn (fadeTimeOut);

		}, slideTimeOut);
	}

});




function handle_header (type) {

	var items = $('ul#header-quotes li');
	var active_img = $('ul#header-quotes li.active');

	if (type == 'next') {

		// Calculate next
		next = items.index(active_img) + 1;

		// Check for offset
		if (next == items.length) {
			next = 0;
		}

		slide_img = items.get(next);

	} else {

		// Calculate next
		prev = items.index(active_img) - 1;

		if (prev == -1) {
			prev = items.length -1;
		}

		slide_img = items.get(prev);
	}

	// Remove active
	$(active_img).removeClass('active');
	$(slide_img).addClass('active');

	$(active_img).fadeOut(headerFade);
	$(slide_img).fadeIn (headerFade);
}

/**
 * Change view method
 */

function setView (method) {

	$.ajax({
		type: 'GET',

		url: 'action/setviewmethod.php',
		data: 'method=' + method,
	  	success: function(d) {

	  		$('#view-options span').removeClass('active');
	  		$('#view-'+method).addClass('active');

			$.ajax({
				type: 'GET',

				url: 'action/product_list.php',
				data: 'searchstring=' + $('#livesearch').val(),
			  	success: function(data) {

			  		$('#product-overview').html(data);

			  		Cufon.refresh();
			  	},
				error: function(error) {
		    		alert(error);
				}
			});
	  	},
		error: function(error) {
    		alert(error);
		}
	});
}

