// JavaScript Document
var newpage = 1;

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
createCookie('verify_cookie', 'Y', 1);


$(document).ready(function(){
	$('ul#slideshow').innerfade({
		speed: 1000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '370px'
	});
	
	/*$.getScript("http://www.hagsus.com/feedback/feedback-1.0.js", function(){
	});*/
	
	$.getScript("http://www.hagsus.com/feedback/fancybox/jquery.fancybox-1.3.0.pack.js", function(){
	});
	
	$.getScript("http://www.hagsus.com/feedback/fancybox/jquery.mousewheel-3.0.2.pack.js", function(){
	})
	
	/***** jQuery - history *****/
	
	// PageLoad function
	// This function is called when:
	// 1. after calling $.historyInit();
	// 2. after calling $.historyLoad();
	// 3. after pushing "Go Back" button of a browser

	// Initialize history plugin.
	// The callback is called at once by present location.hash.
	
	var pathname = window.location.pathname;
	$.historyInit(pageload, pathname);
	
	// set onlick event for buttons
	$("a.pager22").click(function(){
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
		return false;
	});  

	
	/***** Sortera *****/
	$('#togglePlayfunctions').click(function() {
    	$('#playfunctionsContainer').slideToggle("normal");
	});
	
	function hags_init() {
		Cufon.replace('ul#topnav li a span, h1,h2,ul#topNavigation li a,ul#subNavigation li a, div#left_searchfield .left, ul#producttypelist li, .header_1, .fotter_headline',{ fontFamily: 'stagsemibold'});
		Cufon.replace('h3, #subHolder',{ fontFamily: 'stag_sans_semibold'});
		
		Cufon.now();
		setup_paging();
		
		
	}
	
	function setup_paging(){
		newpage = 1;
		
		$("a.pager").click(function(){	
			newpage = $(this).attr('rel')	
			$('input#page_nr').val(newpage);	
			dothesearch(true);	
			return false;
	});
	}
	
	function checkContentHeight() {
	  var browserName=navigator.appName; 
	  var rightcolum_h = $('#right_column').height(); //document.getElementById("right_column").clientHeight;
	  var content_h = $('#tab-1').height(); //document.getElementById("content").clientHeight;
	  var bigbanner_h = $('#bigBanner').height();//document.getElementById("bigBanner").clientHeight;
	  var productspec_h = $('#productspecifikation').height();//document.getElementById("productspecifikation").clientHeight;
	  var relatedprod_h = $('#relatedprod').height();//document.getElementById("relatedprod").clientHeight;
	  if((rightcolum_h) > (content_h+bigbanner_h)-(relatedprod_h)){
		var diff = (rightcolum_h)-(content_h+bigbanner_h)-(relatedprod_h);
		if (diff<150){
			$('#tab-1').height((rightcolum_h-bigbanner_h)-125); 
		}else{
			$('#tab-1').height((rightcolum_h-bigbanner_h)-200);
		}
		
		//;document.getElementById("productspecifikation").style.height
	  }
	  
	  
	}
	
	function initImages(){	
		var url = $('a.thumb:first').attr('href');	
		var img = new Image();
		
		$(img).load(function () {
			/*
			$(this).hide();		
			
			if($('#bigPic').attr('class')!='inspiration_image'){
				//$('#bigPic').width(this.width);
			}else{
				//$(this).width(620);
			}
			*/
			$('#bigPic').empty().append($(this));
		
			$(this).fadeIn();
			
		}).error(function () {
			// notify the user that the image could not be loaded
		}).attr('src', url);
	}
	
	/*
	$('a#showPlayfunctions').click(function() {
		$('#playfunctions').show();
		$('#showPlayfunctions_box').hide();
		$('#hidePlayfunctions_box').show();
	});
	
	$('a#hidePlayfunctions').click(function() {
		$('#playfunctions').hide();	
		$('#showPlayfunctions_box').show();
		$('#hidePlayfunctions_box').hide();
	});
	*/
	
	$("#inputString").click(function () {
		$(this).select();
	});
	
	$(".icon").click(function () {
		var thebox = '#' + $(this).attr('rel') + '_box';	
		var theinput = 'input.' + $(this).attr('rel') + 'Alt';
		
		if($(this).hasClass('hide')){
			 $(thebox).hide();
			
			$(this).removeClass('hide');
			$(this).addClass('show');
			 
			$(this).html('<img src="fileadmin/www/hags.com/usa/img/icon_show.png" alt="show" />');
		}else if($(this).hasClass('show')){
			$(thebox).show();
			$(this).removeClass('show');
			$(this).addClass('hide');
			$(this).html('<img src="fileadmin/www/hags.com/usa/img/icon_hide.png" alt="hide"/>');
		}
		else if($(this).hasClass('reset')){
			$(theinput).each(function(i) {
		   		this.checked = false;
			});
		}
		
	});

	$("thesearch").submit(function() {
      
	  if ($("input:first").val() == "correct") {
        $("span").text("Validated...").show();
        return true;
      }
      $("span").text("Not valid!").show().fadeOut(1000);
      return false;
	  
    });
	
	var catinfo_box_height = $("div#catinfo_box").height();
	var slide_catinfo_box = true;
	if(catinfo_box_height<197){
		$("div#show_hide_catinfo").html('');
		slide_catinfo_box=false;
	}
	$("div#catinfo_box").height(197);
	
	$("div#show_hide_catinfo").click(function(){
		if($(this).attr("rel") == "show"){
			$(this).attr("rel", "hide");
			$(this).html('<img src="/fileadmin/www/hags.com/usa/img/icon/hide.png" alt="Hide"/ >');
			$("div#catinfo_box").animate({ 
				height: catinfo_box_height
			 }, 500 );
		}else{
			$(this).attr("rel", "show");
			$(this).html('<img src="/fileadmin/www/hags.com/usa/img/icon/show.png" alt="Show"/ >');
			$("div#catinfo_box").animate({ 
				height: 197
			 }, 500 );
		}
								  									  
	});

	
	$("#searchbutton").click(function () {	   				   
		
		$("#searchbutton_box").fadeOut('slow',function () {
			$("#loader_box").height($('#searchbutton_box').height());		
        	$("#loader_box").fadeIn('slow');
		});
		
		//$('form#thesearch').submit();
		
		return true;
	});
	
	$("form#makesearch").submit(function() { 		
		dothesearch(false);
		
		return false;
	});
	
	$("li.producttypeList").each(function() {
		if($(this).find("input.producttypeAlt").is(":checked")){
			$(this).find("div.right").addClass("checked");
		}else{
			$(this).find("div.right").addClass("unchecked");
		}								  
	});
	
	$("h2#download_cat").click(function(){
		$("div#downloadbox_" + $(this).attr("rel")).slideToggle();
		if($(this).attr("class") == "show"){
			$(this).removeClass("show");
			$(this).addClass("hide")
		}else{
			$(this).removeClass("hide");
			$(this).addClass("show")
		}
	});
	
	$("div.downloadbox").each(function(){
		$(this).hide();								   
	});
	
	
	$("div.downloadbox:first").show();
	$("h2#download_cat:first").addClass("hide")
	
	$("li.producttypeList").click(function() { 
		
		if($(this).find("input.producttypeAlt").is(":checked")){
			$(this).find("input.producttypeAlt").attr('checked', false);
			$(this).find("div.right").removeClass("checked");
			$(this).find("div.right").addClass("unchecked");
		}else{
			$(this).find("input.producttypeAlt").attr('checked', true);
			$(this).find("div.right").addClass("checked");
			$(this).find("div.right").removeClass("unchecked");
		}
		dothesearch(false);
		
		return true;							   
	});
	
	$("div.tabs").each(function(){
		$(this).hide();							
	});
	
	$("div.tabs:first").show();
	
	$("li.clicktabs").click(function(){
		$("li.clicktabs").each(function(){
			$('h1',this).removeClass('active');					
		});
		$("div.tabs").each(function(){
			$(this).hide();							
		});
		$('#' + $(this).attr('rel')).show();
		$('h1',this).addClass('active');
		
		 var browserName=navigator.appName; 
		  var rightcolum_h = $('#right_column').height(); //document.getElementById("right_column").clientHeight;
		  var content_h = $('#' + $(this).attr('rel')).height(); //document.getElementById("content").clientHeight;
		  var bigbanner_h = $('#bigBanner').height();//document.getElementById("bigBanner").clientHeight;
		  var productspec_h = $('#productspecifikation').height();//document.getElementById("productspecifikation").clientHeight;
		  var relatedprod_h = $('#relatedprod').height();//document.getElementById("relatedprod").clientHeight;
		  if((rightcolum_h) > (content_h+bigbanner_h)-(relatedprod_h)){
		
			if (browserName=="Microsoft Internet Explorer"){
				
			}else{
				$('#' + $(this).attr('rel')).height((rightcolum_h-bigbanner_h)-200);
			}
		  }
	});
	
	$('a.limit_products').click(function(){
		$('input#num_show_products').val($(this).attr('rel'));
		dothesearch(true);
		return false;
	});
			
	function dothesearch(paging){
		var producttypes = '';
		var playfunctions = ''; 
		var price;
		var savedproducts = (paging) ? $('input#prod_uids').val() : '';
		var pid = $('input#pid').val();
		var age;
		var catUid = $('input#catUid').val();	
		var area = $('input#area').val();
		var limit = $('input#num_show_products').val();
		
		$('div#loaderbox').fadeIn('normal');
		
		$('input.priceAlt').each(function(i) { 
		   if($(this).is(":checked")) {
		  	 price += $(this).val() + '-';
		   }
		});
		
		$('input.ageAlt').each(function(i) { 
		   //age = $(this).val();
		   if($(this).is(":checked")) {
			   age += $(this).val() + ',';
		   }
		});
		
		$('input.producttypeAlt').each(function(i) { 
		   if($(this).is(":checked")) {
		  	 producttypes += $(this).val() + ',';
		   }
		});
		
		$('input.playfunctionsAlt').each(function(i) { 
		   if($(this).is(":checked")) {
		  	 playfunctions += $(this).val() + ',';
		   }
		});
		
		
		
		$.ajax({  
           type: 'GET',  
           url: '/fileadmin/www/hags.com/usa/php/dolivesearch.php',  
           data: 'producttypes=' + producttypes + '&playfunctions=' + playfunctions + '&price=' + price + '&age=' + age + '&pid=' + pid + '&area=' + area + '&catUid=' + catUid + '&newpage=' + newpage + '&limit=' + limit + '&saved=' + savedproducts,   
           success: function(data)  
           {  		   		
		   		$('div#loaderbox').fadeOut('slow');
           		$('#products').html(data); 
				hags_init();
				setup_paging();	
				
				//alert(pathname);
				//hash = window.location.pathname;
				//hash = hash.replace(/^.*#/, '');
			
				//alert(hash);
			
				// moves to a new page. 
				// pageload is called at once. 
				// hash don't contain "#", "?"
				//$.historyLoad(hash);
				
           		
		   }
       });	
	
	}
	
	$('intput.searcher').click(function(){
		$('input#prod_uids').val('');							
	});
	
	$('input.spec_propertie').click(function(){
											 
	});
				
	$('a.thumb').click(function(){
										
		var url =$(this).attr('href');
		
		//var newHeight = 450;
		
		description = '.'+$(this).attr('id');
		
		//$('.image_description div').each(function() { $(this).hide(); })
		//$(description).show();
		$('#bigPic').fadeOut('fast', function(){	
						
			var img = new Image();
			
			$(img).load(function () {	 
				
				$('#bigPic').empty().append(this).fadeIn("slow");
				
			}).error(function () {
				// notify the user that the image could not be loaded
			}).attr('src', url);								  								  
		});
		
		
		return false;
	});
	
	if($('div#products').hasClass('product_container')){	
		newpage = $('input#page_nr').val();
		dothesearch(true);	
	}
	
	hags_init();
	checkContentHeight();
	initImages();
	
	
	
	
});
/***** History funktioner *****/
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();
	}
}

