﻿
//var prm = Sys.WebForms.PageRequestManager.getInstance();
//if (!prm.get_isInAsyncPostBack()) {
//    prm.add_pageLoaded(pageLoaded);
//    prm.add_initializeRequest(initRequest);
//    prm.add_endRequest(endRequest);
//}

var t;
var WebSyncDebug = false;
var iVehPromo = 0;
var thumbTemplate = 'http://pix.ridesafely.com/?IID=#INVENTORYID#&PN=1&PID=#INVENTORYPICTURESID#&TM=1&T=96&AC=&WID=0';

function loadContent(url, placeholder) {
    // document.getElementById(placeholder).innerHTML = '<table width=100% height=100%><tr><td align=center valign=middle><img src="/images/progress.gif" width="30" height="30"></td></tr></table>';
    myAjax = new Ajax.Updater(placeholder, url);
    var test = 'LOADING';
}
function RemoveConfirm(displayText) {
    return confirm(displayText);
}
function pageLoaded(sender, args) {

    window.scrollTo(0, 0);
}

function initRequest(sender, args) {

    var pop = $find("<%=ModalPopupExtender1.ClientID%>");
    pop.show();
}

function endRequest(sender, args) {
    var pop = $find("<%=ModalPopupExtender1.ClientID%>");
    pop.hide();
}

function mouseX(e) {

    var evt = window.event || e //cross browser event object
    if (evt.pageX) return evt.pageX;
    else if (evt.clientX)
        return evt.clientX + (document.documentElement.scrollLeft ?

   document.documentElement.scrollLeft :
   document.body.scrollLeft);
    else return null;
}

function mouseY(e) {

    var evt = window.event || e //cross browser event object
    if (evt.pageY) return evt.pageY;
    else if (evt.clientY)
        return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
    else return null;
}

function popupWindow(page, name, w, h, add) {
    // var win = null;
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    if (winl < 0) winl = 0;
    if (wint < 0) wint = 0;
    var settings = 'height=' + h + ',';
    settings += 'width=' + w + ',';
    settings += 'top=' + wint + ',';
    settings += 'left=' + winl + ',';
    settings += add;
    win = window.open(page, name, settings);
    // window.open(page, name, settings);
    win.window.focus();
    // window.focus();
}

function numbersOnly(textbox, e, dec) {
    var key;
    var keychar;

    if (window.event)
        key = window.event.keyCode;
    else if (e)
        key = e.which;
    else
        return true;
    keychar = String.fromCharCode(key);

    // control keys
    if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27))
        return true;

    // numbers
    else if ((("0123456789").indexOf(keychar) > -1))
        return true;

    // decimal point jump
    else if (dec && (keychar == ".")) {
        textbox.form.elements[dec].focus();
        return false;
    }
    else
        return false;
}

function addOption(selectbox, text, value) {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}

function getValue(lblName) {
    return document.getElementById(lblName) == null ? '' : document.getElementById(lblName).value;
}
function getLabel(lblName) {
    return document.getElementById(lblName) == null ? '' : document.getElementById(lblName).innerHTML;
}
function setValue(lblName, lblValue) {
    if (getValue(lblName) != lblValue) {
        document.getElementById(lblName).value = lblValue;
    }
}
function setLabel(lblName, lblValue) {
    if (getLabel(lblName) != lblValue) {
        document.getElementById(lblName).innerHTML = lblValue;
    }
}
function resetObj(lblName, newValue) {
    document.getElementById(lblName).outerHTML = newValue;
}

function spb(pId) {
    var blocks = document.getElementById("content-slider-inside").getElementsByTagName("li");
    var nav = document.getElementById("navigation").getElementsByTagName("li");

    for (i = 0; i < blocks.length; i++) {
        if (i == pId) {
            blocks[i].style.display = "block";
            nav[i].className = "selected";
        }
        else {
            blocks[i].style.display = "none";
            nav[i].className = "";
        }
    }

    clearTimeout(t);
    t = setTimeout("spbAuto()", 4000);
}

