$(document).ready(function(){

    var contactMenuOpen=false;

    $('.contactMenu .contactButton').click(function(){
        if(contactMenuOpen) logicContactMenuClose();
        else logicContactMenuOpen();
        contactMenuOpen=(contactMenuOpen) ? false : true;
        return false;
    });

    $('.contactMenu .scroll a').click(function(){
        logicContactMenuScroll(this);
        return false;
    });

    $('.contactMenu .countryListItem').click(function(){
        $('.contactMenu .countryListItem').removeClass('select');
        $(this).addClass('select');
        logicShowCountyCityList($(this).attr('rel'));
        return false;
    });

    $('.contactMenu .cityListItem').click(function(){
        $('.contactMenu .cityListItem').removeClass('select');
        $(this).addClass('select');
        logicShowCityHotelList($(this).attr('rel'));
        return false;
    });

});





function logicContactMenuOpen() {    
    $('.contactMenu .bg').show();
    $('.contactMenu .bg').animate({
        width: '677px',
        height: '387px'
    }, 500, function() {
        $('.contactMenu .countryList .countryListItem:first').click();   
        $('.contactMenu .content').fadeIn(300);    
    });
}



function logicContactMenuClose() {
    $('.contactMenu .content').fadeOut(200, function() {
        $('.contactMenu .bg').animate({width: '219px',height: '37px'}, 400,function(){
            $('.contactMenu .bg').hide();
        });
    });
}



function logicContactMenuScroll(element) {
    heightWindow=$('.contactMenu .hotels').height();
    heightList=$('.contactMenu .hotelList:visible').innerHeight();
    // console.log(heightWindow+' - '+heightList);
    if(heightList>heightWindow){
        var topOffsetSrt=$('.contactMenu .hotelList:visible').css('margin-top'); 
        var topOffset=parseInt(topOffsetSrt.substr(0,topOffsetSrt.length-2),10); 
        var offset=200;
        if($(element).hasClass('up')){
            if(topOffset<0) $('.contactMenu .hotelList:visible').animate({'margin-top':(topOffset+offset)+'px'},400);
        } else {
            if((topOffset+heightList)>heightWindow) $('.contactMenu .hotelList:visible').animate({'margin-top':(topOffset-offset)+'px'},400);
        }
    }
}



function logicShowCountyCityList(countyId) {
    $('.contactMenu .cityList').hide();
    $('.contactMenu .cityList.'+countyId+' .cityListItem:first').click();
    $('.contactMenu .cityList.'+countyId).show();
}



function logicShowCityHotelList(cityId) {

    $('.contactMenu .hotelList').hide();
    $('.contactMenu .hotelList.'+cityId).show();

    var heightWindow=$('.contactMenu .hotels').height();
    var heightList=25;
    
    $('.contactMenu .hotelList:visible .hotelListItem').each(function(){
           heightList+=parseInt($(this).innerHeight(),10);
    });
    
    if(heightList>heightWindow){

        $('.contactMenu .hotelList.'+cityId).jScrollPane(
        {
            showArrows: true,
            verticalDragMinHeight: 86,
            verticalDragMaxHeight: 86,
            horizontalGutter: 10
        }
        );

        // $('.contactMenu .scroll').css({'background-image':'url(client/img/contactMenu/hotelsScroll.png)'});
        // $('.contactMenu .scroll').show();
    } else {
        // $('.contactMenu .scroll').css({'background-image':'url(client/img/contactMenu/hotelsScrollWithoutPointers.png)'});      
        //$('.contactMenu .scroll').hide();
    }

}
