// JScript File
// Ajax engine function
function getXMLHTTPRequest() {
    http_request = false;
    if (window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); }
    } else if (window.ActiveXObject) {
        try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } }
    }
    return http_request;
}

function displayContents(http_request, panel, getCityOrArea, cityId) {
    if (http_request.readyState == 4 && http_request.status == 200) {
        var xmldoc = http_request.responseXML;
        if (xmldoc.getElementsByTagName("root")[0]) {
            var str = '';
            var optlen = xmldoc.getElementsByTagName("options").length;
            var opt = '';
            var xmlopt = '';
            var objId = '';
            var checkedId = 0;

            // if there are options returned
            if (optlen > 0) {
                // If area is not passed then update both real estate city and holiday city
                if (!getCityOrArea) {
                    // for city label in real estate search
                    if (document.getElementById('leftSide_drpRECityHtml')) {
                        document.getElementById('leftSide_drpRECityHtml').options.length = 0;
                        document.getElementById('leftSide_drpRECityHtml').options.add(new Option('City:', 0));
                        for (i = 0; i < optlen; i++) {
                            xmlopt = xmldoc.getElementsByTagName("options")[i];
                            xmlopt_desc = xmlopt.childNodes[0].firstChild.nodeValue;
                            xmlopt_id = xmlopt.childNodes[1].firstChild.nodeValue;
                            document.getElementById('leftSide_drpRECityHtml').options.add(new Option(xmlopt_desc, xmlopt_id));
                        }
                    }
                    if (document.getElementById('ucRightSide_drpHolidayCouncilHtml')) {
                        // for city label in real estate search
                        document.getElementById('ucRightSide_drpHolidayCouncilHtml').options.length = 0;
                        document.getElementById('ucRightSide_drpHolidayCouncilHtml').options.add(new Option('City:', 0));
                        for (i = 0; i < optlen; i++) {
                            xmlopt = xmldoc.getElementsByTagName("options")[i];
                            xmlopt_desc = xmlopt.childNodes[0].firstChild.nodeValue;
                            xmlopt_id = xmlopt.childNodes[1].firstChild.nodeValue;
                            document.getElementById('ucRightSide_drpHolidayCouncilHtml').options.add(new Option(xmlopt_desc, xmlopt_id));
                        }
                    }
                } else {
                    objId = 'leftSide_drpREAreaHtml';
                    // if panel is for holiday search
                    if (panel) { objId = 'ucRightSide_drpHolidayAreaHtml'; }

                    if (document.getElementById(objId)) {
                        document.getElementById(objId).options.length = 0;
                        document.getElementById(objId).options.add(new Option('Area:', 0));
                        for (i = 0; i < optlen; i++) {
                            xmlopt = xmldoc.getElementsByTagName("options")[i];
                            xmlopt_desc = xmlopt.childNodes[0].firstChild.nodeValue;
                            xmlopt_id = xmlopt.childNodes[1].firstChild.nodeValue;
                            xmlopt_checked = xmlopt.childNodes[2].firstChild.nodeValue;
                            document.getElementById(objId).options.add(new Option(xmlopt_desc, xmlopt_id));
                            if (xmlopt_checked) { checkedId = document.getElementById(objId).options.length - 1; }
                        }
                        if (!checkedId) { document.getElementById(objId).options[checkedId].selected = true; }
                    }
                }
            }
        } else {
            alert("There was a problem with the request.");
        }
    }
}

function makeRequest(realHoliday, getCityOrArea, cityId) {
    getXMLHTTPRequest();
    if (http_request) {
        var strURL = 'getCouncils=1';
        if (getCityOrArea) {
            strURL = 'getAreas=1';
            if (cityId !== null) { strURL += '&cityId=' + cityId; }
        }
        http_request.open('GET', '/ajaxsearch/getCities.aspx?' + strURL + '&bustcache=' + new Date().getTime(), false);
        http_request.send(null);
        displayContents(http_request, realHoliday, getCityOrArea, cityId);
    }
}

function valSearch(panel) {
    if (panel === 0) {
        if (document.getElementById('leftSide_drpREPropertyType').options[document.getElementById('leftSide_drpREPropertyType').selectedIndex].value + document.getElementById('leftSide_drpRECityHtml').options[document.getElementById('leftSide_drpRECityHtml').selectedIndex].value + document.getElementById('leftSide_drpREAreaHtml').options[document.getElementById('leftSide_drpREAreaHtml').selectedIndex].value + document.getElementById('leftSide_drpREBedrooms').options[document.getElementById('leftSide_drpREBedrooms').selectedIndex].value + document.getElementById('leftSide_drpRELivingArea').options[document.getElementById('leftSide_drpRELivingArea').selectedIndex].value + document.getElementById('leftSide_drpREBudget').options[document.getElementById('leftSide_drpREBudget').selectedIndex].value === 0) {
            alert('You must select at least one search option.');
            return false;
        } else {
            return true;
        }
    } else {
        if (document.getElementById('ucRightSide_drpHolidayPropertyType').options[document.getElementById('ucRightSide_drpHolidayPropertyType').selectedIndex].value + document.getElementById('ucRightSide_drpHolidayCouncilHtml').options[document.getElementById('ucRightSide_drpHolidayCouncilHtml').selectedIndex].value + document.getElementById('ucRightSide_drpHolidayAreaHtml').options[document.getElementById('ucRightSide_drpHolidayAreaHtml').selectedIndex].value + document.getElementById('ucRightSide_drpHolidayNrPersons').options[document.getElementById('ucRightSide_drpHolidayNrPersons').selectedIndex].value + document.getElementById('ucRightSide_drpHolidayPrice').options[document.getElementById('ucRightSide_drpHolidayPrice').selectedIndex].value === 0) {
            alert('You must select at least one search option.');
            return false;
        } else {
            return true;
        }
    }
}

