 var maxOff = false;
 var ajaxOff = false;
 var pngfixOff = true;
 var datepickerOff= false;
 var cookie_options = { path: '/', expires: 0 };
 var target = null;
 var scrollTo = 0;
 var scrollHash = false;
 var modget = false;
 var menuselect = '';
 var hash = '#/';
 var katalogURL='zima';

function myready() {
	if(!ajaxOff) {
		tmpurl=location.href.replace(location.hostname,'').replace(location.hash,'').replace('http://','').replace('.html','');  		
//		if(location.hash=='' && tmpurl!='/' && tmpurl.substr(1,9)!='index.php') { location.href='/#'+tmpurl;	return true; }
	}
//	if(location.hostname=='nev.nipax.cz') alert('onload-start');
	$.datepicker.setDefaults($.datepicker.regional['cs']);
//	$(window).bind('resize', function() { $('body').css("display","none"); $('body').css("display","block"); if(!pngfixOff) DD_belatedPNG.fix('*'); });
	window.onunload = EventCache.flush;
	$('#c').prepend('<div id="preload"><div><img src="/img/preload.gif" alt="preloader" class="preloader"/><p>probíhá načítání <br/><br/><br /><br /><span style="color:#aaa" class="ie6">pro rychlejší prohlížení webu aktualizujte prosím svůj prohlížeč</span></p></div></div>');
//	if(!pngfixOff) $('#logo img,#menu ul.png,#menu ul li.png').each(function(){ DD_belatedPNG.fixPng(this); });
	afterload("body");
 	if(!ajaxOff && location.hash!='') {
		tmp=location.hash.replace('%23','#');
//		tmp=tmp.split('#'); 
//		if(tmp.length>2) scrollHash=tmp[2]; 
//		var hash='#'+tmp[1]; 
		location.hash=tmp;
		loadIt(tmp,'ajax main');
	} 
	if(modget != false) $('#body2').load('/null.php?trg=body2&modget='+modget,{},function() { $('.body2crew').fadeIn("fast"); afterload('#body2'); });
//	$(window).bind('hashchange', hashchange);
 }

function hashchange() { 
//	if(location.hostname=='nev.nipax.cz') alert('hashchange-start');
//	tmpurl=location.href.replace(location.hostname,'').replace(location.hash,'').replace('http://','').replace('.html',''); 
 	if(location.hash!=hash) { 
		$('div#cboxClose:visible').click();
// 		tmp=location.hash.split('#'); 
// 		if(tmp.length>2) scrollHash=tmp[2]; 
 		loadIt(location.hash,'ajax main'); 
 	} else {
//	if(location.hostname=='nev.nipax.cz') alert('hashchange-skip');
 	}
}

function loadIt(url,css) {
//	if(location.hostname=='nev.nipax.cz') alert('loadit-start (url:'+url+',css:'+css);
	tmp=url.split('#'); 
	if(tmp[0]=="")  { 
		if(tmp.length>2) scrollHash=tmp[2]; 
		url=tmp[1]; 
//		if(location.hostname=='nev.nipax.cz') alert('scrollHash='+scrollHash);
	} else { 
		if(tmp.length>1) scrollHash=tmp[1]; 
		url=tmp[0]; 
//		if(location.hostname=='nev.nipax.cz') alert('scrollHash='+scrollHash);
	}
	if(ajaxOff) { 
		url=url.replace("_none&","?");
		tmp=url.split("?"); if(tmp.length==1) { tmp=url.split("&"); url=url.replace(tmp[0]+'&',tmp[0]+'?'); }
		location.href=url; return false; 
	}
	showPreload();
	tmpcss=css.split(" ");
//	$('html, body').scrollTop(0);
//	if(scrollHash) alert(url+' = '+css+' = '+scrollHash); 
//	if(url=="") url="_none";
//	if(location.href.replace(location.hostname,'').replace(location.hash,'').replace('http://','')!='/') {
		// $.cookie('forceredirect',url,cookie_options); 
//		return true; } 
	$('select').css({'visibility':'hidden'});
	i=1; while((tmpcss)&&(i<tmpcss.length)&&(tmpcss[i-1]!='ajax')) i=i+1; 
	for(;i<tmpcss.length;i++) {
		if($('#'+tmpcss[i]).has('.content').length==1 && $('#'+tmpcss[i]).has('.content').parent().attr('id')==tmpcss[i]) temp='#'+tmpcss[i]+' > .content'; else temp='#'+tmpcss[i];
//		if(location.hostname=='nev.nipax.cz') alert('loadit-2-'+tmpcss[i]);
		$(temp).load('/null.php?trg='+tmpcss[i]+'&get='+url,{},function () { afterload('#'+$(this).attr('id')); }); 			
		if(tmpcss[i]=='main') 	{ $('#bannery-right').load('/null.php?trg=banner-left&get='+url,{},function () { afterload('#'+$(this).attr('id')); }); }
		}
	tmpurl=url.split("&"); url=tmpurl[0];
//	if(location.hostname=='nev.nipax.cz') alert('active:'+activeURL+',url:'+url);
	if(url != '_none') var activeURL=url;
	if(url && url.substr(0,1)=='/') {  
		hash='#'+url;
//		$(window).bind('hashchange', function(){});
//		location.hash='#'+url; 
		location.href=url; 
//		$(window).bind('hashchange', hashchange);
	}
}


