$(document).ready(function(){

$('[name=login]').focus();

if ($("form#search-form").length >0) {
// Если есть на странице форма поиска с id=search-form
// то в плейсхолдер с id=news_placeholder вставить код ifame
$("#news_placeholder").html('<iframe src="http://www3.coral.ru/bilet/city/1343.aspx" noresize="true" scrolling="false" width="100%" height="750px" frameborder="0" marginwidth="0" marginheight="0"></iframe><p class="agency_name_footer_new">ООО "КОРАЛ ТРЕВЕЛ" <a href="mailto:onlineregular@coral.ru">onlineregular@coral.ru</a></p>');
}

if ($(".logged_in").length > 0) {
    $(".header-hold").show();
    $(".header-hold").children(".top-menu").hide();
}

});


function addLoadEvent(func)
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    } else
    {
        window.onload = function()
        {
            if (oldonload)
            {
                oldonload();
            }
            func();
        }
    }
}

//gallery scrolling
var timer;
var interval = 15;
function ScrollTop()
{
    timer = setInterval("document.getElementById('gallery_old').scrollTop -= 10", interval);
}
function ScrollBottom()
{
    timer = setInterval("document.getElementById('gallery_old').scrollTop += 10", interval);
    var e = document.getElementById('gallery_old');
    images = e.getElementsByTagName('img');
    images[images.length - 1].style.marginBottom = 0;
    images[images.length - 2].style.marginBottom = 0;
    images[images.length - 3].style.marginBottom = 0;
}


function ScrollTop2(a) {
    timer = setInterval("document.getElementById('" + a + "').scrollTop -= 10", interval);
}
function ScrollBottom2(a) {
    timer = setInterval("document.getElementById('" + a + "').scrollTop += 10", interval);
}

//popup submenu

function getOffsetRect(elem) {
    var box = elem.getBoundingClientRect()
    var body = document.body
    var docElem = document.documentElement
    var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop
    var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft
    var clientTop = docElem.clientTop || body.clientTop || 0
    var clientLeft = docElem.clientLeft || body.clientLeft || 0
    var top = box.top + scrollTop - clientTop
    var left = box.left + scrollLeft - clientLeft
    
    return { top: Math.round(top), left: Math.round(left) }
}

function getOffsetSum(elem) {
    var top = 0, left = 0
    while (elem) {
        top = top + parseInt(elem.offsetTop)
        left = left + parseInt(elem.offsetLeft)
        elem = elem.offsetParent
    }
    return { top: top, left: left }
}

function getOffset(elem) {
    if (elem.getBoundingClientRect) {
        // "����������" �������
        return getOffsetRect(elem)
    } else {
    // ����� �������� ���� ���-��
        return getOffsetSum(elem)
    }
}

function getCurrentPosition(e)
{
    var currentHeight = e.offsetHeight;
    var currentWidth = e.offsetWidth;
    var o = getOffset(e);

    return { left: o.left, top: o.top, width: currentWidth, height: currentHeight };
}

function getClientWidth() {
    return $('body').width();
}

function getClientHeight() {
    return $('body').height();
}


function ShowSubmenu(e, z)
{
    var current = getCurrentPosition(e);
    var submenu = $("#"+z);
    var docWidth = getClientWidth();
    submenu.css({ right: docWidth - (current.left + current.width) + "px", top: (current.top + current.height)-3 + "px" });
    submenu.show();
}

function prepareLinks() {

    var foodconcept = document.getElementById('foodconcept');
    if (foodconcept)
    {
        var links = foodconcept.getElementsByTagName('a');
        for (i = 0; i < links.length; i++)
        {
            links[i].onclick = function()
            {
                showInfo();
            }
        }
    }
}
function showInfo(mealid) {
    $("#divBack").html('<div onclick="hideAllPopups()" style="position:absolute;z-index:1; width:'+(getClientWidth()-20)+'px; height:'+getDocumentHeight()+'px; left:0; top:0"></div>');
    $("#infoblock_" + mealid).css({display:"block"});
    return;    
}

function showServInfo(id) {
    $("#divBack").html('<div onclick="hideAllPopups()" style="position:absolute;z-index:1; width:'+(getClientWidth()-20)+'px; height:'+getDocumentHeight()+'px; left:0; top:0"></div>');
    $("#serv_infoblock_" + id).css({ display: "block" });
    return;
}

function hideSubmenu(e)
{
    document.getElementById(e).style.display = 'none';
}