function gosearch(panel) {
    var queryString = '';
    var url = '/real-estate/';
    if (panel == 1) { url = '/holiday-rentals/'; }

    if (panel === 0) {
        if (document.getElementById('leftSide_drpREPropertyType').selectedIndex > 0) {
            if (queryString !== '') { queryString += '&'; }
            queryString += "propertyType=" + document.getElementById('leftSide_drpREPropertyType').options[document.getElementById('leftSide_drpREPropertyType').selectedIndex].value;
        }
        if (document.getElementById('leftSide_drpRECityHtml').selectedIndex > 0) {
            if (queryString !== '') { queryString += '&'; }
            queryString += "city=" + document.getElementById('leftSide_drpRECityHtml').options[document.getElementById('leftSide_drpRECityHtml').selectedIndex].value;
        }
        if (document.getElementById('leftSide_drpREAreaHtml').selectedIndex > 0) {
            if (queryString !== '') { queryString += '&'; }
            queryString += "area=" + document.getElementById('leftSide_drpREAreaHtml').options[document.getElementById('leftSide_drpREAreaHtml').selectedIndex].value;
        }
        if (document.getElementById('leftSide_drpREBedrooms').selectedIndex > 0) {
            if (queryString !== '') { queryString += '&'; }
            queryString += "bedRooms=" + document.getElementById('leftSide_drpREBedrooms').options[document.getElementById('leftSide_drpREBedrooms').selectedIndex].value;
        }
        if (document.getElementById('leftSide_drpRELivingArea').selectedIndex > 0) {
            if (queryString !== '') { queryString += '&'; }
            queryString += "livingArea=" + document.getElementById('leftSide_drpRELivingArea').options[document.getElementById('leftSide_drpRELivingArea').selectedIndex].value;
        }
        if (document.getElementById('leftSide_drpREBudget').selectedIndex > 0) {
            if (queryString !== '') { queryString += '&'; }
            queryString += "budget=" + document.getElementById('leftSide_drpREBudget').options[document.getElementById('leftSide_drpREBudget').selectedIndex].value;
        }
    } else {
        if (document.getElementById('ucRightSide_drpHolidayPropertyType').selectedIndex > 0) {
            if (queryString !== '') { queryString += '&'; }
            queryString += "holpropertyType=" + document.getElementById('ucRightSide_drpHolidayPropertyType').options[document.getElementById('ucRightSide_drpHolidayPropertyType').selectedIndex].value;
        }
        if (document.getElementById('ucRightSide_drpHolidayCouncilHtml').selectedIndex > 0) {
            if (queryString !== '') { queryString += '&'; }
            queryString += "holcouncil=" + document.getElementById('ucRightSide_drpHolidayCouncilHtml').options[document.getElementById('ucRightSide_drpHolidayCouncilHtml').selectedIndex].value;
        }
        if (document.getElementById('ucRightSide_drpHolidayAreaHtml').selectedIndex > 0) {
            if (queryString !== '') { queryString += '&'; }
            queryString += "holarea=" + document.getElementById('ucRightSide_drpHolidayAreaHtml').options[document.getElementById('ucRightSide_drpHolidayAreaHtml').selectedIndex].value;
        }
        if (document.getElementById('ucRightSide_drpHolidayNrPersons').selectedIndex > 0) {
            if (queryString !== '') { queryString += '&'; }
            queryString += "holbedRooms=" + document.getElementById('ucRightSide_drpHolidayNrPersons').options[document.getElementById('ucRightSide_drpHolidayNrPersons').selectedIndex].value;
        }
        if (document.getElementById('ucRightSide_drpHolidayPrice').selectedIndex > 0) {
            if (queryString !== '') { queryString += '&'; }
            queryString += "holprice=" + document.getElementById('ucRightSide_drpHolidayPrice').options[document.getElementById('ucRightSide_drpHolidayPrice').selectedIndex].value;
        } 
    }
    if (queryString !== '') { url += "?" + queryString; }
    window.location = url;
}

function valLeftSearchRef() {
    if (document.getElementById('leftSide_txtREReference').value === '') {
        alert('The property reference is a required field.');
        return false;
    } else {
        return true;
    }
}

