/*
* Zhmayev Yaroslav 2009 / Matteo Maguolo 2010
*
*/

function location_hash()
{
	var url_hash = window.location.hash.substring(2);
	
	if(url_hash != "")
	{
		url_hash = url_hash.split("/");
		for(var i=0;i<url_hash.length;i++)
		{			
			inner_hash = url_hash[i].split("!");
			
			switch(inner_hash[1])
			{
				case "click":
					$(inner_hash[0]).click();
				break;
				case "scroll":
					window.scrollTo(0, $(inner_hash[0]).position().top);
				break;
				case "tab_select":
					$(inner_hash[0]).tabs({ selected: parseInt(inner_hash[2]) });	
				break;
			}								
		}
	}
}



// le prossime 40 righe servono a calcolare la dimensione della finestra broswer per lo scrolling
 var viewportwidth;
 var viewportheight;
 
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 
 if (typeof window.innerWidth != "undefined")
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 }
 
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

 else if (typeof document.documentElement != "undefined"
     && typeof document.documentElement.clientWidth !=
     "undefined" && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }
 
 // older versions of IE
 
 else
 {
       viewportwidth = document.getElementsByTagName("body")[0].clientWidth,
       viewportheight = document.getElementsByTagName("body")[0].clientHeight
 }

	function PRINT(mex,tit){
		var errorbox = $("#errorbox1");
		var title = "Masterchef server says";
		
		if(tit != null && tit != "")
			title = tit;
			
		errorbox.html(mex);
		errorbox.dialog({
			bgiframe: true,
			autoOpen: true,
			height: 150,
			title: title,
			width: 400,
			modal: true,
			resizable: false,
			buttons:{
				OK: function() {
					errorbox.dialog( "close" );
				}
			},
			close: function(ev, ui) { errorbox.dialog("destroy"); },
		});
	}
	
	function loadToTable($id,$scope,$complete)
	{
		$.ajax({
			url: "../operations/retrieve_data.php",
			type: "POST",
			data: "id="+$id+"&scope="+$scope,
			success: function(data) {
				$scope = $scope.replace("_minor","");
				$("#"+$scope).html(data).show( "size", {}, 200 );
			},
			error: function(data) {
				PRINT("<p>{^_ERROR^}</p>");
			},
			complete: $complete
		});
	}
	
	function ajaxCall($page,$data,$success,$error,$complete)
	{
		$.ajax({
			url: $page,
			type: "POST",
			data: $data,
			success: $success,
			error: $error,
			complete: $complete
		});
	}
		
	function randomString(string_length,case_select) {
		var chars = "";
		
		switch(case_select)
		{
			case "all_string":
				chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
				break;
			case "numeric":
				chars = "0123456789";
				break;
			case "lower_alpha":
				chars = "0123456789abcdefghiklmnopqrstuvwxyz";
				break;
			case "upper_alpha":
				chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
				break
			default:
				chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		}
		var randomstring = '';
		
		for (var i=0; i<string_length; i++) {
			var rnum = Math.floor(Math.random() * chars.length);
			randomstring += chars.substring(rnum,rnum+1);
		}
		
		return randomstring;
	}

$(function(){
	
	
	
	if($.cookie("manual_lang")==null){
		if (navigator.appName == 'Netscape')
			var language = navigator.language;
		else
			var language = navigator.browserLanguage;
		
		if(language.length > 2)
			language = language.substring(0,2)
	
	
		var all_languages = $('#language_list').attr('data-list');
		
		if($.cookie('language')!=language&&all_languages.search(language)>-1){
			$.cookie('language', language);
			window.location.reload();
		}
	}	
	
	var activemodule = $('#dialog').attr('data-module');
	activemodule=activemodule.substring(1, activemodule.length);
	var ind=activemodule.indexOf('/');
	
	if(ind>-1){
		var ind=activemodule.indexOf('/');
		activemodule=activemodule.substring(0,ind);
	}
	
	$("li[id='"+activemodule+"'] > a").css("color","#900");
   
	$("button", ".pulsante_jquery",  ".pulsante_jquery2").button();
		   
	$("a", ".pulsante_jquery3",  ".pulsante_jquery4").button();

	
	var $search = $('.search');
	if($search)
	{
		$search.focus(function() {	
			$(this).val('');	
		})

		$search.blur(function(){
			$(this).val('Search');
		})
	}
	
	if($('#errorbox1').find('p').size())
	{				
		$('#errorbox1').dialog({
			bgiframe: true,
			autoOpen: false,
			height: 150,
			modal: true
		});
		$('#errorbox1').dialog('open');
	}
	
	$("#dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 200,
		demo:"",
		resizable: false,
		prodotto: "" ,
		sezione:"",
		modal: true
	});
	
	if( $("#main").attr('class')  ==  "unlogged"){	
			$('.area_riservata').click(function() {	
			$('#dialog').dialog('open');
			return false;
	})}
		
	$('#login').click(function() {
			$('#dialog').dialog('open');
	})
	
	if(($('#login').find("a > span").html()).search(/@/)<0){
		$('#logout').hide();
	}else{
		$('#register').hide();
	}
	
	$('#langbox').dialog({
		bgiframe: true,
		resizable: false,
		autoOpen: false,
		height: 300,
		modal: true
	});
	
	$('.showlogin').click(function(){
		var path = $('#dialog').attr('action');
		path=path.replace($('#dialog').attr('data-module'), '/'+$(this).attr('data-module'));
		$('#dialog').attr('action',path);
		$('#dialog').dialog('open');
	});
	
	$('#changelang').click(function(){
		$('#langbox').dialog('open');
		$.cookie('manual_lang', 'OK');
	})
	
	//$("#bg_fill").snowfall({flakeCount : 200, maxSpeed : 10});
	
});
