// JavaScript Document




$(document).ready(function(){
	
	/* jQuery - History
	 * Funktion för att hantera history bland produkter */
	/*
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload, "index.php");
	
	// set onlick event for buttons
	$("a[rel='history']").click(function(){
		// 
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		// moves to a new page. 
		// pageload is called at once. 
		// hash don't contain "#", "?"
		$.historyLoad(hash);
		return false;
	});
	*/
	
	$('#topnav_v2_toggle').click(function() {
		$('#topnav_v2_container').slideToggle(400);	
		return false;
	});
	
	function init_Crescent() {
		
		Cufon.replace('h1,h2,h3,ul#topNavigation li a,ul#subNavigation li a, #topnav_v2 a, #topnav_v2_toggle, #topnav_v2_title',{ fontFamily: 'cachetstd'});
		 //#topnav_v2 a, #topnav_v2_toggle, #topnav_v2_title'
		Cufon.replace('#topnav_v2 li a', {
			hover: true,
			hoverables: { color: "#FFFFFF", strong: true, em: true }
		});

		Cufon.now();
		
		$(function() {
			$(".image").click(function(e) {
				var image = $(this).attr("rel");
				var popup = $(this).find('#popup_imageSrc').html();
				
				$('#image').hide();
				$('#image').fadeIn(300);
				$('#image').html('<a id="fancy_productImage" href="'+popup+'"><img src="' + image + '" border="0" /></a>');
				
				$("a#fancy_productImage").fancybox({
					'speedIn'		:	600, 
					'speedOut'		:	200, 
					'overlayShow'	:	false
				});
				
				return false;
			});
		});
	}
	
	//$("a#fancy_productImage").fancybox();
	$("a#fancy_productImage").fancybox({
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false
	});


	
	$('#technicalSpec').click(function() {
		$('#technicalSpecInfo').slideToggle();
	});
	
	/***** Övriga funktioner *****/
	//Tar bort tomma <p> - templösning
	$(".blurb").each(function() {
		if(!$(this).find("p:first").html().length) {
			$(this).find("p:first").remove();
		}
	});
	
	/***** Formulärfunktioner *****/
	$('.submit_searchProducts').click(function() {	   
		$('.forceCategory').val("");						   
		$('#form_filterProducts').submit();
	});
	$('.submit_compareProducts').click(function() {
		$( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
		$('#form_listProducts').submit();
	});

	$('#form_singleProduct').ajaxForm({
		url:		'/fileadmin/www/crescent.se/php/product_functions/crescent_formFunctions.php',
		target:		'#singleProduct2',   	// target element(s) to be updated with server response 
		success:	success_singleProduct	// post-submit callback 
	});

	$('#form_filterProducts').ajaxForm({
		url:		'/fileadmin/www/crescent.se/php/product_functions/crescent_formFunctions.php',
		target:		'#listProducts',   // target element(s) to be updated with server response 
		success:	success_listProducts  // post-submit callback 
	});
	
	$('#form_listProducts').ajaxForm({
		url:		'/fileadmin/www/crescent.se/php/product_functions/crescent_formFunctions.php',
		target:		'#singleProduct',   // target element(s) to be updated with server response 
		success:	success_listProducts  // post-submit callback 
	});	
	
	function success_listProducts(responseText, statusText) {

		init_Crescent();
		
		$(".displayProduct").click(function() {
			$('#singleUid').val($(this).attr("id"));
			//$('#form_singleProduct').submit();
			
			$( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
		});
	}
	
	function success_singleProduct(responseText, statusText) {
		//Sätter cufon
		init_Crescent();
	}
	
	function showResponse(responseText, statusText)  {
		//init_Crescent();
	}
	
	//Postar formuläret när sidan laddas för att hämta produkter
	if($('.forceCategory').length) {
		$('.forceCategory').val($.query.get('forceCategory'));
		$('#form_filterProducts').submit();
	}
	
	if($('#force_singleUid').val()) {
		$('#singleUid').val($('#force_singleUid').val());
		$('#form_singleProduct').submit();
	}
	
	/***** Crescent Återförsäljare *****/
	$("#listResellers").tablesorter();
	
	init_Crescent();
});

//Öppnar tillbehör
function show_asseccories(id) {
	//alert("Kommer hit " +id);
	$('#singleUid').val(id);
	$('#product_type').val('accessory');
	$('parent_title').val('accessory');
	$('parent_uid').val('accessory');
	$('#form_singleProduct').submit();
}

/* jQuery - History
 * Funktion för att hantera history bland produkter */
function pageload(hash) {
	// alert("pageload: " + hash);
	// hash doesn't contain the first # character.
	if(hash) {
		// restore ajax loaded state
		if($.browser.msie) {
			// jquery's $.load() function does't work when hash include special characters like aao.
			hash = encodeURIComponent(hash);
		}
		$("#load").load(hash + ".html");
	} else {
		// start page
		$("#load").empty();
	}
}