function show(e)
{
    e.style.display = 'block';
}
function hide(e)
{
    e.style.display = 'none';
}

var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera')  > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);

function getDocumentHeight() {
  return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight());
}

function getViewportHeight() {
  return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight;
}

function hideAllPopups(){
$('div.infoblock').hide(); 
$("#divBack").html('');
}
function hide2(e) {
$("#divBack").html('');
    $("#" + e).css({display:"none"});
}

// CityUa
function ShowSubmenuUa(e, z)
{
    var current = getCurrentPosition(e);
    var submenu = $("#"+z);
    var docWidth = getClientWidth();
    submenu.css({ right: docWidth - (current.left + current.width) + "px", top: (current.top + current.height)-3 + "px" });
    submenu.show();
}
function hideSubmenuUa(e)
{
    document.getElementById(e).style.display = 'none';
}

function showUa(e)
{
    e.style.display = 'block';
}
function hideUa(e)
{
    e.style.display = 'none';
}


// OnlineMenu
function ShowSubmenuonline(e, z)
{
    var current = getCurrentPosition(e);
    var submenu = $("#"+z);
    var docWidth = getClientWidth();
    submenu.css({ right: docWidth - (current.left + current.width) + "px", top: (current.top + current.height)-3 + "px" });
    submenu.show();
}
function hideSubmenuonline(e)
{
    document.getElementById(e).style.display = 'none';
}

function showonline(e)
{
    e.style.display = 'block';
}
function hideonline(e)
{
    e.style.display = 'none';
}

//search input
function searchOver(e)
{
    e.parentNode.parentNode.style.background = "url(/templates/modern/css/images/search_200.png) 0px -24px no-repeat";
    if (e.value == '����� �� �����') (e.value = '');
}
function searchOut(e)
{
    e.parentNode.parentNode.style.background = "url(/templates/modern/css/images/search_200.png) no-repeat";
    if (e.value == '') (e.value = '����� �� �����');
}

//belarus
function belarus_flag_over(imgname){
imgname.src = "/templates/modern/css/images/belarus02.gif"
}
function belarus_flag_out(imgname){
imgname.src = "/templates/modern/css/images/belarus01.gif"
}

//buttons hover, IE fix
function infobar2Hover(e)
{
    var infobar2 = document.getElementById('infobar2');
    if (infobar2)
    {
        var links = infobar2.getElementsByTagName('a');
        for (i = 0; i < links.length; i++)
        {
            links[i].onmouseover = function()
            {
                this.getElementsByTagName('img')[0].style.marginTop = -27
            }
            links[i].onmouseout = function()
            {
                this.getElementsByTagName('img')[0].style.marginTop = 0
            }
        }
    }
}

addLoadEvent(infobar2Hover);

//�� ������� �����
function openadr()
{
    document.getElementById("showadr").style.display = "block";
}
function closeadr()
{
    document.getElementById("showadr").style.display = "none";
}

function showform()
{
    var tmpDiv = document.getElementById("oscript");

    if (tmpDiv && (tmpDiv.childNodes.length == 0)) {
        var tmpframe = document.createElement('iframe');

        tmpframe.src = '/onlinescript/';
        tmpframe.noresize = true;
        tmpframe.setAttribute('scrolling', false);
        tmpframe.frameBorder = 0;
        tmpframe.marginWidth = 0;
        tmpframe.marginHeight = 0;
        tmpframe.style.width = '100%';
        tmpframe.style.height = '120px';
        tmpframe.style.overflow = 'hidden';

        tmpDiv.appendChild(tmpframe);
    }

    document.getElementById("show").style.display = "block";
}
function hideform()
{
    document.getElementById("show").style.display = "none";
}

var id_menu = new Array('sub_menu_1', 'sub_menu_2', 'sub_menu_3', 'sub_menu_7', 'sub_menu_8', 'sub_menu_9');

function allclose()
{
    for (i = 0; i < id_menu.length; i++)
    {
        document.getElementById(id_menu[i]).style.display = "none";
    }
}

function openMenu(id)
{
    for (i = 0; i < id_menu.length; i++)
    {
        if (id != id_menu[i])
        {
            document.getElementById(id_menu[i]).style.display = "none";
        }
    }
    if (document.getElementById(id).style.display == "block")
    {
        document.getElementById(id).style.display = "none";
    } else
    {
        document.getElementById(id).style.display = "block";
    }
}

// CountryBlock

function roundNumber(rnum, rlength) {
    // Arguments: number to round, number of decimal places
    var newnumber = Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
    return newnumber; // Output the result to the form field (change for your purposes)
}