function afterload(prefix) {
//	if(location.hostname=='nev.nipax.cz') alert('afterload-start('+prefix+')');
 	$(prefix+' .imgREPLACE').each(function (i) { if($(this).attr('class')!='') {
 		imgname=$(this).attr('class').split(" ")[1];
 		img=imgname.split("*");
 		if(img) {
	 		if(img.length==1) {	$(this).html('<img src="img/'+img[0]+'.png" class="png" alt="'+$(this).html()+'"/>'); }
 			if(img.length==3) {	$(this).html('<img src="img/'+img[0]+'.png" class="png" style="width:'+img[1]+'px;height:'+img[2]+'px" alt="'+$(this).html()+'"/>'); }
 		}
 	}});
// 	if(!pngfixOff && prefix!='body') 
// 			$(prefix+' .png, '+prefix+' .bbotb').each(function(){ DD_belatedPNG.fixPng(this); });
	$(prefix+' .red_hover').hover(function() { $(this).addClass('redhover'); },function() { $(this).removeClass('redhover'); });
	$(prefix+' #wrap').anythingZoomer({expansionSizeX: 105,expansionSizeY: 80,speedMultiplier: 2});
	$(prefix+' [rel*=external]').click(function(){ this.target = "_blank";});
	$(prefix+' .tgalerie a.nonefg').each(function() { 
		url=$(this).children('img').attr('src')+'&height=0&width=0&crop=0';
		$(this).removeClass("ajax").addClass("lightbox").attr('href',url);
	});

 	if(!ajaxOff) {
 		$(prefix+' a.ajax').each(function (i) { 
			$(this).attr('href','/#'+$(this).attr('href').replace('/#','').replace(location.hostname,'').replace('http://','')).click(function() { 
				$(this).attr('onclick'); 
				loadIt($(this).attr('href').replace('/#',''),$(this).attr('class')); 
				return true;
			}); 
	 	});
 		$(prefix+' a.filter').each(function (i) { 
 			if($(this).attr('href')) 
				$(this).attr('href','/#'+$(this).attr('href').replace('/#','').replace(location.hostname,'').replace('http://',''));
	 	});
 		$(prefix+' select.ajax').each(function (i) { 
			$(this).change(function() { 
 					$(this).attr('onchange'); 
 					loadIt($(this).val(),$(this).attr('class')); 
					location.href='#'+$(this).val(); 					
 				}); 
	 	});
	 	$(prefix+' select.ajaxOption').each(function (i) { 
	 		$(this).change(ajaxifikaceOption); 
	 	}); 
		if((menuselect) && (menuselect.length>0)) {
			$('#menu a.selected').each(function() { $(this).removeClass('selected'); CorrectIE6(this); });	
			$('#menu a.'+menuselect).each(function() { $(this).addClass('selected'); CorrectIE6(this); });
		}
 	} 
 		$(prefix+' form.ajaxform').each(function (i) { 
 			$(this).attr('action','/#'+$(this).attr('action').replace('/#','').replace(location.hostname,'').replace('http://','')).submit(function() { defaultSubmit($(this)); return false; }); 
 			$(this).find('input, select').each( function() { $(this).keypress(disableEnterKey); });
 		});
	 	$(prefix+' a.ajaxmodal').each(function (i) { 
			$(this).attr('href','/#modelize'+$(this).attr('href').replace('/#modelize','').replace(location.hostname,'').replace('http://','')).click(function() { 
				$('html, body').scrollTop(0);
				showPreload();
				$('#body2').load('/null.php?get=_none&trg=body2&modget='+$(this).attr('href').replace('/#modelize/',''),{},function() { $('.body2crew').fadeIn("fast"); afterload('#body2');  });
		 		return false; 
		 	}); 
		});

 	$(prefix+' a.disabled').each(function (i) { $(this).removeAttr('href').click(false); });
	if(!maxOff) { maximalizace(); }
	
	if(!datepickerOff) {
		$(prefix+' form.form-vyhledavani-zima input.datepicker').datepicker({showOn: 'button', buttonImage: '/img/form-kalendar-2.gif', buttonImageOnly: true});
		$(prefix+' form.form-vyhledavani-leto input.datepicker').datepicker({showOn: 'button', buttonImage: '/img/form-kalendar.gif', buttonImageOnly: true});
		$(prefix+' input.datepicker').datepicker({showOn: 'button', buttonImage: '/img/form-kalendar.gif', buttonImageOnly: true, onSelect:function(){ return false; }, showAnim: 'bounce' });
	}
//	$(prefix+' .back').each(function (i) { $(this).click('history.back();'); });
//	$(prefix+' a.lightbox').lightBox({fixedNavigation:true});
	$(prefix+' a.lightbox').colorbox({photo:true, rel: 'all', next: 'další', previous: 'předchozí', slideshow: true, slideshowAuto: false, slideshowStart: 'přehrát', slideshowStop: 'zastavit'});
	if(prefix=='#main') { scrollToHash(scrollHash); }
	$('#admin-right').each(function () { if(prefix=='#main' || prefix=='#detail') loadIt('_none','ajax admin-right'); });
	maxmodlize();
	hidePreload();
}

