﻿var IE = document.all ? true : false
var mouseX;
var mouseY;

function popMusic( tapeid ){
	window.open( "http://www.datpiff.com/pop-mixtape-player.php?id=" + tapeid , "playMixtape", "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=550,height=250,left=250,top=50");
	return false;
}

function Maximum(obj, maxlen, fieldname) {
    if (obj.length > maxlen) {
        alert('Maximum characters for ' + fieldname + ' is ' + maxlen);
        document.chars.tests.value = obj.substring(0, maxlen);
    }
}

function Minimum(obj, minlen, fieldname) {
    if (obj.value.length < minlen) 
        alert('Minimum characters for ' + fieldname + ' is ' + minlen);
}

function popupDJs(audioid, questionid, answer, submissionid) {
    if (audioid > 0)
        popup = window.open("djlist.aspx?audioid=" + audioid + "&questionid=" + questionid + "&answer=" + answer, "DJ", "left=100,top=100,width=600,height=600,resizable,scrollbars=yes,status=1,toolbar=yes");
    else
        popup = window.open("djlist.aspx?submissionid=" + submissionid + "&questionid=" + questionid + "&answer=" + answer, "DJ", "left=100,top=100,width=600,height=600,resizable,scrollbars=yes,status=1,toolbar=yes");
}

function toggleDiv(divElement) {
    var elm = document.getElementById(divElement);
    elm.style.display = elm.style.display == 'block' ? 'none' : 'block';
}

function activateElement(name, e) {
    var lis = document.getElementsByName(name);
    for (var i = 0; i < lis.length; i++) {
        lis[i].className.replace("active", "");
    }
    e.className += " active";
}

function captureMouseMoves() {
    if (!IE) 
        document.captureEvents(Event.MOUSEMOVE)
    document.onmousemove = getMouseXY;
}

function calendarPicker(strField, doSubmit) {
    //position window just below current field
    datewindow = window.open('/controls/DatePicker.aspx?field=' + strField + '&dosubmit=' + doSubmit, 'calendarPopup',
        'left=' + mouseX + ',top=' + (mouseY + 200) + ',width=250,height=190,resizable=0,status=0,toolbar=0,menubar=0,scrollbars=0');
}

function getMouseXY(e) {
    if (IE) { // Grab the x-y pos.s if browser is IE.
        mouseX = event.clientX + document.body.scrollLeft;
        mouseY = event.clientY + document.body.scrollTop;
    }
    else {  // Grab the x-y pos.s if browser isn't IE.
        mouseX = e.pageX;
        mouseY = e.pageY;
    }
    if (mouseX < 0) { mouseX = 0; };
    if (mouseY < 0) { mouseY = 0; };

    if (targetIsBody(e)) {
        document.body.style.cursor = 'pointer';
    }
    else
        document.body.style.cursor = 'auto';

    return true;
}

//Add a javascript file include after a particular tag (head, body,etc.)
function addJavascript(jsname, pos) {
    var th = document.getElementsByTagName(pos)[0];
    var s = document.createElement('script');
    s.setAttribute('type', 'text/javascript');
    s.setAttribute('src', jsname);
    th.appendChild(s);
}

function fblogin(message) {
    window.location.reload(true);
}
function fblogout(message) {
    FB.logout();
    window.location.reload(true);
}

function includeCSS(p_file) {
    var v_css = document.createElement('link');
    v_css.rel = 'stylesheet'
    v_css.type = 'text/css';
    v_css.href = p_file;
    document.getElementsByTagName('head')[0].appendChild(v_css);
}

function clickBg(e, targetUrl) {
    if (targetIsBody(e)) {
        window.location = targetUrl;
        return false;
    }
    return true;
}

function fbConnect() {
    FB.login(function (response) {
        if (response.authResponse) {
            document.getElementById("fbvoting").value = "1";
            document.forms["aspnetForm"].submit();
        } 
    }, { scope: 'email,publish_stream,user_birthday,user_location,offline_access,publish_actions' });
}

function setTwitterVotingFlag() {
    document.getElementById("twittervoting").value = "1";
}

function sendFacebookRequest(requestMessage) {
    FB.ui({
        method: 'apprequests',
        message: requestMessage,
        title: 'Send your friends an application request'
    },
    function (response) {
        if (response && response.request_ids) {
            var requests = response.request_ids.join(',');
            document.getElementById("request_ids").value = requests;
            document.forms.frm.submit();
        }
    });
    return false;
}

function targetIsBody(e)
{
    evt = e || window.event;
    if (e.target)
        targ = e.target;
    else
        if (e.srcElement)
            targ = e.srcElement;
    if (targ.nodeType == 3) // Safari bug
        targ = targ.parentNode;

    return (targ.id == 'body' || targ.className == 'w1');
}

