﻿//onerror = handleError;
var Campaigns = new Array();
var FeaturedProperties = new Array();
var SurpressRecordClickCount = false;
var DisplayPageType = 0;
var BROWSER_IE = 0;
var BROWSER_FIREFOX = 1;
var BROWSER_CHROME = 2;
var BROWSER_OPERA = 3;
var BROWSER_SAFARI = 4;
var BROWSER_IE8 = 5;
var BrowserAgent = determineBrowserAgent();

/*
*   ADVERT CLASS AND RELATED FUNCTIONS
*/

function Advert(image, linkURL, timing, imageID)
{
    this.image = image;
    this.linkURL = linkURL;
    this.timing = timing;
    this.imageID = imageID;
}

/*
*   ADCAMPAIGN CLASS AND RELATED FUNCTIONS
*/

function AdCampaign(id)
{
    this.adverts = new Array();
    this.nextAdvert = -1;
    this.displayControl = null;
    this.campaignID = id;
}

function AddCampaign(campaign)
{
    Campaigns.push(campaign);
}

function AddAdvert(advert)
{
    with (this)
    {
        adverts.push(advert);
    }
}

function AdvertCount()
{
    with (this)
    {
        return adverts.length;
    }
}

function SetNextAdvert()
{
    with (this)
    {
        if (nextAdvert >= AdvertCount() - 1)
        {
            nextAdvert = 0;
        }
        else
        {
            nextAdvert++;
        }
    }
}

function GetNextAdvert()
{
    with (this)
    {
        SetNextAdvert();
        return adverts[nextAdvert];
    }
}

function GetCurrentAdvert()
{
    with (this)
    {
        return adverts[nextAdvert];
    }
}

function SetDisplayControl(control)
{
    with (this)
    {
        var ctrl = document.getElementById(control);
        var found = false;

        if (ctrl == null)
        {
            ctrl = SearchForControl(control);

            if (ctrl == null)
            {
                //setTimeout("this.SetDisplayControl(" + control + ");", 500);
                setTimeout(function() { this.SetDisplayControl(control) }, 500);
            }
            else
            {
                found = true;
            }
        }
        else
        {
            found = true;
        }

        if (found)
        {
            try
            {
                //try to bind the events for IE browsers
                ctrl.setAttribute("campaign", this);
                ctrl.setAttribute("campaignid", this.campaignID);
                ctrl.attachEvent("onclick", handleClick);
                ctrl.attachEvent("onmouseover", handleMouseOver);
                ctrl.attachEvent("onmouseout", handleMouseOut);
                if (BrowserAgent == BROWSER_OPERA)
                {
                    ctrl.addEventListener('onmouseover', handleMouseOver, false);
                }
            }
            catch (e)
            {
                //try to bind the events for other browsers, such as FireFox, Chrome, Safari
                ctrl.setAttribute("campaignid", this.campaignID);
                ctrl.onclick = handleClick;
                ctrl.onmouseover = handleMouseOver;
                ctrl.onmouseout = handleMouseOut;
            }

            this.displayControl = ctrl;
        }
    }
}

function SearchForControl(control)
{
    var allTags = document.getElementsByTagName("*");
    var tmpCtrl = null;

    for (i = 0; i < allTags.length; i++)
    {
        if (allTags[i].id.indexOf(control) != -1)
        {
            tmpCtrl = document.getElementById(allTags[i].id);
            break;
        }
    }

    return tmpCtrl;
}

function GetDisplayControl()
{
    with (this)
    {
        return displayControl;
    }
}

function DisplayAdvert()
{
    with (this)
    {
        var ctrl = this.GetDisplayControl();
        var curAd = this.GetNextAdvert();
        //ctrl.value = curAd.linkURL;

        //for visual advert display using img control
        ctrl.src = curAd.image;
        setTimeout(function() { DisplayAdvert() }, curAd.timing * 1000);
    }
}

function RunCampaign()
{
    with (this)
    {
        if (this.GetDisplayControl() == null)
        {
            setTimeout(function() { RunCampaign() }, 500);
        }
        else
        {
            DisplayAdvert();
        }
    }
}

