$(document).ready(function(){
    $('#header_search_form #query').keyup(function() {
        $('.ac_results').css('dispaly','block');
        $('.search_text_wrapper_inputbox').css('dispaly','block');
        var len = $('#query').val();
        if((len.length)>=2 && $('#mainpageresult').length<1) {
        // $('.search_text_wrapper_inputbox').append('<div class="ac_results" id="mainpageresult" style="padding-left:15px; width:auto;" ><img height="15" width="128" id="notam_loader" style="margin:2px;" src="/images/ajax-loader.gif"/></div>');
        }
        if((len.length)>=15 || (len.length)<2) { //$('#mainpageresult').remove();
    }
    });

    $('#ev_homepage').click(function(){ //	$('#mainpageresult').remove();
        });

    //Home Page Js for popups
    $("#signIn, #notLogin,.signpopup").bind('mouseenter', function(event) {
        $('.signpopup').css({
            "visibility":"visible",
            "display":"block"
        });
        $('#longLogin').css({
            "visibility":"hidden",
            "display":"none"
        });
        $('.signpopin').css({
            "padding":"0px"
        });
    });
    $(".signpopup,#signIn").bind('mouseleave', function(event) {
        $('.signpopup').css({
            "visibility":"hidden",
            "display":"none"
        });
    });
    $("#help, #help_txt").click(function(){
        if($('#longLogin').css('visibility')=='visible'){
            $('#longLogin').css({
                "visibility":"hidden",
                "display":"none"
            });
            $('.signpopin').css({
                "padding":"0px"
            });
        }else{
            $('#longLogin').css({
                "visibility":"visible",
                "display":"block"
            });
            $('.signpopin').css({
                "padding":"0 0 10px"
            });
        }
    });
    $("#send_pass").click(function(){
        var email=$("#emailId").val();
        if(email!="")
            $("#forgot_pass").submit();
        else
            $('#emailId').css({
                "border":"1px solid #C01F2F",
                "background-color":"#FFEAEA",
                "background-image":"none"
            })
    });
    $("#send_pass2").click(function(){
        var email=$("#email_Id").val();
        if(email!="")
            $("#forgotPass").submit();
        else
            $('#email_Id').css({
                "border":"1px solid #C01F2F",
                "background-color":"#FFEAEA",
                "background-image":"none"
            })
    });

    // Submenu div
    $("#submenu_show, #sub_menu li").mouseover(function(){
        $("#sub_menu").show();
        $("#lang_list").hide();
        $("#currency_list").hide();
    });
    $("#sub_menu,#submenu_show").bind('mouseleave', function(event) {
        $("#sub_menu").hide();
    });

    $("#submenu_showDiscover, #sub_menuDiscover li").mouseover(function(){
        $("#sub_menuDiscover").show();
        $("#lang_list").hide();
        $("#currency_list").hide();
    });
    $("#sub_menuDiscover,#submenu_showDiscover").bind('mouseleave', function(event) {
        $("#sub_menuDiscover").hide();
    });

    // Language selection div
    $("#lang_show, #lang_list li").mouseover(function(){
        $("#lang_list").show();
        $("#sub_menu").hide();
        $("#currency_list").hide();
    });

    $("#lang_list,#lang_show").bind('mouseleave', function(event) {
        $("#lang_list").hide();
    });
    // currency selection div
    $("#currency_show, #currency_list li").mouseover(function(){
        $("#currency_list").show();
        $("#lang_list").hide();
        $("#sub_menu").hide();
    });
    $("#currency_list,#currency_show").bind('mouseleave', function(event) {
        $("#currency_list").hide();
    });

    $(".currencyChange").click(function(){
        var cur=$(this).attr('id');
        $("#currentCurrency").val(cur)
        $("#currencyFrm").submit();
    });

    $("#lang_list li").click(function(){
        var lid=$(this).attr('id');
        $(location).attr('href','/home/index/language/'+lid);
    });

    $("#div_map_popup").hide();
    var checkW=0;var checkF=0;var checkP=0;var checkR=0;
    $(".texthelpwrapper,.ourspecial_wrapper,.tripobenifit_wrapper,.enjoyitwrapper").click(function(){
       if ($(".div_tripofinder_popup").is(":visible")) {
            $(".div_tripofinder_popup").hide();
            checkW=0;checkF=0;checkP=0;checkR=0;
        }
    })
    //what
    $("#showWhat").click(function(){
        if(checkW==0){
           $("#div_what_popup").show();
           $("#div_map_popup").hide();checkR=0;
           $("#div_for_popup").hide();checkF=0;
           $("#div_price_popup").hide();checkP=0;
           checkW=1;
        }
        else if(checkW==1){
           $("#div_what_popup").hide();
           checkW=0;
        }
       
    });
    //for
     $("#showFor").click(function(){
        if(checkF==0){
           $("#div_for_popup").show();
           $("#div_what_popup").hide();checkW=0;
           $("#div_map_popup").hide();checkR=0;
           $("#div_price_popup").hide();checkP=0;
           checkF=1;
        }
        else if(checkF==1){
           $("#div_for_popup").hide();
           checkF=0;
        }
        
    });
    //price
    $("#showPrice").click(function(){
        if(checkP==0){
           $("#div_price_popup").show();
           $("#div_what_popup").hide();checkW=0;
           $("#div_map_popup").hide();checkR=0;
           $("#div_for_popup").hide();checkF=0;
           checkP=1;
        }
        else if(checkP==1){
           $("#div_price_popup").hide();
           checkP=0;
        }
       
    });

    //where
    $("#showWhere").click(function(){
       if(checkR==0){
           $("#div_map_popup").show();
           $("#div_what_popup").hide();checkW=0;
           $("#div_for_popup").hide();checkF=0;
           $("#div_price_popup").hide();checkP=0;
           checkR=1;
        }
        else if(checkR==1){
           $("#div_map_popup").hide();
           checkR=0;
        }
       
    });
    $("div..headerbg").hover(function(){
        $("div..headerbg").css('z-index',2);
    },function(){
        $("div..headerbg").css('z-index',0);
    });
    $("#div_price_popup ul li").click(function(){
        var params={};
        var priceRange=$(this).attr('id');
        priceRange=priceRange.split("-");
        priceRangeMax=priceRange[1];
        priceRangeMin=priceRange[0];
        if(priceRangeMin==0)
        {
            priceRangeMin=1;
        }
        $("#min_price").val(priceRangeMin);
        $("#max_price").val(priceRangeMax);
        var selectedPrice=$(this).children().text();
        //$("#selectedPrice").css({'width':'113px'});
        $("#selectedPrice").text(selectedPrice.substring(0,11));
        $("#priceRange").val(selectedPrice);
        $("#cancelPrice").show();
        $("#div_price_popup").hide();
        checkP=0;
        //$("#form_price").submit();
    });
    $("#div_what_popup ul li").click(function(){
        var url= $(this).attr('id');
        var typeId= $(this).attr('class');
        var selectedWhat=$(this).children().text();
        $("#selectedWhat").text(selectedWhat);
        $("#whatSelectedtext").val(selectedWhat);
        $("#whatSelected").val(url);
        $("#layoutId").val(typeId);
        $("#cancelWhat").show();
        $("#div_what_popup").hide();
        checkW=0;
    });
    $("#div_for_popup ul li").click(function(){
        var url= $(this).attr('id');
        var selectedFor=$(this).children().text();
        $("#selectedFor").text(selectedFor.substring(0,11));
        $("#forSelected").val(url);
        $("#cancelFor").show();
        $("#div_for_popup").hide();
        checkF=0;
    });
    $(".wherepopupBody p a").live('click',function(){
        var city= $(this).text();
        $("#selectedWhere").text(city);
        if($(this).attr('class')=='reg'){
            $("#regionid").val($(this).attr('id'));
            $("#cityid").val(0);
        }
        else if($(this).attr('class')=='city'){
            $("#cityid").val($(this).attr('id'));
            $("#regionid").val(0);
        }
        $("#cancelWhere").show();
        $("#whereSelected").val(city);
        $("#div_map_popup").hide();
        checkR=0;
    });
    
});