function spbAuto() {

    var blocks = document.getElementById("content-slider-inside").getElementsByTagName("li");

    for (i = 0; i < blocks.length; i++) {
        if (blocks[i].style.display == "block") {
            if (i == blocks.length - 1) {
                spb(0);
            }
            else {
                spb(i + 1);
            }
            break;
        }
    }
}

///////////// Header Menu ////////////////////////////
var cssmenuids = ["ulMA", "ulA", "ulTZ", "ulL", "ulO"]
var submenu1offset = -1; 
var submenu2offset = -28;

function createmenu() {     
    for (var i = 0; i < cssmenuids.length; i++) {
        var ultags = document.getElementById(cssmenuids[i]).getElementsByTagName("ul");
        for (var t = 0; t < ultags.length; t++) {
            if (ultags[t].className == "submenu2") {
                ultags[t].style.top = ultags[t].parentNode.offsetHeight + submenu2offset + "px";
            }
            else {
                ultags[t].style.top = ultags[t].parentNode.offsetHeight + submenu1offset + "px";
//                if(ultags[t].id != "ulLsb")
//                {
//                    var spanref = document.createElement("span")
//                    spanref.className = "arrowdiv"
//                    spanref.innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;"
//                    ultags[t].parentNode.getElementsByTagName("a")[0].appendChild(spanref)
//                }
            }
            ultags[t].parentNode.onmouseover = function() {
                this.style.zIndex = 100
                this.getElementsByTagName("ul")[0].style.visibility = "visible";
                this.getElementsByTagName("ul")[0].style.zIndex = 0;
            }
            ultags[t].parentNode.onmouseout = function() {
                this.style.zIndex = 0;
                this.getElementsByTagName("ul")[0].style.visibility = "hidden";
                this.getElementsByTagName("ul")[0].style.zIndex = 100;
            }
        }
    }
}

function redirect(lst) {
    var qs = lst.options[lst.selectedIndex].value;
    window.location = window.location.pathname + "?" + qs;
}

///////////// ToolTip ////////////////////////////////
var tooltip = function() {
    var id = 'tt';
    var top = 3;
    var left = 3;
    var maxw = 300;
    var speed = 100;
    var timer = 1;
    var endalpha = 95;
    var alpha = 0;
    var tt, t, c, b, h;
    var ie = document.all ? true : false;
    return {
        show: function(obj, v, w) {
            if (tt == null) {
                tt = document.createElement('div');
                tt.setAttribute('id', id);
                t = document.createElement('div');
                t.setAttribute('id', id + 'top');
                c = document.createElement('div');
                c.setAttribute('id', id + 'cont');
                b = document.createElement('div');
                b.setAttribute('id', id + 'bot');
                tt.appendChild(t);
                tt.appendChild(c);
                tt.appendChild(b);
                document.body.appendChild(tt);
                tt.style.opacity = 0;
                tt.style.filter = 'alpha(opacity=0)';
                //document.onmousemove = this.pos;
            }
            obj.onmousemove = this.pos;
            tt.style.display = 'block';
            c.innerHTML = v;
            tt.style.width = w ? w + 'px' : 'auto';
            if (!w && ie) {
                t.style.display = 'none';
                b.style.display = 'none';
                tt.style.width = tt.offsetWidth;
                t.style.display = 'block';
                b.style.display = 'block';
            }
            if (tt.offsetWidth > maxw) { tt.style.width = maxw + 'px' }
            h = parseInt(tt.offsetHeight) + top;
            clearInterval(tt.timer);
            tt.timer = setInterval(function() { tooltip.fade(1) }, timer);
        },
        pos: function(e) {
            var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
            var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
            tt.style.top = (u - h) + 'px';
            tt.style.left = (l + left) + 'px';
        },
        fade: function(d) {
            var a = alpha;
            if ((a != endalpha && d == 1) || (a != 0 && d == -1)) {
                var i = speed;
                if (endalpha - a < speed && d == 1) {
                    i = endalpha - a;
                } else if (alpha < speed && d == -1) {
                    i = a;
                }
                alpha = a + (i * d);
                tt.style.opacity = alpha * .01;
                tt.style.filter = 'alpha(opacity=' + alpha + ')';
            } else {
                clearInterval(tt.timer);
                if (d == -1) { tt.style.display = 'none' }
            }
            
        },
        hide: function() {
            clearInterval(tt.timer);
            tt.timer = setInterval(function() { tooltip.fade(-1) }, timer);
        }
    };
} ();