AdCampaign.prototype.AddAdvert = AddAdvert;
AdCampaign.prototype.AdvertCount = AdvertCount;
AdCampaign.prototype.SetNextAdvert = SetNextAdvert;
AdCampaign.prototype.GetNextAdvert = GetNextAdvert;
AdCampaign.prototype.SetDisplayControl = SetDisplayControl;
AdCampaign.prototype.GetDisplayControl = GetDisplayControl;
AdCampaign.prototype.DisplayAdvert = DisplayAdvert;
AdCampaign.prototype.RunCampaign = RunCampaign;
AdCampaign.prototype.handleClick = handleClick;
AdCampaign.prototype.GetCurrentAdvert = GetCurrentAdvert;


/*
*   EVENT HANDLERS
*/
function handleClick(evt)
{
    //needs to redirect to a page where it will record the click on the advert and then
    //redirect to the link found from the linkURL property.
    //var openAdvert = window.open(event.srcElement.campaign.GetCurrentAdvert().linkURL, "advert");
    var currentAdvert = null;
    var currentCampaign = null;
    try
    {
        //try to get the details from the event for IE
        currentAdvert = event.srcElement.campaign.GetCurrentAdvert();
        currentCampaign = event.srcElement.campaign;
    }
    catch (e)
    {
        //try to get the details from the event for other browsers ie. Chrome, Safari, Firefox
        currentCampaign = getAdCampaignByID(evt.target.getAttribute("campaignid"));
        currentAdvert = currentCampaign.GetCurrentAdvert();
    }

    //replace all '&' in the linkURL with hex code '%26' so that reading of the url later on will not have url truncated if querystring
    //parameters are being passed in the linkURL - the /g ensures that all '&' in the string are replaced not just the 1st occurance
    var openAdvert = window.open("AdvertTransfer.aspx?adurl=" + currentAdvert.linkURL.replace(/&/g, '%26') + "&imageid=" + currentAdvert.imageID + "&surpressRecordClickCount=" + SurpressRecordClickCount + "&campaignid=" + currentCampaign.campaignID + "&displaypagetype=" + DisplayPageType, "AdvertTransfer");
}

function handleMouseOver(evt)
{
    try
    {
        //try to get details from the event for IE browsers
        event.srcElement.style.cursor = 'hand';
        event.srcElement.style.cursor = 'pointer';
        window.status = event.srcElement.campaign.GetCurrentAdvert().linkURL;
        if (BrowserAgent == BROWSER_OPERA)
        {
            evt.target.style.cursor = 'pointer';
        }
    }
    catch (e)
    {
        try
        {
            //try to get details from the event for other browsers ie. Chrome, Firefox, Safari, Opera
            evt.target.style.cursor = 'pointer';
            window.status = getAdCampaignByID(evt.target.getAttribute("campaignid")).GetCurrentAdvert().linkURL;
        }
        catch (ee)
        {
        }
    }
}

function handleMouseOut(evt)
{
    try
    {
        //try to get details from the event for IE browsers
        event.srcElement.style.cursor = 'auto';
        window.status = "";
    }
    catch (e)
    {
        //try to get details from the event for other browsers ie. Chrome, Firefox, Safari, Opera
        evt.target.style.cursor = 'auto';
        window.status = "";
    }
}

function handleClick2(evt)
{
    //needs to redirect to a page where it will record the click on the advert and then
    //redirect to the link found from the linkURL property.
    var currentFP = null;
    try
    {
        currentFP = getFeaturedPropertyByID(evt.target.getAttribute("FPObjectID"));
    }
    catch (e)
    {
        currentFP = event.srcElement.FPObject;
    }

    window.location = "InternetPropertyTransfer.aspx?PropertyID=" + currentFP.PropertyID + "&RecordAsFeaturedPropertyInFP=yes", "FeaturedPropertyTransfer";
}

function handleMouseOver2(evt)
{
    try
    {
        event.srcElement.style.cursor = 'hand';
        event.srcElement.style.cursor = 'pointer';
    }
    catch (e)
    {
        evt.target.style.cursor = 'pointer';
    }
}

