﻿//GLOBAL VARIABLES////////////////////////////////////////////////////////////////////////////////

var lrs_showLoading = true;
var lrs_slideSpeed = 250;

/////////////////////////////////////////////////////////////////////////////////////////////////////////

function autoFill(id, v) {

    if ($(id).val() == "" || $(id).val() == v) {
        $(id).css({ color: "#b2adad" }).attr({ value: v }).focus(function () {
            if ($(this).val() == v) {
                $(this).val("").css({ color: "#333" });
            }
        }).blur(function () {
            if ($(this).val() == "") {
                $(this).css({ color: "#b2adad" }).val(v);
            }

        });
    }

}

function addWModeParameter(oembedData) {
    if (oembedData.type == "video") {
        var code = oembedData.code;
        var wmode = "transparent";
        if (code != null && code.indexOf("wmode") < 0) {
            code = code.replace("<embed ", "<param name=\"wmode\" value=\"" + wmode + "\"></param>\n<embed ");
            code = code.replace("<embed ", "<embed wmode=\"" + wmode + "\"");
        }
        oembedData.code = code;
    }
}

location.querystring = (function () {

    // The return is a collection of key/value pairs
    var queryStringDictionary = {};

    // Gets the query string, starts with '?'
    var querystring = decodeURI(location.search);

    // document.location.search is empty if no query string
    if (!querystring) {
        return {};
    }

    // Remove the '?' via substring(1)
    querystring = querystring.substring(1);

    // '&' seperates key/value pairs
    var pairs = querystring.split("&");

    // Load the key/values of the return collection
    for (var i = 0; i < pairs.length; i++) {
        var keyValuePair = pairs[i].split("=");
        queryStringDictionary[keyValuePair[0]]
                        = keyValuePair[1];
    }

    // toString() returns the key/value pairs concatenated
    queryStringDictionary.toString = function () {

        if (queryStringDictionary.length == 0) {
            return "";
        }

        var toString = "?";

        for (var key in queryStringDictionary) {
            toString += key + "=" +
                        queryStringDictionary[key];
        }

        return toString;
    };

    // Return the key/value dictionary

    return queryStringDictionary;
})();

$.maxZIndex = $.fn.maxZIndex = function (opt) {
    /// <summary>
    /// Returns the max zOrder in the document (no parameter)
    /// Sets max zOrder by passing a non-zero number
    /// which gets added to the highest zOrder.
    /// </summary>    
    /// <param name="opt" type="object">
    /// inc: increment value, 
    /// group: selector for zIndex elements to find max for
    /// </param>
    /// <returns type="jQuery" />
    var def = { inc: 10, group: "*" };
    $.extend(def, opt);
    var zmax = 0;
    $(def.group).each(function () {
        var cur = parseInt($(this).css('z-index'));
        zmax = cur > zmax ? cur : zmax;
    });
    if (!this.jquery)
        return zmax;

    return this.each(function () {
        zmax += def.inc;
        $(this).css("z-index", zmax);
    });
}

function isValidURL(url) {
    var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;

    if (RegExp.test(url)) {
        return true;
    } else {
        return false;
    }
}

$.fn.sumValues = function () {
    var sum = 0;
    this.each(function () {
        if ($(this).is(':input')) {
            var val = $(this).val();
        } else {
            var val = $(this).text();
        }
        sum += parseFloat(('0' + val).replace(/[^0-9-\.]/g, ''), 10);
    });
    return sum;
};

function embed(url, maxwidth, maxheight, tag, useThumbnail) {

    var itWorked = false;
    var timestamp = new Date().getTime();

    $.ajax({
        type: "GET",
        dataType: "json",
        url: "/Application/GetJsonFromWeb/?timestamp=" + timestamp + "&url=" + url + "&maxwidth=" + maxwidth + "&maxheight=" + maxheight,
        async: false,
        success: function (msg) {
            if (useThumbnail) {
                if (msg.thumbnail_url) {
                    $(tag).html('<img src="' + msg.thumbnail_url + '" />');
                    itWorked = true;
                } else if (msg.html) {
                    $(tag).html(msg.html);
                    itWorked = true;
                } 
            } else {
                if (msg.html) {
                    $(tag).html(msg.html);
                    itWorked = true;
                } 
            }
        }
    });


    return itWorked;

}

function TrackAjax(url) {
    _gaq.push(['_trackPageview', url]);
}


