var searchThrobber_display = true;
var last_div_channel = 'welcome';
var tellIsLoaded = false;
var channelListingIsLoaded = false;

function showLayerHideOthers(prefix, channelLayerToShow) {
    // hides all div_channel_* layers except the one named
    layerToShow = prefix + channelLayerToShow;
    allDivs = document.getElementsByTagName('div');
    for (var i=0; i<=allDivs.length; i++) {
        if (allDivs.item(i)) {
            if (allDivs.item(i).id.indexOf(prefix) > -1) {
                if (allDivs.item(i).id.indexOf(layerToShow) <= -1) {
                    // hide
                    allDivs.item(i).style.visibility = "hidden";
                }
            }
        }
    }
    if (document.getElementById(layerToShow)) document.getElementById(layerToShow).style.visibility = "visible";
    if (prefix.indexOf("div_channel_") > -1 && channelLayerToShow.indexOf("null") <= -1) last_div_channel = channelLayerToShow;
}

function colorTdUncolorOthers(channelTdToColor) {
    // colors one td background, uncolors the rest
    tdToColor = "td_channel_" + channelTdToColor;
    allTds = document.getElementsByTagName('td');
    for (var i=0; i<=allTds.length; i++) {
        if (allTds.item(i)) {
            if (allTds.item(i).id.indexOf("td_channel_") > -1) {
                if (allTds.item(i).id.indexOf(tdToColor) <= -1) {
                    allTds.item(i).style.backgroundColor = "white";
                }
            }
        }
    }
    document.getElementById(tdToColor).style.backgroundColor = "#E8E8E8";
}

function colorTabUncolorOthers(tabToColor) {
    thisTabToColor = "td_tab_" + tabToColor;
    allTds = document.getElementsByTagName('td');
    for (var i=0; i<=allTds.length; i++) {
        if (allTds.item(i)) {
            if (allTds.item(i).id.indexOf("td_tab_") > -1) {
                if (allTds.item(i).id.indexOf(thisTabToColor) <= -1) {
                    allTds.item(i).style.background = "url('/static/images/tabs/tab-background.gif')";
                }
            }
        }
    }
    document.getElementById(thisTabToColor).style.background = "url('/static/images/tabs/tab-foreground.gif')"
}

function ahah(url,target,postargs) {
    var req;
    var sendtype = "GET";
    var sendstring = "";
    if (postargs) {
        sendtype = "POST";
        for (var i in postargs) {
            sendstring += i + "=" + postargs[i] + "&";
        }
    }
    document.getElementById(target).innerHTML = '<span class="style8">Fetching...</span>';
    //searchThrobber_display = true;
    //searchThrobber(target);
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {ahahDone(req, target);};
        req.open(sendtype, url, true);
        if (postargs) {
            req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            req.send(sendstring);
        } else {
            req.send(null);
        }
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() {ahahDone(req, target);};
            req.open(sendtype, url, true);
            if (postargs) {
                req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                req.send(sendstring);
            } else {
                req.send();
            }
        }
    }
}  

function ahahDone(thisreq, target, delay, method, parameters) {
    if (thisreq.readyState == 4) {
        //searchThrobber_display = false;
        if (thisreq.status == 200) {
            document.getElementById(target).innerHTML = thisreq.responseText;
        } else {
            document.getElementById(target).innerHTML="ahah error:\n"+thisreq.statusText;
        }
    }
}

function getChannelListing(channel) {
	var thischannel = (channel == null) ? 'aaa' : channel;
	if (!channelListingIsLoaded) {
		document.getElementById("div_channel_welcome").style.visibility = 'hidden';
		channelListingIsLoaded = true;
	}
	document.getElementById("div_channel_listing").style.visibility = "visible";
	ahah('/app/chlist?channel=' + escape(channel), 'div_channel_listing');
}

function getTell() {
    if (!(tellIsLoaded)) { 
        ahah('/app/tell', 'span_tab_tell');
        tellIsLoaded = true;
    }
}

function postTell() {
    var form_tell = document.getElementById("form_tell");
    // build a postargs object from whatever's in form_tell
    var postargs = new Object();
    for (var i = 0; i < form_tell.length; i++) {
        postargs[form_tell[i].name] = form_tell[i].value;
    }
    // make request
    ahah('/app/tell', 'span_tab_tell', postargs);
    tellIsLoaded = false;
}

function postFeedback() {
    var form_feedback = document.getElementById("form_feedback");
    var postargs = new Object();
    for (var i = 0; i < form_feedback.length; i++) {
        postargs[form_feedback[i].name] = form_feedback[i].value;
    }
    // make request
    ahah('/app/feedback', 'span_tab_messages', postargs);
}

function searchThrobber(elemToUpdate, tick) {
    if (searchThrobber_display) {
        var c = (tick == null) ? 1 : tick;
        s = '<span class="style2">Searching';
        for (var i=0; i<=c; i++) {
            s += ".";
        }
        s += '</span>';
        document.getElementById(elemToUpdate).innerHTML = s;
        if (c > 9) c = 1;
        else c += 1;
        setTimeout("searchThrobber('"+elemToUpdate+"', '"+c+"')", 1000);
    }
}

function getSearchResults(whichElem) {
    var elemWithValue = (whichElem == null) ? 'searchterm' : whichElem;
    var searchterm = document.getElementById(elemWithValue).value;
    if (elemWithValue == 'searchterm') document.getElementById("searchterm2").value = searchterm;
    if (searchterm) {
        colorTabUncolorOthers('search');
        showLayerHideOthers('div_tab_', 'search');
        showLayerHideOthers('div_channel_', 'null');
        //document.getElementById("span_search_or_welcome").innerHTML = "Results for " + searchterm + ":";
        document.getElementById("div_tab_search").style.backgroundColor = "#E8E8E8";
        
        ahah('/app/search?searchterm=' + escape(searchterm), 'div_search_results');
        ahah('/app/search_group?searchterm=' + escape(searchterm), 'div_search_group');
    }
}

function searchAgain(searchterm) {
	document.getElementById("searchterm2").value = searchterm;
	getSearchResults('searchterm2');
}

function loadTab() {
    document.getElementById("td_tab_browse").style.background = 'url(/static/images/tabs/tab-foreground.gif)';
}

function addBookmark() {
	if (window.external) window.external.AddFavorite("http://www.accutunes.com","AccuTunes");
}

function setHomepage() {
	if (window.external) {
		this.style.behavior='url(#default#homepage)';
		this.setHomePage('http://www.accutunes.com');
	}
}

function firefoxVersionHelper() {
	if (navigator.userAgent) {
		if (navigator.userAgent.indexOf("Firefox") > -1) {
			// It's Firefox
			var ua_split = navigator.userAgent.split(" ");
			var version = (ua_split[ua_split.length-1].split("/"))[1]
			var v = parseInt(version);
			var ff = "You want the plugin for Firefox 1.5";
			if (v < 1.5 && v > 1.0) ff = "You want the plugin for Firefox " + String(v);
			else if (v < 1.0) ff = "You want the plugin for \"older browsers\".";
			document.write(ff);
		}
	}
}