function handleMouseOut2(evt)
{
    try
    {
        event.srcElement.style.cursor = 'auto';
    }
    catch (e)
    {
        evt.target.style.cursor = 'auto';
    }
}

function handleError(evt)
{
    return true;
}

/*
* HELPER FUNCTIONS
*/
function determineBrowserAgent()
{
    var brwsrAgent = navigator.userAgent.toLowerCase();
    if (brwsrAgent.indexOf("opera") != -1)
    {
        return BROWSER_OPERA;
    }
    if (brwsrAgent.indexOf("firefox") != -1)
    {
        return BROWSER_FIREFOX;
    }
    if (brwsrAgent.indexOf("chrome") != -1)
    {
        return BROWSER_CHROME;
    }
    if (brwsrAgent.indexOf("safari") != -1)
    {
        return BROWSER_SAFARI;
    }
    if (brwsrAgent.indexOf("msie 8") != -1)
    {
        return BROWSER_IE8;
    }
    if (brwsrAgent.indexOf("msie") != -1)
    {
        return BROWSER_IE;
    }
    //return IE as default if for some reason none of the above were true
    return BROWSER_IE;
}

function getAdCampaignByID(ID)
{
    for (var i = 0; i < Campaigns.length; i++)
    {
        if (Campaigns[i].campaignID == ID)
        {
            return Campaigns[i];
        }
    }
}

function getFeaturedPropertyByID(ID)
{
    for (var i = 0; i < FeaturedProperties.length; i++)
    {
        if (FeaturedProperties[i].PropertyID == ID)
        {
            return FeaturedProperties[i];
        }
    }
}

/*
*   FEATURE PROPERTY METHODS AND CLASSES
*/

function FeatureProperty(propertyid, image, price, imageWidth, imageHeight)
{
    this.PropertyID = propertyid;
    this.Image = image;
    this.Price = price;
    this.ImageWidth = imageWidth;
    this.ImageHeight = imageHeight;
    this.ImageDisplayControl = null;
    this.PriceDisplayControl = null;
    this.LinkDisplayControl = null;
}

function AddFeaturedProperty(fp)
{
    FeaturedProperties.push(fp);
}

function DisplayFeatureProperty()
{
    with (this)
    {
        //set the img src to be the image
        var imgCtrl = GetImageDisplayControl();
        imgCtrl.src = Image;
        imgCtrl.width = ImageWidth; //GetImageWidth;
        imgCtrl.height = ImageHeight;

        try
            {
            //try to bind events for IE browsers
            imgCtrl.setAttribute("FPObject", this);
            imgCtrl.setAttribute("FPObjectID", this.PropertyID);
            imgCtrl.attachEvent("onclick", handleClick2);
            imgCtrl.attachEvent("onmouseover", handleMouseOver2);
            imgCtrl.attachEvent("onmouseout", handleMouseOut2);
            if (BrowserAgent == BROWSER_OPERA)
            {
                imgCtrl.addEventListener('onclick', handleClick2, false);
            }
        }
        catch (e)
        {
            //try to bind events for other browsers such as FireFox, Chrome, Safari
            imgCtrl.setAttribute("FPObjectID", this.PropertyID);
            imgCtrl.onclick = handleClick2;
            imgCtrl.onmouseover = handleMouseOver2;
            imgCtrl.onmouseout = handleMouseOut2;
        }

        //set the price to be the price
        var priceCtrl = GetPriceDisplayControl();
        priceCtrl.innerHTML = "£" + Price;

        //set the link to be the correct link
        var lnkCtrl = GetLinkDisplayControl();
        lnkCtrl.innerHTML = "<a href='InternetPropertyTransfer.aspx?PropertyID=" + PropertyID + "&RecordAsFeaturedPropertyInFP=yes'>more ></a>";
    }
}

function GetPropertyID()
{
    with (this)
    {
        return PropertyID;
    }
}

function GetPrice()
{
    with (this)
    {
        return Price;
    }
}

