// JavaScript Document

function listentoall(){
	this.podcontainer = $("#content > .pods");
	this.pod_list = $("#history-list");
	this.input = null;
	this.nowplaying = $("#nowplaying"); 
	this.playinfo = $("#playinfo");
	this.videopod = $("#videopod");
	this.videolist = $("#video-list");
	this.playerpod = $("#videobox");
	this.tabs = $("#tabs");
	this.infobox = $("#infobox > div:first");
	this.size = "normal";
	this.requeue = new requestsQueue();
	this.beforeReq = [];
	this.requests = [];

	this.search = {
		"s": ""
	}
	this.hash = window.location.hash;
	this.timer = {};
	this.barsVisible = false;
	this.leftVisible = false;
	this.messageDisplay = $("#head-logo");
	
	this.loaded = {
		"page": false,
		"video": false,
		"lyrics": false, 
		"infos": false,
		"listen": false,
		"request": 0,
		"translator": false,
		"pl_check": false
	}
		

	this.addPod = function(p){
		if(!p) p = {};
		p.owner = this;
		var t = new pod(p);
		if(!p.noindex) {
			var h = t.headline();
			this.pod_list.append("<a title='"+h.replace(/\'/, "")+"' id='index-"+t.id+"' href=\"?a=show-"+t.id+"\">"+h+"</a>");
		}
		t.show(p);
		return t.id;
	}
	
	this.deletePod = function(id, action){
		pods[id].deleted = true;
		if(pods[id].pre) pods[id].gotoPre();
		$("#removed_pods").append($("#" + id));
		$("#index-"+id).hide();
		var deleted = $(".pod", "#removed_pods").length;
	}
	this.removePod = function(id){
		lastrequests[pods[id].lastrequest] = null;
		pods[id] = null;
		$("#index-"+id).hide();
		$("#"+id).remove();
	}
	this.currentPod;
	
	this.showPod = function(pod, mode){
		var p = {"nohash": mode == "nohash"};
		if(!this.currentPod) {
			this.currentPod = pod;
			pods[this.currentPod].show(p);
			return;
		}
		if(this.currentPod == pod) return;
		var pre = this.currentPod;
		this.currentPod = pod;
		if(!pods[pod]) return;
		if(!mode){
			if(pods[pod].pre && pods[pod].next){
				pods[pods[pod].pre].setNext(pods[pod].next);
				pods[pods[pod].next].setPre(pods[pod].pre);
			}
			pods[pod].setPre(pre);
			pods[pre].setNext(pod);
			pods[pod].setNext(false);
		}
		pods[this.currentPod].show(p);
		pod = pod.split("_");
		if(pod[0].search(/lta|user|playlist/) == -1 && pod[1] == "start") lta.search.s = pod[0];
	}
	
	this.next = function(){
		var pod = pods[this.currentPod];
		if(pod.next) {
			this.showPod(pod.next, "next");
		}
	}
	this.pre = function(){
		var pod = pods[this.currentPod];
		if(pod.pre) {
			this.showPod(pod.pre, "pre");
		}
	}
	
	
	this.unload = function(){
		var p = {
			"a":"unload",
			"nopod": 1
		}
		requestP(p);
		return 0;
	}
	
	this.showBars = function(){
		$("#down-nav").show();
		$("#tabs").show();
		this.showLeft();
		this.messageDisplay = $(".message-display", "#tabs");
	}
	this.hideLeft = function(){
		$("#left").hide();
		$("#content").css("width", "100%");
		this.leftVisible = false;
		this.barsVisible = false;
		setSizes();
	}
	this.showLeft = function(){
		$("#left").show();
		this.leftVisible = true;
		setSizes();
	}
	this.loggedin = function(p){
		$("#loginbox").hide();
		$("#login-password").val("");
		$("#login-name").val("");
		$("#user-btn").show();
		loge(p.user);
		user = eval(p.user);
		$("a:first,span:first", '#user-btn').html(user.uname);
		var o = linkP("?a=show&b=start&m=user&pod=user_start");
		requestP(o);
	}
	this.loggedout = function(p){
		reload_page();
	}
	
	this.addOwn = function(){
		var p = {};
		p.a = "add-own";
		p.m = "track";
		p.track = trim($("#own-track").val());
		p.artist = trim($("#own-artist").val());
		if(p.track == "" || p.artist == "") return;
		requestP(p);
		dialogs.close("ownAdd");
	}
	this.tips = function(){
		this.next = function(){
		}
		this.pre = function(){
		}
		this.cont = $("#tips");
	}
}

function createPlaylist(name, pid){

}

/***************************PODS****************************************************/
/***************************PODS****************************************************/
/***************************PODS****************************************************/
/***************************PODS****************************************************/

function pod(p){
	this.owner = lta;
	if(!p) p = {};
	if(!p.podid) p.podid = generateID();
	
	this.id = p.podid;
	this.container = $("#pods");	
	var check = $("#"+this.id);
	this.setAttr = function (p){for(i in p) this.self.attr(i, p[i]);}
	this.setPre = function(pod){this.pre = pod;}
	this.setNext = function (pod){this.next = pod;}
	this.gotoPre = function(){lta.pre();}
	this.gotoNext = function(){lta.next();}
	this.repair = function(change){this.deleted = false;}
	
	this.load = function(){
		$(".pod-loading", this.self).show();
		$(".sub", this.self).hide();
		this.state("loading");
	}
	this.unload = function (){
		$(".pod-loading", this.self).hide();
		$(".sub", this.self).show();
		this.state("ok");
	}
	this.show = function(p){
		$(".pod").hide();
		if(this.deleted) this.self.prependTo("#pods");
		$("#index-"+this.id).show();
		this.self.show();
		if(p && p.nohash) return;
		hash_change(this.self.attr("href"));
	}
	this.lastrequest = "";
	this.deleteIt = function(action){
		this.owner.deletePod(this.id, action);
	}

	this.pre = p.pre;
	this.next = p.next;
	this.buttons = function(b){
		return this.self.find("."+b+"_pod");
	}

	this.headline = function(val){
		if(val) { 
			$(".headline > span", this.self).html(val);
			$("#index-"+this.id).html(val);
			val = $("#index-"+this.id).text();
			$("#index-"+this.id).attr("title", val);
		} else
		val = $(".headline > span", this.self).text();
		//if(val == null) this.deleteIt();
		if(!val) val = "";
		return val;
	}
	this.refresh = function(){
		var href = decodeHTML2(this.self.attr("href"));
		var p = linkP(href.replace(/%252526/, "%26"));
		p.sub = 0;
		p.pod = this.id;
		p.o = "refresh";
		p.headline = this.headline();
		this.can("refresh", 0);
		this.load();
		requestP(p);
	}
	this.state = function(val){if(val) this.pstate = val; return this.pstate;}
	this.pstate = "loading";
	
	this.can = function(w, value){
		if(value != undefined)
		switch(w){
			case "close": case "delete":
				if(value) this.buttons("close").show(); else this.buttons("close").hide(); 
			break;
			case "refresh":
				if(value) this.buttons("refresh").show(); else this.buttons("refresh").hide(); 
			break;
		}
		return value;
	}

	this.addSub = function(){
		var id = generateID();
		this.sub.append("<div id=\""+id+"\"></div>");
		return this.sub;
	}
	this.deleted = false;
	this.sub = function(){
		return $("#"+this.id+" > .sub:first");
	}
	
	//alert(this.id);
	//Falls der Pod an sich schon vorhanden is...
	if(check.length == 1) {
		this.self = check;
		var lr = this.self.attr("lastrequest");
		if(lr) { 
			requests.push(lr);
			this.lastrequest = lr;
			var q = new Object();
			q.pod = this.id;
			//alert(lr + " - " + q.pod);
			lastrequests[lr] = q; 
		} 
		pods[this.id] = this;
		return;
	}
	this.self = $("#pod_html > .pod:first").clone(true);
	
	this.self.attr("id", this.id);
	pods[this.id] = this;
	this.container.prepend(this.self); 
}



var user_is_active = 0;
function userActive(val){
	if(lta.loaded.page && val) user_is_active = val; 
	return user_is_active;
}
function appInterval(){
	if(userActive()) {
		userActive(0);
	}
}









/**
 * Converts the given data structure to a JSON string.
 * Argument: arr - The data structure that must be converted to JSON
 * Example: var json_string = array2json(['e', {pluribus: 'unum'}]);
 * 			var json = array2json({"success":"Sweet","failure":false,"empty_array":[],"numbers":[1,2,3],"info":{"name":"Binny","site":"http:\/\/www.openjs.com\/"}});
 * http://www.openjs.com/scripts/data/json_encode.php
 */
function array2json(arr) {
    var parts = [];
    var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');

    for(var key in arr) {
    	var value = arr[key];
        if(typeof value == "object") { //Custom handling for arrays
            if(is_list) parts.push(array2json(value)); /* :RECURSION: */
            else parts[key] = array2json(value); /* :RECURSION: */
        } else {
            var str = "";
            if(!is_list) str = '"' + key + '":';

            //Custom handling for multiple data types
            if(typeof value == "number") str += value; //Numbers
            else if(value === false) str += 'false'; //The booleans
            else if(value === true) str += 'true';
            else str += '"' + value + '"'; //All other things
            // :TODO: Is there any more datatype we should be in the lookout for? (Functions?)

            parts.push(str);
        }
    }
    var json = parts.join(",");
    
    if(is_list) return '[' + json + ']';//Return numerical JSON
    return '{' + json + '}';//Return associative JSON
}