//where pop up ajax 

    function loadRegion(country,match){
        $("#showCountrylist").hide();
        jQuery('#regionsAlphabets').html('<span><img src="/images/indicator.gif" id="load_img" class="float_left"  style="margin-left: 202px;"/></span>');
        var firstLetter="";
        jQuery.ajax({
            type:'POST',
            dataType:'html',
            data:'country=' + country,
            url:'/home/loadRegions',
            success:function(data){//alert(data);
                //firstLetter=data.split("*");
                jQuery('#regionsAlphabets').html(data);
                if(match=='B')
                    firstLetter='B';
                else
                    firstLetter=$("#firstLetter").val();
                $('#regionsAlphabets a').each(function(){
                    if($(this).text()==firstLetter)
                       $(this).addClass('selectedLetter');
                });
                loadCity(country,firstLetter)
            }
            });
    }

    $("#regionsAlphabets a").live('click',function(){
       $("#regionsAlphabets").children().removeClass('selectedLetter');
       var country=$("#countryCode").val();
       var matchLetter=$(this).text();
       $(this).addClass('selectedLetter');
       loadCity(country,matchLetter);
    });
    function loadCity(country,matchLetter){
        $(".countryList").hide();
        jQuery('#wherepopupBody').html('<span id="loadImg"><img src="/images/fo/ajax-loader.gif" id="load_img" class=""/></span>');
        jQuery.ajax({
        type:'POST',
        dataType:'html',
        data:'country=' + country+'&match=' + matchLetter,
        url:'/home/loadCities',
        success:function(data1){//alert(data1);
            data1=data1.split("*");
            if(data1[1]>25){
                $("div #div_map_popup_result").css({'height':'250px','margin-top':'-261px'});                
                $("div #div_map_popup").css({'height':'250px','margin-top':'-260px'});
            }
            else{
                $("div #div_map_popup_result").css({'height':'180px','margin-top':'-191px'});
                $("div #div_map_popup").css({'height':'180px','margin-top':'-190px'});
            }
            $("#showCountrylist").show();
            jQuery('#wherepopupBody').html(data1[0]);
        }
    });
    }
    ///////////////////////////