function ajaxifikaceOption() { 
	$(this).attr('onchange');
	tmp=$(this).attr('class').split(" ");
	i=1; while((tmp)&&(i<tmp.length)&&(tmp[i-1]!='ajaxOption')) i=i+1; 
	if(tmp) { for(;i<tmp.length;i++) { if($('#'+tmp[i]).length>0) {
			$('#'+tmp[i]).load('/null.php?trg='+tmp[i]+'&get=_none',{},function() { });
	}}}
	return true; 
}


function fromFlash(num,text){ loadIt('seznam-prodejcu&cookie-prodejce_where_okres=&cookie-prodejce_where_kraj='+num,'ajax prodejci'); }    

function maximalizace(){
	 $('.max').each(function (i) { $(this).css({'display':'none'}); });
	 k=$('#coright').height();
  	 $('#coleft').css({'height': 'auto'});
	 m=$('#coleft').height();
	 if(k>m) {
	 	$('#coleft').css({'height': k + 'px'}); 
	 }else{
	 	$('#coleft').css({'height': 'auto'});
	 }
	 $('.max').each(function (i) {
		parentHeight=$(this).parent().height();
		siblinks=0;
		$(this).siblings().each(function (i) { siblinks+=$(this).height();});
		me=parentHeight-siblinks;
		
		if(me > 410) {$(this).css({'display':'block','height': me+'px'});}
	 });
}

function maxmodlize(){
	$('select').css({'visibility':'hidden'});
	var k=$('#inbod2').height() + 600;
	if(k<($('#container').height())) k=$('#container').height();
	//alert (k);
	$('#body2').css({'height': k + 'px'}); 
	$('#body2overlay').css({'height': k + 'px'}); 
	$('#body2 select').css({'visibility':'visible'});
	return true;
}

function unmodalize(){
	$('select').css({'visibility':'visible'});
	$('.body2crew').html('').fadeOut("fast");
	$('html, body').scrollTop(0);
//	location.href=hash.replace('/#','').replace(location.hostname,'').replace('http://','');
	return true;
}

