$(window).load(function() {
    LiWidthF();
    hPicWidth();
    hPicWidth2();




    $(window).resize(function() {
        LiWidthF();
        hPicWidth();
        hPicWidth2();
    });

    /*header menu*/
    $('.header-menu li:last').addClass('last');
    $('.header-menu .l2:first').addClass('first');
    $('.header-menu .l2:last').addClass('last');
    $('.header-menu .l1').hover(function() {
            $('.header-menu').css('overflow','visible');
            $(this).prev().addClass('prev');
            $(this).addClass('hover');
        
    }, function() {

            $(this).prev().removeClass('prev');
            $(this).removeClass('hover');

    })

    $('.header-menu .l2').hover(function() {

            $(this).prev().addClass('prev');
            $(this).addClass('hover2');

    }, function() {

            $(this).prev().removeClass('prev');
            $(this).removeClass('hover2');

    })

    /*left menu*/
    $('.left-menu a').click(function() {
        if ($(this).siblings('ul').hasClass('closed'))
        {

            $(this).parent().siblings('li').find('ul').removeClass('opened').addClass('closed').slideUp(200);
            $(this).parent().siblings('li').find('.l2-a').removeClass('arrow');

            $(this).parent().parent().siblings('li').find('ul').removeClass('opened').addClass('closed').slideUp(200);
            $(this).parent().parent().siblings('li').find('.l1-a').removeClass('arrow');

            $(this).siblings('ul').removeClass('closed').addClass('opened').slideDown(200);
            $(this).addClass('arrow');
        }
        else
        {
            $(this).siblings('ul').removeClass('opened').addClass('closed').slideUp(200);
            $(this).removeClass('arrow');
        }

        if ($(this).attr('href') == '#') return false;
        if ($(this).siblings('ul').length > 0) return false;
    });

    $('.city-menu-l').click(function() {
            $(this).parent().siblings('li').removeClass('active');
            $(this).parent().addClass('active');
            $('#phones').html($(this).attr('phones'));
            $('#emails').html('<a href="mailto:' + $(this).attr('emails') + '">' + $(this).attr('emails') + '</a>');
            $('#addresses').find('a:first').attr('href',$(this).attr('addresses'));
            $('#work-hours').html($(this).attr('work-hours'));
            var ac_id = $(this).attr('id').substr(2);
            $.cookie('aero_id',ac_id, { expires: 65535, path: '/' });
            if (document.forms['sf'] && document.forms['sf'].elements['departFrom']) document.forms['sf'].elements['departFrom'].value = ac_id;
            var h = $('#ext-srch');
            if (h.get(0)) h.attr('href',h.attr('href').substr(0,h.attr('href').indexOf('departFrom=') + 11) + $(this).attr('oid'));
            h = document.getElementById('spec-offers-filter-form');
            if (h)
            {
//                 h.elements['ac'].selectedIndex = 0;
                 var si = 0;
                 for (var x = 1; x < h.elements['ac'].length; x++)
                      if (h.elements['ac'].options[x].value == ac_id) si = x;// h.elements['ac'].selectedIndex = x;

                 if (si > 0)
                 {
                     h.elements['ac'].selectedIndex = si;
                     if (typeof(reMainTrFilter) == 'function')
                     {
//                         reMainTrFilter();
                         h.submit();
                     }
                     else if (typeof(reCntrTrFilter) == 'function')
                     {
//                         reCntrTrFilter();
                         h.submit();
                     }
                     else if (typeof(reCityTrFilter) == 'function')
                     {
//                         reCityTrFilter();
                         h.submit();
                     }
                 }
            }

            if (location.href.indexOf('/tourindex/') >= 0) location.reload();

            if (location.href.indexOf('/aerocity/') > 0 && location.href.substr(location.href.length - 9) == '/offices/')
			location.href = '/aerocity/' + ac_id + '/offices/';

            return false;
    });

    /*spec offers filter*/
/*    $('.reset').click(function() {
        $('#spec-offers-filter-form').trigger('reset');
    });
*/    /*spec offers table*/
    $('table.tours tr').find('td:last').addClass('last');
    $('table.tours tr').find('td:first').next().addClass('tour-name-cell');

    $('td.tour-type').parent().prev().find('td').css('border-bottom', '0');





    $('.all-manag-btn').click(function() {
        if ($('.hidden-managers').css('display')=='none')
        {
            if ($('html').hasClass('ie6') || $('html').hasClass('ie7')){
                  $('.hidden-managers').css('display','block');
            }
            else{
                   $('.hidden-managers').slideDown(300);
            }

            $(this).addClass('opened2');
        }
        else
        {
            if ($('html').hasClass('ie6') || $('html').hasClass('ie7')){
                  $('.hidden-managers').css('display','none');
            }
            else{
                   $('.hidden-managers').slideUp(300);
            }
            $(this).removeClass('opened2');
        }

        return false;
    })

    $('.tour-name-cell a').hover(function(){
        var popup = $(this).siblings('.tour-info-popup-cont').find('.tour-info-popup');
        $(this).siblings('.tour-info-popup-cont').find('.tour-info-popup').css({display: 'block'});
    },function(){
        $(this).siblings('.tour-info-popup-cont').find('.tour-info-popup').css('display','none');
    })

    $('.currency').hover(function(){
        var popup = $(this).siblings('.curr-info-popup-cont').find('.curr-info-popup');
        $(this).siblings('.curr-info-popup-cont').find('.curr-info-popup').css({display: 'block'});
    },function(){
        $(this).siblings('.curr-info-popup-cont').find('.curr-info-popup').css('display','none');
    })


       
})


