// JavaScript Document
window.openedMenu = null;
window.openedMenuDIV = null;
var dropDawnLeft = 0;
var dropDawnTop = 0;
var dropDawnButtonLeft = 0;
var dropDawnButtonTop = 0;

function browserType() {
    var agent = navigator.userAgent;
    var browser;
    if (agent.lastIndexOf('MSIE') != -1) browser = 'MSIE';
    else if (agent.lastIndexOf('Safari') != -1) browser = 'Safari';
    else if (agent.lastIndexOf('Firefox') != -1) browser = 'Firefox';
    else browser = 'Unknown';
    return browser;
}

function setUpPage() {
    $('#messageDiv').css('height', $(document).height() + 'px');
    $('#coveringDiv').css('height', $(document).height() + 'px');
    $('#coveringDiv').css('opacity', 0);
    
    //Search block styles            
    if (browserType() == 'Firefox') 
        $('#ctl00_searchCtrl_searchPopularResult').css('left', 171 + 'px');
    if (!(browserType() == 'Firefox' || browserType() == 'Safari')) {
        $('#ctl00_searchCtrl_searchPopularResult').css('left', 173 + 'px');
        $('div.searchDiv').css('right', '-1px');
    }

    searchResultLinkView();

    //FF and Safari styles    
    if (browserType() == 'Firefox' || browserType() == 'Safari') 
    {
        $('div.searchAgain a.buttonTemplate').css('margin-top', '5px');          
    }
    if (browserType() == 'Firefox') 
        $('div.breadCrumbs').css('margin-left', '-476px');
        
    //right block
    shadowCreation();

    //bottom area for index page
    indexBottomBlocks();
    
    //bottom line for more then 5 blocks
    bottomLine();

    if (document.getElementById('descriptionBg') != null)
        if (document.getElementById('descriptionBg').clientWidth > 920)
            $('div.descriptionOverflow div').css('width', '920px').css('white-space', 'normal');

    //contact form styles    
    $("div.contactForm input[type='file']").addClass('FileField');
    $("div.contactForm input[type='radio']").addClass('Radio');
    $("div.contactForm input[type='submit']").addClass('sendForm');
    
    if (browserType() == 'Firefox' || browserType() == 'Safari') {                
        if (browserType() == 'Firefox') {
            $("div.contactForm input[type='text']").addClass('Mozilla');
            $("div.contactForm td.input20 input[type='text']").addClass('Firefox');
            $("div.contactForm td.input40 input[type='text']").addClass('Firefox');
        }
        if (browserType() == 'Safari') {
            $("div.contactForm td.input20 input[type='text']").addClass('Safari');
            $("div.contactForm td.input40 input[type='text']").addClass('Safari');
        }
        $("div.contactForm input[type='radio']").css('top', '0px');
        $("div.contactForm input[type='file']").removeClass('FileField');
        $("div.contactForm input[type='file']").addClass('FileFieldMozilla');
        $("div.vacturesControls").css('margin-bottom', '10px');        
    }

    //button template
    buttonLenghtCheck();
    
    //pagging width
    if (document.getElementById('navigator') != null) {
        var a = document.getElementById('navigator').childNodes.length;
        $('#navigator').css('width', a * 10 + 'px');
    }
}

$(document).ready(function() {
    setUpPage();
    $('div.dropDownHolder > img').wrap('<div class="left"><div class="right"></div></div>');
    callMenu();
    if (typeof (openSearchForm) != 'undefined') {
        openSearchForm();
    }
});

function shadowCreation() {
    $('span.shadow1')        
        .css('top', '-' + $('span + span.shadow1').attr('offsetHeight') - 1 + 'px');        
    $('span.shadow2')        
        .css('top', '-' + $('span + span.shadow2').attr('offsetHeight') - 1 + 'px');        
    $('span.shadow3')        
        .css('top', '-' + $('span + span.shadow3').attr('offsetHeight') - 1 + 'px');

    $('span.shadow1').parent().css('height', $('span + span.shadow1').attr('offsetHeight') + 'px');
    $('span.shadow2').parent().css('height', $('span + span.shadow2').attr('offsetHeight') + 'px');
    $('span.shadow3').parent().css('height', $('span + span.shadow3').attr('offsetHeight') + 'px');
        
    //$('div.RightGrayBlock').css('margin-bottom', '30px');

    $isImg1 = $('div.RightGrayBlock span.text1 > span >img').get(0);
    $isImg2 = $('div.RightGrayBlock span.text2 > span >img').get(0);
    $isImg3 = $('div.RightGrayBlock span.text3 > span >img').get(0);

    if ($isImg1 != null && $isImg1 != 'undefined' && $isImg1.tagName == 'IMG') {
        $('span.shadow1')
            .css('top', '-' + $('span.text1').attr('offsetHeight') - 1 + 'px')
            .css('margin-left', $('span.text1 img').attr('offsetWidth') - 1 + 'px');
    }
    if ($isImg2 != null && $isImg2 != 'undefined' && $isImg2.tagName == 'IMG') {
        $('span.shadow2')
            .css('top', '-' + $('span.text2').attr('offsetHeight') - 1 + 'px')
            .css('margin-left', $('span.text2 img').attr('offsetWidth') - 1 + 'px');
    }
    if ($isImg3 != null && $isImg3 != 'undefined' && $isImg3.tagName == 'IMG') {
        $('span.shadow3')
            .css('top', '-' + $('span.text3').attr('offsetHeight') - 1 + 'px')
            .css('margin-left', $('span.text3 img').attr('offsetWidth') - 1 + 'px');
    }

    if (browserType() == 'Safari') {
        if ($isImg1 != null && $isImg1 != 'undefined' && $isImg1.tagName == 'IMG')
            $('span.shadow1').css('top', '-37px')
                             .css('margin-left', '15px')
                             .css('width', '153px');
        if ($isImg2 != null && $isImg2 != 'undefined' && $isImg2.tagName == 'IMG')
            $('span.shadow2').css('top', '-22px').css('margin-left', '15px');
        if ($isImg3 != null && $isImg3 != 'undefined' && $isImg3.tagName == 'IMG')
            $('span.shadow3').css('top', '-37px').css('margin-left', '15px');
    }         
}


