var cDurationError = 1500;
var cDuration = 1000;
var activeTab = '';
var IE6gif = 'gif';


var cDuration = 1000;
var res = '500x280';

var returnURL = '';

var ErrorBoxOffsetLeft = 0;
var ErrorBoxOffsetTop = 0;
var ErrorBoxOffsetIsDef = true;


function wpSetErrorBoxOffset(left,top){
	ErrorBoxOffsetLeft = left;
	ErrorBoxOffsetTop = top;
	ErrorBoxOffsetIsDef = false;
}

function linkRadio(){

	var box1 = $('_econom');
	var box2 = $('_business');
	var thisParam = $('class_a');
	if (!box1) return;
	if (!box2) return;
	if (!thisParam) return;

	box1.addEvent('click', function(event){
		event.stop();
		$('class_a').value='1';

		if  (thisParam.value == 1)
		{
			box1.set('src','wport/images/r'+res+'/econom_1.png');
			box2.set('src','wport/images/r'+res+'/business_0.png');
		}
		if (thisParam.value == -1)
		{
			box1.set('src','wport/images/r'+res+'/econom_0.png');
			box2.set('src','wport/images/r'+res+'/business_1.png');
		}
	});

	box2.addEvent('click', function(event){
		event.stop();
		$('class_a').value='-1';

		if  (thisParam.value == 1)
		{
			box1.set('src','wport/images/r'+res+'/econom_1.png');
			box2.set('src','wport/images/r'+res+'/business_0.png');
		}
		if (thisParam.value == -1)
		{
			box1.set('src','wport/images/r'+res+'/econom_0.png');
			box2.set('src','wport/images/r'+res+'/business_1.png');
		}
	});
	
}

function linkCheckBox(box){

	var thisBox = $('_'+box);
	var thisParam = $(box);
	if (!thisBox) return;
	if (!thisParam) return;

	thisBox.addEvent('click', function(event){
		event.stop();
		if (thisParam.value == 0)
		{
			thisParam.value = 1;
		} else {
			thisParam.value = -1*thisParam.value;
		}
		if  (thisParam.value == 1)
		{
			thisBox.set('src','wport/images/r'+res+'/'+box+'_1.png');		
		}
		if (thisParam.value == -1)
		{
			thisBox.set('src','wport/images/r'+res+'/'+box+'_0.png');		
		}
	});
}

function highlightIcon(icon){

	showthis = $(icon);
	if (!showthis) return;

	if (showthis.getStyle('opacity') != '1') return;
	showthis.setStyles({'opacity':'0.5'});
	var showEffect = new Fx.Morph(showthis, {duration: cDuration});
	showEffect.start({'opacity':'1'});

}

function linkIcons(icons){
		
	$$('.'+icons).each(function(el,index){
	el.addEvent('mouseover',function(event){
		
		if (el.getStyle('opacity') == '1')
		{
			el.setStyles({'opacity':'0.5'});
			var showEffect = new Fx.Morph(el, {duration: cDuration});
			showEffect.start({'opacity':'1'});
		}	
		if (el.getStyle('opacity') <= 0.05)
		{
			el.setStyles({'opacity':'0.35'});
			var showEffect = new Fx.Morph(el, {duration: cDuration});
			showEffect.start({'opacity':0.05});
		}	


	})
	})	
}

function showAt(elem,elemAt,offsetLeft,offsetTop)
{

	var el = $(elemAt);

	if (!el) return;
	if (!$(elem)) return;


    var w = el.offsetWidth;
    var h = el.offsetHeight;
	
    var l = 0;
    var t = 0;
	
    while (el)
    {
        l += el.offsetLeft;
        t += el.offsetTop;
        el = el.offsetParent;
    }


	t = t + h + offsetTop;
	l = l + offsetLeft;
	$(elem).setStyles({'left':l,'top':t,'width':w});
	$(elem).setStyles({'display':'inline','position':'absolute','z-index':999});
}


