// Show popup function
function show_popup(html_data,headline,current_div)
{
		$("body").prepend('<div class="sb_popup_bg"></div>');
		
		// Only for ie
		if($.browser.msie)
		{
			scrollTop = document.body.scrollTop + 'px';
			$("div.sb_popup_bg").css({"top":scrollTop});
		}
		
		current_div.append('<div class="sb_popup_over"><div class="sb_popup">' +
								'<div class="top_part">' +
									'<h2>' + headline + '</h2>' +
									'<a class="close_popup">Stäng [x]</a>' +
								'</div>' +
								'<div class="table_scrollbox">' +
									html_data +
								'</div>' +
							'</div></div>');
							
		if(window.pageYOffset)
		{
			$("div.sb_popup_over").css({'top' : (window.pageYOffset+50)});
		}
		else
		{
			$("div.sb_popup_over").css({'top' : (document.body.scrollTop+50)});
		}
		
		$("a.close_popup").live("click",function()
		{
			$(this).parent().parent().parent().remove();
			$("div.sb_popup_bg").remove();
		});
		
}

// Set listing design on category listing page
function setListingDesign(design_nr)
{
	alert($('#listing_ul').className);
	$('#listing_ul').removeClass($('#listing_ul').className); 
	$('#listing_ul').addClass("product_listing"+design_nr); 
}
	
// Javascript cookie handling
function setCookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
					( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
					( ( path ) ? ";path=" + path : "" ) +
					( ( domain ) ? ";domain=" + domain : "" ) +
					( ( secure ) ? ";secure" : "" );
}

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
	
	


// Set listing design on category listing page
function setListingDesign(design_nr)
{
	
	$('#page_type_image1').attr("src","/img/page_types/images.gif");
	$('#page_type_image2').attr("src","/img/page_types/image_list.gif");
	$('#page_type_image3').attr("src","/img/page_types/list.gif");
	//$('.headers_ul').hide();
	$('.headers_sort_header').hide();
	
	
	if(design_nr == '1') 
	{
		$('.headers_sort_header').show();
		$('#page_type_image'+design_nr).attr("src","/img/page_types/images_active.gif");
		$('.headers_ul').attr("style","margin-bottom:10px");
		$('.headers_ul').hide();
	}
	else if(design_nr == '2') 
	{
		$('.headers_sort_header').show();
		$('#page_type_image'+design_nr).attr("src","/img/page_types/image_list_active.gif");
		$('.headers_ul').attr("style","margin-bottom:10px");
		$('.headers_ul').hide();
	}
	else if(design_nr == '3') 
	{
		$('#page_type_image'+design_nr).attr("src","/img/page_types/list_active.gif");
		$('.headers_ul').attr("style","margin-bottom:0px");
		$('.headers_ul').show();
	}
	
		$('#listing_ul').hide();
	
		$('#listing_ul').removeClass($('#listing_ul').attr("class")); 
		$('#listing_ul').addClass("product_listing"+design_nr);
		setCookie( 'page_type', design_nr, '', '/', '', '' );
		$('#listing_ul').fadeIn(500);
}

// Setting the correct page type from cookie
function setCurrentDesign()
{
	if ( getCookie( 'page_type' ) )
	{
		setListingDesign(getCookie('page_type'));
	}
	
}

function submitAnswers(orders_id)
{
	
	extra={};
	extra['page']      = "savesurvey";
	extra['orders_id'] = orders_id;
	
	// Setting all inputs
	$(".survey input[type='radio']:checked").each(function(){
		extra[$(this).attr("name")] = $(this).val();
		
		if($(":input[name='surveys_answers_text[" + $(this).val()  + "]']").length > 0)
		{
			extra['surveys_answers_text_'+$(this).val()] = $(":input[name='surveys_answers_text[" + $(this).val()  + "]']").val();
		}
		else
		{
			extra['surveys_answers_text'] = "";
		}
	});
	// Requesting survey page
	$.get("ajax_include.php",
			extra,
			function(data)
			{
				$('.survey').html(data);
				$('.submit_survey').hide();
			},
			"text");
}
	
// Go back to previous page
function back_to_previous()
{
	window.location = $(":input[name='referer_url']").val();
}

// Function for switching to a product
function switchToProduct(n)
{
	
	if(!n)
	{
		if(parseInt(current_product) < $(".switch_to li").size())
		{
			n = parseInt(current_product) + 1;
		}
		else
		{
			n = 1;	
		}
	}
	else
	{
		current_product = n;
		clearInterval(switcher_interval);
		switcher_interval = setInterval("switchToProduct()", 15000);
	}
	
	$("#switcher_box_ul .switcher_li").animate({width: 'hide'},1000);
	$(".switch_to li a").removeClass();
	
	$("#switcher_box_ul .switcher_li").each(function (x) {
		if((x+1) == n)
		{
			$(this).animate({width: 'show'},1000);
			$(".switch_to li:nth-child(" + (x+1) + ") a").addClass('active_li');
		}
   });
   
   
   current_product = n;
}

var current_product = 1;
	
	
$(document).ready(function() 
{
	if($(".switch_to li").size() > 0)
	{
		window.switcher_interval = setInterval("switchToProduct()", 15000);
	}
});