function clearField(id, value) {
    var el = document.getElementById(id);
    if (el.value == value) {
        el.value = '';
    }
}
function checkFieldValue(id, value) {
    var el = document.getElementById(id);
    if (el.value.length == 0 || el.value == null) {
        el.value = value;
    }
}
function checkFieldValue_tiny(ed, value) {
    //alert(value);
    var count=(ed.activeEditor.getContent()).replace(/(<([^>]+)>)/ig,""); 
    if(count.length == 0){
        ed.activeEditor.setContent(value);
    }
}
function slideShowHide(details_id, summary_id) {
    $(summary_id).slideUp();
    $(details_id).slideDown();
}
function toggleImage(details_id, image_id) {
    if ($(image_id).attr('alt') == 'Search_small_arrow_right') {
        $(details_id).slideDown();
        $(image_id).attr('src', $(image_id).attr('src').replace('right', 'down'));
        $(image_id).attr('alt', 'Search_small_arrow_down');
    } else {
        $(details_id).slideUp();
        $(image_id).attr('src', $(image_id).attr('src').replace('down', 'right'));
        $(image_id).attr('alt', 'Search_small_arrow_right');
    }
}
function toggleImagenew(details_id, image_id) {
    //alert($(image_id).attr('src'));
    if ($(image_id).attr('alt') == 'Search_arrow_down') {
        $(details_id).slideDown();
        $(image_id).attr('src', $(image_id).attr('src').replace('filtericon_openarrow', 'filtericon_downarrow'));
        $(image_id).attr('alt', 'Filtericon_downarrow');
    //alert($(image_id).attr('src'));
    } else {
        $(details_id).slideUp();
        $(image_id).attr('src', $(image_id).attr('src').replace('filtericon_downarrow', 'filtericon_openarrow'));
        $(image_id).attr('alt', 'Search_arrow_down');
    }
} 
function toggleImageExtraFilters(details_id, image_id) {
    if ($(image_id).attr('alt') == 'Search_small_arrow_right') {
        if (details_id == '#equipment_content') {
            $('#environment_content').slideUp();
            $('#environment_arrow').attr('src', $('#environment_arrow').attr('src').replace('down', 'right'));
            $('#environment_arrow').attr('alt', 'Search_small_arrow_down');

            $('#area_content').slideUp();
            $('#area_arrow').attr('src', $('#area_arrow').attr('src').replace('down', 'right'));
            $('#area_arrow').attr('alt', 'Search_small_arrow_down');
        } else if (details_id == '#environment_content') {
            $('#equipment_content').slideUp();
            $('#equipment_arrow').attr('src', $('#equipment_arrow').attr('src').replace('down', 'right'));
            $('#equipment_arrow').attr('alt', 'Search_small_arrow_down');

            $('#area_content').slideUp();
            $('#area_arrow').attr('src', $('#area_arrow').attr('src').replace('down', 'right'));
            $('#area_arrow').attr('alt', 'Search_small_arrow_down');
        } else if (details_id == '#area_content') {
            $('#equipment_content').slideUp();
            $('#equipment_arrow').attr('src', $('#equipment_arrow').attr('src').replace('down', 'right'));
            $('#equipment_arrow').attr('alt', 'Search_small_arrow_down');

            $('#environment_content').slideUp();
            $('#environment_arrow').attr('src', $('#environment_arrow').attr('src').replace('down', 'right'));
            $('#environment_arrow').attr('alt', 'Search_small_arrow_down');
        }
     
        $(details_id).slideDown();
        $(image_id).attr('src', $(image_id).attr('src').replace('right', 'down'));
        $(image_id).attr('alt', 'Search_small_arrow_down');
    } else {
        $(details_id).slideUp();
        $(image_id).attr('src', $(image_id).attr('src').replace('down', 'right'));
        $(image_id).attr('alt', 'Search_small_arrow_right');
    }
}
function toggleAccordionImage(image_id, max, text_id) {
    if (!text_id) {
        text_id = 'space_';
    }
    for (var i=1; i<=max; i++) {
        $('#'+text_id+i).attr('src', $('#'+text_id+i).attr('src').replace('down', 'right'));
        $('#'+text_id+i).attr('alt', 'Search_right_arrow');
    }
    $(image_id).attr('src', $(image_id).attr('src').replace('right', 'down'));
    $(image_id).attr('alt', 'Search_down_arrow');
}
function accordion(id, max) {
    for(var i=1; i<=max; i++) {
        if (i != id) {
            $('#job'+i).slideUp();
            $('#job_heading' + i + ' td').css({
                'font-weight': 'normal'
            });
            $('#job_heading' + i + ' td.last a').css({
                'font-weight': 'bold'
            });
            $('#job_heading' + i + ' td').css({
                'text-decoration': 'none'
            });
        }
    }
    $('#job'+id).slideToggle();
    if ($('#job_heading'+id + ' td.last a').css('font-weight') == 'bold') {
        $('#job_heading' + id + ' td').css({
            'font-weight': 'bold'
        });
        $('#job_heading' + id + ' td.last a').css({
            'font-weight': 'normal'
        });
        $('#job_heading' + id + ' td').css({
            'text-decoration': 'underline'
        });
    } else {
        $('#job_heading' + id + ' td').css({
            'font-weight': 'normal'
        });
        $('#job_heading' + id + ' td.last a').css({
            'font-weight': 'bold'
        });
        $('#job_heading' + id + ' td').css({
            'text-decoration': 'none'
        });
    }
}
function accordionRoom(id, max) {
    for(var i=1; i<=max; i++) {
        if (i != id) {
            $('#room_details_'+i).slideUp();
            $('#arrow_'+i).attr('src', $('#arrow_'+i).attr('src').replace('down', 'right'));
            $('#arrow_'+i).attr('alt', 'Search_right_arrow');
        }
    }
    $('#room_details_'+id).slideToggle();
    if ($('#arrow_'+id).attr('alt') == 'Search_right_arrow') {
        $('#arrow_'+id).attr('src', $('#arrow_'+id).attr('src').replace('right', 'down'));
        $('#arrow_'+id).attr('alt', 'Search_down_arrow');
    } else {
        $('#arrow_'+id).attr('src', $('#arrow_'+id).attr('src').replace('down', 'right'));
        $('#arrow_'+id).attr('alt', 'Search_right_arrow');
    }
}
function displayStar(image_id, max) {
    for (var i=1; i<=max; i++) {
        if (i <= image_id) {
            $('#star_'+ i).attr('src', $('#star_'+ i).attr('src').replace('off', 'on'));
            $('#star_'+ i).attr('alt', 'Search_big_star_on');
        } else {
            $('#star_'+ i).attr('src', $('#star_'+ i).attr('src').replace('on', 'off'));
            $('#star_'+ i).attr('alt', 'Search_big_star_off');
        }
    }
    $('#max_stars').val(image_id);
}
function submitFilters() {
    setproperUrl2();
    $('#frmFilters').submit();
}
function showImage(image, room_id) {
    $('#bigImage_'+room_id).attr('src', image);
}
function saveFavoritesList(id) {
    if(!$.cookie('favoritesList')) {
        $.cookie('favoritesList', id, {
            expires: 7,
            path: '/'
        } );
        return;
    }
    var space_data = $.cookie('favoritesList').split("^");
    var isAlreadySaved = false;
    for(var i=0; i < space_data.length; i++) {
        if(space_data[i] == id){
            isAlreadySaved = true;
            break;
        }
    }
    if(!isAlreadySaved) $.cookie('favoritesList' , $.cookie('favoritesList') + "^" + id, {
        expires: 7,
        path: '/'
    } );
    if( $.cookie('favoritesList') ) {
        var space_data = $.cookie('favoritesList').split("^");
        $('#favorites_count').text(space_data.length);
    }
}
function getFavoritesList() {
    var ret = new Array();
    if( $.cookie('favoritesList') ) {
        $('#favorites').val($.cookie('favoritesList'));
    }
}
function countFavorites() {
    if( $.cookie('favoritesList') ) {
        var space_data = $.cookie('favoritesList').split("^");
        $('#favorites_count').text(space_data.length);
    } else {
        $('#favorites_count').text(0);
    }
}
$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input',this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea' || (type == 'hidden' && this.name != 'query')) {
            this.value = '';
        }
        else if (type == 'checkbox' || type == 'radio') {
            this.checked = false;
        }
        else if (tag == 'select') {
            this.selectedIndex = -1;
        }
    });
};
function togglePlans(level, level_id, max_level_elements) {
    if ($('#arrow_'+level+level_id).attr('alt') == 'Search_right_arrow') {
        $('#arrow_'+level+level_id).attr('src', $('#arrow_'+level+level_id).attr('src').replace('right', 'down'));
        $('#arrow_'+level+level_id).attr('alt', 'Search_down_arrow');
    } else {
        $('#arrow_'+level+level_id).attr('src', $('#arrow_'+level+level_id).attr('src').replace('down', 'right'));
        $('#arrow_'+level+level_id).attr('alt', 'Search_right_arrow');
    }
    for (var i = 1; i <= max_level_elements; i++) {
        if (i != level_id) {
            $('#'+level+i).slideUp();
            $('#arrow_'+level+i).attr('src', $('#arrow_'+level+i).attr('src').replace('down', 'right'));
            $('#arrow_'+level+i).attr('alt', 'Search_right_arrow');
        }
    }
    $('#'+level+level_id).slideToggle();
}
var parentULSlen='';
 