function gosearchRef() {
    var url = '/real-estate/';
    var queryString = '';
    if (document.getElementById('leftSide_txtREReference').value !== '') {
        queryString += "txtREReference=" + document.getElementById('leftSide_txtREReference').value;
    }
    if (queryString !== '') { url += "?" + queryString; }
    window.location = url;
}

function valRightSearchRef() {
    if (document.getElementById('ucRightSide_txtHolidaySearchName').value === '') {
        alert('The property name is a required field.');
        return false;
    } else {
        return true;
    }
}

function gosearchName() {
    var url = '/holiday-rentals/';
    var queryString = '';
    if (document.getElementById('ucRightSide_txtHolidaySearchName').value !== '') {
        queryString += "txtHolidaySearchName=" + document.getElementById('ucRightSide_txtHolidaySearchName').value;
    }
    if (queryString !== '') { url += "?" + queryString; }
    window.location = url;
}

// Menu
var arrowimages = { down:['downarrowclass', '/images/down.gif', 23], right:['rightarrowclass', '/images/right.png'] }
var jqueryslidemenu = {
    animateduration: { over: 450, out: 450 }, //duration of slide in/ out animation, in milliseconds
	buildmenu: function(menuid, arrowsvar) {
		jQuery(document).ready(function($) {
			var $mainmenu = $("#" + menuid + ">ul")
			var $headers = $mainmenu.find("ul").parent()

			$headers.each(function(i) {
				var $curobj = $(this);
				var $subul = $(this).find('ul:eq(0)');
				this._dimensions = { w: this.offsetWidth, h: this.offsetHeight, subulw: $subul.outerWidth(), subulh: $subul.outerHeight() }
				this.istopheader = ($curobj.parents("ul").length == 1? true : false);
				$subul.css({ top: this.istopheader? this._dimensions.h + "px" : 0 });
				if(!this.istopheader) {
					$curobj.children("a:eq(0)").css({}).append(
						'<img src="'+ arrowsvar.right[1]
						+'" class="' + arrowsvar.right[0]
						+ '" style="border:0; width: 8px; height: 11px;" />')
				}
				
				$curobj.hover(
					function(e){
						var $targetul = $(this).children("ul:eq(0)");
						this._offsets = { left: $(this).offset().left, top: $(this).offset().top }
						var menuleft = (this.istopheader? 0 : this._dimensions.w);
						menuleft = (this._offsets.left+menuleft+this._dimensions.subulw>$(window).width())? (this.istopheader? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft
						if ($targetul.queue().length <= 1) //if 1 or less queued animations
							$targetul.css({ left: menuleft + "px", width: this._dimensions.subulw + 'px' }).slideDown(jqueryslidemenu.animateduration.over)
					},
					function(e){
						var $targetul = $(this).children("ul:eq(0)");
						$targetul.slideUp(jqueryslidemenu.animateduration.out);
					}
				) //end hover
				$curobj.click(function() { $(this).children("ul:eq(0)").hide() })
			}) //end $headers.each()
			$mainmenu.find("ul").css({display:'none', visibility:'visible'})
		}) //end document.ready
	}
}

function preview_picture(id) {
    if (typeof (id) == 'number') {
        url = '/PreviewPicture.aspx?picture=' + id;
        preview = window.open(url, 'pic_preview', 'width=100,height=100');
        preview.focus();
    } else {
        alert('An problem as ocourred with this page, please try again later.');
    }
}

function getDevelopments() {
    $.ajax({
        type: 'GET',
        url: '/ajaxsearch/getDevelopments.aspx',
        dataType: "xml",
        data: 'bustcache=' + new Date().getTime(),
        success: function(xml) { loadDevelopments(xml); },
        error: function(xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(thrownError); }
    });
}

function loadDevelopments(xml) {
    var output = '';
    var counter = 0;

    $(xml).find('developments').each(function() {
        var urlImage = $(this).find('urlImage').text();
        var alt = $(this).find('alt').text();
        var urlSource = $(this).find('urlSource').text();

        output += '<div id="dev_' + counter + '" style="display: none; width: 155px; height: 90px;">';
        output += '<a href="' + urlSource + '">';
        output += '<img src="' + urlImage + '" alt="' + alt + '" title="' + alt + '" class="propertypictures" width="150" height="86" style="margin-bottom: 10px;" />';
        output += '</a>';
        output += '</div>';

        counter++;
    });

    if (counter == 0) {
        $('#mainDevelopments').css('display', 'none');
    } else {
        $('#developments').html(output);
    }    

    dspDevelopments(0);
}

function dspDevelopments(index) {
    $('#dev_' + index).fadeIn('slow').delay(5000).fadeOut('slow', function() {
        var divsLen = $('#developments').find('div').length - 1;

        if (index < divsLen) {
            index = index + 1;
        } else {
            index = 0;
        }

        dspDevelopments(index);
    });
}

$(document).ready(function() {
    //build menu with ID="myslidemenu" on page:
    jqueryslidemenu.buildmenu("myslidemenu", arrowimages)

    getDevelopments();

    makeRequest(null, 0);
    makeRequest(0, 1, 0);
    makeRequest(1, 1, 0);
});
