﻿var $NPlayWidget = {};
var Control = {};

$NPlayWidget.isIE7 = (navigator.appVersion.match("MSIE 7.0") == "MSIE 7.0") || (navigator.userAgent.indexOf('Opera/9.80') != -1);
$NPlayWidget.useXDR = window.XDomainRequest ? true : false;
$NPlayWidget.dynObj = null;
$NPlayWidget.baseURL = 'http://nplaymarket.discovertecweb.com/widget2.ashx';
$NPlayWidget.divElement = null;
$NPlayWidget.overlayCss = { 'zIndex': '1000', 'position': 'fixed', 'backgroundImage': "url('http://nplaymarket.discovertecweb.com/Images/overlay.png')",
    'top': '0px', 'left': '0px', 'width': '100%', 'height': '100%', 'textAlign': 'center', 'verticalAlign': 'middle'
};
$NPlayWidget.widgetCss = { 'zIndex': '1010', 'top': '0px', 'position': 'absolute', 'display': 'block', 'width': '95%', 'height': '100%', 'margin': '0 auto' };


function addStylesToElement(elem, styles) {
    for (var styleName in styles)
        elem.style[styleName] = styles[styleName];
}

function loadHTMLIntoDOM(html) {
    html = CreateScriptTags(html);
    html = CreateStyleSheetTags(html);

    var div = document.createElement('div');
    addStylesToElement(div, $NPlayWidget.overlayCss);
    div.setAttribute('id', 'nplay-overlay');
    var body = document.getElementsByTagName('body').item(0);
    body.appendChild(div);

    div = document.createElement('div');
    div.innerHTML = html;
    div.setAttribute('id', 'nplay-widget-container');
    addStylesToElement(div, $NPlayWidget.widgetCss);

    body.appendChild(div);
}

function fetchHTML() {

    if (document.getElementById('nplay-overlay') != null && document.getElementById('nplay-widget-container') != null) {

        // document.getElementById('nplay-overlay').style.display = 'block';
        // document.getElementById('nplay-widget-container').style.display = 'block';
        //return;

        var nplayOverlay = document.getElementById('nplay-overlay');
        var nplayWidgetContainer = document.getElementById('nplay-widget-container');
        nplayOverlay.parentNode.removeChild(nplayOverlay);
        nplayWidgetContainer.parentNode.removeChild(nplayWidgetContainer);

    }

    //var nShowFeatured = true;
    //var nBackgroundColor = "#FF0000";
    //var nWebURL = "www.atlantarealestate.net";

    var params = {};
    params.nShowFeatured = nShowFeatured;
    params.nBackgroundColor = nBackgroundColor;
    params.nWebURL = nWebURL;

    var url = createURL(params);

    if ($NPlayWidget.isIE7) {
        $NPlayWidget.dynObj = new JSONscriptRequest(url);
        $NPlayWidget.dynObj.buildScriptTag();
        $NPlayWidget.dynObj.addScriptTag();

    }
    else if ($NPlayWidget.useXDR) {

        $NPlayWidget.req = new XDomainRequest();
        $NPlayWidget.req.onload = loadXDR;
        $NPlayWidget.req.open('get', url);
        $NPlayWidget.req.send();

    }
    else {

        $NPlayWidget.req = new XMLHttpRequest();
        $NPlayWidget.req.onreadystatechange = xhrReadyStateChange;
        $NPlayWidget.req.open('get', url);
        $NPlayWidget.req.send(null);

    }
}

function loadXDR() {
    if ($NPlayWidget.req.responseText.length > 0)
        loadHTMLIntoDOM($NPlayWidget.req.responseText);
}

function xhrReadyStateChange() {
    if ($NPlayWidget.req.readyState == 4) {
        if ($NPlayWidget.req.status == 200)
            loadHTMLIntoDOM($NPlayWidget.req.responseText);
    }
}

function scriptHTMLCallback(newHTML) {
    if ($NPlayWidget.dynObj != null) {
        $NPlayWidget.dynObj.removeScriptTag();
        $NPlayWidget.dynObj = null;
    }

    loadHTMLIntoDOM(newHTML);
}

function createURL(params) {
    var url = $NPlayWidget.baseURL;
    var added = false;

    for (var propName in params) {
        if (params.hasOwnProperty(propName)) {
            url += added ? "&" : "?";
            url += propName + "=" + encodeURI(params[propName]);
            added = true;
        }
    }

    if ($NPlayWidget.isIE7) {
        url += added ? "&" : "?";
        url += "script=1";
    }

    return url;
}

//
// JSONScriptRequest object, used to emulate Cross-Domain HttpRequests on IE7
//