function changeCurrency(newCurrency,liObj) {
    parentULSlen=$(liObj).parents("ul").length;
    $('#currency').val(newCurrency);
    $('#min_price').val('');
    $('#max_price').val('');
    submitFilters();
}
function changeMeasureUnits(newUnits,liObj) {
    parentULSlen=$(liObj).parents("ul").length;
    //parentULS=$(liObj).parent('ul').parent('li').parent('ul').attr('class');
    $('#units').val(newUnits);
    submitFilters();
}
function changeUnits() {
    $('#details_form').submit();
}

var parentULSlen1='';
function setType(id, layout_name,layoutobj,id_type) {
    //	parentULSlen1=$(liObj).parents("ul").length;
    parentULSlen1 = $(layoutobj).parent('div').attr('id');
    $('#type_layout').val(id);
    $('#layout_class').val(layout_name);
    $('#type_id').val(id_type);
    submitFilters();
}
function updateCapacity(max_capacity) {
    if ($('#max_peoples').val() > max_capacity) {
        $('#max_peoples').val(max_capacity);
    }
    submitFilters();
}
function newToggleAcc(id){
    if(parentULSlen1 != 'main_div'){
        if($(id).children('ul').css('display')=='none')
            $(id).children('ul').show();
        else
            $(id).children('ul').hide();
    }
}
function onlyImageToggle(image_id, max, text_id) {
/* if ($(image_id).attr('alt') == 'Search_right_arrow') {
    $(image_id).attr('src', $(image_id).attr('src').replace('right', 'down'));
    $(image_id).attr('alt', 'Search_down_arrow');
  } else {
    $(image_id).attr('src', $(image_id).attr('src').replace('down', 'right'));
    $(image_id).attr('alt', 'Search_right_arrow');
  }*/
}
function onlyImageTogglenew(image_id, max, text_id) {
    if ($(image_id).attr('alt') == 'Filtericon_rightarrow') {
        $(image_id).attr('src', $(image_id).attr('src').replace('filtericon_rightarrow', 'filtericon_downarrow'));
        $(image_id).attr('alt', 'Filtericon_downarrow');
    } else {
        $(image_id).attr('src', $(image_id).attr('src').replace('filtericon_downarrow', 'filtericon_rightarrow'));
        $(image_id).attr('alt', 'Filtericon_rightarrow');
    }
}
function toggleForMeasure(id){
    if(parentULSlen<=3){
        //alert($(id).parent('ul').attr('class'));
        //alert(parentULS);
        if($(id).children('ul').css('display')=='none')
        {
            $(id).children('ul').show();
            $(id).children('ul').css({
                "height":"auto"
            });
        }
        else
            $(id).children('ul').hide();
    }
}

