
function flip_frame(show) {
	// описания
	var frame=parent.document.getElementById("left_right");
	var tblContent=parent.left.document.getElementById("tblContent");
	var btShow=parent.left.document.getElementById("btShow");

	// обработка параметра или автопилот
	if (!show) {
		if (btShow && btShow.style.display=='none') show="hide"; else show="show";
	}

	// прячем-с...
	if (show=='hide') {
			frame.cols = '6,*';
			if (btShow) btShow.style.display='block';
			if (tblContent) tblContent.style.display="none";
	} else {
		frame.cols = '280,*';
		if (btShow) btShow.style.display='none';
		if (show!='unhide' && tblContent) tblContent.style.display="block";
	}
}

function print_r(aObj,offset) {
	if (typeof(aObj)!='object') return aObj;
	if (!offset) offset='';
	var out='';
	for (var i in aObj) {
		if (typeof(aObj[i])=='object') out+=offset+i+'\r\n'+print_r(aObj[i],offset+'  ');
		else if (typeof(aObj[i])=='function') out+=offset+i+' - function\r\n';
		else out+=offset+i+' - '+aObj[i]+'\r\n';
	}
	if (offset=='') return alert(out); else return out;
}


function win(url,awidth,aheight,wnd,no_scroll) {
	var scroll = (no_scroll) ? 'no':'yes';
	var x=(self.screen.width-awidth)/2; if (x<0) x=0;
	var y=(self.screen.height-aheight)/2; if (y<0) y=0;
	window.open(url,wnd,"width=" + awidth + ", height=" + aheight + ", replace=no, scrollbars=" + scroll + ", resize=no, left="+ x +", top=" + y);
}

function menuSelected(obj) {
	// обработка событий от меню и toolbar
	var get='', left=false;

	// обработка extra
	if (obj.extra) {
		if (obj.extra==':about:') {
			showModalDialog("/tpl/_sys/pop/about_cms.htm", window, "resizable: yes; help: no; status: no; scroll: no; ");
			return;
		} else if (obj.extra==':udc:') {
			alert ("Находится в стадии разработки!");
			return;
		} else if (obj.extra.indexOf(':win:')==0) {
			// открываем в новом окне
			var tmp=obj.extra.split('|');
			win(obj.link, parseInt(tmp[1]), parseInt(tmp[2]), 'popup',true);
			return;
		}

 		var extra=obj.extra;
		var tmp=extra.split('?');
		if (tmp.length>1) {
			get='?'+tmp[1]; extra=tmp.slice(2).join('?');
		}
	}

	// обработка ссылок
	var tmp=obj.link.split('|');
	if (tmp.length>1) {
		if (tmp[0]=='') {
			flip_frame('hide'); parent.left.document.location='about:blank';
		} else {
			flip_frame('unhide'); parent.left.document.location=tmp[0] + get; left=true;
		}

		if (tmp[1]=='') loc_R='about:blank';
		else {
			loc_R=tmp[1] + get;
		}
		if (!left) {
			parent.right.document.location=loc_R; loc_R=false;
		}

	} else {
		loc_R='about:blank';
		flip_frame('unhide');
		parent.left.document.location=obj.link + get;
	}
}

var tbOptions = {
	action : menuSelected,
	owner:   false,
	target:  false,
	link:    'javascript:void(0)',
	actStyle: 'tbActive',
	inaStyle: 'tbInactive',
	imgPath:   'img/cms/buttons/'
}

var tbHandler = {
	all   : [],
	mOver : function (item) { document.getElementById(item.id+'-out').className=tbOptions.actStyle; },
	mOut  : function (item) { document.getElementById(item.id+'-out').className=tbOptions.inaStyle; },
	click : function (item) { this.all[item.id].action(this.all[item.id]); },
	add   : function (obj) {this.all[obj.id]=obj;}
}

function button(id,img,alt,link,target,action) {
	this.id=id;
	this.img=img;
	this.alt=alt;
	this.link = link ? link : tbOptions.link;
	this.target = target ? target : tbOptions.target;
	this.action = action ? action : tbOptions.action;
	
	this.toString = function (pid) {
		var s = '<td class="' + tbOptions.inaStyle + '" id="' + this.id + '-out">' +
		'<img id="' + this.id + '" src="' + tbOptions.imgPath + this.img + '" alt="' + this.alt + '" ' +
			'onmouseover="tbHandler.mOver(this)" onmouseout="tbHandler.mOut(this)" ' + 
		'onclick="tbHandler.click(this)" /></td>';
		return s;
	}
}

