﻿
var currentPageAllPropertyCount = 23379;
var currentPageIndex = 0;
var currentPageSize = 50;
var currentPageSorting = 'SortByPropertyRank';
var currentPageFreshness = 'Freshness_AllTime';
var currentPageSortBy = 11;
var currentPageSortOrder = 1;
var currentPageCurrency = 'GBP';
var currentPageDaysWithin = 0;
var currentPageMinPrice = 0;
var currentPageMaxPrice = 0;
var currentPageMinBeds = 0;
var currentPageMaxBeds = 0;
var currentPropertyRef = '';
var currentPropertyName = '';
var currentLocationID = 5663;
var currentCountryName = 'France';
var currentLocation = 'France';
var currentLocationPath = 'France';

Array.prototype.exists = function (o) {
    for (var i = 0; i < this.length; i++)
        if (this[i] === o)
            return true;
    return false;
}

var listingPage = {
    pageTitle: 'Title',
    openedPanels: new Array(), //new Array('Filters_SortBy','Filters_Freshness'),
    isOpenedPanel: function (listName) {
        return listingPage.openedPanels.exists(listName);
    },
    SaveFormVariablesSubmit: function () {
        listingPage.SaveFormVariables();
        $j('#aspnetForm')[0].submit();
        return false;

    },
    SaveFormVariables: function () {
        var cookieContent = "";
        $j('#searchBox_Form :input').each(function (index) {
            var jid = '#' + this.id;
            cookieContent += this.id + '=' + encodeURIComponent($j(jid).val()) + '&';
            //alert(this.id);
        });
        cookieContent += 'browsePageID=' + 2393;

        //alert(cookieContent);
        $j.cookie('tmc_browseForm2', cookieContent);
        //alert($j.cookie('tmc_browseForm2'))
        return false;
    }
};

function SearchSortByClick(sort) {
    $j("#searchBox_action").val(sort);
    listingPage.SaveFormVariables();
    $j('#aspnetForm')[0].submit();
    return false;
}
function SearchCurrencyClick(currency) {
    $j('#searchBox_action').val(currency + '|AsCurrency');
    listingPage.SaveFormVariablesSubmit(); 
    return false;
}

function SearchPriceClick(tier, min, max) {
    $j('#aspnetForm')[0].action = '';
    $j("#searchBox_MinPrice").val(0);
    $j("#searchBox_MaxPrice").val(0);
    $j("#searchBox_minPriceIndex").val(min);
    $j("#searchBox_maxPriceIndex").val(max);
    listingPage.SaveFormVariablesSubmit();
    return false;
}

function CurrentFiltersMinPriceClick() {
    $j("#searchBox_MinPrice").val(0);
    $j("#searchBox_minPriceIndex").val(0);
    $j("#searchBox_maxPriceIndex").val(0);    
    listingPage.SaveFormVariables();
    $j('#aspnetForm')[0].action = '';
    $j('#aspnetForm')[0].submit();
    return false;
}
function CurrentFiltersMaxPriceClick() {
    $j("#searchBox_MaxPrice").val(0);
    $j("#searchBox_minPriceIndex").val(0);
    $j("#searchBox_maxPriceIndex").val(0);    
    listingPage.SaveFormVariables();
    $j('#aspnetForm')[0].action = '';
    $j('#aspnetForm')[0].submit();
    return false;
}
function CurrentFiltersBedsClick() {
    $j("#searchBox_BedsFilter").val(-1);
    $j('#aspnetForm')[0].action = '';
    $j('#aspnetForm')[0].submit();
    return false;
}

function CurrentFiltersLocationClick(lnk) {
    $j("#searchBox_action").val('');
    $j("searchBox_changeValue").val('{name}|{id}');
    $j('#aspnetForm')[0].action = lnk;
    listingPage.SaveFormVariables();
    $j('#aspnetForm')[0].submit(); 
    return false;
}

function TopSearchedTypeFilterClick(lnk) {
    $j("#searchBox_action").val('');
    $j('#aspnetForm')[0].action = lnk;
    listingPage.SaveFormVariables();
    $j('#aspnetForm')[0].submit();
    return false;
}
function DetailsButtonClick(lnk) {
    
    $j('#aspnetForm')[0].action = lnk;        
    $j('#aspnetForm')[0].submit();
    return false;
}

function TypeFilterClick(lnk)
{
	$j("#searchBox_action").val('');    
    $j('#aspnetForm')[0].action = lnk;
    listingPage.SaveFormVariables();
    $j('#aspnetForm')[0].submit();
    return false;
}

function TypeFilterClick(lnk, typeID) {

    //alert(dummyHtml);

    if (typeID != 59) {        
        if ($j("li[parentID=" + typeID + "]").length > 0 ) {//check if this type have any subtypes

            if ($j("li[parentID=" + typeID + "]").is(":visible")) //if subtypes are expended then unexpend subtypes
            {
                $j("li[parentID=" + typeID + "]").slideUp(); //unexpend                
            }
            else //if subtypes are unexpended make them expended
                {
                    $j("li[parentID=" + typeID + "]").slideDown(); //expend suntypes
                    $j("li[parentID!=" + typeID + "][rel='TypesFilter'][parentID!=-1][parentID!=59]").slideUp(); //make all other expended sub types unexpended                                       
                }
                return true;     
               
        } 
    }
    
    //go to the selected type page
    $j("#searchBox_action").val('');    
    $j('#aspnetForm')[0].action = lnk;
    listingPage.SaveFormVariables();
    $j('#aspnetForm')[0].submit();
    return false;
}

function CurrentFiltersTypeClick(lnk) {
    var CategoryID = $j("#searchBox_categoryID").val();
    var SubCategoryIDs = $j("searchBox_subCategoryIDs").val();
    CategoryID = '';
    $j("#searchBox_categoryID").val(CategoryID);
    SubCategoryIDs = '';
    $j("#searchBox_subCategoryIDs").val('');
    $j("#searchBox_featureID").val('');
    $j("#searchBox_featureIDs").val('');
    $j('#aspnetForm')[0].action = lnk;
    listingPage.SaveFormVariables();
    $j('#aspnetForm')[0].submit(); 
    return false;
}




$j(document).ready(function () {
    //alert($j("#searchBox_categoryID").val());
    $j('#__VIEWSTATE').val("");
    //$j("li[rel='TypesFilter'][parentID!=-1][parentID!=59][parentID!=" + $j("#searchBox_categoryID").val() + "]").hide(); //hid all sub types excluding residential subtypes
    $j('.FiltersHeader').click(function () {
        // now get list id that's related to this group header
        var listID = this.id.replace('_Group', '');
        if (listID == 'Filters_Price' || listID == 'Filters_Currency' || listID == 'Filters_SortBy' || listID == 'Filters_Freshness')
        //for testing locally it should be: listID = "ContentPlaceHolder1_" + listID;
            listID = "ContentPlaceHolder1_" + listID;
        

        var imgID = '#' + this.id + ' img';

        if ($j('#' + listID).is(':visible')) {
            $j('#' + listID).slideUp();
            $j(imgID).attr('src', 'http://www.themovechannel.com/images/art/bulletDown.gif');
        }
        else {
            $j('#' + listID).slideDown();
            $j(imgID).attr('src', 'http://www.themovechannel.com/images/art/bulletLeftRed.gif');
        }
    });
});
