//boxes height

function updateBoxesHeight()
{
	var height = $('level_right').getSize().y;	
	var height2 = $('box_direction_top').getSize().y;	
	var height3 = $('box_direction_top2').getSize().y;
	var height4 = $('lm_super').getSize().y;
	
	if (height2 > height - 219 || height3 > height - 219)
	{
		if (height2 > height3)
			height = height2 + 219;
		else			
			height = height3 + 219; 
	}
	
	if (height > 435)
	{
		$('box_direction_top').setStyle('height', height-219 + 'px');
		$('box_direction_top2').setStyle('height', height-219 + 'px');
		$('topoffer_list').setStyle('height', height-258 + 'px');
		$('lastminute_list').setStyle('height', height-height4-10 + 'px');
	}
}

//offers

var count;
var counter;

function selectOffersInit()
{
    count = $$('div.offers_item').length;
    counter = 0;

    (function(){ selectOffersNext(); }).periodical(5000);
}

function selectOffersNext()
{
    counter++

    if (counter > count-1)
        counter = 0;
    
    selectOffers(counter);
}

function selectOffers(id)
{
    $$('a.offers_select_a').each( function(elem) {
        elem.className = 'offers_select_i';
    });

    $$('div.offers_item').each( function(elem){ elem.setStyle('display', 'none'); });

    $('offers_item_'+id).setStyle('display', '');
    $('offers_select_'+id).className = 'offers_select_a';    
}

//direction

function selectDirection(id)
{
    $$('a.direction_list_a').each( function(elem){
        elem.className = 'direction_list_i';
    });

    $$('div.direction_item').each( function(elem){ elem.setStyle('display', 'none'); });

    $('direction_item_'+selected_tab+id).setStyle('display', '');
    $('direction_list_'+selected_tab+id).className = 'direction_list_a';
}

var selected_tab;

function selectTabDirection(id)
{
	var taba = $('box_direction_'+id);
	
	if (id == 'summer')
	{
		$('box_direction_tab_s').set('class', 'box_direction_tab_s_a');
		$('box_direction_tab_w').set('class', 'box_direction_tab_w');
		
		var tabi = $('box_direction_winter');
		selected_tab = 's';
	}
	else
	{
		$('box_direction_tab_s').set('class', 'box_direction_tab_s');
		$('box_direction_tab_w').set('class', 'box_direction_tab_w_a');
		
		var tabi = $('box_direction_summer');
		selected_tab = 'w';
	}
	
	if (!taba) return;
	
	tabi.setStyle('display', 'none');
	taba.setStyle('display', '');
	selectDirection(0);
}

//countries, hotels

function countryShowTabInit(id_cat, hash)
{
	if (hash == '') return;
	
	hash = hash.substr(1);
		
	if (hash == 'hotels')
		var id = 1;
	if (hash == 'resorts')
		var id = 2;
	if (hash == 'tours')
		var id = 3;	
	if (hash == 'info')
		var id = 4;	
	if (hash == 'program')
		var id = 5;		
	
	initTab(id);
	hideTab();
	
    var url = host_url+'/www/tabs.php';
    var request = {cat: id_cat, type: hash, sort: ''};
	var json = new cmsAjaxResponse(url, {loader: false }).doRequest(request);       
}

function hideTab()
{
	var con = $('tab_entry');
	con.addClass('loaderb');
	con.set('html', '');
}

function countryShowTab(id, id_cat, mode, sorted)
{
	initTab(id);
	
	document.location.hash = mode;
	
	hideTab();
	
    var url = host_url+'/www/tabs.php';
    var request = {cat: id_cat, type: mode, sort: sorted};
    
	var json = new cmsAjaxResponse(url, {loader: false }).doRequest(request);       
}

function hotelShowTab(id)
{
	initTab(id);
		
    $$('div.tab_entry').each(function(elem){ elem.setStyle('display', 'none'); });
    $('tab_entry_'+id).setStyle('display', '');
    
    if (id == 1)
    	$('hotel_list').setStyle('display', '');
    else
    	$('hotel_list').setStyle('display', 'none');
}

function resortsShowHotels(id_cat)
{
	var hotels = $('hotels_'+id_cat);
	var more = $('more_'+id_cat);
	
	if (hotels.getStyle('display') != 'none')
	{
		hotels.setStyle('display', 'none');
		more.className = 'more_i';
		more.set('text', 'Rozwiń');
	}
	else
	{
		hotels.setStyle('display', '');
		more.className = 'more_a';
		more.set('text', 'Zwiń');
	}
}

function initTips2()
{
	myTips.attach($$('span.Tip'));
}

function initTips()
{
	myTips.attach($$('img.Tip'));
}

//search

function initTab(id)
{
	$$('div.tab_a').each(function(elem){ elem.className = 'tab tab_i left'});
	
	var tab = $('tab_'+id);
	tab.removeEvents();
	tab.className = 'tab tab_a left';
	
    $$('div.tab_i').each( function(elem){
    	elem.removeEvents();
	    elem.addEvent('mouseenter', function(e){ elem.className = 'tab tab_a left'; });
	    elem.addEvent('mouseleave', function(e){ elem.className = 'tab tab_i left'; });
	});	
      
}