function GetImageWidth()
{
    with (this)
    {
        return ImageWidth;
    }
}

function GetImageHeight()
{
    with (this)
    {
        return ImageHeight;
    }
}

function SetImageDisplayControl(control)
{
    with (this)
    {
        ImageDisplayControl = SetGenericDisplayControl(control);
    }
}

function GetImageDisplayControl()
{
    with (this)
    {
        return ImageDisplayControl;
    }
}

function SetPriceDisplayControl(control)
{
    with (this)
    {
        PriceDisplayControl = SetGenericDisplayControl(control);
    }
}

function GetPriceDisplayControl()
{
    with (this)
    {
        return PriceDisplayControl;
    }
}

function SetLinkDisplayControl(control)
{
    with (this)
    {
        LinkDisplayControl = SetGenericDisplayControl(control);
    }
}

function GetLinkDisplayControl()
{
    with (this)
    {
        return LinkDisplayControl;
    }
}

function SetGenericDisplayControl(control)
{
    var ctrl = document.getElementById(control);
    var found = false;

    if (ctrl == null)
    {
        ctrl = SearchForControl(control);

        if (ctrl == null)
        {
            setTimeout(function() { SetGenericDisplayControl(control) }, 500);
        }
    }
    return ctrl;
}

FeatureProperty.prototype.SetImageDisplayControl = SetImageDisplayControl;
FeatureProperty.prototype.GetImageDisplayControl = GetImageDisplayControl;
FeatureProperty.prototype.SetPriceDisplayControl = SetPriceDisplayControl;
FeatureProperty.prototype.GetPriceDisplayControl = GetPriceDisplayControl;
FeatureProperty.prototype.SetLinkDisplayControl = SetLinkDisplayControl;
FeatureProperty.prototype.GetLinkDisplayControl = GetLinkDisplayControl;
FeatureProperty.prototype.DisplayFeatureProperty = DisplayFeatureProperty;
FeatureProperty.prototype.GetPrice = GetPrice;
FeatureProperty.prototype.GetImageWidth = GetImageWidth;
FeatureProperty.prototype.GetImageHeight = GetImageHeight;
FeatureProperty.prototype.GetPropertyID = GetPropertyID;
FeatureProperty.prototype.handleClick2 = handleClick2;


function _resetTabs(tabItem)
{
    try
    {
        if (tabItem.getAttribute("className") != "disabledTab" || tabItem.getAttribute("class") != "disabledTab")
        {
            tabItem.setAttribute("class", "tab");
            tabItem.setAttribute("className", "tab");
            tabItem.style.height = "25px";
        }
    }
    catch (e)
    {
    }
}

/*
* Javascript functions for performing Asynchronous callbacks to enable the tabs to be
* changed asynchronously
*/
function DisplaySelectedTab(selectedTab)
{
    //get the list of campaign ids to pass to the server
    //so that we can record the advert shows if that tab is selected
    var campaignIdString = "";
    for (i = 0; i < Campaigns.length; i++)
    {
        campaignIdString += Campaigns[i].campaignID + "|";
    }

    CallServer(campaignIdString + selectedTab, "");
}

