var setCenterWin= function(w,h) {
			w=w*1;
			h=h*1;
			if(!$('darkbg')) {
				var db =new Element("div", {"id":"darkbg"}).inject(document.body);
				var ft =new Element("div", {"id":"bigfoto"}).inject(document.body);
				var cl =new Element("img", {"id":"clfoto", 'src':'/lib/im/close.png', 'width':'24','height':'24'}).inject(document.body);
				db.setStyles({position:'absolute',cursor:'pointer', background:'#000'});
				
				ft.setStyles({position:'fixed',cursor:'default',padding:'10px', 'background-color':'#faeedd'});
				cl.setStyles({position:'fixed',cursor:'pointer'});
				cl.addEvent('click',function(){
					$('darkbg').destroy();
					$('bigfoto').destroy();
					$('clfoto').destroy();
					});
				db.addEvent('click',function(){
					$('darkbg').destroy();
					$('bigfoto').destroy();
					$('clfoto').destroy();
					});
				}
			var win=document.getSize(); 
			var scrl=document.getScroll(); 
			var scrlwin=document.getScrollSize();
			$('darkbg').setStyles({
					display: '',
					position: 'absolute',
					overflow: 'hidden',
					top: '0px',
					left: '0px',
					width: scrlwin.x+'px',
					height: scrlwin.y+'px', 
					opacity: '0',
					'z-index':'100'
					});
			$('darkbg').morph({
				opacity: '0.7'
			});	
			$('bigfoto').setStyles({
				overflow: 'hidden',
				display: '',
				top: win.y/2-(h/2)+'px',
				left: win.x/2-(w/2)+'px',
				width: w+'px',
				height:  h+'px',
				'z-index':'101'
			});		
			$('clfoto').setStyles({
				top: win.y/2-(h/2)-12+'px',
				left: win.x/2-(w/2)+w+8+'px',
				'z-index':'102'
			});			
};
var morphCenterWin= function(w,h) {
			w=w*1;
			h=h*1;
			var win=document.getSize(); 
			var scrl=document.getScroll(); 
			$('bigfoto').morph({
				top: win.y/2-(h/2)+'px',
				left: win.x/2-(w/2)+'px',
				width: w+'px',
				height:  h+'px'
			});	
			$('clfoto').morph({
				top: win.y/2-(h/2)-12+'px',
				left: win.x/2-(w/2)+w+8+'px'
			});		
};
var drawPic=function(url){
	new Request.JSON({ url: '/includ/getsize.php?url='+encodeURIComponent(url) ,
			onComplete: function(jsonObj) {
					if(!jsonObj.w)
						jsonObj.w=300;	
					if(!jsonObj.h)
						jsonObj.h=300;	
					var win=document.getSize(); 
					var zoom=1;
					if((win.y-100)<jsonObj.h) {
						zoom=(win.y-100)/jsonObj.h;
						jsonObj.h=jsonObj.h*zoom;
						jsonObj.w=jsonObj.w*zoom;
						}
					if((win.x-50)<jsonObj.w) {
						zoom=(win.x-50)/jsonObj.w;
						jsonObj.h=jsonObj.h*zoom;
						jsonObj.w=jsonObj.w*zoom;
						}
					setCenterWin(jsonObj.w, 10);
					var div=$('bigfoto');
					div.empty();
					var img=new Element('img', {'src':  url, width:jsonObj.w, height:jsonObj.h}).inject(div);	
					img.setStyles({'opacity':0});
					morphCenterWin(jsonObj.w, jsonObj.h);	
					setTimeout(function() { 
						img.morph({'opacity':1});
						}, 500);	
					}
				}).send();
	};

var drawWopen=function(elem){
	var rl=elem.get("rel").split("|");
	if(!rl[2])	rl[2]=300;	
	if(!rl[1])	rl[1]=300;	
	var win=document.getSize(); 
	var zoom=1;
	if((win.y-100)<rl[2]) {
		zoom=(win.y-100)/rl[2];
		rl[2]=rl[2]*zoom;
		rl[1]=rl[1]*zoom;
	}
	if((win.x-50)<rl[1]) {
		zoom=(win.x-50)/rl[1];
		rl[2]=rl[2]*zoom;
		rl[1]=rl[1]*zoom;
	}
	setCenterWin(rl[1], rl[2]);
	var div=$('bigfoto');
	div.setStyle('background-color','#fff');
	if(rl[1])
		div.setStyle('overflow','auto');
	loadDiv(div,elem.get("href"));
	};
