var CH=(navigator.userAgent.indexOf('Chrome')!=-1);
var SF=(navigator.userAgent.indexOf('Safari')!=-1 && !CH);
var OP=(navigator.userAgent.indexOf('Opera')!=-1);
var IE=(navigator.userAgent.indexOf('MSIE')!=-1 && !OP);
var IE6=(navigator.userAgent.indexOf('MSIE 6')!=-1 && !OP);
var FF=(navigator.userAgent.indexOf('Firefox')!=-1);
var MZ=(navigator.userAgent.indexOf('Gecko')!=-1 && !SF && !CH);

function getWindowW() { 
    var b=(document.compatMode=="CSS1Compat")? document.documentElement : document.body;
    return (IE)?(b.clientWidth):window.innerWidth; 
}
function getWindowH() { 
    var b=(document.compatMode=="CSS1Compat")? document.documentElement : document.body;
    return (IE)?(b.clientHeight):window.innerHeight; 
}
function getScrollBody() {
    return (CH || SF)?document.body:document.documentElement;
}
function getScrollW() { return getScrollBody().scrollWidth; }
function getScrollH() { return getScrollBody().scrollHeight; }
function getScrollX() { return getScrollBody().scrollLeft; }
function getScrollY() { return getScrollBody().scrollTop; }
function getDocumentW() { return document.body.offsetWidth; }
function getDocumentH() { return document.body.offsetHeight; }

var onloadFuncs=new Array();
function _onload() {
    for (i=0;i<onloadFuncs.length;i++) {
        eval(onloadFuncs[i]);
    }
}

/* >>>>> для стилей */
function StyleSheet_makeCompatible(style) {
// Mozilla не даёт доступа к cssRules до загрузки стиля
    try {
        style.cssRules;
    }
    catch (e) {
        return style;
    }
    // Создаём CSSStyleSheet.cssRules
    if (typeof style.cssRules == 'undefined' && typeof style.rules != 'undefined') {
        style.cssRules = style.rules;
    }
    // Создаём CSSStyleSheet.insertRule и CSSStyleSheet.deleteRule
    if (typeof style.insertRule == 'undefined' && typeof style.addRule != 'undefined') {
        style.insertRule = StyleSheet_insertRule;
    }
    if (typeof style.deleteRule == 'undefined' && typeof style.removeRule != 'undefined') {
        style.deleteRule = style.removeRule;
    }

    // Проверяем, существуют ли все нужные свойства
    if (typeof style.cssRules == 'undefined' || typeof style.insertRule == 'undefined'
            || typeof style.deleteRule == 'undefined') {
        return null;
    }
    else {
        return style;
    }
}