function ReceiveServerData(data)
{
    //display the correct div here
    HideDiv('divPictures');
    HideDiv('divDetails');
    HideDiv('divLocalInfo');
    HideDiv('divNextSteps');
    DisplayDiv(data);
    //document.getElementById(data).style.display = 'block';

    //HighlightSelectedTab('cellPictures');

    var tabHeight;
    var tabItem;
    
    //local and test server variables    
    //var imgPathPrefix = "http://localhost/web/";
    //live server variables    
    //var imgPathPrefix = "http://www.farrellheyworth.co.uk/";

    //change the tab style to show it is selected, alter all the other tabs to show they are not selected
    _resetTabs(document.getElementById('<%= lblPictures.ClientID %>'));
    _resetTabs(document.getElementById('<%= lblDetails.ClientID %>'));
    _resetTabs(document.getElementById('<%= lblMapTab.ClientID %>'));
    _resetTabs(document.getElementById('<%= lblLocalInfo.ClientID %>'));
    _resetTabs(document.getElementById('<%= lblNextSteps.ClientID %>'));

    shadowItem = document.getElementById('<%= imgTabsShadow.ClientID %>');

    switch (data)
    {
        case 'divPictures':
            tabItem = document.getElementById('<%= lblPictures.ClientID %>');
            //shadowItem.src = imgPathPrefix + "resources/images/system/PhotoTabShadow.png";
            //shadowItem.style.height = 236;
            //shadowItem.style.top = -233;
            showTabShadow('divShadowPanelPhoto');
            break;
        case 'divDetails':
            tabItem = document.getElementById('<%= lblDetails.ClientID %>');
            //display larger shadow image
            //shadowItem.src = imgPathPrefix + "resources/images/system/DetailsTabShadow.png";
            //shadowItem.style.height = 431;
            //shadowItem.style.top = -428;
            showTabShadow('divShadowPanelDetails');
            break;
        case 'divLocalInfo':
            tabItem = document.getElementById('<%= lblLocalInfo.ClientID %>');
            //shadowItem.src = imgPathPrefix + "resources/images/system/NextStepsTabShadow.png";
            //shadowItem.style.height = 271;
            //shadowItem.style.top = -268;
            showTabShadow('divShadowPanelNextSteps');
            break;
        case 'divNextSteps':
            tabItem = document.getElementById('<%= lblNextSteps.ClientID %>');
            //shadowItem.src = imgPathPrefix + "resources/images/system/NextStepsTabShadow.png";
            //shadowItem.style.height = 271;
            //shadowItem.style.top = -268;
            showTabShadow('divShadowPanelNextSteps');
            break;
        default:
            tabItem = document.getElementById('<%= lblPictures.ClientID %>');
            break;
    }

    tabItem.setAttribute("class", "selectedTab");
    tabItem.setAttribute("className", "selectedTab");
    tabHeight = tabItem.style.height.split("px")[0];
    tabHeight++;
    tabItem.style.height = tabHeight + "px";
}

function showTabShadow(shadowToShow)
{
    document.getElementById('divShadowPanelPhoto').style.display = 'none';
    document.getElementById('divShadowPanelDetails').style.display = 'none';
    document.getElementById('divShadowPanelNextSteps').style.display = 'none';
    document.getElementById(shadowToShow).style.display = 'block';
}

function ShowDiv(divID, bShow)
{
    try
    {
        var div = document.getElementById(divID);
        div.style.display = bShow ? 'block' : 'none';
    }
    catch (e)
    {
        alert("ERROR: function:HideDiv(" + divID + " = " + e);
    }
}

function DisplayDiv(div)
{
    ShowDiv(div, true);
}
function HideDiv(div)
{
    ShowDiv(div, false);
}

//function HighlightSelectedTab(tabID)
//{
//    var selectedTab = document.getElementById(tabID);
//    selectedTab.style.backgroundColor = 'red';
//}

function ChangeElementFontStyle(element, size, weight)
{
    try
    {
        var tmpElement = document.getElementById(element);
        tmpElement.style.fontSize = size;
        tmpElement.style.fontWeight = weight;
    }
    catch (e)
    {
    }
}

function ChangeImage(element, imageURL)
{
    try
    {
        $get(element).src = imageURL;
    }
    catch (e)
    {
    }
}

function ChangeLocalInfoTabDisplay(display)
{
    var LocalServicesLabel = '<%=lblLocalServices.ClientID %>';
    var BranchLabel = '<%=lblBranch.ClientID %>';
    var LocalServicesImage = '<%=imgLocal.ClientID %>';
    var BranchImage = '<%=imgBranch.ClientID %>';    

    ShowDiv('divBranchDetails', (display == "1"));
    ShowDiv('divAdverts', (display != "1")); 

    if (display == "0")
    {
        //show local services        
        ChangeElementFontStyle(LocalServicesLabel, 14, 'bold');
        ChangeElementFontStyle(BranchLabel, 12, 'normal');
        ChangeImage(LocalServicesImage, 'resources/images/system/services-a.jpg');
        ChangeImage(BranchImage, 'resources/images/system/branch2-b.jpg');        
    }
    else if (display == "1")
    {
        //show branch        
        ChangeElementFontStyle(LocalServicesLabel, 12, 'normal');
        ChangeElementFontStyle(BranchLabel, 14, 'bold');
        ChangeImage(LocalServicesImage, 'resources/images/system/services-b.jpg');
        ChangeImage(BranchImage, 'resources/images/system/branch2-a.jpg');        
    }
}