/*
* This function changes the style for 'Description' field(Spaces)
* @author:shripadj@siddhatech.com
*/
function change_spanstyle(){
    $('#description').find('span').css({
        "font-family":"inherit",
        "color":"#414141"
    });
}
function changecolor(obj,lblobj){
    $('#'+obj).css('background-color','#FFFFFF')
    .css('border','1px solid #414141');
    if($('#'+lblobj).hasClass('errorlbl'))
    {
        $('#'+lblobj).toggleClass('errorlbl');
    }
	 
    if($('#'+lblobj).hasClass('error_label'))
    {
        $('#'+lblobj).toggleClass('error_label');
    }
}
//Unit conversions
    function transformUnits(){
    var frmUnits=$("#frmUnits").val();
    var toUnits=$("#measuringUnits").val();
    var multiplyer=3.28;
    if(frmUnits=='M' && toUnits=='F'){
         multiplyer=3.28;
    }
    else if(frmUnits=='F' && toUnits=='M'){
         multiplyer=1/3.28;
    }
    $("[name='spaceArea']").each(function(){    
        result=($(this).val()*multiplyer).toFixed(0) ;

        $(this).val(result)
        var sId=$(this).attr('id');
        $("#capacity_"+sId).text(result);
        
    })
    $("#frmUnits").val($("#measuringUnits").val());
    }
