$(document).ready(function() {
	// FancyBox for images
	$("a[rel=fancybox]").fancybox();
	
	// Submenu hovering for IE6
	if ($.browser.msie && parseInt($.browser.version) == 6) { 
		$(".subcats").mouseover(function(){
			$(this).find('ul').css('visibility','visible');
			$(this).unbind('mouseout').mouseout(function(){
				$(this).find('ul').css('visibility','hidden');
			});
		});
	}
	
});


var xmlHttp = createXmlHttpRequestObject();
	function createXmlHttpRequestObject() {
	var xmlHttp;
	
	try {
		//pro vsechny prohlizece krome IE6 a starsi
		xmlHttp = new XMLHttpRequest(); 
	} catch(e) {
		//pro IE6 a starsi
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
	
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
			try { 
			// try to create XMLHttpRequest object
			xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			} catch (e) {}
			}
	}
		
	// test na vytvoreni spojení
	if (!xmlHttp)
		alert("Došlo k chybe pri vytvorení spojení.");
	else 
		return xmlHttp;
}

//volani ajaxu
function process_ajax(METHOD,FILE,STATUS,BACK, PARAMS) {
	//alert(PARAMS);
	if (xmlHttp) {
    // try to connect to the server
		//var params = "lorem=ipsum&name=binny";
    	try {
        	// volani soubor
			xmlHttp.open(METHOD, FILE, STATUS);
			if (METHOD == 'GET') {
				xmlHttp.onreadystatechange = BACK;
				xmlHttp.send(null);	
			} else {
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
				xmlHttp.setRequestHeader("Content-length", PARAMS.length);
				xmlHttp.setRequestHeader("Connection", "close");
				xmlHttp.onreadystatechange = BACK;
				xmlHttp.send(PARAMS);
			}	
    	} catch (e) {
		alert("Nemohu se spojit se serverem:\n" + e.toString());	
		}
  }
}


function toggle(ID){
	var me = document.getElementById(ID);
	if (me.style.display=="none"){
		me.style.display="block";
	} else {
		me.style.display="none";
	}
}


/*------FORMULAR-----*/
function test_radio () {
	for (i=0; i<document.f.vyber.length; i++){
		if (document.f.vyber[i].checked==true) {
	  		return document.f.vyber[i].value;
		}
	}
	return false;
}

function formular() {
	var error = '=== Nemáte vyplnené všechny údaje ===\n\n';
	var error2 = true;
	var radio = test_radio ();
	if (!radio>0) {
		error += 'Vyberte jednu z možností\n';
		error2 = false;	
	}
	
	if (radio==1) {
		var specifikace = encodeURIComponent(document.f.popis_noviny.value); 
		if (!document.f.popis_noviny.value) {
			error += 'Zadejte specifikaci "Noviny, časopisy"\n';
			error2 = false;			
		}	
	}
	
	if (radio==5) {
		var specifikace = encodeURIComponent(document.f.popis_jine.value); 
		if (!document.f.popis_jine.value) {
			error += 'Zadejte specifikaci "Jiné"\n'; 
			error2 = false;		
		}	
	}
	
	if (error2) {
		process_ajax("POST","formular/send.php?send=1234", true, return_form, "hodnota="+radio+"&specifikace="+specifikace);		
	} else {
		error += '';
		alert (error);	
	}
}

function return_form() {
  myDiv = document.getElementById("formular");
  	if (xmlHttp.readyState == 1)  {
    	myDiv.innerHTML = "Odesílám data........ <br/>Chviličku strpení děkuji.";
	} else if (xmlHttp.readyState == 4)  { 
    	if (xmlHttp.status == 200) {
			try {
				response = xmlHttp.responseText;
				// display the message 
				//myDiv.innerHTML = response;
				var myJsonObj = jsonParse(response)
				if (myJsonObj.error) {
					myDiv.innerHTML = "Děkujeme za vyplnění formuláře";
					alert("Děkujeme za vyplnění formuláře");	
					toggle('f-visible');
				} else {
					myDiv.innerHTML = "Došlo k chybe pri odesílání";	
				}
      		} catch(e) {
        	// display error message
        	alert("Error reading the response: " + e.toString());
      		}
    	} else {
      	// display status message
      	alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
    	}
  	}
}

window.jsonParse=function(){var r="(?:-?\\b(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b)",k='(?:[^\\0-\\x08\\x0a-\\x1f"\\\\]|\\\\(?:["/\\\\bfnrt]|u[0-9A-Fa-f]{4}))';k='(?:"'+k+'*")';var s=new RegExp("(?:false|true|null|[\\{\\}\\[\\]]|"+r+"|"+k+")","g"),t=new RegExp("\\\\(?:([^u])|u(.{4}))","g"),u={'"':'"',"/":"/","\\":"\\",b:"\u0008",f:"\u000c",n:"\n",r:"\r",t:"\t"};function v(h,j,e){return j?u[j]:String.fromCharCode(parseInt(e,16))}var w=new String(""),x=Object.hasOwnProperty;return function(h,
j){h=h.match(s);var e,c=h[0],l=false;if("{"===c)e={};else if("["===c)e=[];else{e=[];l=true}for(var b,d=[e],m=1-l,y=h.length;m<y;++m){c=h[m];var a;switch(c.charCodeAt(0)){default:a=d[0];a[b||a.length]=+c;b=void 0;break;case 34:c=c.substring(1,c.length-1);if(c.indexOf("\\")!==-1)c=c.replace(t,v);a=d[0];if(!b)if(a instanceof Array)b=a.length;else{b=c||w;break}a[b]=c;b=void 0;break;case 91:a=d[0];d.unshift(a[b||a.length]=[]);b=void 0;break;case 93:d.shift();break;case 102:a=d[0];a[b||a.length]=false;
b=void 0;break;case 110:a=d[0];a[b||a.length]=null;b=void 0;break;case 116:a=d[0];a[b||a.length]=true;b=void 0;break;case 123:a=d[0];d.unshift(a[b||a.length]={});b=void 0;break;case 125:d.shift();break}}if(l){if(d.length!==1)throw new Error;e=e[0]}else if(d.length)throw new Error;if(j){var p=function(n,o){var f=n[o];if(f&&typeof f==="object"){var i=null;for(var g in f)if(x.call(f,g)&&f!==n){var q=p(f,g);if(q!==void 0)f[g]=q;else{i||(i=[]);i.push(g)}}if(i)for(g=i.length;--g>=0;)delete f[i[g]]}return j.call(n,
o,f)};e=p({"":e},"")}return e}}();