function checkPicturesExist()
{
    var divPics = $get('divPictures');

    if (divPics == undefined)
    {
        DisplayDiv('divDetails');
        //show the larger shadow image
        //local and test server variables        
        //var imgPathPrefix = "http://localhost/web/";
        //live server variables        
        //var imgPathPrefix = "http://www.farrellheyworth.co.uk/";

        //var shadowItem = document.getElementById(namePrefix + 'imgTabsShadow');
        //shadowItem.src = imgPathPrefix + "resources/images/system/DetailsTabShadow.png";
        //shadowItem.style.height = 431;
        //shadowItem.style.top = -428;
        //remove the photo shadow
        document.getElementById('divShadowPanelPhoto').style.display = 'none';
        //disable the photo tab
        document.getElementById('<%=lblPictures.ClientID %>').disabled = true;
        document.getElementById('<%=lblPictures.ClientID %>').onclick = function() { return false; };
        document.getElementById('<%=lblPictures.ClientID %>').onmouseover = function() { return false; };
        document.getElementById('<%=lblPictures.ClientID %>').onmouseout = function() { return false; };
        document.getElementById('<%=lblPictures.ClientID %>').setAttribute("class", "disabledTab");
        document.getElementById('<%=lblPictures.ClientID %>').setAttribute("className", "disabledTab");
        //show the brochure details shadow
        document.getElementById('divShadowPanelDetails').style.display = 'block';
        //show the details tab as selected
        document.getElementById('<%=lblDetails.ClientID %>').setAttribute("class", "selectedTab");
        document.getElementById('<%=lblDetails.ClientID %>').setAttribute("className", "selectedTab");
    }
}

/*
* FUNCTIONS REFERENCED FROM PROPERTYSEARCHHEADER.ASCX
*/
//var posLeft = 2;
//var posTop = 2;

function showFilter()
{
    var myFilterDiv = document.getElementById('propertySearchFilterDiv');
    myFilterDiv.style.display = 'block';

    flipSearchHeaderButtons("left");
}

function hideFilter(e)
{
    resetSearchHeaderButtons();
    
    var myFilterDiv = document.getElementById('propertySearchFilterDiv');
    var elements = myFilterDiv.getElementsByTagName("select");

    for (var i = 0; i < elements.length; i++)
    {
        var activeObject = "";
        if (BrowserAgent == BROWSER_FIREFOX)
        {
            activeObject = e.target;
        }
        else
        {
            activeObject = event.srcElement.name;
        }
        if (activeObject == elements[i].name)
        {
            return;
        }
    }
    myFilterDiv.style.display = 'none';
}

function flipSearchHeaderButtons(position)
{
    var buttonImg;
    switch (position.toLowerCase())
    {
        case "left":
            buttonImg = "ButtonFlipLeft.png";
            break;
        case "centre":
            buttonImg = "ButtonFlipCentre.png";
            break;
        case "right":
            buttonImg = "ButtonFlipRight.png";
            break;
        default:
            buttonImg = "buttonNormal.png";
            break;
    }
    var divBackground = document.getElementById('divBackground');
    divBackground.style.backgroundImage = 'url(resources/images/system/' + buttonImg + ')';
}

function resetSearchHeaderButtons()
{
    var divBackground = document.getElementById('divBackground');
    divBackground.style.backgroundImage = 'url(resources/images/system/buttonNormal.png)';
}