function LiWidthF() {
    var LiWidth = $('.header-menu').width() / $('.header-menu .l1').size() - 2;
    $('.header-menu .l1').width(LiWidth);
    $('.header-menu .l1 .sub-menu').width(LiWidth);
    $('.header-menu').css('overflow','hidden');
}

function hPicWidth()
{
	width = $('.jbnr').width();
	$('.jbnr-slide').width(width);
}
function hPicWidth2()
{
	width = $('.jbnr-2').width();
	$('.jbnr-slide').width(width);
}




var cssFix = function(){
  var u = navigator.userAgent.toLowerCase(),
  addClass = function(el,val){
    if(!el.className) {
      el.className = val;
    } else {
      var newCl = el.className;
      newCl+=(" "+val);
      el.className = newCl;
    }
  },
  is = function(t){return (u.indexOf(t)!=-1)};
  addClass(document.getElementsByTagName('html')[0],[
    (!(/opera|webtv/i.test(u))&&/msie (d)/.test(u))?('ie ie'+RegExp.$1)
      :is('firefox/2')?'gecko ff2'
      :is('firefox/3')?'gecko ff3'
      :is('gecko/')?'gecko'
      :is('opera/9')?'opera opera9':/opera (d)/.test(u)?'opera opera'+RegExp.$1
      :is('konqueror')?'konqueror'
      :is('applewebkit/')?'webkit safari'
      :is('mozilla/')?'gecko':'',
    (is('x11')||is('linux'))?' linux'
      :is('mac')?' mac'
      :is('win')?' win':''
  ].join(" "));
}();

function currSw(val)
{
    $('.curr-val').each(function() {
        if ($(this).hasClass(val)) $(this).css('display','');
        else $(this).css('display','none');
    });

    return false;
}

function extSearchSend(frm)
{
	var ext = frm.elements['country'].options[frm.elements['country'].selectedIndex].getAttribute('ext');

	if (ext == 'pnt')
	{
		location.href = frm.action + '?departFrom=' + frm.elements['departFrom'].options[frm.elements['departFrom'].selectedIndex].value 
			+ '&country=pnt_' + frm.elements['country'].options[frm.elements['country'].selectedIndex].value 
			+ '&dateFrom=' + frm.elements['dateFrom'].value + '&dateTo=' + frm.elements['dateTo'].value 
			+ '&nights=pnt_' + frm.elements['nights'].options[frm.elements['nights'].selectedIndex].value;
		return false;
	}

	return true;
}

function showAllTrz(el,cid,tid)
{
	el.style.display = "none";
	el.nextSibling.style.display = "";

	for (var x = 0; x < el.parentNode.parentNode.parentNode.rows.length; x++)
		if (el.parentNode.parentNode.parentNode.rows[x].getAttribute("sect") == cid + "_" + tid)
			el.parentNode.parentNode.parentNode.rows[x].style.display = "";

	return false;
}

function hideAllTrz(el,cid,tid)
{
	el.style.display = "none";
	el.previousSibling.style.display = "";

	for (var x = 0; x < el.parentNode.parentNode.parentNode.rows.length; x++)
		if (el.parentNode.parentNode.parentNode.rows[x].getAttribute("sect") == cid + "_" + tid)
			el.parentNode.parentNode.parentNode.rows[x].style.display = "none";

	return false;
}