function selectTabSearch(id, align)
{	 		
	if (id == 'summer')
	{
		$('box_search_tab_s').set('class', 'box_search_tab_s_' + align + '_a');
		$('box_search_tab_w').set('class', 'box_search_tab_w_' + align);
	}
	else
	{
		$('box_search_tab_s').set('class', 'box_search_tab_s_' + align);
		$('box_search_tab_w').set('class', 'box_search_tab_w_' + align + '_a');
	}
	
	searchUpdateWinter(id);
	searchUpdateCountry();
	searchUpdateAirports();
}

function selectWinterSub(id, align)
{ 		
	if (id == 'summer')
	{
		$('sub_search_tab_s').set('class', 'sub_search_tab_s_' + align + '_a');
		$('sub_search_tab_w').set('class', 'sub_search_tab_w_' + align);
	}
	else
	{
		$('sub_search_tab_s').set('class', 'sub_search_tab_s_' + align);
		$('sub_search_tab_w').set('class', 'sub_search_tab_w_' + align + '_a');
	}	
	
	searchUpdateWinter(id);
	searchUpdateCountry();
	searchUpdateAirports();
	searchUpdateKind();
}

function selectWinterPage(id, align)
{ 		
	if (id == 'summer')
	{
		$('search_offers_tab_s').set('class', 'search_offers_tab_s_' + align + '_a');
		$('search_offers_tab_w').set('class', 'search_offers_tab_w_' + align);
	}
	else
	{
		$('search_offers_tab_s').set('class', 'search_offers_tab_s_' + align);
		$('search_offers_tab_w').set('class', 'search_offers_tab_w_' + align + '_a');
	}	
	
	searchUpdateWinter(id);
	searchUpdateCountry();
	searchUpdateAirports();
	searchUpdateKind();
}

function searchUpdateWinter(id)
{
	if (isNaN(id))
	{	
		if (id == 'summer')
			var hot_winter = 0;
		else
			var hot_winter = 1;
	}
	else
		var hot_winter = id;
			
	var url = host_url+'/www/search.php';
	var request = {input: 'hot_winter', value: hot_winter};			    
	var json = new cmsAjaxResponse(url, {loader: false }).doRequest(request); 	
}

function searchCar(id)
{
	$('box_search').set('class', 'box_search left box_search_car');
	
//	$('box_search_sub_bus').setStyle('display', 'none');
//	$('box_search_sub').setStyle('display', '');
	
	initTab(id);
    $('airport').setStyle('display', 'none');
    $('hot_kind').value = 'I';    
    
    searchUpdateCountry();    
}

function searchBus(id)
{
	$('box_search').set('class', 'box_search left box_search_bus');
	
//	$('box_search_sub_bus').setStyle('display', '');
//	$('box_search_sub').setStyle('display', 'none');
	
	initTab(id);
    $('airport').setStyle('display', 'none');
    $('hot_kind').value = 'A';
    
    searchUpdateCountry();
}

function searchPlane(id)
{
	$('box_search').set('class', 'box_search left box_search_plane');
	
//	$('box_search_sub_bus').setStyle('display', 'none');
//	$('box_search_sub').setStyle('display', '');
	
	initTab(id);
    $('airport').setStyle('display', '');
    $('hot_kind').value = 'S';    
    
    searchUpdateCountry(); 
    searchUpdateAirports();
}

var cal1, cal2, hot_country, hot_childage1, hot_childage2, hot_airports, hot_kind, hot_adults, hot_stars;

function searchInit(global, session) //mainpage search box
{
	searchUpdateWinter(session);
	
	searchInitCalendars();				
	searchInitCountry();
	searchInitAirports();
	searchInitPersons();
}

function searchInitBox(global, session)
{
	searchInit(global, session);
	searchInitWinterTab('box', global, session)	
}

function searchInitPage(global, session) //search page
{	
	searchInit(global, session);
	searchInitStars();
	searchInitKind();	
	searchInitWinterTab('page', global, session)
}

function searchInitSub(global, session) //subpage search box
{	
	var elem = $('sub_search_offers');
	
	if (elem != undefined)
	{
		if (elem.getStyle('display') == 'none')
		{
			elem.setStyle('display', '');			
			searchInit(global, session);	
			searchInitKind();
		}
		else
			elem.setStyle('display', 'none');
	}
	
	searchInitWinterTab('sub', global, session)	
}

function searchInitWinterTab(page, global, session)
{
	if (global == session)
		global = 't';
	else
		global = 'b';
					
	if (session)
		session = 'winter';
	else
		session = 'summer';
	
	if (page == 'page')
		selectWinterPage(session, global);
	else if (page == 'sub') 
		selectWinterSub(session, global);
	else if (page == 'box') 
		selectTabSearch(session, global);
}