function optionsFlip(section, propertyID)
{
    var arrImgTags = document.getElementsByTagName('div');
    //var arrPropertyImages = new Array();

    for (var i = 0; i < arrImgTags.length; i++)
    {
        //if the img tag id has the propertyidpart string containted within it then we are interested with this img
        if (arrImgTags[i].id.indexOf(propertyID) > -1)
        {
            var divOptions = document.getElementById(arrImgTags[i].id);
            //divOptions.style.textDecoration = "none";

            if (section == 'left')
            {
                //alert('hello' + propertyID);
                divOptions.style.backgroundImage = 'url(resources/images/system/optionsLeft.jpg)';
            }
            else if (section == 'center')
            {
                divOptions.style.backgroundImage = 'url(resources/images/system/optionsCenter.jpg)';
            }
            else if (section == 'right')
            {
                divOptions.style.backgroundImage = 'url(resources/images/system/optionsRight.jpg)';
            }
        }
    }
    
    //alert(divOptions + ' ' + propertyID);
}

function optionsUnFlip(section, propertyID)
{
    var arrImgTags = document.getElementsByTagName('div');
    //var arrPropertyImages = new Array();

    for (var i = 0; i < arrImgTags.length; i++)
    {
        //if the img tag id has the propertyidpart string containted within it then we are interested with this img
        if (arrImgTags[i].id.indexOf(propertyID) > -1)
        {
            var divOptions = document.getElementById(arrImgTags[i].id);
            //divOptions.style.textDecoration = "none";
            divOptions.style.backgroundImage = 'url(resources/images/system/optionsUnselected.jpg)';
        }
    }
}

function ppOptionsFlip(section, propertyID)
{
    var arrImgTags = document.getElementsByTagName('div');
    //var arrPropertyImages = new Array();

    for (var i = 0; i < arrImgTags.length; i++)
    {
        //if the img tag id has the propertyidpart string containted within it then we are interested with this img
        if (arrImgTags[i].id.indexOf(propertyID) > -1)
        {
            var divOptions = document.getElementById(arrImgTags[i].id);

            if (section == 'left')
            {
                //alert('hello' + propertyID);
                divOptions.style.backgroundImage = 'url(resources/images/system/PPOptionsLeft.jpg)';
            }
            else if (section == 'center')
            {
                divOptions.style.backgroundImage = 'url(resources/images/system/PPOptionsCenter.jpg)';
            }
            else if (section == 'right')
            {
                divOptions.style.backgroundImage = 'url(resources/images/system/PPOptionsRight.jpg)';
            }
        }
    }

    //alert(divOptions + ' ' + propertyID);
}

function ppOptionsUnFlip(section, propertyID)
{
    var arrImgTags = document.getElementsByTagName('div');
    //var arrPropertyImages = new Array();

    for (var i = 0; i < arrImgTags.length; i++)
    {
        //if the img tag id has the propertyidpart string containted within it then we are interested with this img
        if (arrImgTags[i].id.indexOf(propertyID) > -1)
        {
            var divOptions = document.getElementById(arrImgTags[i].id);
            divOptions.style.backgroundImage = 'url(resources/images/system/PPOptionsUnselected.jpg)';
        }
    }
}

/*
* FUNCTIONS REFERENCED FROM PROPERTYRESULTS.ASCX
*/

var minHeight = 95;
var maxHeight = 580;
var windowSizeMinThreashold = 150; // vary between 150 and 400 (400 gives larger whitespace)
var clearProcessingImg;
var resultsPanelSize;
var inProcessing = false;
var triggerSearchBeforeBottomOfScroll = 0;

function showProcessingImage()
{
    //determine if at bottom of scroll bar
    var atBottom = atBottomOfScroll(document.getElementById(resultsPanelName).scrollTop);
    var gotAllProperties = retrievedAllProperties();
    if (!inProcessing && atBottom && !gotAllProperties)
    {
        inProcessing = true;
        var processingImgPanel = document.getElementById(processingImage);
        processingImgPanel.style.display = 'block';
        checkClearProcessingImage();
    }
    //record the scroll position here so it can be re-set when the users return to the results section
    //record in the window.name property as this persits across several pages
    window.name = document.getElementById(resultsPanelName).scrollTop;
}

