/*global window, theform, alert, document, jwplayer*/
//Vertigo Tip by www.vertigo-project.com
//add class 'vtip' to any link and title of link becomes tip bubble

function vtip() {
    var xOffset = -12, yOffset = 30;  
    $(".vtip, a.vtip").unbind().hover(
        function (e) {
            this.t = this.title;
            this.title = '';
            this.top = (e.pageY + yOffset);
            //alert(e.pageY);
            this.left = (e.pageX + xOffset);
            $('body').append('<p id="vtip"><img id="vtipArrow" /><span class="vtip-in">' + this.t + '</span></p>');
            $('p#vtip #vtipArrow').attr("src", '/assets/css/images/vtip_arrow.png');
            $('p#vtip').css("top", this.top + "px").css("left", this.left + "px").fadeIn("slow");
        }, 
        function () {
            this.title = this.t;
            $("p#vtip").fadeOut("slow").remove();
        }
    ).mousemove(
        function (e) {
            this.top = (e.pageY + yOffset);
            this.left = (e.pageX + xOffset);
            $("p#vtip").css("top", this.top + "px").css("left", this.left + "px");
        }
    );
}

//Popup alert for ASP.NET linkbuttons


function popupAlert(alertText, doThis) {
    if (!alertText) {
        alertText = 'Are you sure?';
    }
    if ($("#popupAlert").Length > 0) {
        $("#popupAlert").remove();
    }
    $("body").append('<div id="popupAlert" style="display:none;"><p>' + alertText + '</p></div>');
    $("#popupAlert").dialog({
        modal: true,
        title: 'Alert',
        buttons: {
            'Yes': function () {
                eval(doThis);
                $(this).dialog('close');
            },
            Cancel: function () {
                $(this).dialog('close');
            }
        }
    });
    return false;
}

function getASPControlID(ctrlID) {
    return $("*[id$='" + ctrlID + "']").attr('id');
}

function urlVarObj(URL) {
    var i, vars = [], hash, hashes = URL.slice(URL.indexOf('?') + 1).split('&');
    for (i = 0; i < hashes.length; i = i + 1) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

// Get object of URL parameters
//      var allVars = $.getUrlVars();
// Getting URL var by its nam
//      var byName = $.getUrlVar('name');
$.extend({
    getUrlVars: function (stringURL) {
        if (!stringURL) {stringURL = window.location.href;}
        var vars = urlVarObj(stringURL); return vars; 
    },
    getUrlVar: function (name, stringURL) { 
        if (!stringURL) {stringURL = window.location.href;}
        return $.getUrlVars(stringURL)[name]; 
    }
});

function removeVariableFromURL(url_string, variable_name) {
    URL = String(url_string);
    var regex = new RegExp( "\\?" + variable_name + "=[^&]*&?", "gi");
    URL = URL.replace(regex,'?');
    regex = new RegExp( "\\&" + variable_name + "=[^&]*&?", "gi");
    URL = URL.replace(regex,'&');
    URL = URL.replace(/(\?|&)$/,'');
    regex = null;
    return URL;
}

function getTiny(tinyURL) {
    var cdomain, oldURL, splitURL, httptest, sufftest, newTiny;
    cdomain = document.domain.replace(/dev1\.|dev\.|dev0\.|wwwdev\./, "www.");
    oldURL = tinyURL.replace(/dev1\.|dev\.|dev0\.|wwwdev\./, "www.");
    splitURL = oldURL.split("/");
    httptest = /(http|https)/;
    sufftest = /\.com|\.org|\.edu/;
    if (!httptest.test(splitURL[0])) {
        if (sufftest.test(splitURL[0])) {
            oldURL = "http://" + oldURL;
        } else if (oldURL.indexOf('/') === 0) {
            oldURL = "http://" + cdomain + oldURL;
        } else {
            oldURL = "http://" + cdomain + "/" + oldURL;
        }
    }
    $.ajax({
        url: "/assets/tinyurl/create.aspx?url=" + oldURL,
        type: 'GET',
        async: false,
        success: function (data) {
            newTiny = data;
        }
    });
    return newTiny;
}

$(document).ready(function () {
    var thisIdent, oldURL;
    vtip();
    $("#accordion, .accordion").accordion({
        autoHeight: false
    });
    $("#tabs, .tabs").tabs();
    $('.calendar').datepicker({changeMonth: true, changeYear: true, yearRange: "-100:+10"});
    //hide any div by adding hide_div var to a URL
    if ($.getUrlVar("hide_div")) {
        $("#" + $.getUrlVar("hide_div")).hide();
    }
    $("a.getTiny").each(function () {
        if (!this.id) {
            this.id = $.uuid();
        }
        thisIdent = '#' + this.id;
        oldURL = this.href.replace(/dev1\.|dev\.|dev0\.|wwwdev\./, "www.");
        $(thisIdent).attr('href', getTiny(oldURL));
    });
    $(".blogSearchText").focus(function () {
        if (this.value == this.defaultValue) {
            this.value = "";
        }
    }).blur(function () {
        if (!this.value.length) {
            this.value = this.defaultValue;
        }
    });
});

//This area creates Unique IDs for anything. just call: var what = $.uuid();


function uuid() {
    var S4 = function () {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}