function calendarSetDate()
{
	var dateTmp = $('date_to');
	cal2 = null;	
	dateTmp.removeEvents();
	
	var date = $('date_from').value;
	var myDate = new Date();
	myDate.setFullYear(date.substr(0, 4), date.substr(5, 2)-1, date.substr(8, 2));
	myDate.setTime(7 * 24 * 60 * 60 * 1000 + myDate.getTime());
	
	var day = myDate.getDate();
	if (day.toString().length < 2)
		day = '0' + day;
	
	var month = myDate.getMonth()+1;
	if (month.toString().length < 2)
		month = '0' + month;	
	
	dateTmp.value = myDate.getFullYear() + '-' + month + '-' + day;
	dateTmp.getNext().dispose();
	
	cal2 = new Calendar({ date_to: 'Y-m-d' }, { direction: 1 });
}

function searchInitCalendars()
{
	if (cal1 == undefined)
		cal1 = new Calendar({ date_from: 'Y-m-d' }, { direction: 1, onHideStart: calendarSetDate });
	
	if (cal2 == undefined)
		cal2 = new Calendar({ date_to: 'Y-m-d' }, { direction: 1 });
}

function searchInitStars()
{
	if (hot_stars == undefined)
		hot_stars = new MavSelectBox('hot_stars');
}

function searchInitPersons()
{
	if (hot_childage1 == undefined)
	{
		hot_childage1 = new MavSelectBox({ elem: $('hot_childage1'), onShow: function(){
			if ($('hot_adults').value > 4)
			{
				alert('Liczba dorosłych musi być z zakresu 1-4');
				(this.hide()).delay(10); 				
			} 
		}});
	}

	if (hot_childage2 == undefined)
	{
		hot_childage2 = new MavSelectBox({ elem: $('hot_childage2'), onShow: function(){
			if ($('hot_adults').value > 4)
			{
				alert('Liczba dorosłych musi być z zakresu 1-4');
				(this.hide()).delay(10); 				
			} 
		}});
	}
	
	if (hot_adults == undefined)
	{	
		hot_adults = new MavSelectBox({ elem: $('hot_adults'), onSelect: function(){			
			if ($('hot_adults').value > 4)
			{
				hot_childage1.select($('hot_childage1_opt0'));
				hot_childage2.select($('hot_childage2_opt0'));	
			} 
		}});
	}
}

function searchInitKind()
{
	if (hot_kind == undefined)
	{
		hot_kind = new MavSelectBox({ elem: $('hot_kind'), onSelect: function(){			
			
			if ($('hot_kind').value != 'S')
			{
				hot_airports.select($('hot_airports_opt0'));	
				if ($('airport'))
				{
					$('airport').setStyle('visibility', 'hidden');
				}
			}
			else
			{
				if ($('airport'))
				{
					$('airport').setStyle('visibility', '');
				}
			}

			searchUpdateCountry();				
		}});		
	}
}

function searchInitCountry()
{
	if (hot_country == undefined)
	{
		hot_country = new MavSelectBox({ elem: $('hot_country'), onSelect: function()
		{					
			searchUpdateAirports();
			
			if (!isMain)
				searchUpdateKind();
		}});	
	}
}

function searchInitAirports()
{
	if (hot_airports == undefined)
	{				
		hot_airports = new MavSelectBox({ elem: $('hot_airports'), onShow: function(){
			var kind = $('hot_kind').value; 
			if (kind != 'S' && kind != '-')
			{
				alert('Wybór miejsca wylotu dostępny jedynie dla transportu samolotowego.');
				(this.hide()).delay(10); 
			}
		}});
		
		var kindval = $('hot_kind').value; 
		
		if ($('airport'))
		{
			if (kindval == 'S')			
				$('airport').setStyle('visibility', '');
			else
				$('airport').setStyle('visibility', 'hidden');
		}
	}
}

function searchUpdateCountry()
{
	if (hot_country)
	{	
		hot_country.destroy();
		hot_country = undefined;	
		
		loaderShow('search_country');
		
		var url = host_url+'/www/search.php';
		var request = {input: 'kind-region', value: $('hot_kind').value, selected: $('hot_country').value};			    
		var json = new cmsAjaxResponse(url, {loader: false }).doRequest(request);
	}
}

function searchUpdateAirports()
{	
	if (hot_airports)
	{			
		hot_airports.destroy();
		hot_airports = undefined;
	
		loaderShow('search_airports');
		
		var url = host_url+'/www/search.php';	
		var request = {input: 'region-airports', value: $('hot_country').value, selected: $('hot_airports').value};			    
		var json = new cmsAjaxResponse(url, {loader: false }).doRequest(request);
	}
}

function searchUpdateKind()
{	
	hot_kind.destroy();
	hot_kind = undefined;
	
	loaderShow('search_kind');
	
	var url = host_url+'/www/search.php';
	var request2 = {input: 'region-kind', value: $('hot_country').value, selected: $('hot_kind').value};			    
	var json2 = new cmsAjaxResponse(url, {loader: false }).doRequest(request2);
}

function loaderShow(id)
{
	$(id).addClass('loader');
}

function loaderHide()
{
	$$('.loader').each(function(el){ el.removeClass('loader') });
	$$('.loaderb').each(function(el){ el.removeClass('loaderb') });
}