//menu activation and navigation code
var colors = Array();
colors[0] = "none";
colors[1] = "lblue";
colors[2] = "green";
colors[3] = "yellow";
colors[4] = "red";
colors[5] = "dblue";

function activateMenu(i) {
    var rec;
    if (document.getElementById("record" + colors[i])) {
        rec = document.getElementById("record" + colors[i]);
        pos = parseInt(rec.style.left.substring(0, rec.style.left.length - 2));
        if (pos >= -35) {
            rec.style.left = "-35px";
        }
        document.getElementById("glow").src = "assets/images/glow"+colors[i]+".png"
        if (document.getElementById("area" + colors[i])) {
            document.getElementById("area" + colors[i]).style.display = "block";
        }
    }
}
function deactivateMenu(i) {
    var rec;
    if (document.getElementById("record" + colors[i])) {
        rec = document.getElementById("record" + colors[i]);
        pos = parseInt(rec.style.left.substring(0, rec.style.left.length - 2));
        if (pos <= -20) {
            rec.style.left = "-20px";
            if (document.getElementById("area" + colors[i])) {
                document.getElementById("area" + colors[i]).style.display = "none";
            }
        }
    }
}
function clickMenu(num) {
    for (var i = 0; i < colors.length; i++) {
        deactivateMenu(i);
    }
    activateMenu(num);
}

//General AJAX code
function getstuff(url, DivToPut, callback) {
    var xhr;
    try {
        xhr = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (e) {
        try {
            xhr = new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch (e2) {
            try {
                xhr = new XMLHttpRequest();
            }
            catch (e3) {
                xhr = false;
            }
        }
    }

    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var divdoc = document.createElement('div');
                var txt; 
                if (callback) {
                    txt = callback(xhr.responseText);
                } else {
                    txt = xhr.responseText;
                }
                divdoc.innerHTML = txt;
                if (document.getElementById(DivToPut)) {
                    document.getElementById(DivToPut).replaceChild(divdoc, document.getElementById(DivToPut).childNodes[0]);
                    document.getElementById(DivToPut).innerHTML = document.getElementById(DivToPut).innerHTML;
                }
            }
        }
    };

    xhr.open("GET", url, true);
    xhr.send(null);
}

function poststuff(url, postdata, DivToPut) {
    var xhr;
    try {
        xhr = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (e) {
        try {
            xhr = new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch (e2) {
            try {
                xhr = new XMLHttpRequest();
            }
            catch (e3) {
                xhr = false;
            }
        }
    }

    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                var divdoc = document.createElement('div');
                divdoc.innerHTML = xhr.responseText;
                document.getElementById(DivToPut).replaceChild(divdoc, document.getElementById(DivToPut).childNodes[0]);
                document.getElementById(DivToPut).innerHTML = document.getElementById(DivToPut).innerHTML;
            }
        }
    };
    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("Content-length", postdata.length);
    xhr.setRequestHeader("Connection", "close");
    xhr.send(postdata);
}

function formatPostData(keyarray, valuearray){
	var i;
	var output="";
	for (i=0;i<keyarray.length;i++){
	    if (valuearray[i]) {
	        output = i == 0 ? output + URLEncode(keyarray[i]) + "=" + URLEncode(valuearray[i]) : output + "&" + URLEncode(keyarray[i]) + "=" + URLEncode(valuearray[i]);
	    } else {
	        output = i == 0 ? output + URLEncode(keyarray[i]) + "=" : output + "&" + URLEncode(keyarray[i]) + "=";
	    }
	}
	return output;
}

function URLEncode (clearString) {
	var output = '';
	var x = 0;
	clearString = clearString.toString();
	var regex = /(^[a-zA-Z0-9_.]*)/;
	while (x < clearString.length) {
		var match = regex.exec(clearString.substr(x));
		if (match != null && match.length > 1 && match[1] != '') {
			output += match[1];
			x += match[1].length;
		} else {
				if (clearString[x] == ' '){
					output += '+';
				}else {
					var charCode = clearString.charCodeAt(x);
					var hexVal = charCode.toString(16);
					output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
			}
			x++;
		}
	}
	return output;
}

//Callback functions for AJAX
var identity = function(str) { return str; };

var setDuration = function(str) {
    var duration = parseInt(str);
    if (duration > 0) {
        window.setTimeout("getCurrent();", duration * 1000)
    } else {
        window.setTimeout("getCurrent();", 25000)
    }
    return str;
};