var loadDiv = function(dv,url){
	dv.empty();
	dv.addClass('loading');
	new Request.HTML({url: url , 
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
				dv.removeClass('loading');
				dv.innerHTML=responseHTML;
				var la=$$('#'+dv.id+' a');
				if(la.length)
					la.addEvent('click',function(e){
						e.stop();
						loadDiv(dv, this.get("href"));
						});
				}
			}).send();
	};

var slideShow = function(imgID, arrayIMG, delaySEC){
	var img=$(imgID);

		var title=img.get("alt");
		var w=img.get("width");
		var h=img.get("height");
		img.setStyle("position","absolute");
		img.addClass("slideShowClass");
		new Element("div",{"style":"width:"+w+"px; height:"+h+"px"}).inject(img,"after");
		for(c=arrayIMG.length-1;c>=0;c--) {
			var elm = new Element("img",{"src":arrayIMG[c],"width":w,"height":h,"alt":title,"style":"position: absolute;","id":imgID+"|"+c,"class":"slideShowClass"}).inject(img,"before");
			
			}
		checkNext(delaySEC);	
	};
	
var checkNext = function(delaySEC){
	var chck=1;
	var lst=$$('.slideShowClass');
	for(c=0;c<lst.length;c++)
		if(!lst[c].complete)
			chck=0;
	if(chck)
		setInterval(function(){ slNext()}, 1000*delaySEC);
	else
		setTimeout(function(){ checkNext(delaySEC); }, 500 );
	}
var slNext = function() { 
	var lst=$$('.slideShowClass');
	var t=0
	for(c=0;c<lst.length;c++)
		if(lst[c].getStyle("opacity")==1)
			t=c;
	if(t)
		lst[t].morph({"opacity":0});
	else {
		lst[lst.length-1].morph({"opacity":1});
		setTimeout(function(){ lst.setStyle("opacity",1); }, 1000 );
		}
	};


var setCalendar=function(){		 
 var clst=$$('.calform');
 if(clst.length) {
	 for(c=0;c<clst.length;c++)
	 	clst[c].set("autocomplete","off");
 clst.removeEvent('click');
 clst.addEvent('click', function(){	showCalendar(this, '', false);	});			
 if($('container'))
		new Element('div', {'id':'shCalendar'}).inject($('container'));	
 }	
 
};


var getDaysBetween = function (to) {
	
	 var from = new Date();	
	 var from = new Date(from.getFullYear(), from.getMonth()+1, from.getDate());
		
	var arr = to.split('.');
	var to = new Date(arr[2],arr[1], arr[0],12,0,0,0);
	var result=(to.getTime() - from.getTime())/(1000 * 60 * 60 * 24);
   
	
	return result;
};		 

function check_tour(forma) {
	if(forma)
		{
			var check=false;
			var t = 0;
			for(i=0;i<forma.elements.length;i++)
			{
				if(forma.elements[i].checked)
				{
					check=true;
				}
			}			
			
			if(check) forma.submit();				
			else alert('Please, select type of price');
		}	
	} 
var checkReq = function(iform){
	var lst=$$('#'+iform+" .required");
	var ch=1;
	for(c=0;c<lst.length;c++)
		if(!lst[c].value.length)
			ch=0;
	if(ch)
		$(iform).submit();
	else
		alert('Fill in all of the required fields, please');
	};	
	
var setTips = function() {
	if($$('.infoTips').length)
		new Tips($$('.infoTips'));
	if($$('.picTips').length)
		new Tips($$('.picTips'), {'className':'tip-pic'});	
	};
					
var addDest = function(arr) {
		var sel=$('destination');
		sel.empty();
		for(c=0;c<arr.length;c++)
			 var opt = new Element('option', {'value': arr[c]["url"], 'html': arr[c]["name"]}).inject(sel);
	};