function WSConnect() {
    client.connect({
        stayConnected: true,
        onSuccess: function(args) {
            setLabel('LCStatus', 'Connected');
            if (WebSyncDebug) { util.log('The client connected and has ID ' + args.clientId + '.'); }

            if (typeof (isAuctionBroadcast) != 'undefined' && isAuctionBroadcast == true) {
                // Subscribe
                subscribeAuctionBroadcast(channelGeneric);
            }

        },
        onFailure: function(args) {
            setLabel('LCStatus', 'Unable to establish connection');
            if (WebSyncDebug) { util.log('The client could not connect... ' + args.error); }
        },
        onStreamFailure: function(args) {
            setLabel('LCStatus', 'Disconnected');
            if (WebSyncDebug) { util.log('The client could not stream...' + args.error); }
            if (WebSyncDebug) { util.log('The client ' + (args.willReconnect ? 'will' : 'will not') + ' reconnect.'); }
        }
    });
}
//function WSSubscribe(channel) {
//    client.subscribe({
//        channel: channel,
//        onSuccess: function(args) {
//            setLabel('LCStatus', 'Connected');
//            if (WebSyncDebug) { util.log('subscribed to ' + channel + '.'); }
//        },
//        onFailure: function(args) {
//            setLabel('LCStatus', 'Disconnected');
//            if (WebSyncDebug) { util.log('The client could not subscribe to ' + channel + '... ' + args.error); }
//        },
//        onReceive: function(args) {
//            if (WebSyncDebug) { util.log('The client received data...'); }
//        }
//    });
//}
function vehPromo() {

    if (document.location.protocol == 'https:') return;

    var InventoryIDLocal = typeof (_IID) != 'undefined' ? _IID : 0;
    var vehPromoChannel = _SID + '_promo';


    client.subscribe({
        channel: '/' + vehPromoChannel,
        onSuccess: function(args) {

            client.publish({
                requestUrl: 'http://api.ridesafely.com/WebSync/Action.aspx',
                channel: '/VEHPROMO',
                data: {
                    Action: 'VEHPROMO',
                    InventoryID: InventoryIDLocal,
                    Misc: vehPromoChannel
                },
                onSuccess: function(args) {
                    // DO NOTHING
                },
                onFailure: function(args) {
                    // DO NOTHING
                }
            });

        },
        onFailure: function(args) {
            // util.log('The client could not subscribe to ' + channel + '... ' + args.error);
        },
        onReceive: function(args) {

            if (iVehPromo >= 3) return;

            document.getElementById('divNFAll').style.display = '';
            var nfRowTemplate = getLabel('divNFTemplate');
            setLabel('divNFItems', getLabel('divNFItems') + nfRowTemplate.replace(/\###/g, args.data.InventoryID));
            setLabel('divNFImage' + args.data.InventoryID, '<img src="' + (document.location.protocol == 'https:' ? (args.data.Image).replace('http:', 'https:') : args.data.Image) + '" width="96" height="72" />');
            setLabel('divNFVehicle' + args.data.InventoryID, args.data.Vehicle);
            setLabel('divNFMileage' + args.data.InventoryID, formatNumber(args.data.Mileage) + ' ' + getValue('ctl00_' + (args.data.IsKM ? 'hdnNFKM' : 'hdnNFMiles')));
            setLabel('divNFPrice' + args.data.InventoryID, args.data.Price);
            iVehPromo++;
            return;

        }

    });


}
function formatNumber(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function invNav(row, aid) {
    window.location.href = '/Inventory.aspx?InventoryID=' + row + '&AID=' + aid;
}
function getArrayIndex(arr, obj) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == obj) return i;
    }
    return -1;
}