var trunc = function(str) { return str.substring(55, str.length - "</body></html>".length - 2); };

var getalldata = function(str) {
    var strArr = new Array();
    var playingnow = "";
    strArr = str.split("{{/now}}");
    playingnow = strArr[0].replace("{{now}}", "");
    var w = 400;
    var h = 400;
    var previousStuff;
    previousStuff = document.getElementById("nowplaying").childNodes[0].innerHTML;
    updatePlayerData(strArr[1]);
    if (playingnow.indexOf("**live**") == -1) {
        document.getElementById("onair").src = "assets/images/onair.gif";
        document.getElementById("onair").onclick = function() { dedication('http://www.discomaniaradio.com/playlist.php?limit=25', '', '') };
    } else if (playingnow.indexOf("Samios") != -1) {
        //Sam+Saint
        if (playingnow.indexOf("Apostolidis") != -1) {
            document.getElementById("onair").src = "assets/images/producers/samios-saint.gif";
            document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/samios.html", w, h) };
            //Sam alone
        } else {
            document.getElementById("onair").src = "assets/images/producers/samios-alone.gif";
            document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/samios.html", w, h) };
        }
    } else if (playingnow.indexOf("Apostolidis") != -1) {
        //Saint alone
        document.getElementById("onair").src = "assets/images/producers/apostolidis.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/saint.html", w, h) };
    } else if (playingnow.indexOf("Zoumpos") != -1) {
        //Zoumpos alone
        document.getElementById("onair").src = "assets/images/producers/zoumpos.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/zoumpos.html", w, h) };
    } else if (playingnow.indexOf("Anagnostopoulos") != -1) {
        //Anagnostopoulos alone
        document.getElementById("onair").src = "assets/images/producers/anagnostopoulos.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/anagnostopoulos.html", w, h) };
} else if (playingnow.indexOf("Staikos") != -1) {
        //Staikos alone
        document.getElementById("onair").src = "assets/images/producers/staikos.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/staikos.html", w, h) };
    } else if (playingnow.indexOf("Fotiou") != -1) {
        //Fotiou alone
        document.getElementById("onair").src = "assets/images/producers/fotiou.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/fotiou.html", w, h) };
    } else if (playingnow.indexOf("Kostantinos") != -1) {
        //Konstantinos alone
        document.getElementById("onair").src = "assets/images/producers/Konstantinos.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/konstantinos.html", w, h) };
    } else if (playingnow.indexOf("Konstantinos") != -1) {
        //Konstantinos alone
        document.getElementById("onair").src = "assets/images/producers/Konstantinos.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/konstantinos.html", w, h) };
    } else if (playingnow.indexOf("Megalive") != -1) {
        //Megalive
        document.getElementById("onair").src = "assets/images/producers/megalive.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/samios.html", w, h) };
    } else if (playingnow.indexOf("Maxx") != -1) {
        //DJ Maxx
        document.getElementById("onair").src = "assets/images/producers/maxx.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/maxx.html", w, h) };
    } else if (playingnow.indexOf("Sagnier") != -1) {
        //Sagnier
        document.getElementById("onair").src = "assets/images/producers/sagnier.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/sagnier.html", w, h) };
    } else if (playingnow.indexOf("Alexiou") != -1) {
        //Alexiou
        document.getElementById("onair").src = "assets/images/producers/alexiou.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/alexiou.html", w, h) };
    } else if (playingnow.indexOf("Syriopoulou") != -1) {
        //Syriopoulou
        document.getElementById("onair").src = "assets/images/producers/syriopoulou.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/syriopoulou.html", w, h) };
    } else if (playingnow.indexOf("Natasha") != -1) {
        //Natasha
        document.getElementById("onair").src = "assets/images/producers/natasha.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/natasha.html", w, h) }; 
	} else if (playingnow.indexOf("Jack") != -1) {
        //Jack
        document.getElementById("onair").src = "assets/images/producers/jack.gif";
        document.getElementById("onair").onclick = function() { dedication("http://www.discomaniaradio.com/contact/jack.html", w, h) }; 
 } else {
        //Unknown live broadcast
        document.getElementById("onair").src = "assets/images/onair.gif";
        document.getElementById("onair").onclick = function() { };
    }
    return playingnow;
}