function initDropDown() {
    var nav = document.getElementById("nav");
    if (nav) {
        var lis = nav.getElementsByName("SubNavItem");
        for (var i = 0; i < lis.length; i++) {
            if (lis[i].getElementsByTagName("ul").length > 0) {
                lis[i].className += " has-drop-down"
                lis[i].getElementsByTagName("a")[0].className += " has-drop-down-a"
            }
            lis[i].onmouseover = function () {
                this.className += " hover";
            }
            lis[i].onmouseout = function () {
                this.className = this.className.replace(" hover", "");
            }
        }
    }
}

function initPage() {
    initAutoScalingNav({
        menuId: "nav",
        sideClasses: true,
        spacing: 30,
        minPaddings: 8
    });
    initAutoScalingNav({
        menuId: "footer-nav",
        sideClasses: true,
        minPaddings: 6
    });
    initDropDown();
}

function initAutoScalingNav(o) {
    if (!o.menuId) o.menuId = "nav";
    if (!o.tag) o.tag = "a";
    if (!o.spacing) o.spacing = 0;
    if (!o.constant) o.constant = 0;
    if (!o.minPaddings) o.minPaddings = 0;
    if (!o.liHovering) o.liHovering = false;
    if (!o.sideClasses) o.sideClasses = false;
    if (!o.equalLinks) o.equalLinks = false;
    if (!o.flexible) o.flexible = false;
    var nav = document.getElementById(o.menuId);
    if (nav) {
        nav.className += " scaling-active";
        var lis = document.getElementsByName("TopNavItem");
        var asFl = [];
        var lisFl = [];
        var width = 0;
        for (var i = 0, j = 0; i < lis.length; i++) {
            if (lis[i].parentNode == nav) {
                var t = lis[i].getElementsByTagName(o.tag).item(0);
                asFl.push(t);
                asFl[j++].width = t.offsetWidth;
                lisFl.push(lis[i]);
                if (width < t.offsetWidth) width = t.offsetWidth;
            }
            if (o.liHovering) {
                lis[i].onmouseover = function () {
                    this.className += " hover";
                }
                lis[i].onmouseout = function () {
                    this.className = this.className.replace("hover", "");
                }
            }
        }
        var menuWidth = nav.clientWidth - asFl.length * o.spacing - o.constant;
        if (o.equalLinks && width * asFl.length < menuWidth) {
            for (i = 0; i < asFl.length; i++) {
                asFl[i].width = width;
            }
        }
        width = getItemsWidth(asFl);
        if (width < menuWidth) {
            var version = navigator.userAgent.toLowerCase();
            if (o.spacing > 0) {
                for (var i = 0; i < asFl.length; i++) {
                    asFl[i].width += o.spacing;
                }
            }
            if (asFl.length.length > 0) {
                for (i = 0; getItemsWidth(asFl) < menuWidth; i++) {
                    asFl[i].width++;
                    if (!o.flexible) {
                        asFl[i].style.width = asFl[i].width + "px";
                    }
                    if (i >= asFl.length - 1) i = -1;
                }
            }
            if (o.flexible) {
                for (i = 0; i < asFl.length; i++) {
                    width = (asFl[i].width - o.spacing - o.constant / asFl.length) / menuWidth * 100;
                    if (i != asFl.length - 1) {
                        lisFl[i].style.width = width + "%";
                    }
                    else {
                        if (navigator.appName.indexOf("Microsoft Internet Explorer") == -1 || version.indexOf("msie 8") != -1 || version.indexOf("msie 9") != -1)
                            lisFl[i].style.width = width + "%";
                    }
                }
            }
        }
        else if (o.minPaddings > 0) {
            for (i = 0; i < asFl.length; i++) {
                asFl[i].style.paddingLeft = o.minPaddings + "px";
                asFl[i].style.paddingRight = o.minPaddings + "px";
            }
        }
        if (o.sideClasses && lisFl.length > 0) {
            lisFl[0].className += " first-child";
            lisFl[0].getElementsByTagName(o.tag).item(0).className += " first-child-a";
            lisFl[lisFl.length - 1].className += " last-child";
            lisFl[lisFl.length - 1].getElementsByTagName(o.tag).item(0).className += " last-child-a";
        }
        nav.className += " scaling-ready";
    }
    function getItemsWidth(a) {
        var w = 0;
        for (var q = 0; q < a.length; q++) {
            w += a[q].width;
        }
        return w;
    }
}


if (window.addEventListener)
    window.addEventListener("load", initPage, false);
else if (window.attachEvent)
    window.attachEvent("onload", initPage);

captureMouseMoves();
