$(document).ready(function(){
	
	//validace formu
	$("#mailSend").ndFormValidator();
	
	//taby
	$("#tabs").ndTabs({ animation: "true" });

	//rozbalovaci menu
	$("#menuExtra .item").hover(
		function(){ 
			if ($.browser.msie){$("#compareCart").css("position","static");}
			$(this).find("a.level1").toggleClass("active");
			$(this).find("div.submenu-env").fadeIn(200, function(){
				if ($.browser.msie){ this.style.removeAttribute('filter');}
			});			 
		},
		function(){ 
			$("#compareCart").css("position","relative");
			$(this).find("a.level1").toggleClass("active");
			$(this).find("div.submenu-env").hide(); 
		}
	);

	var articleCount = 0;
	var stopStatus = false;		
	
	
	// produkty

function removeOptions( pElementId ){
	var element = pElementId + " option";

	$(element).each(function(i){
		if( $(this).val() != 0 ){ $(this).remove(); }
	});						
}

//validace registrace
	if ($("#reg").length != 0) {
			if(window.opera){
				if(navigator.userAgent.indexOf('Opera Mobi') !=-1){ }		
				 else {
				 	$(".phone").mask("(999) 999 999 999");
					$(".input-zip").mask("999 99");
					$("#buyDate").mask("99-99-9999");
				 }				
			}
			else {
				$(".phone").mask("(999) 999 999 999");
				$(".input-zip").mask("999 99");
				$("#buyDate").mask("99-99-9999");
			}

			
			$("#reg").validate({
				debug: false,
				errorElement: "em",
				errorPlacement: function(error, element) {
					if ( element.is(":radio") )
						error.appendTo( element.parents("fieldset").children("p") );
					else
						error.appendTo( element.parent("td").next("td") );
				},
				success: function(label) {
					label.html("&nbsp;&nbsp;&nbsp;&nbsp;").addClass("success");
				},
				rules: {
					//zip: "digits"
					//payment: "required"
				},
				messages: {
					ordered: "Zvolte typ poukazu"
					//payment: "Zvolte typ platby"
				}
			});			
		};	

// --------------------------- produkty

function getType(){
	var xmlPath = "data type";	
				
	$.ajax({
		type: "GET", dataType: "xml", url: "/zaruka/products-xml/",		
		beforeSend: function(){
			$("#waitTp").show();
		},
		success: function(xml) {
			
			// prochazeni datove kolekce
			if( $(xml).find( xmlPath ).length > 0 ){
				$(xml).find( xmlPath ).each(function(){
												
					var option = $("<option></option>").attr("value", $(this).attr("id") ).text( $(this).attr("name") );
					$("#selTp").append(option);
					
				});
			}
			
			$("#waitTp").hide();
		}
	});		
}

// ---------------------------		

function getModel( pProductId ){
	var xmlPath = "data type model";	
	
	$.ajax({
		type: "GET", dataType: "xml", url: "/zaruka/products-xml/",		
		beforeSend: function(){
			$("#waitMark").show();
		},				
		success: function(xml) {
			
			// prochazeni datove kolekce
			if( $(xml).find( xmlPath ).length > 0 ){
				$(xml).find( xmlPath ).each(function(){

					var productId = $(this).attr("typeId");							
					if( productId == pProductId ){
						var option = $("<option></option>").attr("value", $(this).attr("id") ).text( $(this).attr("name") );
						$("#selMark").append(option);								
					}
												
				});
			}
			
			$("#waitMark").hide();
		}
	});						
}
// ---------------------------

var productId;
getType();		

$("#selTp").change(function(){			
	
	productId = $(this).val();
	var productText = $(this).find("option:selected").text();	
	
	// nejaky kraj je vybrany
	if( productId > 0 ){
						
		// promazani selectboxu s mesty	
		removeOptions("#selMark");				
		// naplneni selectboxu
		getModel( productId );
		// povoleni
		$("#selMark").removeAttr("disabled");			
		
		// prace se hidden inputy
		$("#hidMark").val("");
		$("#hidTp").val( productText );
	}
	// zadny kraj neni vybrany
	else{
		// promazani selectboxu a zakazani
		removeOptions("#selMark");
		$("#selMark").attr("disabled", "disabled");

		// prace s hidden inputy
		$("#hidTp").val("");				
		$("#hidMark").val("");
	}
});

$("#selMark").change(function(){		
	var modelId = $(this).val();
	var modelText = $(this).find("option:selected").text();

	if( modelId > 0 ){
		$("#hidMark").val(modelText );
	}
	else{
		$("#hidMark").val("");
	}
	
});		



// --------------------------- mesta a kraje

function getKraj(){
	var xmlPath = "cities region";	
				
	$.ajax({
		type: "GET", dataType: "xml", url: "/files/importy/obce.xml",		
		beforeSend: function(){
			$("#waitKraj").show();
		},
		success: function(xml) {
			// prochazeni datove kolekce
			if( $(xml).find( xmlPath ).length > 0 ){
				$(xml).find( xmlPath ).each(function(){									
												
					var option = $("<option></option>").attr("value", $(this).attr("id") ).text( $(this).attr("name") );
					$("#cz").append(option);
					
				});
			}
			
			$("#waitKraj").hide();
		}
	});		
}

// ---------------------------		

function getCity( pKrajId ){
	
	var xmlPath = "cities region[id="+pKrajId+"] city";	
	var cities = [];
	
	$.ajax({
		type: "GET", dataType: "xml", url: "/files/importy/obce.xml",	
			
		beforeSend: function(){
			$("#waitCity").show();
		},
		success: function(xml) {
			// prochazeni datove kolekce
			if( $(xml).find( xmlPath ).length > 0 ){
				$(xml).find( xmlPath ).each(function(){

					cities.push($(this).attr("name"));

				});
			}
			
			$("#storeCity").focus().autocomplete(cities);
			//console.log(cities);
			$("#waitCity").hide();
		}
	});	

}


// ---------------------------

var krajId;
getKraj();

$("#storeRegion").change(function(){	
	krajId = $(this).val();
	var krajText = $(this).find("option:selected").text();	
	
	// nejaky kraj je vybrany
	if( krajId > 0 ){		
		// promazani selectboxu s mesty	
		
		//removeOptions("#storeCity");				
		// naplneni selectboxu

		getCity(krajId);
  		$("#storeCity").val('');
  		
		// povoleni
		//$("#storeCity").removeAttr("disabled");			
		//$("#storeCity").focus().autocomplete($('#xmlCity'));
		
		// prace se hidden inputy
		$("#hidCity").val("");
		$("#hidKraj").val( krajText );
	}
	// zadny kraj neni vybrany
	else{
		// promazani selectboxu a zakazani
		removeOptions("#storeCity");
		//$("#storeCity").attr("disabled", "disabled");

		// prace s hidden inputy
		$("#hidKraj").val("");				
		$("#hidCity").val("");
	}
});

$("#storeCity").change(function(){		
	var mestoId = $(this).val();
	var mestoText = $(this).find("option:selected").text();

	if( mestoId > 0 ){
		$("#hidCity").val( mestoText );
	}
	else{
		$("#hidCity").val("");
	}
	
});		
});	



function GenerateCustomFckGallery(files_array)
{
		var result="";
		for (var a=0;a<files_array.length;a++)
			result+="<a href=\""+files_array[a].path+"\" class=\"nyroModal\" rel=\"lightbox\"><img alt=\"\" src=\""+files_array[a].thumb+"\" /></a>";
		return result;
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function genCookie(){
		var cookieName = "HiRes";
		var cookieSaveVal = "1";	

		$.cookie(cookieName, cookieSaveVal);
}