function checkLength(sel,n,min,max) {
//	$(sel).each(function() {
//	if ( $(this).val().length > max || $(this).val().length < min ) {
//		$(this).addClass('ui-state-error');
//		updateTips("Length of " + n + " must be between "+min+" and "+max+".");
//		return false;
//	} });
	return true;
}

function checkRegexp(sel,regexp,n) {
	var result = true;
	$(sel).each(function() {
	if ( !( regexp.test( $(this).val() ) ) ) {
		$(this).parent('fieldset').addClass('alert');
		result = false;
	} });
	return result;
}


function ValidateForm(formsel) {
	var bValid = true;
	if(formsel.attr('id')=='') formsel.attr('id','blabla'+Math.round(Math.random()*100));
	var formsel='#'+formsel.attr('id');
	$(formsel+' *').each(function(){$(this).removeClass('alert');});
	bValid = checkRegexp(formsel+' input.date',/^[0-9]|[1-3][0-9]\.[1-9]|1[0-2]\.(19|20)\d{2}$/) && bValid;
	bValid = checkRegexp(formsel+' input.integer',/^([0-9])+$/) && bValid;
	bValid = checkRegexp(formsel+' input.float',/^([0-9])+(,?([0-9])+)$/) && bValid;
	bValid = checkRegexp(formsel+' input.username',/^[a-z]([0-9a-z_])+$/i) && bValid;
	bValid = checkRegexp(formsel+' input.email',/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i) && bValid;
	bValid = checkRegexp(formsel+' input.password',/^([0-9a-zA-Z])+$/) && bValid;
	$(formsel+' input.notnull').each(function(){ if($(this).val()=='') { $(this).parent('fieldset').addClass('alert'); bValid=false; } }); 
	$(formsel+' select.notnull').each(function(){ if($(this).val()=='') { $(this).parent('fieldset').addClass('alert'); bValid=false; } }); 
	if($(formsel+' input.password').length==2) {
		var tmppasswd='';
		$(formsel+' input.password').each(function(){ 
			if( tmppasswd == '' ) tmppasswd = $(this).val(); 
			else if (tmppasswd != $(this).val()) {
				bValid = false;
				$(this).parent('fieldset').addClass('alert');
			} });
	}
	if(!bValid) $(formsel).addClass('alert');
	$('html, body').scrollTop(0);
	return bValid;
}

function PostForm(formsel,afterFunction,uurl) {
	if($(formsel).attr('action')=='') $(formsel).attr('action',location.href);
	$.post("null.php?get="+$(formsel).attr('action').replace('/#','')+uurl, $(formsel).serialize(), afterFunction );
}

function PostVyhledavani() {
	var tmpurl=$('#form-vyhledavani').attr('action').replace('/#','').replace(location.hostname,'').replace('http://','')+'&cookie-search-set=1';
	$('#form-vyhledavani input,#form-vyhledavani select').each(function () { tmpurl=tmpurl+'&cookie-'+$(this).attr('name')+'='+encodeURI($(this).val()); });
	$('#form-vyhledavani input:checkbox:not(:checked)').each(function () { tmpurl=tmpurl+'&cookie-'+$(this).attr('name')+'=0'; });
	loadIt(tmpurl,'ajax main');
	$('html, body').scrollTop(0);
	return false;
}

function defaultSubmit(formsel) {
	if(ValidateForm(formsel)) {		
		var tmpcss=$(formsel).attr('class').split(" "); i=1; while(tmpcss[i-1]!='ajaxform'&&i<tmpcss.length) i=i+1; 
		PostForm(formsel,function(result) { 
			if(result==1) { $(formsel).removeClass('failed').addClass('success'); setTimeout('unmodalize();',5000); 
							for(;i<tmpcss.length;i++) {
								if($('#'+tmpcss[i]).has('.content').length==1 && $('#'+tmpcss[i]).has('.content').parent().attr('id')==tmpcss[i]) temp='#'+tmpcss[i]+' > .content'; else temp='#'+tmpcss[i];			
								$(temp).load("/null.php?trg="+tmpcss[i]+"&get="+$(formsel).attr('action').replace('/#',''),{},function() { afterload('#'+$(this).attr('id'));  });
							}
			} else		  { $(formsel).addClass('failed'); $(formsel+' .failed').html(result); 
							for(;i<tmpcss.length;i++) {
								if($('#'+tmpcss[i]).has('.content').length==1 && $('#'+tmpcss[i]).has('.content').parent().attr('id')==tmpcss[i]) temp='#'+tmpcss[i]+' > .content'; else temp='#'+tmpcss[i];			
								$(temp).html(result);
								afterload('#'+$(this).attr('id'));  			
						  	}
			}
		});
}
	$('html, body').scrollTop(0);
	return false;
}