function SelectCurrency(name, newVal, oldValElementId, SelCurId, className, a1, a2) {
    //�������� ������� ������

    var oldVal = parseFloat(document.getElementById(oldValElementId).value);

    for (j = 0; document.getElementById(className + "" + j) != null; j++) {
        //������ �������� ����
        var obj = document.getElementById(className + "" + j); //TD ���� ����� ������ ����������� ����
        var val = document.getElementById("hid" + className + "" + j); //hidden,��� �������� ���� ��� ���������� 
        var tVal = parseFloat(val.value); //���� � ���� �����
        var eVal = tVal * oldVal / parseFloat(newVal);
        val.value = eVal;
        obj.innerHTML = "�� " + roundNumber(eVal, 0);

        //������ ������
        var objCur = document.getElementById("r" + className + "" + j);
        var txtData = objCur.innerHTML.replace(document.getElementById(a1).innerHTML, name); //��������� ����������(������,���������,�������)
        objCur.innerHTML = txtData;

        //������ ���
        var objUrl = document.getElementById("url" + className + j);
        var idOldSelectedCurrence = document.getElementById(a2).value;
        objUrl.innerHTML = objUrl.innerHTML.replace("CURRENCY=" + idOldSelectedCurrence, "CURRENCY=" + SelCurId);
    }

    document.getElementById(oldValElementId).value = newVal;
    document.getElementById(a2).value = SelCurId;
    document.getElementById(a1).innerHTML = name;

    return false;
}

//MegaBanners
// MegaBanner
function showBanner(selectedItemIndex) {
    img = document.getElementById('megaImg_' + selectedItemIndex);
if(img!=null)
{
    if (img.loaded == true) {
        for (i = 1; i < maxHeaderlineCount; i++) {
            if (i != selectedItemIndex) {
                document.getElementById('HeaderTable_' + i).style.display = 'none';
                document.getElementById('HeaderOK_' + i).style.display = 'none';
                document.getElementById('img_' + i).style.border = '1px solid #0175dc';
            }
            else {
                document.getElementById('HeaderTable_' + i).style.display = 'block';
                document.getElementById('HeaderOK_' + i).style.display = 'none';
                document.getElementById('img_' + i).style.border = '1px solid white';
            }
        }
    }
}
}

function ShowArticle(selectedItemIndex) {
    stopTimer();
    showBanner(selectedItemIndex);
}

function timerStart(conterNum) {
    if (conterNum != -1)
        conter = conterNum;
    if (conter >= maxHeaderlineCount) conter = 1;
    showBanner(conter);
    conter = conter + 1;
    timerCount = setTimeout('timerStart(-1)', 4000);
}

function stopTimer() {
    clearTimeout(timerCount);
}


function loadNextImg(a) {

    a = a + 1;
    if (a < 7) {
        img = document.getElementById('megaImg_' + a);
		if(img!=null)
		{
        if (img.loaded != true) {
            img.src = eval('banner' + a);
            img.onload = function() { loadNextImg(a); this.loaded = true; }
            img.onerror = function() { loadNextImg(a); }
        }
		}
    }
    
}

function loadImg(a) {
    img = document.getElementById('megaImg_' + a);
	if(img!=null)
	{
    if (img.loaded != true) img.src = eval('banner' + a);
    img.onload = function() { this.loaded = true; };
    img.onerror = function() { alert('!'); };
	}
}

// Vertical MegaBanner
function vshowBanner(vselectedItemIndex) {
    vimg = document.getElementById('megaImgV_' + vselectedItemIndex);
 if(vimg!=null)
{
	if (vimg.loaded == true) {
        for (vi = 1; vi < vmaxHeaderlineCount; vi++) {
            if (vi != vselectedItemIndex) {
                document.getElementById('HeaderTableV_' + vi).style.display = 'none';
                document.getElementById('HeaderOKV_' + vi).style.display = 'none';
                document.getElementById('imgV_' + vi).style.border = '0px solid #0175dc';
            }
            else {
                document.getElementById('HeaderTableV_' + vi).style.display = 'block';
                document.getElementById('HeaderOKV_' + vi).style.display = 'none';
                document.getElementById('imgV_' + vi).style.border = '0px solid white';
            }
        }
    }
}
}

function vShowArticle(vselectedItemIndex) {

    vstopTimer();
    vshowBanner(vselectedItemIndex);
}