/***** Övriga funktioner *****/

$(function()  
 {  
   var hideDelay = 100;    
   var currentID;  
   var hideTimer = null;  
   
   // One instance that's reused to show info for the current person  
   var container = $('<div id="playfeatureContainer">'  
       + '   <div id="playfeatureContent"></div>'  
       + '</div>');  
   
   $('body').append(container);  
   
   $('.playfeatureTrigger').live('mouseover', function()  
   {  
       // format of 'rel' tag: pageid,personguid  
       var settings = $(this).attr('rel').split(',');  
       var pageID = settings[0];  
       currentID = settings[1];  
   
       // If no guid in url rel tag, don't popup blank  
       if (currentID == '')  
           return;  
   
       if (hideTimer)  
           clearTimeout(hideTimer);  
   
       var pos = $(this).offset();  
       var width = $(this).width();
	   var height = $(this).height()
       container.css({  
           left: (pos.left - 250) + 'px',  
           top: pos.top + (height)-20 + 'px'  
       });
   
   		
       $.ajax({  
           type: 'GET',  
           url: '/fileadmin/www/hags.com/usa/php/getplayfunctiondata.php',  
           data: 'page=' + pageID + '&guid=' + currentID,  
           success: function(data)  
           {  
                    
                   $('#playfeatureContent').html(data); 
			 
           }
		  
		   
       });  
   
       container.css('display', 'block');  
   });  
   
     
   $('.playfeatureTrigger').live('mouseout', function()  
   {  
       if (hideTimer)  
           clearTimeout(hideTimer);  
       hideTimer = setTimeout(function()  
       {  
           container.css('display', 'none');  
       }, hideDelay);  
   });  
   
   // Allow mouse over of details without hiding details  
   $('playfeatureContainer').mouseover(function()  
   {  
       if (hideTimer)  
           clearTimeout(hideTimer);  
   });  
   
   // Hide after mouseout  
   $('playfeatureContainer').mouseout(function()  
   {  
       if (hideTimer)  
           clearTimeout(hideTimer);  
       hideTimer = setTimeout(function()  
       {  
           container.css('display', 'none');  
       }, hideDelay);  
   });  
 });