function toolbar (id,owner) {
	this.id = id;
	this.bar = [];
	this.owner = owner ? owner : tbOptions.owner;
	
	this.show = function() {
		var out='';
		for(var i=0; i<this.bar.length; i++) out+=this.bar[i].toString(this.id);
		out='<table id="' + this.id +'"><tr>' + out + '</tr></table>';
		if (this.owner) {
			var elm=document.getElementById(this.owner);
			if (!elm) this.owner=false;
			else elm.innerHTML=out;
		}
		
		if (!this.owner) document.write(out);
	}
	
	this.add = function(obj) {
		this.bar.push(obj);
		tbHandler.add(obj);
	}
}

function set_now(id) {
	var elm=document.getElementById(id); if (!elm || elm.value!='') return;
	var dt=new Date();
	var vl='', s=0;
	s=dt.getDate(); if (s<10) vl+='0'+s+'.'; else vl+=s+'.';
	s=dt.getMonth()+1; if (s<10) vl+='0'+s+'.'; else vl+=s+'.';
	elm.value=vl+dt.getFullYear();
}

function oBlind(id,img,min,max) {
	this.oid=id;
	this.img=img;
	this.min=min;
	this.max=max;
	this.state=this.min-1;
	
	this.init = function(qty,rel) { // rel - relative
		if (rel && qty) qty=this.state+qty;
		if (qty) qty=parseInt(qty,10);
		if(!qty || qty>this.max) qty=this.max; else if (qty<this.min) qty=this.min; 
		if (qty==this.state) return; else this.state=qty;
		var elm=document.getElementById(this.oid); if (!elm) return;
		
		var out='';
		for (var i=this.min; i<=Math.round(this.state);i++) {
			out+='<img src="'+this.img+'" id="blind'+i+'">';
		}
		elm.innerHTML=out;
	}
	this.set = function(x,y) {
		var np=Math.round((this.max-this.min)/-60*y+this.max);
		if (this.state==np) return;
		if (this.state>np) {
			for (var i=this.state; i>np; i--) {
				var elm=document.getElementById('blind'+i); if (!elm) return;
				elm.style.display='none';
			}
		} else {
			for (var i=this.state; i<=np; i++) {
				var elm=document.getElementById('blind'+i); if (!elm) return;
				elm.style.display='block';
			}
		}
		this.state=np;
		
		var sp=Math.round(usScroll.scrollH/60*y); usScroll.jumpTo(0,sp);
	}
	this.step = function(dir) {
		// set position
		var pos=this.state+dir; if (pos<this.min || pos>this.max) return;
		// set blind
		this.set(0,(pos-this.max)/(this.max-this.min)*-60);
		// set handle
		//oHdl.style.top=-Math.round(60/(this.max-this.min)*pos);
	}
}

function fp_loaded() {
	Popup.Init('btUpParent','btUpChild',0); //Popup.Init('btDwParent','btDwChild',0);
	Popup.Init('bestParent','bestChild',3); Popup.Init('FrmParent','FrmChild',1);
	blind=new oBlind('win_blind','/img/win_blind0.gif',1,35); blind.init();
	oHdl=document.getElementById('win_handle');
	var oRoot=document.getElementById('win_root');
	Drag.init(oHdl,oRoot,0,0,0,60); oRoot.onDrag = function(x,y) { blind.set(x,y) }
	usScroll = new oScroll('scroll', 2, 19, 128, 75); usScroll.load(); 

}