function vtimerStart(vconterNum) {
    if (vconterNum != -1)
        vconter = vconterNum;
    if (vconter >= vmaxHeaderlineCount) vconter = 1;
    vshowBanner(vconter);
    vconter = vconter + 1;
    vtimerCount = setTimeout('vtimerStart(-1)', 4000);
}

function vstopTimer() {
    clearTimeout(vtimerCount);
}


function vloadNextImg(va) {
    va = va + 1;
    if (va < 7) {
        vimg = document.getElementById('megaImgV_' + va);
		if(vimg!=null)
		{
        if (vimg.loaded != true) {
            vimg.src = eval('banner_' + va);
            vimg.onload = function() { vloadNextImg(va); this.loaded = true; }
            vimg.onerror = function() { vloadNextImg(va); }
        }
		}
    }
    
}

function vloadImg(va) {

    vimg = document.getElementById('megaImgV_' + va);
	if(vimg!=null)
	{
    if (vimg.loaded != true) vimg.src = eval('banner_' + va);
    vimg.onload = function() { this.loaded = true; };
    vimg.onerror = function() { alert('!'); };
	}
}

// VertMenu

function menuStart() {
    // ���������, � ������� ����� ����
    var parent = jQuery("#sub_menuparent", document);

    // �� ��� ������ ������ ������� ����
    jQuery(".country", parent).click(function() {
        // ������������ ����� �� �������
        var _this = jQuery(this);

        // ������� �� ������ ����, ��� �� ����� ��� �����, ������� ����� �����������
        var pid = _this.attr("id");

        // ������������� ����� ����
        var sub = jQuery("#sub_" + pid, parent);

        if (sub.css("display") == "none") {
            // ����� �������� ������������� � ������
            _this.removeClass("right");
            _this.addClass("expanded");
        }
        else {
            // ����� �������� ������������� � ������
            _this.removeClass("expanded");
            _this.addClass("right");
        }
        sub.slideToggle("slow");
        return false;
    });

    jQuery(".notcountry", parent).click(function() {
        // ������������ ����� �� �������
        var _this = jQuery(this);

        // ������� �� ������ ����, ��� �� ����� ��� �����, ������� ����� �����������
        var pid = _this.attr("id");

        // ������������� ����� ����
        var sub = jQuery("#sub_" + pid, parent);

        if (sub.css("display") == "none") {
            // ����� �������� ������������� � ������
            _this.removeClass("right");
            _this.addClass("expanded");
        }
        else {
            // ����� �������� ������������� � ������
            _this.removeClass("expanded");
            _this.addClass("right");
        }
        sub.slideToggle("slow");
        return false;
    });
}

function openTab(a) {
    a.style.display = "";
}

function initCountriesBlock() {

    var width = 550;
    if (tabcols > 0) {

        $(function() {
            var tabContainers = $('div.tabs > div'); // �������� ������ �����������
            tabContainers.hide().filter(':first').show(); // ������ ���, ����� �������
            // ����� �������������� ���� �� �������
            $('ul.tabNavigation a').click(function() {
                tabContainers.hide(); // ������ ��� ����
                tabContainers.filter(this.hash).show(); // ���������� ���������� ��������
                $('ul.tabNavigation a').removeClass('selected'); // � ���� ������� ����� 'selected'
                $(this).addClass('selected'); // ������� ������� ��������� ����� 'selected'
                return false;
            }).filter(':first').click();
        });

        var ww = Math.ceil(width / tabcols);

        if (ww < 100) {
            var t1 = Math.ceil(tabcols / 2);
            var t2 = tabcols - t1;
            var w1 = Math.ceil(width / t1)-1;
            var w2 = Math.ceil(width / t2)-1;

            for (i = 0; i < t1; i++) {
                $(tabs[i]).width(w1);
            }

            for (i = 0; i < t2; i++) {
                $(tabs[t1 + i]).width(w2);
            }

        }
    }
    else {
        $("#countriesBlock").hide();
    }
}


//right nav
function right_nav(item) {
   obj=document.getElementById(item);
   if (obj != null)
   {
   	visible=(obj.style.display!="none")
   	key=document.getElementById("x" + item);
   	if (visible) {
     obj.style.display="none";
     key.innerHTML="<img src='/templates/modern/css/images/arrow_state_blue_right.png' width='16' height='16' vspace='2' vspace='0' border='0'>";
   	} else {
      obj.style.display="block";
      key.innerHTML="<img src='/templates/modern/css/images/arrow_state_blue_expanded.png' width='16' height='16' vspace='2' vspace='0' border='0'>";
   	}
   }
}