function showHideSim(elemAt) {

	if ($(elemAt).value == 'Туристическая sim-карта')
	{
		
		$$('.tour-main').each(function(el,index){
			el.setStyles({'display':'none'});
		})
		$$('.tour-sim').each(function(el,index){
			el.setStyles({'display':'block'});
		})

		
	} else {

		$$('.tour-main').each(function(el,index){
			el.setStyles({'display':'block'});
		})
		$$('.tour-sim').each(function(el,index){
			el.setStyles({'display':'none'});
		})

	}
}

function initList(elemAt,elemList) {
	
	el = $(elemAt);
	if (!el) return;
	el.setStyles({'cursor':'pointer','background-image':'url(wport/images/r'+res+'/list_arr.png)','background-repeat': 'no-repeat',
		'background-position': 'right'});

	el.addEvent('click', function(event){
		event.stop();	
		if ($(elemList).getStyle('display') == 'none')
		{
			showAt(elemList,elemAt,0,0);
		} else {
			$(elemList).setStyles({'display':'none'});
			showHideSim(elemAt);
		}
			
	})
	
	
	$(elemList).getElements('ul').each(function(el,index){

		el.getElements('li').each(function(el_li,index){

			el_li.addEvent('mouseover', function(event){
				if (el_li.getStyle('opacity') != '1') return;
				el_li.setStyles({'opacity':'0.5'});
				var showEffect = new Fx.Morph(el_li, {duration: cDuration});
				showEffect.start({'opacity':'1'});
			})	
			
			el_li.addEvent('click', function(event){
				event.stop();
				$(elemAt).set('value',el_li.innerHTML);
				$(elemList).setStyles({'display':'none'});
				showHideSim(elemAt);
			})


		})

	})
/*
	el = $(elemList);
	if (!el) return;

	el.addEvent('click', function(event){
		event.stop();	
//		showAt(elemList,elemAt);

	})
	*/
}

function showTab(tab) {
	$('wpmainframe').setStyles({'background-image':'url(wport/images/r'+res+'/'+tab+res+'.png)'});
	$('avia').setStyles({'display':'none'});
	$('rail').setStyles({'display':'none'});
	$('hotel').setStyles({'display':'none'});
	$('tour').setStyles({'display':'none'});

	$(tab).setStyles({'display':'block'});	
	
	thisBox = $('wperror');
	if (!thisBox) return;
	thisBox.setStyles({'display':'none'});
	showHideSim('servicekind_s');
	
}

function initTab(tab) {

	var el = $('btn_'+tab); 
	el.setStyles({'display':'block','opacity':0.05});
	el.addEvent('click', function(event){
		event.stop();	
		showTab(tab);
		$('form_act').value = tab;
	})
}

function initFind(tab) {

	var el = $('find_'+tab); 
	el.addEvent('click', function(event){
		event.stop();
		$('atask').value = 'check';
		$('returnURL').value = returnURL;

		form = $('wpmain');
		if (!form) return;
		form.send();
	});
}

function initSend() {
	$('wpmain').set('send', {
		onSuccess: function(response){
			
			if (!response.trim()) {return;}

			$('wperror').innerHTML = response;
			if (!response.contains('Внимание'))
			{	
				sendForm();
			} else 	{
				showError(response);
			}
			
			
		}
	}).addEvent('submit', function(event){
		event.preventDefault();
		this.send(); //Uses the URL from “action”
	});	
}