function reSearchFilter()
{
	var frm = document.forms["sf"];
	var tmp = document.createElement("SCRIPT");
	tmp.src = "/ajax/search/?ac=" 
		+ frm.elements["departFrom"].options[frm.elements["departFrom"].selectedIndex].value + "&cn=" 
		+ frm.elements["country"].options[frm.elements["country"].selectedIndex].value + "&df=" 
		+ frm.elements["dateFrom"].value + "&dt=" 
		+ frm.elements["dateTo"].value + "&nt=" 
		+ (frm.elements["nights"].selectedIndex >= 0 ? frm.elements["nights"].options[frm.elements["nights"].selectedIndex].value : "");

	document.body.appendChild(tmp);
	return true;
}

function setSearchFilter(a_ac,a_cn,a_dt,a_nt)
{
	var frm = document.forms["sf"];
	var def = { ac: frm.elements["departFrom"].options[frm.elements["departFrom"].selectedIndex].value, cn: frm.elements["country"].options[frm.elements["country"].selectedIndex].value, df: frm.elements["dateFrom"].value, dt: frm.elements["dateTo"].value, nt: frm.elements["nights"].selectedIndex >= 0 ? frm.elements["nights"].options[frm.elements["nights"].selectedIndex].value : 7 };

	frm.elements["departFrom"].selectedIndex = -1;
	frm.elements["country"].selectedIndex = -1;
	frm.elements["nights"].selectedIndex = -1;

	for (var x = frm.elements["departFrom"].length - 1; x >= 0; x--) frm.elements["departFrom"].remove(x);
	for (x = frm.elements["country"].length - 1; x >= 0; x--) frm.elements["country"].remove(x);
	for (x = frm.elements["nights"].length - 1; x >= 0; x--) frm.elements["nights"].remove(x);

	for (x in a_ac)
	{
		frm.elements["departFrom"].options.add(new Option(a_ac[x][1],a_ac[x][0]));
		if ("" + a_ac[x][0] == "" + def["ac"]) frm.elements["departFrom"].selectedIndex = frm.elements["departFrom"].length - 1;
	}
	for (x in a_cn)
	{
		frm.elements["country"].options.add(new Option(a_cn[x][1],a_cn[x][0]));
		if (a_cn[x][2]) frm.elements["country"].options[frm.elements["country"].length - 1].setAttribute("ext",a_cn[x][2]);
		if (a_cn[x][0] == def["cn"]) frm.elements["country"].selectedIndex = frm.elements["country"].length - 1;
	}
	valid_sdates = a_dt;
	for (x = 0; x < a_nt.length; x++)
	{
		frm.elements["nights"].options.add(new Option(a_nt[x],a_nt[x]));
		if (a_nt[x] == def["nt"]) frm.elements["nights"].selectedIndex = frm.elements["nights"].length - 1;
	}
}

function attr_click(id,now)
{
	if (!now)
	{
		window.setTimeout(function() { attr_click(id,1); }, 100);
		return false;
	}

	var tmp = document.getElementById("attr_" + id);
	var atr = document.getElementById("hattrs");
	var str = atr.value;
	if (tmp)
	{
		if (now == 2) tmp.checked = false;
		var lnk = document.getElementById("asel_" + id);
		if (tmp.checked)
		{
			if (lnk) lnk.style.display = "";
			if (str) str += "," + id;
			else str = "" + id;
			atr.value = str;
		}
		else
		{
			if (lnk) lnk.style.display = "none";
			if (str)
			{
				id = "" + id;
				str = "," + str + ",";
				var x = str.indexOf("," + id + ",");
				if (x >= 0) str = str.substr(0,x + 1) + str.substr(x + id.length + 2);
				str = str.substr(1,str.length - 2);
				atr.value = str;
			}
		}
	}

	attr_ajax();

	return false;
}

function attr_reset()
{
	var tmp = document.getElementById("all_param");
	var inp = tmp.getElementsByTagName("INPUT");
	for (var x = 0; x < inp.length; x++) if (inp[x].checked)
	{
		inp[x].checked = false;
		var lnk = document.getElementById("asel_" + inp[x].value);
		if (lnk) lnk.style.display = "none";
	}
	tmp = document.getElementById("hattrs");
	tmp.value = "";

	attr_ajax();
}

function attr_ajax()
{
	var tmp = document.createElement("SCRIPT");
	var atr = document.getElementById("hattrs");
	tmp.src = "/ajax/hotels/" + atr.nextSibling.value + "/?city=" 
		+ atr.form.elements["city"].options[atr.form.elements["city"].selectedIndex].value + "&attrs=" + atr.value;
	document.body.appendChild(tmp);
}