window.addEvent('domready', function(){
	if(document.location.href.indexOf('submit.html')<=0)
	new Request.JSON({		url: "/lib/json/get_order.php",
							onComplete: function(jReq){
								if(jReq.cnt) {
									var instqnt=jReq.cnt-jReq.onreq;
									var cd=new Element('div').inject($('header'));
									cd.setStyles({"width":'232px','position':'absolute','margin-left':'465px','margin-top':'-85px','font-family':'Tahoma, Geneva, sans-serif','font-size':'12px'});
									new Element('div',{'style':'background:url(/img/cart-bg.png) top; height:6px;'}).inject(cd);
									var ccont=new Element('div',{'style':'padding: 0px 8px 2px 8px; background:url(/img/cart-bg.png) center'}).inject(cd);
									new Element('div',{'html':'Your Shopping Cart:', 'style':'font-size:14px;color:#9e222d'}).inject(ccont);
									if(jReq.summ)
										new Element('span',{'html':'<b>'+instqnt+'</b> item'+(instqnt>1?'s':'')+' for <b>'+jReq.summ+' '+jReq.valt+'</b>'}).inject(ccont);
									if(jReq.onreq)
										new Element('span',{'html':(jReq.summ?' and ':'')+'<b>'+jReq.onreq+'</b> item'+(jReq.onreq>1?'s':'')+' <b>on request</b>'}).inject(ccont);
									new Element('div',{'style':'height:3px;'}).inject(ccont);
									new Element('a',{'html':'Proceed to checkout', 'href':'/submit.html', 'style':'font-size:14px; color:#9e222d'}).inject(ccont);
									new Element('div',{'style':'background:url(/img/cart-bg.png) bottom; height:6px;'}).inject(cd);
									}
								}
							}).send();
	var al=$$('a');
	var ext=({"JPG":1,"jpg":1,"JPEG":1,"jpeg":1,"JPE":1,"jpe":1,"PNG":1,"png":1,"GIF":1,"gif":1})
	for(c=0;c<al.length;c++) {
		var hr="";
		var rr="";
		var rl=al[c].get("rel");
		if(rl) rr=rl.split("|");
		if(rr.length>=3 && rr[0]=="wopen") {
			al[c].addEvent('click',function(e){
				e.stop();
				drawWopen(this);
				});
			}
		else {
			var ht=al[c].get("href");
			if(ht) hr=ht.split(".");
			if(ext[hr[hr.length-1]]) {
				al[c].addEvent('click',function(e){
					e.stop();
					drawPic(this.get("href"));
					});
				} }
	}
	if($('prod')){
	addDest(dlist[$('prod').value]);
	$('prod').addEvent('change',function(){
		addDest(dlist[this.value]);
		if(clist[this.value]) {
			$$('.datetd').setStyle('width','100px');
			$$('.sm').setStyle('display','block');
			$('check2in').name=clist[this.value]['i'];
			$('check2out').name=clist[this.value]['o'];
			}
		else {
			$$('.datetd').setStyle('width','85px');
			$$('.sm').setStyle('display','none');
			}
		});
	$('searchd').addEvent('click',function(){
		window.location.href=$('destination').value;
		});
		}
		if($$('.hoverbox').length) {
			var bx=$$('.hoverbox');
			bx.setStyle('background-color','#fff');
			bx.set('morph', {duration: '200'});
			bx.addEvents({
					'mouseover':function(){
						this.morph({'background-color':'#ecedc9'});
						},
					'mouseout':function(){
						this.morph({'background-color':'#fff'});
						}});
			}
	
		$$(".link").addEvent('click',function(){
			$('vcurr').value=this.innerHTML;
			var rel=this.get("rel");
			if(rel) 
				$('currform').action=rel;
			$('currform').submit();
			});
		if($$('.showFoto').length)
		$$('.showFoto').addEvent('click',function(){											
											var div=$('bigfoto');	  
											div.empty();	  
											var arr=this.get("rel").split("|");	  
											var img=new Element('img', {'src':  arr[0], width:arr[1], height:arr[2], 'id':'img'}).inject(div);
											img.setStyle('opacity',0);
											setCenterWin(div, 1, 20);	
											morphCenterWin(div, arr[1], 20);
											setTimeout(function() { morphCenterWin(div, arr[1], arr[2])}, 500);
											setTimeout(function() { 
												img.morph({'opacity':1});
												div.setStyle('overflow','');
												var clsb=new Element('img', {'src':  '/im/close.png'}).inject(div, 'top');
												clsb.setStyles({
															 'position':'absolute',
															 'margin-left':arr[1]-3+'px',
															 'margin-top':'-15px',
															 'border':'none',
															 'cursor':'pointer'
															   });
												clsb.addEvent('click', function(){$('bigfoto').setStyle('display','none'); $('darkbg').setStyle('display','none');});
											
											}, 1200);
												  });
	
	//14/12/2011 - test
	$$('.arr').addEvent("click", function(){
	
	//alert('!!!');
			var div=$(this.get('rel'));
			
			var he=div.getStyle('display');
			if(he=='none') div.setStyles({'display':'block'});
				else div.setStyles({'display':'none'});										
	});	
	//
												  
	setCalendar();						
	setTips();					  	 
});