function initData(obj){


	$$('input.wpedit').each(function(el,index){
							 
		el.set('value',obj[el.get('name')]);

		
	});

	$$('textarea.wpedit').each(function(el,index){
							 
		el.set('value',obj[el.get('name')]);

		
	});	

	
	var modPath = 'wport/';


	new Autocompleter.Ajax.Json('from_a', modPath+'ajx_avia.php', {
					'minLength': 3, 'overflow': 'true', 'postVar': 'q'
	});


	new Autocompleter.Ajax.Json('to_a', modPath+'ajx_avia.php', {
					'minLength': 3, 'overflow': 'true', 'postVar': 'q'
	});
	new Autocompleter.Ajax.Json('from_r', modPath+'ajx_rail.php', {
					'minLength': 3, 'overflow': 'true', 'postVar': 'q'
	});
	new Autocompleter.Ajax.Json('to_r', modPath+'ajx_rail.php', {
					'minLength': 3, 'overflow': 'true', 'postVar': 'q'
	});
	new Autocompleter.Ajax.Json('city_h', modPath+'ajx_hotels.php', {
					'minLength': 3, 'overflow': 'true', 'postVar': 'q'
	});
	

	new vlaDatePicker('date1_a', { format: 'd/m/y',  startMonday: true,
				prefillDate: { day: obj['d1_a'], month: obj['m1_a'], year: obj['y1_a']}, 
				separator: '/',  offset: { y: obj['offsety'], x: obj['offsetx'] }, 
				filePath: 'wport/inc/' });
	new vlaDatePicker('date2_a', { format: 'd/m/y',  startMonday: true,
				prefillDate: { day: obj['d2_a'], month: obj['m2_a'], year: obj['y2_a']}, 
				separator: '/',  offset: { y: obj['offsety'], x: obj['offsetx'] }, 
				filePath: 'wport/inc/' });
	new vlaDatePicker('date1_r', { format: 'd/m/y',  startMonday: true,
				prefillDate: { day: obj['d1_r'], month: obj['m1_r'], year: obj['y1_r']}, 
				separator: '/',  offset: { y: obj['offsety'], x: obj['offsetx'] }, 
				filePath: 'wport/inc/' });
	new vlaDatePicker('date2_r', { format: 'd/m/y',  startMonday: true,
				prefillDate: { day: obj['d2_r'], month: obj['m2_r'], year: obj['y2_r']}, 
				separator: '/',  offset: { y: obj['offsety'], x: obj['offsetx'] }, 
				filePath: 'wport/inc/' });
	new vlaDatePicker('date1_h', { format: 'd/m/y',  startMonday: true,
				prefillDate: { day: obj['d1_h'], month: obj['m1_h'], year: obj['y1_h']}, 
				separator: '/',  offset: { y: obj['offsety'], x: obj['offsetx'] }, 
				filePath: 'wport/inc/' });
	new vlaDatePicker('date2_h', { format: 'd/m/y', startMonday: true,
				prefillDate: { day: obj['d2_h'], month: obj['m2_h'], year: obj['y2_h']}, 
				separator: '/',  offset: { y: obj['offsety'], x: obj['offsetx'] }, 
				filePath: 'wport/inc/' });
	new vlaDatePicker('date1_s', { format: 'd/m/y',  startMonday: true,
				prefillDate: { day: obj['d1_s'], month: obj['m1_s'], year: obj['y1_s']}, 
				separator: '/',  offset: { y: obj['offsety'], x: obj['offsetx'] }, 
				filePath: 'wport/inc/' });
	new vlaDatePicker('date2_s', { format: 'd/m/y',  startMonday: true,
				prefillDate: { day: obj['d2_s'], month: obj['m2_s'], year: obj['y2_s']}, 
				separator: '/',  offset: { y: obj['offsety'], x: obj['offsetx'] }, 
				filePath: 'wport/inc/' });
	
	initList("servicekind_s","lst_service");
	initList("category_h","lst_category");
	initList("room_h","lst_room");
	linkIcons('wpedit');
	linkCheckBox('direct_only_a');
	linkCheckBox('need_return_a');
	linkCheckBox('need_return_r');
	linkRadio();


	initSend();

	initFind('avia');
	initFind('rail');
	initFind('hotel');
	initFind('tour');
	
	initTab('avia');
	initTab('rail');
	initTab('hotel');
	initTab('tour');

	initwpedit('from_a','Откуда');
	initwpedit('from_r','Откуда');
	initwpedit('to_a','Куда');
	initwpedit('to_r','Куда');
	initwpedit('city_h','Город');

	showTab($('form_act').value);

	$('wperror').addEvent('click', function(event){
		event.stop();	
		this.setStyles({'display':'none'});
	})
}