//Content pane filler functions
function getCurrent() {
	d=new Date();
	getstuff("totalupdate.php?t=" + d.getTime(), "nowplaying", getalldata);
}

function updatePlayerData(str) {
    var strArray = new Array();
    strArray = str.split("{{/queue}}");
    document.getElementById("comingupnext").childNodes[0].innerHTML = strArray[0].replace("{{queue}}", "");
    strArray = strArray[1].split("{{/previous}}");
    document.getElementById("previouslyplayed").childNodes[0].innerHTML = strArray[0].replace("{{previous}}", "");
    strArray = strArray[1].split("{{/dedication}}");
    document.getElementById("dedication").childNodes[0].innerHTML = strArray[0].replace("{{dedication}}", "");
    strArray = strArray[1].split("{{/duration}}");
    document.getElementById("duration").childNodes[0].innerHTML = strArray[0].replace("{{duration}}", "");
    var duration = strArray[0].replace("{{duration}}", "");
    strArray = strArray[1].split("{{/topten7}}");
    document.getElementById("Week").childNodes[0].innerHTML = strArray[0].replace("{{topten7}}", "");
    strArray = strArray[1].split("{{/topten30}}");
    document.getElementById("Month").childNodes[0].innerHTML = strArray[0].replace("{{topten30}}", "");
    window.setTimeout("getCurrent();", duration * 500);
}

function ContactForm() {
    var keys = Array();
    var vals = Array();
    keys[0] = "name";
    keys[1] = "email";
    keys[2] = "message";
    for (var i = 0; i < keys.length; i++) {
        vals[i] = document.getElementById(keys[i]).value
    }
    poststuff("formmail.php", formatPostData(keys, vals), "ContactResult");
}

function clearContactForm(formId) {
    document.getElementById(formId).reset();
    div1 = document.createElement("div");
    document.getElementById("ContactResult").replaceChild(div1, document.getElementById("ContactResult").childNodes[0]);
}

function submitForm(posturl) {
    var keys = Array();
    var vals = Array();
    keys[0] = "name";
    keys[1] = "email";
    keys[2] = "comments";
    keys[3] = "captcha_code";
    for (var i = 0; i < keys.length; i++) {
        vals[i] = document.getElementById("contact"+keys[i]).value;
    }
    poststuff(posturl, formatPostData(keys, vals), "ContactResult");
}

function loadLinks() {
    getstuff("links.html", "Links", trunc);
}

//Banner rotation code
function Banner(objName) {
    this.obj = objName;
    this.aNodes = [];
    this.currentBanner = 0;
}

Banner.prototype.add = function(bannerType, bannerPath, bannerDuration, height, width, hyperlink, target, popupWidth, popupHeight) {
    this.aNodes[this.aNodes.length] = new Node(this.obj + "_" + this.aNodes.length, bannerType, bannerPath, bannerDuration, height, width, hyperlink, target, popupWidth, popupHeight);
};

function Node(name, bannerType, bannerPath, bannerDuration, height, width, hyperlink, target, popupWidth, popupHeight) {
    this.name = name;
    this.bannerType = bannerType;
    this.bannerPath = bannerPath;
    this.bannerDuration = bannerDuration;
    this.height = height
    this.width = width;
    this.hyperlink = hyperlink;
    this.target = target;
    this.popupWidth = popupWidth;
    this.popupHeight = popupHeight;
}