function retrievedAllProperties()
{
    var inputTags = document.getElementsByTagName('input');

    for (var i = 0; i < inputTags.length; i++)
    {
        if (inputTags[i].id.indexOf('hidEndOfProperties') > -1)
        {
            if (inputTags[i].value == "true")
            {
                return true;
            }
        }
    }
    
    return false;
}

function atBottomOfScroll(scrollPosition)
{
    //get the height of the scroll pane
    var panelHeight = parseInt(document.getElementById(resultsPanelName).style.height.substring(0, document.getElementById(resultsPanelName).style.height.indexOf("px")));
    var maxScrollHeight = document.getElementById(resultsPanelName).scrollHeight;

    //if the div height is the same as scroll height - return false
    if (panelHeight == maxScrollHeight)
    {
        return false;
    }

    if ((panelHeight + scrollPosition) >= (maxScrollHeight - triggerSearchBeforeBottomOfScroll))
    {   
        //remove the onscroll event listener to see if it will stop listening to the users mouse
        //action if they have left the mouse button down.
        return true;
    }
    return false;
}

function checkClearProcessingImage()
{
    clearProcessingImage();
    clearProcessingImg = setInterval("clearProcessingImage()", 1000);
}

function setResultsScrollPosition(position)
{
    var resultsDIV = document.getElementById(resultsPanelName)
    if (position > resultsDIV.scrollHeight)
    {
        window.name = 0;
        resultsDIV.scrollTop = 0;
    }
    else
    {
        try
        {
            resultsDIV.scrollTop = position;
            resultsDIV.pageYOffset = 300;
        }
        catch (e)
        {
            alert(e);
        }
    }
}

function clearProcessingImage()
{
    if (inProcessing)
    {
        var panelHeight = parseInt(document.getElementById(resultsPanelName).style.height.substring(0, document.getElementById(resultsPanelName).style.height.indexOf("px")));
        var maxScrollHeight = document.getElementById(resultsPanelName).scrollHeight;
        var currentScrollPosition = document.getElementById(resultsPanelName).scrollTop;

        if (maxScrollHeight > panelHeight + currentScrollPosition)
        {
            var processingImgPanel = document.getElementById(processingImage);
            processingImgPanel.style.display = 'none';
            clearInterval(clearProcessingImg);
            inProcessing = false;
        }
    }
}

function changeDIVheight()
{
    var d = document.getElementById(resultsPanelName);
    try
    {
        var winHeight = document.documentElement.clientHeight;
        if (winHeight > windowSizeMinThreashold)
        {
            newHeight = minHeight + (winHeight - windowSizeMinThreashold);
            if (newHeight > maxHeight)
            {
                newHeight = maxHeight;
            }
        }
    }
    catch (e)
    {
        newHeight = minHeight * 3;
    }

    //d.style.height = newHeight + "px";
}

function showThumbRollover(propertyIDpart, imageIDpart)
{
    var arrImgTags = document.getElementsByTagName('img');
    var arrPropertyImages = new Array();

    for (var i = 0; i < arrImgTags.length; i++)
    {
        //if the img tag id has the propertyidpart string containted within it then we are interested with this img
        if (arrImgTags[i].id.indexOf(propertyIDpart) > -1)
        {
            arrPropertyImages.push(arrImgTags[i]);
        }
    }

    var tags = "";
    for (var i = 0; i < arrPropertyImages.length; i++)
    {
        tags += arrPropertyImages[i].id + "\n";
    }

    tags += "Image ID Part: " + imageIDpart;
    
    //alert(tags);

    //go through each image in the arrPropertyImages array and determine which ones to hide and which one to display
    for (var i = 0; i < arrPropertyImages.length; i++)
    {
        if (arrPropertyImages[i].id.indexOf(imageIDpart) > -1)
        {
            document.getElementById(arrPropertyImages[i].id).style.display = 'block';
        }
        else
        {
            document.getElementById(arrPropertyImages[i].id).style.display = 'none';
        }
    }
}

function openBranchWebPage(webAddress)
{
    window.open(webAddress);
}