function wpInitialize(ares,url){
	window.addEvent('domready', function() {

		returnURL = url;
		res = ares;

		var myRequestHTML = new Request({
				url: 'wport/render/render'+res+'.php',
				method: 'get',
				onSuccess: function(responseText, responseXML) {
					
					$('wpmainframe').innerHTML = responseText;
					
					document.body.appendChild(document.getElementById("lst_service")); 
					document.body.appendChild(document.getElementById("lst_category")); 
					document.body.appendChild(document.getElementById("lst_room")); 
					var myRequestVARS = new Request({
							url: 'wport/render/loadvars.php',
							method: 'get',
							onSuccess: function(responseText, responseXML) {						
								
								var oData = JSON.decode(responseText);
								
								initData(oData);
								if (ErrorBoxOffsetIsDef == true)
								{
									ErrorBoxOffsetLeft = oData['offset_err_x'];
									ErrorBoxOffsetTop = oData['offset_err_y'];
								}

								linkIcons('wpedit');
								showHideSim('servicekind_s');

								
								if ($('wpmainframe'))
								{
									$('wpmainframe').addEvent('mouseleave', function(event){
										if ($('wpmain')) 
										{
											
											$('wpmain').send('wport/render/savevars.php'); 
											
										}
									});	
								}

							}
					});
					myRequestVARS.send('');



					var myRequestSIM = new Request({
							url: 'wport/ajx_sim.php',
							method: 'get',
							onSuccess: function(responseText, responseXML) {						
								$('sim-text').innerHTML = responseText;
							}
								
					});
					myRequestSIM.send('');
					

				}
		});
		
		myRequestHTML.send('');





	});

	
}

function highlightIcon(icon){

	showthis = $(icon);
	if (!showthis) return;

	if (showthis.getStyle('opacity') != '1') return;
	showthis.setStyles({'opacity':'0.5'});
	var showEffect = new Fx.Morph(showthis, {duration: cDuration});
	showEffect.start({'opacity':'1'});

}


function initwpedit(elem,defval){
	
	
	fly_from = $(elem);
	if (!fly_from) return;

	if (fly_from.value != defval) 
	{
		fly_from.setStyles({'color':'#000000'});
	} else {
		fly_from.addEvent('click', function(event){
			if ($(elem).value == defval) $(elem).value = '';
			$(elem).setStyles({'color':'#666666'});
		})
	}
}

function initwpedit2(elem,defval){
	
	
	fly_from = $(elem);
	if (!fly_from) return;

	if (fly_from.value != defval) 
	{
		fly_from.setStyles({'color':'#000000'});
	} else {
		fly_from.addEvent('click', function(event){
			if ($(elem).value == defval) $(elem).value = '';
			$(elem).setStyles({'color':'#666666'});
		})
	}
}


function linkIcon(icon){
	if (!$(icon)) return;
	$(icon).addEvent('mouseover', function(event){

		highlightIcon(icon);
	})
}

function showBlock(block,opacity){
	showthis = $(block);
	if (!$(showthis)) return;
	showthis.setStyles({'display':'block','opacity':'0'});
	var showEffect = new Fx.Morph(showthis, {duration: cDuration});
	showEffect.start({'opacity':opacity});
}

function hideBlock(block){
	hidethis = $(block);
	if (!$(hidethis)) return;
	hidethis.setStyles({'display':'none'});
}


function showError(msg) {
	thisBox = $('wperror');
	if (!thisBox) return;
//	thisBox.set('html',msg);

	showAt('wperror','wpmainframe',ErrorBoxOffsetLeft,ErrorBoxOffsetTop);
	thisBox.setStyles({'opacity':'0.0','display':'block'});
	var showEffect = new Fx.Morph(thisBox, {duration: cDurationError});
	showEffect.start({'opacity':'0.75'});
}

function sendForm() {
	form = $('redirect');
	if (!form) return;
	form.submit();
}