Banner.prototype.prestart = function(elementToPut) {
    var div, span, a, obj1, objchild;
    div = document.createElement("div");
    for (var iCtr = 0; iCtr < this.aNodes.length; iCtr++) {
        span = document.createElement("span");
        span.setAttribute("id", this.aNodes[iCtr].name);
        span.setAttribute("class", "BannerOff");
        span.setAttribute("style", "background-color:#000000;text-align:center");
        if (this.aNodes[iCtr].hyperlink != "") {
            a = document.createElement("a");
            a.setAttribute("href", this.aNodes[iCtr].hyperlink);
            if (this.aNodes[iCtr].target == "" || this.aNodes[iCtr].target == "NEWWINDOW") {
                a.setAttribute("target", "_blank");
            } else if (this.aNodes[iCtr].target == "SAMEWINDOW") {
                a.setAttribute("target", "_self");
            } else if (this.aNodes[iCtr].target == "POPUP") {
                a.setAttribute("href", "javascript:dedication('" + this.aNodes[iCtr].hyperlink + "','" + this.aNodes[iCtr].popupWidth + "','" + this.aNodes[iCtr].popupHeight + "')");
            } else {
                a.setAttribute("target", "_blank");
            }
        }
        if (this.aNodes[iCtr].bannerType == "FLASH") {
            obj1 = document.createElement("object");
            obj1.setAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
            obj1.setAttribute("codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0");
            obj1.setAttribute("width", this.aNodes[iCtr].width);
            obj1.setAttribute("height", this.aNodes[iCtr].height);
            obj1.setAttribute("id", "bnr_" + this.aNodes[iCtr].name);
            obj1.setAttribute("align", "");
            obj1.setAttribute("viewastext", "true");
            objchild = document.createElement("param");
            objchild.setAttribute("name", "movie");
            objchild.setAttribute("value", this.aNodes[iCtr].bannerPath);
            obj1.appendChild(objchild);
            objchild = document.createElement("param");
            objchild.setAttribute("name", "quality");
            objchild.setAttribute("value", "high");
            obj1.appendChild(objchild);
            objchild = document.createElement("param");
            objchild.setAttribute("name", "bgcolor");
            objchild.setAttribute("value", "#000000");
            obj1.appendChild(objchild);
            objchild = document.createElement("embed");
            objchild.setAttribute("src", this.aNodes[iCtr].bannerPath);
            objchild.setAttribute("quality", "high");
            objchild.setAttribute("width", this.aNodes[iCtr].width);
            objchild.setAttribute("height", this.aNodes[iCtr].height);
            objchild.setAttribute("name", "bnr_" + this.aNodes[iCtr].name);
            objchild.setAttribute("align", "center");
            objchild.setAttribute("type", "application/x-shockwave-flash");
            objchild.setAttribute("pluginspage", "http://www.macromedia.com/go/getflashplayer");
            obj1.appendChild(objchild);
        } else if (this.aNodes[iCtr].bannerType == "IMAGE") {
            obj1 = document.createElement("img");
            obj1.setAttribute("src", this.aNodes[iCtr].bannerPath);
            obj1.setAttribute("border", "0");
            obj1.setAttribute("height", this.aNodes[iCtr].height);
            obj1.setAttribute("width", this.aNodes[iCtr].width);
            obj1.setAttribute("alt", "");
        }
        if (this.aNodes[iCtr].hyperlink != "") {
            a.appendChild(obj1);
            span.appendChild(a);
        } else {
            span.appendChild(obj1);
        }
        div.appendChild(span);
    }
    document.getElementById(elementToPut).innerHTML = div.innerHTML;
};

Banner.prototype.start = function() {
    this.changeBanner();
    var thisBannerObj = this.obj;
    // CURRENT BANNER IS ALREADY INCREMENTED IN changeBanner() FUNCTION
    setTimeout(thisBannerObj + ".start()", this.aNodes[this.currentBanner].bannerDuration * 1000);
};

Banner.prototype.changeBanner = function() {
    var thisBanner;
    var prevBanner = -1;
    if (this.currentBanner < this.aNodes.length) {
        thisBanner = this.currentBanner;
        if (this.aNodes.length > 1) {
            if (thisBanner > 0) {
                prevBanner = thisBanner - 1;
            } else {
                prevBanner = this.aNodes.length - 1;
            }
        }
        if (this.currentBanner < this.aNodes.length - 1) {
            this.currentBanner = this.currentBanner + 1;
        } else {
            this.currentBanner = 0;
        }
    }


    if (prevBanner >= 0) {
        document.getElementById(this.aNodes[prevBanner].name).className = "BannerOff";
    }
    document.getElementById(this.aNodes[thisBanner].name).className = "BannerOn";
};

Banner.prototype.populate = function(BannerArray) {
    for (var i = 0; i < BannerArray.length; i++) {
        this.add(BannerArray[i][0], BannerArray[i][1], BannerArray[i][2], BannerArray[i][3], BannerArray[i][4], BannerArray[i][5], BannerArray[i][6], BannerArray[i][7], BannerArray[i][8]);
    }
};

//dedication javascript code
function dedication(URL, width, height) {
    if (width == "") {
        width = "590";
    }
    if (height == "") {
        height = "600";
    }
    var day = new Date();
    var id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=" + width + ",height=" + height + ",left = 340,top = 212');");
}