function indexBottomBlocks() {    
    bottomBlock = $("div.bottomItem");    
    maxHeight = 105;
    for (var i = 0; i < bottomBlock.length; i++) 
    {
        //if (bottomBlock[i].clientHeight > maxHeight)
        //    maxHeight = bottomBlock[i].clientHeight;
        if (((browserType() == 'Safari' || browserType() == 'Firefox') && bottomBlock[i].childNodes[1].innerHTML.indexOf("<div") != -1) ||
             (browserType() == 'MSIE' && bottomBlock[i].childNodes[0].innerHTML != ""))
            bottomBlock[i].style.visibility = "visible";                    
    }
     
    /*
    maxHeight = maxHeight - 30;
    for (var i = 0; i < bottomBlock.length; i++) 
    {
        bottomBlock[i].style.height = maxHeight + 'px';               
    }    
    */
}

function bottomLine() {
    var columnArray = $('div.pageBottomHolderDiv > div.middle').children();
    var columsNumber = columnArray.length;
    var newDiv = '<br clear="all"/><div class="bottomSeparator"></div>';

    if (columsNumber > 6) {
        curDiv = $('div.pageBottomHolderDiv div.column:eq(4)');
        curDiv.after(newDiv);
        
        if (columsNumber > 11) {
            curDiv = $('div.pageBottomHolderDiv div.column:eq(9)');
            curDiv.after(newDiv);
        }
    }
}

function buttonLenghtCheck() {
    if (browserType() == 'Safari') {
        $("a.buttonTemplate span.buttonBody").css('padding-top', '1px');
        $("a.buttonTemplate span.buttonBody span.text").css('margin-top', '-11px');
    }

    buttonSpans = $("a.buttonTemplate span.buttonBody span.shadow");
    for (var i = 0; i < buttonSpans.length; i++) {
        if (buttonSpans[i].offsetHeight > 20) {

            divShadow = buttonSpans[i];
            divText = buttonSpans[i].nextSibling.nextSibling;
            setWidth = buttonSpans[i].offsetWidth;

            divShadow.style.width = setWidth - 10 + 'px';
            divText.style.width = setWidth - 10 + 'px';
            if (browserType() == 'Firefox' || browserType() == 'Safari') {
                divShadow.style.width = setWidth - 20 + 'px';
                divText.style.width = setWidth - 20 + 'px';
            }            
             
            divShadow.style.height = '10px';
            divText.style.height = '10px';

            divShadow.style.marginTop = '0px';
            divText.style.marginTop = '-11px';
        }
    }
}

// message window
function scrollProgressDiv() {
    var messageBox = $('#messageBox');
    mesBoxHeight = messageBox.height();
    mesBoxWidth = messageBox.width();
    messageBox.css('top', (document.body.offsetHeight - mesBoxHeight) / 2 + document.documentElement.scrollTop + 'px')
                    .css('left', ($(document).width() - mesBoxWidth) / 2 + document.documentElement.scrollLeft + 'px');
}

$(window).scroll(scrollProgressDiv);

function showProgress(message) {
    $('#messageDiv').css('display', 'block');
    $('#messageBox').css('display', 'block');
    scrollProgressDiv();
    $('#messageBox div').text(message);
}

function hideProgress() {
    $('#messageDiv').css('display', 'none');
    $('#messageBox').css('display', 'none');
}

// search functions

function searchResultLinkView() {
    var linkLength = 0;
    
    rearchResultLink = $("div.search_left a");    
    linksNumber = rearchResultLink.length;

    for (var i = 0; i < linksNumber; i++) {
    
        //method that count number of symbols
        //that should be cut to get max length of string
        //that can be seen with "..." at the end
        
        var innerTextCut = 0;
        var innerTextLength = 0;
        var maxLenght = 0;


        innerTextCut = rearchResultLink[i].innerHTML;
        innerTextWidth = rearchResultLink[i].offsetWidth;
        innerTextLength = innerTextCut.length;
        maxLenght = rearchResultLink[i].parentNode.clientWidth;
       
        if (innerTextWidth > maxLenght) {
            while (innerTextWidth > maxLenght) {
                innerTextLength--;
                innerTextCut = innerTextCut.substring(0, innerTextLength);
                rearchResultLink[i].innerHTML = innerTextCut;
                innerTextWidth = rearchResultLink[i].offsetWidth;
            }

            innerTextLength = innerTextLength - 3;
            innerTextCut = innerTextCut.substring(0, innerTextLength)+"...";            
            rearchResultLink[i].innerHTML = innerTextCut;
        }

        /*        
        //method that cut fixed number of symbols
                
        var linkLength = 0;
        var linkCut = 0;
        var maxLenght = (rearchResultLink[i].parentNode.clientWidth)/10 - 1;

        linkLength = rearchResultLink[i].href.length;
        if (linkLength > maxLenght) {
        linkCut = rearchResultLink[i].href.substring(0, maxLenght) + "...";
        rearchResultLink[i].innerHTML = linkCut;
        }
        */        
    }    
}

function checkSearch(obj, startValue) {
    if (obj.value == startValue) {
        obj.value = '';
        obj.className = 'trueColor';
        return;
    }
    if (obj.value == '') {
        obj.value = startValue;
        obj.className = '';
        return;
    }
    obj.className = 'trueColor';
}