var Popup = {

	timeout : 3000,
	showOnStart : false,
	
	Init : function(parent,child,mode) {
		var prt=document.getElementById(parent);
		var chd=document.getElementById(child);
		prt.popupChild=chd; chd.popupParent=prt;
		
		prt.popupMode = mode; prt.popupTimer=0;
		prt.popupInit=Popup.showOnStart;
		
		// bind events
		prt.onclick=Popup.Click; prt.onmouseout=Popup.Fire;
		if (mode & 2) prt.onmouseover=Popup.Over;
		prt.hidePopup=Popup.Hide;
		
		// show
		if (Popup.showOnStart) prt.onclick();
	},
	
	Click : function () {
		if ( !(this.popupMode & 1) && !this.popupInit) return;
		if (this.popupChild.style.display=='block') return this.hidePopup(); 
		this.popupChild.style.display="block";
		if (this.popupInit) {
			if (this.popupTimer!=0) window.clearTimeout(this.popupTimer);
			this.popupTimer=window.setTimeout("document.getElementById('"+this.id+"').hidePopup()",Popup.timeout);
			this.popupInit=false;
		}
	},

	Over : function () { 
		if (this.popupTimer!=0) { window.clearTimeout(this.popupTimer); this.popupTimer=0; }
		if (this.popupChild.style.display=='block') return;
		this.popupChild.style.display="block";
	},
	
	Hide : function () {
		this.popupChild.style.display="none";
		if (this.popupTimer!=0) window.clearTimeout(this.popupTimer);
		this.popupTimer=0;
	},
	
	Fire : function () {
		if (this.popupTimer!=0) window.clearTimeout(this.popupTimer);
		this.popupTimer=window.setTimeout("document.getElementById('"+this.id+"').hidePopup()",Popup.timeout);
	}
	
}

function set_help() {
	var elm=document.getElementById('help_link'); var frm=document.getElementById('help_src');
	frm.value=elm.innerHTML;
	//var prt=document.getElementById('FrmParent'); prt.hidePopup();
	
}

function make_total() {
	
	// dates
	var arr,sep,dif=1;
	arr=get_dates('arrive'); dep=get_dates('depart');
	if (dep && arr) dif=Math.round((dep-arr)/86400000);
	if (dif<1) dif=1;
	
	// per day
	var per_day=parseInt(document.getElementById('obj_price').innerHTML,10);
	if (isNaN(per_day)) per_day=0;
	
	// people
	var ppl=parseInt(document.getElementById('people_quantity').value,10);
	
	// transfer & breakfast
	var price=per_day*dif+get_price('transfer')+get_price('breakfast')*ppl;
	
	document.getElementById('total_price').innerHTML=price;
	document.getElementById('total_price_hd').value=price;
}

function get_dates(id) {
	var pts=['day','month','year'];
	for (var i=0; i<3; i++) {
		var elm=document.getElementById(id+'_'+pts[i]); if (!elm || !elm.value) return false;
		pts[i]=parseInt(elm.value,10); if (isNaN(pts[i])) return false;
	}
	return new Date(pts[2],pts[1]-1,pts[0]);
}

function get_price(id) {
	var price=0, elm=document.getElementById(id); if (!elm || elm.selectedIndex==0) return price;
	var txt=elm.options[elm.selectedIndex].text;
	var prc=txt.match(/.*\(\s*.(\d+)\s*\).*/); prc=parseInt(prc[1]); if (isNaN(prc)) return price;
	return prc;
}

function set_active(prt,id) {
	var o = document.getElementById(prt); if (!o) return;
	var ids=id.split(','), st='', elm;
	if (o.selectedIndex==0) st=true; else st=false;
	for (var i=0; i<ids.length; i++) {
		elm=document.getElementById(ids[i]);
		if (elm) elm.disabled=st;
	}
}

function sync_drop(cur_drop){ 
	if(cur_drop==undefined){ alert('Не передан параметр синхронизации выпадающего меню'); return false;}
	var ind=0;
	if(parent.topfr.left_sel!=undefined)  ind=parent.topfr.left_sel;
	var curs=document.getElementById(cur_drop);
	if(curs==undefined){alert('Объект с именем '+cur_drop+' не найден'); return;}
	if(ind!=undefined && ind!=false && ind!='') curs.value=ind;
}

var switch_display_id;
function switch_display(id){
	if(switch_display_id && switch_display_id!=id && document.getElementById('tech_'+switch_display_id))
		document.getElementById('tech_'+switch_display_id).style.display='none';
	if(!document.getElementById('tech_'+id)) return;
	var flag=document.getElementById('tech_'+id).style.display;
	document.getElementById('tech_'+id).style.display=(flag==''?'none':'');
	switch_display_id=id;
}