a=function(data,mode){var ajax_data=null;if(mode==null||mode!=true)var mode=false;if(window.XMLHttpRequest)xmlhttp=new XMLHttpRequest;else if(window.ActiveXObject)xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");xmlhttp.open('POST','/'+sys_lang+'/ajax/',mode);xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4){if(xmlhttp.status==200){ajax_data=xmlhttp.responseText;}}};xmlhttp.send(data);return xmlhttp.responseText;};
//a2=function(data,module,mode){if(mode==null||mode!=true)var mode=false;if(window.XMLHttpRequest)xmlhttp=new XMLHttpRequest;else if(window.ActiveXObject)xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");xmlhttp.open('POST','/'+sys_lang+'/'+module+'/',mode);xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4){if(xmlhttp.status==200){var r=xmlhttp.responseText;}}};xmlhttp.send(data); return xmlhttp.responseText};
trim=function(str, chars) {return ltrim(rtrim(str, chars), chars);};
ltrim=function(str, chars) {chars = chars || "\\s";return str.replace(new RegExp("^[" + chars + "]+", "g"), "");};
rtrim=function(str, chars) {chars = chars || "\\s";return str.replace(new RegExp("[" + chars + "]+$", "g"), "");};
urldecode=function(str){var histogram={},histogram_r={},code=0,str_tmp=[];var ret=str.toString();var replacer=function(search,replace,str){var tmp_arr=[];tmp_arr=str.split(search);return tmp_arr.join(replace);};histogram['!']='%21';histogram['%20']='+';for(replace in histogram){search=histogram[replace];ret=replacer(search,replace,ret);}ret=decodeURIComponent(ret);return ret;};
urlencode=function(str){string=encodeURIComponent(str).replace(/~/g,'%7E');return string.replace(/%20/g,'+');};
isMail=function(str){var filter=/^.+@.+\..{2,3}$/; return filter.test(str)};
opener=function(url,w,h){var l=(screen.width-w)/2;var t=(screen.height-h)/2;window.open(url,'','height='+h+',width='+w+',top='+t+',left='+l+',scrollbars=no,resizable=no,location=no,directories=no,status=no,menubar=no,toolbar=no')};
closeLoginLayer=function(){
	if($('#notif').css('display')!='none'){
		if(page=='home'||/MSIE/.test(navigator.userAgent)){
			$('#notif').css({'height':'0px','display':'none','margin-top':'0px'})
		}else{
			$('#notif').animate({'height':'1px','margin-top':'-30px'},300,function(){$('#notif').hide().css({'margin-top':'0px'})})
		}
	}
};
closeLayer=function(){
	$('#layerBG').animate({'opacity':0},400,function(){$('#layerBG').hide()});
	$('#layer').fadeOut(400,function(){$('#layer>.body').html('')});
};
openLayer=function(html,l,t,w){
	if(!w){
		var w='auto';
	}else if(w==parseFloat(w)){
		if(/Mac/.test(navigator.userAgent)){
			w+=50;
		}
	}
	$('#layer>.body').html(html);
	$('#layer').css({'left':l+'px','top':t+'px'}).width(w).fadeIn();
	$('#layerBG').height($('body').height()).show();
	$('#layerBG').animate({'opacity':0.5},400);
};
loadCartScreen=function(del,change){
	var p='act=loadCartListScreen';
	if(del&&del==parseFloat(del)){p+='&delete='+del}
	if(change&&change==parseFloat(change)){p+='&change[id]='+change+'&change[count]='+$('#itemAmount'+change).val()}
	$('#goodlist').html(a(p));
	$('.delete').each(function(){
		$(this).click(function(e){loadCartScreen($(this).attr('rel'));e.preventDefault()});
	});
	$('.cart-item-amount').each(function(){
		$(this).change(function(){
			loadCartScreen(0,$(this).attr('rel'));e.preventDefault()
		});
	});
};
initHeight=function(){
	/*
	var o=$('#mainmenu').offset();
	var h=$('.body').height()-$('.footer').height();
	h-=$('.slideshow').height();
	var m=o.top+$('#mainmenu').height();
	if(m>h){h=m}
	$('.wrapper').height(h);
	*/
};
placeSlideShow=function(){
	//$('#zina').css({'position':'absolute'});
};
loadOrdersList=function(id){
	$('#goodlist').html(a('act=loadOrderListScreen&id='+id));
};
	
$(document).ready(function(){
	if(page!='home'){
		$('.wrapper').css({'min-height':$(window).height()-40});
		var ch=parseFloat($('.middle').height());
		var wh=parseFloat($('.wrapper').height())-55;
		//$('title').text(wh +'...'+ ch);
		if(wh<ch){
			$('.wrapper').css({'display':'table'});
		}
		$('a[rel=epasts]').each(function(){
			var str=$(this).text().replace('(abols)','@');
			$(this).html(str).attr({'href':'mailto:'+str})
		});
		//if(/MSIE/.test(navigator.userAgent)){
		//	$('.footer>.inner').css({'max-width':'1380px'});
		//}
	}
	
	if(/Mac/.test(navigator.userAgent)){
		if(/Opera/.test(navigator.userAgent)){
			$('h1').height('56px');
		} else if(/Safari/.test(navigator.userAgent)){
			$('h1').height('56px');
		} else if(/Chrome/.test(navigator.userAgent)){
			$('h1').height('58px');
		}else{
			$('h1').height('57px');
		}
		if(!/Firefox/.test(navigator.userAgent)){
			$('#loginSubmit').css({'top':'-4px'});
			$('#user, #pssw1, #pssw2').css({'padding':'2px 3px','top':'0px','position':'relative'});
		}else{
			$('#user, #pssw1, #pssw2').css({'padding':'2px 3px 1px 3px','top':'0px','position':'relative'});
		}
	} else {
		if(/Chrome/.test(navigator.userAgent)){
			$('#user, #pssw1, #pssw2').css({'padding':'2px 3px 1px'});
		} else if(/Firefox/.test(navigator.userAgent)){
			//$('#loginSubmit').css({'top':'-5px'});
		}
	}
	
	if(page=='partners'){
		// ---<--- partners
		$($('.partners').find('a')).each(function(){
			var o=$(this).parent().offset();
			$(this)	.css({'position':'absolute'});
		});
		$('.ppt').hover(function(){$(this).addClass('act')},function(){$(this).removeClass('act')});
		$('.wrapper').css({'display':'table'});
		// --->--- partners
	}else if(page=='catalog'){
		$('.dets>.add-to-cart')
			.hover(function(){$(this).addClass('act')},function(){$(this).removeClass('act')})
			.click(function(e){
				$('.addProduct').not('#addProduct_'+$(this).attr('rel')).slideUp().val('1');
				$('#addProduct_'+$(this).attr('rel')).slideDown();
				e.preventDefault();
			});
		$('.add-to-cart-button').click(function(e){
			e.preventDefault();
			var c=$('#addProduct_'+$(this).attr('rel')+'>.amount>input[type=text]');
			if($(c).val()!=parseFloat($(c).val())){$(c).focus();return false}
			$('.cart').html(a('act=addToCart&count='+$(c).val()+'&id='+$(this).attr('rel'),'ajax')).removeClass('empty');
			$('#addProduct_'+$(this).attr('rel')).slideUp(function(){
				$('#addProduct_'+$(this).attr('rel')).val('1');
				$('#layer').width(200);
				openLayer('<div class="success">'+ln[10]+'</div>',(($(window).width()-200)/2)-10,200);
			});
		});
	}else if(page=='portfolio'){
		// ---<--- portfolio list
		initPortfolio=function(){
			var topFix=0;
			$('.pItem').each(function(){
				var o=$(this).offset();
				var p=$(this).parent().find('span').get(0);
				var p2=$(this).parent().find('span.fade').get(0);
				$(p).css({'left':o.left+'px','top':(o.top+topFix)+'px'});
				$(p2).css({'left':o.left+'px','top':(o.top+topFix)+'px'});
				
				$($(this).parent().parent()).hover(function(){
					var sp=$(this).find('.fade').get(0);
					$(sp).animate({'opacity':0},300);
					$($(this).find('.name').get(0)).fadeIn(300);
					$($(this).find('.layer').get(0)).fadeIn(300);
				},function(){
					var sp=$(this).find('.fade').get(0);
					$(sp).animate({'opacity':0.3},300);
					$($(this).find('.name').get(0)).fadeOut(300);
					$($(this).find('.layer').get(0)).fadeOut(300);
				});
				$($(this).parent().find('span')).css({'visibility':'visible'});
			});
		};
		initPortfolio();
		$(window).resize(function(){initPortfolio()});
		// --->--- portfolio list
	}else if(page=='orders'){
	// ---<--- orders
	//loadOrdersList();
	// --->--- orders
	}else if(page=='portfolio-item'){
		// ---<--- portfolio
		initPortfolioItem=function(){
			$('.thm').not('.panorama').not('.video').each(function(){
				var o=$($(this).find('img').get(0)).offset();
				$($(this).find('span').get(0)).css({'left':o.left+'px','top':o.top+'px'});
				$(this).click(function(e){
					$('.thm.act').removeClass('act');
					$(this).addClass('act');
					$('#portfolioMedia').css({'display':'none'});
					$('#portfolioImage').css({'display':'table'});
					$('#portfolioImage').css({'backgroundImage':'url('+$(this).attr('href')+')'});
					e.preventDefault();
				});
				$($(this).parent()).hover(function(){
						//alert($(this).html());
						$(this).addClass('act')
					},function(){
						$(this).removeClass('act')
					});
			});
			$('.panorama').click(function(e){
				$('#portfolioImage').css({'display':'none'});
				$('#portfolioMedia').css({'display':'table'}).html(urldecode(a('act=getPanorama&id='+$(this).attr('rel'))));
				$('.thm.act').removeClass('act');
				$(this).addClass('act');
				e.preventDefault();
			});
			$('.video').click(function(e){
				$('#portfolioImage').css({'display':'none'});
				$('#portfolioMedia').css({'display':'table'}).html(urldecode(a('act=getVideo&id='+$(this).attr('rel'))));
				$('.thm.act').removeClass('act');
				$(this).addClass('act');
				e.preventDefault();
			});
		};
		initPortfolioItem();
		// --->--- portfolio
	}else if(page=='contacts'){
	// ---<--- contacts
	$('#contacts').submit(function(ee){
		var e=0;
		if(!$('#fName').val().length){$('#fName').focus();e=4}
		if(!e&&!isMail($('#fMail').val())){$('#fMail').focus();e=5}
		if(!e&&!$('#fPhone').val().length){$('#fPhone').focus();e=6}
		if(!e&&!$('#fData').val().length){$('#fData').focus();e=8}
		if(!e&&$('#fCode').val().length!=4){$('#fCode').focus();e=9}
		if(e){$('#contacts>.message').html(ln[e]);ee.preventDefault();return false}
	});
	// --->--- contacts
	}else if(page=='cart'){
	// ---<--- cart
	loadCartScreen();
	$('#cartForm').submit(function(ee){
		var e=0;
		if(!e&&!$('#fName').val().length){e='fName';alert(ln[14])}
		if(!e&&!$('#fCompany').val().length){e='fCompany';alert(ln[11])}
		if(!e&&!$('#fAddress').val().length){e='fAddress';alert(ln[12]);}
		if(!e&&!$('#fCountry').val().length){e='fCountry';alert(ln[13]);}
		if(!e&&!$('#fPhone').val().length){e='fPhone';alert(ln[6]);}
		if(!e&&!isMail($('#fEmail').val())){e='fEmail';alert(ln[5]);}
		if(e){
			ee.preventDefault();
			$('#'+e).focus();
			return false;
		}
	});
	// --->--- cart
	}else if(page=='good'){
		var im=$('.content>.images').get(0);
		var dt=$('.content>.info').get(0)
		if($(im).height()>$(dt).height()){
			$(dt).height($(im).height());
		}
		
		$("a[rel=img], a[rel=swf], a[rel=galerija], a[rel=video]").fancybox({
			'transitionIn':'none',
			'transitionOut':'none',
			'titlePosition':'over',
			'titleFormat':function(title,currentArray,currentIndex,currentOpts){}
		});
		$('.thm.www').attr({'target':'_blank'});
		
	}else if(page=='home'){
		// ---<--- home page
		placeSlideShow();
		$(window).resize(function(){placeSlideShow()});
		
		var step=$('#josta').width()/4;
		$('.arrow-right').click(function(e){
			$('.slide:first').appendTo($('.slide').parent());
			e.preventDefault();
		});
		$('.arrow-left').click(function(e){
			$('.slide:last').insertBefore($('.slide:first'));
			e.preventDefault();
		});
		
		$('.search-input')
			.bind('focus',function(){if($(this).val()==$(this).attr('rel')){$(this).val('')}})
			.bind('blur',function(){if($(this).val()=='')$(this).val($(this).attr('rel'))});
		$('.search>form').submit(function(e){
			var q=$('.search-input').get(0);
			if($(q).val()==$(q).attr('rel')||!$(q).val().length){
				$(q).focus();
				e.preventDefault();
				return false;
			}
		});

		// --->--- home page
		var currKnopka=1;
		pressKnopka=function(id){
			$('.aktualitate-'+id).css({'zIndex':10,'opacity':1});
			$('.aktualitate').not('.aktualitate-'+id).css({'zIndex':11}).animate({'opacity':0}, 800);
			$('.knopka').removeClass('act');
			$('.aktualitate').addClass('invisible');
			$('.aktualitate-'+id).removeClass('invisible');
			$('.knopka[rel='+id+']').addClass('act');
		};
		pressKnopka(currKnopka);
		var knopkaInterval=12000;
		$('.knopka').click(function(e){
			clearInterval(intervals);
			pressKnopka($(this).attr('rel'));
			e.preventDefault();
			currKnopka=$(this).attr('rel');
			intervals=setInterval(function(){currKnopka++;if(currKnopka>3)currKnopka=1;pressKnopka(currKnopka)},knopkaInterval);
		});
		
		var intervals=setInterval(function(){
			currKnopka++;
			if(currKnopka>3)currKnopka=1;
			pressKnopka(currKnopka);
		},knopkaInterval);

		if(/MSIE 7.0/.test(navigator.userAgent)){
			$('#zina>.actholder').css({'margin-top':'30px','height':'70px'});
		}
		
	}
	// ---<--- mainmenu
	$('a.first, a.second, a.third').each(function(){
		var cn=$(this).parent();
		if(!/act/.test($(cn).attr('className'))){
			$($(cn).find('ul')).hide();
		}
		if(!/undinamic/.test($(this).attr('className'))){
			$(this).click(function(e){
				var u=$($(this).parent()).find('ul');
				if($(u).size()){
					var u=$(u).get(0);
					e.preventDefault();
					if($(u).css('display')=='none'){
						$(u).slideDown(600,function(){$(this).css('display','table')})
					}else{$(u).slideUp()}
					
					//
				}
			});		
		}
	});
	// --->--- mainmenu

	// ---<--- login
	$('#login').submit(function(e){
		var u=$('#user').val();
		var p=$('#pssw2').val();
		if(a('act=checklogin&u='+urlencode(u)+'&p='+urlencode(p))=='1'){
			return 1;
		}else{
			if(page=='home'||/MSIE/.test(navigator.userAgent)){
				$('#notif').css({'height':'50px','display':'block','margin-top':'-75px'});
			}else{
				$('#notif').animate({'height':'50px','display':'block','margin-top':'-75px'},500);
			}
			e.preventDefault();
			return false;
		}
	});
	$('#pssw1').bind('focus',function(){
		$(this).hide();
		$('#pssw2').show().focus();
		closeLoginLayer();
	});
	$('#pssw2').bind('blur',function(){
		if(!$(this).val().length){
			$(this).hide();
			$('#pssw1').show()
		}});
	$('#user')
		.bind('focus',function(){
			closeLoginLayer();
			if($(this).val()==$(this).attr('rel')){
				$(this).val('')
			}
		})
		.bind('blur',function(){
			if(!$(this).val().length){$(this).val($(this).attr('rel'))}
		});
	// --->--- login
	$('.print').click(function(e){
		opener($(this).attr('href'),1000,700);
		e.preventDefault();
	});
	$('.order').click(function(e){
		var h='<form method="post" action="" id="getPrices" style="width: 230px;">';
		h+='<div style="font-weight: bold;" class="form-row heading">'+ln[15]+'</div>';
		h+='<div style="text-align: left; padding: 0px 0px 0px 10px;" class="form-row">'+ln[16]+'</div>';
		h+='<div class="form-row message">&nbsp;</div>';
		h+='<table cellpadding="0" cellspacing="0">';
		h+='<tr><td class="form-row"><input class="type-text" type="text" name="name" value="" /></td><td><label>'+ln[0]+'</label></td></tr>';
		h+='<tr><td class="form-row"><input class="type-text" type="text" name="mail" value="" /></td><td><label>'+ln[1]+'</label></td></tr>';
		h+='<tr><td class="form-row"><input class="type-text" type="text" name="phone" value="" /></td><td><label>'+ln[2]+'</label></td></tr>';
		h+='</table>';
		h+='<div class="form-row">';
		h+='<input type="submit" class="type-submit" value="'+ln[3]+'" />';
		h+='<input type="hidden" name="id" value="'+$(this).attr('rel')+'" />';
		h+='</div>';
		h+='</form>';
		var o=$(this).offset();
		openLayer(h,o.left,o.top,230);
		$('#getPrices').submit(function(e){
			e.preventDefault();
			var f1=$('#getPrices').find('input[name=name]').get(0);
			var f2=$('#getPrices').find('input[name=mail]').get(0);
			var f3=$('#getPrices').find('input[name=phone]').get(0);
			var m=$('#getPrices').find('.message').get(0);
			if(!$(f1).val().length){$(m).text(ln[4]);$(f1).focus();return false}
			if(!isMail($(f2).val())){$(m).text(ln[5]);$(f2).focus();return false}
			//if(!$(f2).val().length){$(m).text(ln[5]);$(f2).focus();return false}
			if(!$(f3).val().length){$(m).text(ln[6]);$(f3).focus();return false}
			$(m).html('&nbsp;');
			a('act=getPrices&mail='+urlencode($(f2).val())+'&id='+$($('#getPrices').find('input[name=id]').get(0)).val()+'&name='+urlencode($(f1).val())+'&phone='+urlencode($(f3).val()));
			$('#layer>.body').html('<div class="success">'+ln[7]+'</div>');
			//$('#layer').css({'left':(($(window).width()-$('#layer').width())/2)-10+'px','top':'200px'});
			return false;
		});
		try{
			$('#layer>.body').find('input[type=text]').get(0).focus();
		}catch(e){}
		e.preventDefault();
		return false
	});
	$('.add-to-cart').click(function(e){
		e.preventDefault();
		var c=$('#cart_amount');
		if($(c).val()!=parseFloat($(c).val())){$(c).focus();return false}
		
		$('.cart').html(a('act=addToCart&count='+$(c).val()+'&id='+$(this).attr('rel'),'ajax')).removeClass('empty');
		$('#layer').width(200);
		openLayer('<div class="success">'+ln[10]+'</div>',(($(window).width()-200)/2)-10,200);// style="position:relative; left:-35px;"
	});
	$('#layerBG, #layer>.head>.cross').click(function(){closeLayer()});
	$('.graftik, .target-blank').attr('target','_blank');
	initHeight();

});
