var gWidth = 880;//page width in px

$(document).ready(function() {
	/*if($.browser.msie)
	{
		if($.browser.version == 6.0)
		{
			$("#img_slideshow").hide();
		}
		else
		{
			if($.browser.version == 7.0)
				$("#img_slideshow").before("<br/><br/><br/>");
			//
			slideShow(6000);
		}
	}
	else*/
		slideShow(6000);
});


function isset(variable)
{
    var undefined;
    return ( variable == undefined ? false : true );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	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() : '' ) + //expires.toGMTString()
	( ( path ) ? ';path=' + path : '' ) +
	( ( domain ) ? ';domain=' + domain : '' ) +
	( ( secure ) ? ';secure' : '' );
}

function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  
  return cookieValue;
}

function deleteCookie( name, path, domain ) {
	if ( readCookie( name ) ) document.cookie = name + '=' +
	( ( path ) ? ';path=' + path : '') +
	( ( domain ) ? ';domain=' + domain : '' ) +
	';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

function shuffleImages(width)
{
	var img_arr = new Array();
	var rnd = Math.random();
	var rnd_idx = 0;
	var arr_size = 0;
	
	img_arr[0] = "head_bkg_"+width+".jpg";
	img_arr[1] = "head_bkg1_"+width+".jpg";
	
	arr_size = img_arr.length;
	rnd_idx = Math.floor(rnd*arr_size);
	$("#head_div").css("background","#ffffff url(pic/"+img_arr[rnd_idx]+") no-repeat");//change background image depending on width
}

//function for dynamic page width calculation
function setpagewidth(w)
{
	var content_ie_offset = 0;
	var padding_ie = 0;
	var menuid_w = 0;
	if($.browser.msie)
	{
		content_ie_offset = 3;
		padding_ie = 20;
	}
	var avail_w = screen.availWidth;
	var side_w = 160 + content_ie_offset;
	var mainpage_w = w + 2; 
	$("#menuid").find(".firstlevel").each(function () {menuid_w += $(this).width();});
	var main_cont_w = w - side_w + 1;//+1 because of border
	var page_wrap_margin = Math.abs(Math.ceil((main_cont_w - menuid_w - padding_ie)/2));
	var main_menu_w = 600 + page_wrap_margin;
	var cont_l_r_w = (main_cont_w+1)/2 - 14;
	$("#mainmenu").width(main_menu_w); 
	$("#head_div").width(w);//880
	$("#mainpage").width(mainpage_w);//880
	$("#footer").width(w);//880
	$("#footermenu").width((w + 2));//880
	$("#content").width(main_cont_w);//880 - 160 + 1
	$("#content-left").width(cont_l_r_w);//348
	$("#content-right").width(cont_l_r_w);//348
	$("#sidebar").width(side_w);//160
	$("#dropList").width(main_cont_w - 1);
/*	shuffleImages(w);*/
}

//function for returning url parameters value	
function geturlparam(name,url)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(url);
	if(results == null)
	{
		return "";
	}
	else
	{
		return results[1];
	}
}

function sendDownloadAjax(destination, operation, prod_id, os_id)
{
	$("#result_box").html("<div style=\"width:100%;text-align:center;\"><img src=\"pic/ajax-loader50px_50.gif\"></div>");
	$.ajax({
		type: "GET",
		url: destination,
		data: "operation="+operation+"&product="+prod_id+"&os="+os_id,
		async: true,
		success: function(data) {
			if(operation == "changedoscombo")
				fillDownloadCombos("select_product", data, prod_id);
			if(operation == "changedproductcombo")	
				fillDownloadCombos("select_os", data, os_id);
			if(operation == "returnproduct")
				showproduct("result_box",data);
				
		}
	});
}

function fillDownloadCombos(combo_id, data, value)
{
	$("#"+combo_id).html(data);
	selectComboValue(combo_id, value)
}

function showproduct(div_id, data)
{
	$("#"+div_id).html(data);
}

function selectComboValue(combo_id, value)
{
	$("#"+combo_id).find("option").each(function () {
		if($(this).val() == value)
			$(this).attr("selected","selected");
	});
}

function onOSchange()
{
	var os_id = $("#select_os").attr("value");
	var prod_id = $("#select_product").attr("value");
	
	sendDownloadAjax("product_handler.php", "changedoscombo" ,prod_id,os_id);	
	sendDownloadAjax("product_handler.php", "returnproduct", prod_id, os_id);
}

function onProductchange()
{
	var prod_id = $("#select_product").attr("value");
	var os_id = $("#select_os").attr("value");
	
	sendDownloadAjax("product_handler.php", "changedproductcombo" ,prod_id,os_id);
	sendDownloadAjax("product_handler.php", "returnproduct", prod_id, os_id);
}