function JSONscriptRequest(fullUrl) {
    this.fullUrl = fullUrl;
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

JSONscriptRequest.scriptCounter = 1;

JSONscriptRequest.prototype.buildScriptTag = function () {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}

JSONscriptRequest.prototype.removeScriptTag = function () {
    this.headLoc.removeChild(this.scriptObj);
}

JSONscriptRequest.prototype.addScriptTag = function () {
    this.headLoc.appendChild(this.scriptObj);
}




var $SearchResults = {};

$SearchResults.isIE7 = (navigator.appVersion.match("MSIE 7.0") == "MSIE 7.0") || (navigator.userAgent.indexOf('Opera/9.80') != -1);
$SearchResults.useXDR = window.XDomainRequest ? true : false;
$SearchResults.dynObj = null;
$SearchResults.searchURL = 'http://nplaymarket.discovertecweb.com/OfferCenter2.ashx';
$SearchResults.divElement = null;

$SearchResults.widgetCss = { 'zIndex': '1010', 'top': '0px', 'position': 'absolute', 'display': 'block', 'width': '95%', 'height': '100%', 'margin': '0 auto' };


function loadHTMLIntoDOM1(html) {
    //    var div = document.createElement('div');
    //    addStylesToElement(div, $SearchResults.overlayCss);
    //    div.setAttribute('id', 'nplay-overlay');
    //    var targetDiv = document.getElementbyId('targetDiv');
    //    targetDiv.appendChild(div);
    
    var div = document.getElementById('nplay-searchResult-container');
    if (div == null) {
        div = document.createElement('div');
        div.setAttribute('id', 'nplay-searchResult-container');
    }
    div.innerHTML = html;
    var targetDiv = document.getElementById('targetDiv');
    targetDiv.style.display = 'block';
    targetDiv.appendChild(div);

    var targetDiv2 = document.getElementById('targetDiv2');
    targetDiv2.style.display = 'block';
    var divPopUp = document.getElementById('divPopUp');
    divPopUp.style.backgroundImage = "url(http://nplaymarket.discovertecweb.com/images/offer-center_bak.png)"

    // Needed because we need to return execution to the browser so it can load the new HTML into the DOM:
    if (!(navigator.userAgent.match(/iPhone/i)) && !(navigator.userAgent.match(/iPod/i))) {
        window.setTimeout("setScrollers()", 250);
    }
}

function setScrollers() {
    Scroller.setAll();
}

function fetchHTML1(params) {
    var url = createURL1(params);
    if ($SearchResults.isIE7) {
        $SearchResults.dynObj = new JSONscriptRequest1(url);
        $SearchResults.dynObj.buildScriptTag();
        $SearchResults.dynObj.addScriptTag();
    }
    else if ($SearchResults.useXDR) {
        $SearchResults.req = new XDomainRequest();
        $SearchResults.req.onload = loadXDR1;
        $SearchResults.req.open('get', url);
        $SearchResults.req.send();
    }
    else {
        $SearchResults.req = new XMLHttpRequest();
        $SearchResults.req.onreadystatechange = xhrReadyStateChange1;
        $SearchResults.req.open('get', url);
        $SearchResults.req.send(null);
    }
}


function loadXDR1() {
    if ($SearchResults.req.responseText.length > 0)
        loadHTMLIntoDOM1($SearchResults.req.responseText);
}

function xhrReadyStateChange1() {
    if ($SearchResults.req.readyState == 4) {
        if ($SearchResults.req.status == 200)
            loadHTMLIntoDOM1($SearchResults.req.responseText);
    }
}

function scriptHTMLCallback1(newHTML) {
    if ($SearchResults.dynObj != null) {
        $SearchResults.dynObj.removeScriptTag();
        $SearchResults.dynObj = null;
    }

    loadHTMLIntoDOM1(newHTML);
}


function createURL1(params) {
    var url = $SearchResults.searchURL;
    var added = false;
    for (var propName in params) {
        if (params.hasOwnProperty(propName)) {
            url += added ? "&" : "?";
            url += propName + "=" + encodeURI(params[propName]);
            added = true;
        }
    }

    if ($SearchResults.isIE7) {
        url += added ? "&" : "?";
        url += "script=1";
    }
    return url;
}

//
// JSONScriptRequest object, used to emulate Cross-Domain HttpRequests on IE7
//

function JSONscriptRequest1(fullUrl) {
    this.fullUrl = fullUrl;
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'JscriptId' + JSONscriptRequest1.scriptCounter++;
}

JSONscriptRequest1.scriptCounter = 1;

JSONscriptRequest1.prototype.buildScriptTag = function () {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}

JSONscriptRequest1.prototype.removeScriptTag = function () {
    this.headLoc.removeChild(this.scriptObj);
}

JSONscriptRequest1.prototype.addScriptTag = function () {
    this.headLoc.appendChild(this.scriptObj);
}

function NPlayOfferCenterSearch(pageNum) {
    NPlayOfferCloseAll();
    var params = {};
    var myTextField = document.getElementById('txtSearch');
    if (myTextField.value != '' && myTextField.value != 'Enter Address, Zip, MLS ID or Agent Name') {
        myTextField.style.color = '#555555';
        params.a = myTextField.value;
        params.nShowFeatured = nShowFeatured;
        params.nBackgroundColor = nBackgroundColor;
        params.nWebURL = nWebURL;
        params.pageNum = pageNum;
        var nplayBHId = document.getElementById('nplayBHId');
        params.bhId = nplayBHId.value;

        var url = createURL(params);
        var lblSearchCriteria = document.getElementById('lblSearchCriteria');
        lblSearchCriteria.innerHTML = myTextField.value;
        fetchHTML1(params);

        var nplayOfferCenterBottomLink = document.getElementById('nplayOfferCenterBottomLink');

        nplayOfferCenterBottomLink.style.display = 'block';
    }
    else {
        myTextField.value = 'Enter Address, Zip, MLS ID or Agent Name';
        myTextField.style.color = '#FF1111';
    }
    var nplaySearchDone = document.getElementById('nplaySearchDone');
    if (nplaySearchDone)
        nplaySearchDone.value = "YES";
}


function NPlayOfferCloseAll() {
    var divLearnMore = document.getElementById('divNPlayLearnMore');
    divLearnMore.style.display = 'none';

    var targetDiv = document.getElementById('targetDiv');
    targetDiv.style.display = 'none';

    var targetDiv2 = document.getElementById('targetDiv2');
    targetDiv2.style.display = 'none';

    var nplayOfferCenterBottomLink = document.getElementById('nplayOfferCenterBottomLink');
    nplayOfferCenterBottomLink.style.display = 'none';

    var divPopUp = document.getElementById('divPopUp');
    divPopUp.style.backgroundImage = "url(http://nplaymarket.discovertecweb.com/images/offer-center_baksm.png)"
}

function NPlayOfferCenterLearnMore() {

    NPlayOfferCloseAll();
    var divLearnMore = document.getElementById('divNPlayLearnMore');

    divLearnMore.style.display = 'block';

    var nplayOfferCenterBottomLink = document.getElementById('nplayOfferCenterBottomLink');
    nplayOfferCenterBottomLink.style.display = 'block';

    var divPopUp = document.getElementById('divPopUp');
    divPopUp.style.backgroundImage = "url(http://nplaymarket.discovertecweb.com/images/offer-center_bak.png)"
}

Array.prototype.find = function (searchStr) {
    var returnArray = false;
    for (i = 0; i < this.length; i++) {
        if (typeof (searchStr) == 'function') {
            if (searchStr.test(this[i])) {
                if (!returnArray)
                    returnArray = []
                returnArray.push(i);
            }
        }
        else {
            if (this[i] === searchStr) {
                if (!returnArray)
                    returnArray = []
                returnArray.push(i);
            }
        }
    }
    return returnArray;
}

var NPlayScripts = [];
var NPlayStyles = [];

function CreateScriptTags(html) {
    var regex = /<script.*?src="([^"]*).*?<\/script>/
    var match = regex.exec(html);
    while (match != null) {
        var filename = match[1];
        if (NPlayScripts.find(filename) === false) {
            var scriptObj = document.createElement("script");
            scriptObj.setAttribute("type", "text/javascript");
            scriptObj.setAttribute("src", filename);
            document.getElementsByTagName("head").item(0).appendChild(scriptObj);
            NPlayScripts.push(filename);
        }

        html = html.replace(match[0], "");
        regex.lastIndex = 0;
        match = regex.exec(html);
    }

    return html;
}

