﻿(function($) { //Get outerHTML content of the object using jquery
    $.fn.outerHTML = function() {
        var $this = $(this);
        if ($this.length > 1)
            return $.map($this, function(el) { return $(el).outerHTML(); }).join('');
        return $this.clone().wrap('<div/>').parent().html();
    }
})(jQuery);


$(document).ready(function() {
    var now = new Date(); var hour = now.getHours(); if (Number(hour) > 19 || Number(hour) < 7) { document.body.className += ' night '; } else document.body.className += ' day ';
    setWidgetCount(false);

});


function setWidgetCount(animate) {
    var ajaxUrl = ResolveUrl("~/AjaxHandler.aspx?action=getTotalWidgetCount");
    $.ajax({
        url: ajaxUrl,
        type: "POST",
        data: ({}),
        dataType: "text",
        success: function(res) {
            if (res && res.length > 0) {
                var data = eval("(" + res + ")");
                if (data == null) { return; }
                var totalWidgetCount = data.TotalWidgetCount;
                setOdoTicker(totalWidgetCount, animate);
            }
        }
    });
}


function setOdoTicker(number, animate) {
    var speed = (animate == false) ? 0 : 1500;
    number = number.toString().lpad("0", 7)
    $('#odoMeter').odoTicker({
        number: number, //Number to load
        speed: speed, 		//speed in ms
        height: 37 		//height of a single number in the CSS sprite
    });
}



//pads left
String.prototype.lpad = function(padString, length) {
    var str = this;
    while (str.length < length)
        str = padString + str;
    return str;
}

//pads right
String.prototype.rpad = function(padString, length) {
    var str = this;
    while (str.length < length)
        str = str + padString;
    return str;
}

String.prototype.startsWith = function(prefix) {
    return this.indexOf(prefix) === 0;
}

String.prototype.endsWith = function(suffix) {
    return this.match(suffix + "$") == suffix;
};

function copyDataToClipboard(id) {
    var value = $(id).val();
    if (value) {
        copyToClipboard(value);
    }
}
function copyToClipboard(s) {
    if (window.clipboardData && clipboardData.setData) {
        s = s.replace(/(\n)/g, '\r\n');
        if (!window.clipboardData.setData("Text", s)) {
            return false;
        }
    } else {
        // You have to sign the code to enable this or allow the action in about:config by changing
        //user_pref("signed.applets.codebase_principal_support", true);
        try {
            netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
        } catch (e) {
            alert("Your current Internet Security settings restrict copying of data to clipboard.");
            return false;
        }

        try {
            var clip = Components.classes['@mozilla.org/widget/clipboard;1'].getService();
            clip = clip.QueryInterface(Components.interfaces.nsIClipboard);
        } catch (e) {
            alert('@mozilla.org/widget/clipboard');
            return false;
        }

        if (!clip) return;

        // create a transferable
        try {
            var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance();
            trans = trans.QueryInterface(Components.interfaces.nsITransferable);
        } catch (e) {
            alert('@mozilla.org/widget/transferable');
            return false;
        }

        if (!trans) return;

        // specify the data we wish to handle. Plaintext in this case.
        trans.addDataFlavor('text/unicode');

        // To get the data from the transferable we need two new objects
        var str = new Object();

        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);

        str.data = s;

        trans.setTransferData("text/unicode", str, s.length * 2);

        try {
            var clipid = Components.interfaces.nsIClipboard;
        } catch (e) {
            return false;
        }

        if (!clip) return false;

        clip.setData(trans, null, clipid.kGlobalClipboard);
    }
}


function OpenCompensationPlanPage() {
    $("#WidgetPageFooterTemplate").html("");
    //window.open('CompensationPlan.aspx', '', 'fullscreen,scrollbars');
    return true;
}


function showLegalNoticePopup() {
    var url = ResolveUrl("~/LegalNotice.aspx");
    $.fancybox({
        type: 'iframe',
        href: url,
        'cssClass': 'fbLegalNotice',
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'easingIn': 'easeOutBack',
        'easingOut': 'easeInBack',
        width: 610,
        height: 345
    });
    return false;
}

function showContactUsPopup() {
    var url = ResolveUrl("~/ContactUs.aspx");
    $.fancybox({
        type: 'iframe',
        href: url,
        'cssClass': 'fbLegalNotice',
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'easingIn': 'easeOutBack',
        'easingOut': 'easeInBack',
        width: 610,
        height: 495
    });
    return false;
}

function showAboutUsPopup() {
    var url = ResolveUrl("~/AboutUs.aspx");
    var popupHeight = Math.max($(window).height() - 5, 535);
    $.fancybox({
        type: 'iframe',
        href: url,
        'cssClass': 'fbLegalNotice',
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'easingIn': 'easeOutBack',
        'easingOut': 'easeInBack',
        /*width: 1114,
        height: 658*/
        height: popupHeight,
        width: 1114,

    });
    return false;
}


function showLoginForFacebookShare(sender) {
    var url = sender.href;
    alert(url);
    $.fancybox({
        type: 'iframe',
        href: url,
        'cssClass': 'fbLegalNotice',
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'easingIn': 'easeOutBack',
        'easingOut': 'easeInBack',
        width: 675,
        height: 403

    });
    return false;
}

function showPrivacyPolicyPopup() {
    var url = ResolveUrl("~/PrivacyPolicy.aspx");
    var popupHeight = Math.max($(window).height() - 5, 535);
    $.fancybox({
        type: 'iframe',
        href: url,
        'cssClass': 'fbLegalNotice',
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'easingIn': 'easeOutBack',
        'easingOut': 'easeInBack',
        /*width: 1114,
        height: 658*/
        height: popupHeight,
        width: 800,
    });
    return false;
}


function showIndexPagePopup() {
    var url = ResolveUrl("~/Index.aspx");
    $.fancybox({
        type: 'iframe',
        href: url,
        'cssClass': 'fbLegalNotice',
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'easingIn': 'easeOutBack',
        'easingOut': 'easeInBack',
        height: 626,
        width: 1135
    });
    return false;
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
}

function checkCookie() {
    username = getCookie('username');
    if (username != null && username != "") {
        alert('Welcome again ' + username + '!');
    }
    else {
        username = prompt('Please enter your name:', "");
        if (username != null && username != "") {
            setCookie('username', username, 365);
        }
    }
}