function disableEnterKey(e) {
     var key;
     if(window.event) key = window.event.keyCode;     //IE
     else             key = e.which;     //firefox
     if(key == 13)    return false;
     else             return true;
}

function CorrectIE6(sender) {
    if($(sender).width()>0) {
        $(sender).width($(sender).width()+1);
        $(sender).width($(sender).width()-1);
    }
}

function scrollToHash(name) {
//	if(location.hostname=='nev.nipax.cz') alert('scrollHash='+name);
	if(name==0) { $('html, body').scrollTop(0); return true; }
	tmp=$('#detail a[name='+name+']');
	if(name && tmp.length==1) { scrollTo=tmp.offset().top; scrollHash=false; }
	else return false;
//	alert('scroll');
	$('html, body').scrollTop(scrollTo);
}

function showPreload() {
	$('select').css({'visibility':'hidden'});
	offset=$('#bottom').offset().top-140;
	$('#preload').height(offset).show();
}

function hidePreload() {
	$('#preload').fadeOut(500,function(){ 
			if(modget==false) $('select').css({'visibility':'visible'});  });
}

function fixIE(el) {
	if(window.event && $(el).length>0) $(el).each(function () { 
		$(this).width($(this).width()+1).width($(this).width()-1); 
	});
}

function getBrowser() {
	var nVer = navigator.appVersion;
	var nAgt = navigator.userAgent;
	var browserName  = navigator.appName;
	var fullVersion  = ''+parseFloat(navigator.appVersion); 
	var nameOffset,verOffset,ix;

	// In MSIE, the true version is after "MSIE" in userAgent
	if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
	 browserName = "Microsoft Internet Explorer";
	 fullVersion = nAgt.substring(verOffset+5);
	}
	// In Opera, the true version is after "Opera" 
	else if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
	 browserName = "Opera";
	 fullVersion = nAgt.substring(verOffset+6);
	}
	// In Chrome, the true version is after "Chrome" 
	else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
	 browserName = "Chrome";
	 fullVersion = nAgt.substring(verOffset+7);
	}
	// In Safari, the true version is after "Safari" 
	else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
	 browserName = "Safari";
	 fullVersion = nAgt.substring(verOffset+7);
	}
	// In Firefox, the true version is after "Firefox" 
	else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
	 browserName = "Firefox";
	 fullVersion = nAgt.substring(verOffset+8);
	}
	// In most other browsers, "name/version" is at the end of userAgent 
	else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) 
	{
	 browserName = nAgt.substring(nameOffset,verOffset);
	 fullVersion = nAgt.substring(verOffset+1);
	 if (browserName.toLowerCase()==browserName.toUpperCase()) {
	  browserName = navigator.appName;
	 }
	}
	// trim the fullVersion string at semicolon/space if present
	if ((ix=fullVersion.indexOf(";"))!=-1) fullVersion=fullVersion.substring(0,ix);
	if ((ix=fullVersion.indexOf(" "))!=-1) fullVersion=fullVersion.substring(0,ix);
	
	var OSName="Unknown OS";
	if (nVer.indexOf("Win")!=-1) OSName="Windows";
	else if (nVer.indexOf("Mac")!=-1) OSName="MacOS";
	else if (nVer.indexOf("X11")!=-1) OSName="UNIX";
	else if (nVer.indexOf("Linux")!=-1) OSName="Linux";
		
	return browserName+' '+fullVersion+' ('+OSName+')';
}