function CreateStyleSheetTags(html) {
    var regex = /<link.*?href="([^"]*).*?\/>/
    var match = regex.exec(html);
    while (match != null) {
        var filename = match[1];
        if (NPlayStyles.find(filename) === false) {
            var scriptObj = document.createElement("link");
            scriptObj.setAttribute("type", "text/css");
            scriptObj.setAttribute("href", filename);
            scriptObj.setAttribute("rel", "stylesheet");
            document.getElementsByTagName("head").item(0).appendChild(scriptObj);
            NPlayStyles.push(filename);
        }

        html = html.replace(match[0], "");
        regex.lastIndex = 0;
        match = regex.exec(html);
    }

    return html;
}


function submitenter(myfield, e) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13) {
        NPlayOfferCenterSearch();
        return false;
    }
    else
        return true;
}

function showNPlayOfferLink() {

    
    document.getElementById("NplayOfferCenterAjaxVer").style.display = "block";

    if (nShowImageLink) {

        if (nplayImageLinkUrl == undefined || nplayImageLinkUrl == "")
            nplayimageLinkUrl = "http://nplaymarket.discovertecweb.com/Images/NPNonBB.png";
        document.getElementById("NplayOfferCenterAjaxVer").innerHTML = "<a href='#' onclick='fetchHTML();'><img src=" + nplayImageLinkUrl + " alt='clear' width='203' height='32' border='0' /></a>";
    }
    else {
        if (nplayTextLink == undefined || nplayTextLink == "")
            nplayTextLink = "Click Here";

        document.getElementById("NplayOfferCenterAjaxVer").innerHTML = "<a href='#' onclick='fetchHTML();'>" + nplayTextLink + "</a>";
    
    }
}
setTimeout("showNPlayOfferLink()", 1000);