function StyleSheet_insertRule(rule, index) {
    // Выделяем селектор и стиль из параметра
    if (rule.match(/^([^{]+)\{(.*)\}\s*$/))
    {
        this.addRule(RegExp.$1, RegExp.$2, index);
        return index;
    }
    throw "Syntax error in CSS rule to be added";
}

function getStyleSheet(index) {
    // Проверяем параметр
    if (typeof index == 'undefined') {
        index = 0;
    }
    if (index < 0) {
        index = document.styleSheets.length + index;
    }
    if (typeof document.styleSheets == 'undefined' || index < 0
            || index >= document.styleSheets.length) {
        return null;
    }
    // Находим стиль
    var style = document.styleSheets[index];
    // Делаем объект совместимыми с W3C DOM2 (для IE)
    return StyleSheet_makeCompatible(style);
}
function addStyleSheet(url) {
    // Создаём элемент LINK/STYLE и добавляем в документ
    var style;
    if (typeof url == 'undefined') {
        style = document.createElement('style');
    }
    else {
        style = document.createElement('link');
        style.rel = 'stylesheet';
        style.type = 'text/css';
        style.href = url;
    }
    document.getElementsByTagName('head')[0].appendChild(style);
    // Находим новый стиль в коллекции styleSheets
    style = document.styleSheets[document.styleSheets.length - 1];
    // Делаем объект совместимыми с W3C DOM2 (для IE)
    return StyleSheet_makeCompatible(style);
}

function stylesheet(stitle) {
    var stylesheetId=false;
    for (i=0;i<document.styleSheets.length;i++) {
        if (document.styleSheets[i].title==stitle) { stylesheetId=i; break; }
    }
    
    if (stylesheetId==false) {
        stylesheetId=document.styleSheets.length;
        var nstyle=document.createElement('style');
        nstyle.type="text/css";
        nstyle.title=stitle;
        document.getElementsByTagName('head')[0].appendChild(nstyle);

    }
    return StyleSheet_makeCompatible(document.styleSheets[stylesheetId]);
    //return (IE) ? document.styleSheets[stylesheetId].rules : document.styleSheets[stylesheetId].cssRules;
}
/* <<<<< для стилей */


/* >>>>> показ инфы по стране */
var addInfoTimer=0;
var addInfoTimeout=6;
var addInfoStep=30;
var addInfoInProcess=[];
function addInfoClick(id) {
    if (addInfoInProcess[id]) { return; }
    addInfoInProcess[id]=true;
    if ($('AddInfoItem_'+id).className.indexOf('opened')==-1) {
        addInfoShow(id,true);
    }
    else {
        addInfoHide(id,true);
    }
}
function addInfoShow(id,f) {
    if (f) {
        $('AddInfoItem_'+id).style.height=$('AddInfoItem_'+id).offsetHeight+'px';
        $('AddInfoItem_'+id).style.overflow='hidden';
        $('AddInfoItem_'+id).className='item opened';
    }
    var needH=$('AddInfoItemTitle_'+id).offsetHeight+$('AddInfoItemInner_'+id).offsetHeight;
    var newH=$('AddInfoItem_'+id).offsetHeight+addInfoStep;
    if (needH > newH) {
        $('AddInfoItem_'+id).style.height=newH+'px';
        setTimeout('addInfoShow('+id+',false)',addInfoTimeout);
    }
    else {
        $('AddInfoItem_'+id).style.overflow='';
        $('AddInfoItem_'+id).style.height='';
        addInfoInProcess[id]=false;
        if (IE6) { 
            document.body.style.paddingBottom=(document.body.style.paddingBottom=="1px")?"0px":"1px";  
        }
    }
    
}
function addInfoHide(id,f) {
    if (f) {
        $('AddInfoItem_'+id).style.height=$('AddInfoItem_'+id).offsetHeight+'px';
        $('AddInfoItem_'+id).style.overflow='hidden';
    }
    var needH=$('AddInfoItemTitle_'+id).offsetHeight;
    var newH=$('AddInfoItem_'+id).offsetHeight-addInfoStep;
    if (needH < newH) {
        $('AddInfoItem_'+id).style.height=newH+'px';
        setTimeout('addInfoHide('+id+',false)',addInfoTimeout);
    }
    else {
        $('AddInfoItem_'+id).className='item';
        $('AddInfoItem_'+id).style.overflow='';
        $('AddInfoItem_'+id).style.height='';
        addInfoInProcess[id]=false;
        if (IE6) { 
            document.body.style.paddingBottom=(document.body.style.paddingBottom=="1px")?"0px":"1px";  
        }
    }
    
}

/* <<<<< показ инфы по стране */

function checkboxCheckParent(obj) {
    var flag=obj.checked;
    var rx=/checked/g;
    if (flag) { obj.parentNode.className+=' checked'; }
    else {
        obj.parentNode.className=obj.parentNode.className.replace(rx,'');
    }
    if (obj.name=='service_packet') {
        // жмакнули на пакетные услуги
        var elems=$('CompanyServices').getElementsByTagName('input');
        for (i=0;i<elems.length;i++) {
            if (elems[i].parentNode.className.indexOf('li')==0) {
                elems[i].checked=flag;
                if (flag) { elems[i].parentNode.className+=' checked'; }
                else { elems[i].parentNode.className=elems[i].parentNode.className.replace(rx,''); }
            }
        }
    }
    else if (obj.parentNode.className.indexOf('li')==0) {
        // жмакнули на внутренность пакетных услуг
        var elems=$('CompanyServices').getElementsByTagName('input');
        var checkAll=true;
        for (i=0;i<elems.length;i++) {
            if (elems[i].parentNode.className.indexOf('li')==0) {
                checkAll=checkAll && elems[i].checked;
            }
        }
        $('S_packet').checked=checkAll;
        $('S_packet').parentNode.className=(checkAll)?'checked':'';
    }
}


function countryMakeVisaCost() {
    var theform=$('CountryVisa');
    if ($('ConsularType')) {
        var sumC=Number(ConsularCost[$('ConsularType').value]);//*CurrencyExchange;
    }
    else {
        var sumC=Number(ConsularCost);//*CurrencyExchange;
    }
    for (i=0;i<RequiredCost.length;i++) {
        sumC+=Number(RequiredCost[i]);
    }
    var sum=0;
    var elems=$('CompanyServices').getElementsByTagName('input');
    var checkAll=$('S_packet').checked;
    for (i=0;i<elems.length;i++) {
        if (elems[i].type=='checkbox' && elems[i].checked==true) {
            if (!(checkAll && elems[i].parentNode.className.indexOf('li')==0)) {
                var stype=elems[i].name.replace(/service_/,'');
                var c=1;
                if (stype=='insurance') { c=14; }
                else if (stype=='translate') { c=4; }
                sum+=Number(ServicesCost[stype])*c;
            }
        }
    }
    if (CurrencyKey && CurrencyKey!='uah') {
        $('VisaCost').innerHTML=Math.round(sum);
        $('VisaCostC').innerHTML=Math.round(sumC);
        $('VisaSummC').style.display=(sumC)?'':'none';
        
    }
    else {
        $('VisaCost').innerHTML=Math.round(sum+sumC);
    }
}

function orderChildrenChange() {
    $('ChildrenTransport').style.display=($('CountChildren').value==0)?'none':'';
    if (IE6) { 
        document.body.style.paddingBottom=(document.body.style.paddingBottom=="1px")?"0px":"1px";  
    }
}
function orderFailChange() {
    $('VisaFailInfo').style.display=($('VisaFail').checked)?'':'none';
    if (IE6) { 
        document.body.style.paddingBottom=(document.body.style.paddingBottom=="1px")?"0px":"1px";  
    }
}
function orderShengenChange() {
    $('VisaShengenInfo').style.display=($('ShengenVisa').checked)?'':'none';
    if (IE6) { 
        document.body.style.paddingBottom=(document.body.style.paddingBottom=="1px")?"0px":"1px";  
    }
}
function orderCountryChange() {
    if ($('CountrySelect').value) {
        window.location='/country/'+$('CountrySelect').value+'/order/';
    }
}

var indexCurrentPromo;
function indexReloadOpeningClick() {
    $('ReloadImage').className='anim';
    setTimeout('indexReloadOpening()',2*1000);
}
function indexReloadOpening() {
    new Ajax.Request( '/index/reload/?'+Math.random(),
        {
            method: 'post',
            postBody: 'ajax=ajax&current='+indexCurrentPromo,
            onSuccess: indexReloadOpeningSet
        }
    );
}
function indexReloadOpeningSet(answer) {
    var result=answer.responseText;
    result=result.split('|+|');
    $('Visual').className='visual '+result[1];
    $('Opening').innerHTML=result[2];
    $('Proposition').innerHTML=result[3];
    $('ReloadImage').className='';
    indexCurrentPromo=result[0];
}