function attr_set(cts,atrs,num)
{
	var tmp = document.getElementById("total_htlz");
	if (tmp) tmp.innerHTML = num;
	tmp = document.getElementById("attr_htlz");
	if (tmp) tmp.innerHTML = num;

	tmp = document.getElementById("hattrs");
	tmp = tmp.form.elements["city"];
	for (var x = 1; x < tmp.length; x++)
	{
		if (cts["c" + tmp.options[x].value])
		{
			tmp.options[x].style.display = "";
			if (cts["c" + tmp.options[x].value] > 1) tmp.selectedIndex = x;
		}
		else
		{
			tmp.options[x].style.display = "none";
		}
	}

	tmp = document.getElementById("all_param");
	if (tmp)
	{
		var inp = tmp.getElementsByTagName("INPUT");
		for (x = 0; x < inp.length; x++)
		{
			if (atrs["a" + inp[x].value])
			{
				inp[x].checked = atrs["a" + inp[x].value] == 1 ? false : true;
				inp[x].disabled = false;
			}
			else
			{
				inp[x].checked = false;
				inp[x].disabled = true;
			}
		}
	}
}

function ScreenSize(){
       if (window.innerHeight && window.scrollMaxY) {
               xScroll = document.body.scrollWidth;
               yScroll = window.innerHeight + window.scrollMaxY;
               myTop = window.pageYOffset;
               myLeft = window.pageXOffset;
       } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
               xScroll = document.body.scrollWidth;
               yScroll = document.body.scrollHeight;
               myTop = document.body.scrollTop;
               myLeft = document.body.scrollLeft;
       } else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
               xScroll = document.documentElement.scrollWidth;
               yScroll = document.documentElement.scrollHeight;
               myTop = document.documentElement.scrollTop;
               myLeft = document.documentElement.scrollLeft;
       } else { // Explorer Mac...would also work in Mozilla and Safari
               xScroll = document.body.offsetWidth;
               yScroll = document.body.offsetHeight;
               myTop = document.body.scrollTop;
               myLeft = document.body.scrollLeft;
       }

/*       var windowWidth, windowHeight;
       if (self.innerHeight) { // all except Explorer
               windowWidth = self.innerWidth;
               windowHeight = self.innerHeight;
       } else*/ if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
               windowWidth = document.documentElement.clientWidth;
               windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
       } else if (document.body) { // other Explorers
               windowWidth = document.body.clientWidth;
               windowHeight = document.body.clientHeight;
       }

       // for small pages with total height less then height of the viewport
       if(yScroll < windowHeight){
               pageHeight = windowHeight;
       } else {
               pageHeight = yScroll;
       }

       // for small pages with total width less then width of the viewport
       if(xScroll < windowWidth){
               pageWidth = windowWidth;
       } else {
               pageWidth = xScroll;
       }
       
       return [pageWidth,pageHeight,windowWidth,windowHeight,myLeft,myTop];
}

function Show_param_layer() {
	sz = ScreenSize();
	backgr = document.getElementById('param_layer_back');
	backgr.style.height = sz[1]+'px';
	h=477; // высота окна с картой
	if(sz[3]>h) {
		document.getElementById('param_layer').style.top = (sz[3]/2+sz[5]-h/2) + "px";	
		document.getElementById('fix_iframe').style.top = (sz[3]/2+sz[5]-h/2) + "px";	
	}
	else {
		document.getElementById('param_layer').style.top = sz[5] + "px";
		document.getElementById('fix_iframe').style.top = sz[5] + "px";
	}
		
	document.getElementById('param_layer_back').style.display = 'block';
	document.getElementById('param_layer').style.visibility = 'visible';
	document.getElementById('fix_iframe').style.visibility = 'visible';
	if($.browser.msie && jQuery.browser.version=='5.0') {
		document.getElementById('param_layer').style.display = 'block';
		document.getElementById('fix_iframe').style.display = 'block';
	}
	
	
	// привязываем событие - закрытие окна по Escape
	$("body").bind('keydown', Close_param_layer);

	return false;
}

function Hide_param_layer() {

	document.getElementById('param_layer_back').style.display = 'none';
	document.getElementById('param_layer').style.visibility = 'hidden';
	document.getElementById('fix_iframe').style.visibility = 'hidden';
	if($.browser.msie && jQuery.browser.version=='5.0') {
		document.getElementById('param_layer').style.display = 'none';
		document.getElementById('fix_iframe').style.display = 'none';
	}
	
	// удаляем событие - закрытие окна по Escape
	$("body").unbind('keydown', Close_param_layer);
}

function Close_param_layer(event)
{
	if (!event) event = window.event;
	if(event.keyCode==27) Confirm_sel_param();
}

function Confirm_sel_param() {
	Hide_param_layer();
}

