var sb;

$(document).ready(function() {	
	var shoutboxshow;
	shoutboxshow = $.cookie('shoutbox-show');
	if(shoutboxshow != 0 && shoutboxshow != 1) {
			shoutboxshow = 1;
	}
	if(shoutboxdeactive == "1") { 
		shoutboxshow = 0;
	}
	if(shoutboxshow == 1) {
			active_shoutbox();
	}
	$("#editpanel").click(function () {
		$("#edit-buttons").toggle();
		return false;
	});  
	$("#shoutbox-showbutton").click(function () {
	  if(shoutboxshow == 1) {
		  	$("#shoutbox-showbutton").text("pokaż shoutbox");
            $("#top-shoutbox").fadeOut("slow");
	  		shoutboxshow = 0;
			$.cookie('shoutbox-show', 0, { expires: 9999999, path: '/' });	
			for(i in sb)
  			{
				delete sb[i];
  			}
			delete sb;
	  }
	  else {
		  	$("#shoutbox-showbutton").text("ukryj shoutbox");
		  	$("#top-shoutbox").fadeIn("slow");
	  		shoutboxshow = 1;
			$.cookie('shoutbox-show', 1, { expires: 9999999, path: '/' });	
			active_shoutbox(true);
	  }
	  
    });    
});

var shoutbox_actived = 0;

function active_shoutbox(always) {
	if(shoutbox_actived == 0 || always == true) {
		//if($.cookie('shoutbox-window') == cfg_window || !$.cookie('shoutbox-window')) {
			delete sb;
			sb = new ShoutBox('sb', cfg_refresh_time,cfg_link,cfg_type,cfg_window);
			sb.getMessages();
			shoutbox_actived = 1;
			var ddate = new Date();
			ddate.setTime(ddate.getTime() + (40 * 1000));
			$.cookie('shoutbox-window', cfg_window, { expires: ddate, path: '/' });	
		//}
		//else {
		//	$("#sb_body").text("Shoutbox moża uruchomić tylko w jednej zakładce");	
		//}
	}
}


function add_to_playlist(id) {
	var addtoplaylistmode = $.cookie('addtoplaylistmode');
	var addtoplaylistmodedate = $.cookie('addtoplaylistmodedate');
	addtoplaylistmodedate = parseInt(addtoplaylistmodedate) +10800;
	var today = new Date();
	if(!(addtoplaylistmode > 0) || ((addtoplaylistmodedate)<(today.getTime()/1000)) ) {
		addtoplaylistmode = 0;	
	}
	if(addtoplaylistmode != 0) {
		$.get("addtoplaylist,"+id+".html", function(data){
  			alert(data);
		});
	}
	else {
		var playlist = $.cookie('playlist');
		var array_playlist = new Array();
		if(playlist != null) {
			array_playlist = playlist.split('.');
			if(array_playlist.length >= 50) {
				alert('Nie możesz dodać więcej utworów do playlisty');	
				return;
			}
			for(var j = 0; j < array_playlist.length; j++) {
				if(array_playlist[j] == id) {
					alert('Ten utwór jest juz na playliście');	
					return;
				}
			}
		}
		array_playlist.push(id);
		var string_playlist = "";
		for(var j = 0; j < array_playlist.length; j++) {
			//if(array_playlist[j] > 0) {
				string_playlist = string_playlist + array_playlist[j] + '.';
			//}
		}
		string_playlist = string_playlist.substring(0,string_playlist.length-1);
		$.cookie('playlist', string_playlist, { expires: 9999999, path: '/' });	
		//show_playlist(1);
		//location.reload();
		alert("Dodano utwór do tymczasowej playlisty");
	}
}

function del_from_playlist(id) {
	var playlist = $.cookie('playlist');
	var array_playlist = new Array();
	array_playlist = playlist.split('.');
	var array_playlist2 = new Array();
	for(var j = 0; j < array_playlist.length; j++) {
		if(array_playlist[j] != id) {
			array_playlist2.push(array_playlist[j]);
		}
	}
	var string_playlist = "";
	for(var j = 0; j < array_playlist2.length; j++) {
		string_playlist = string_playlist + array_playlist2[j] + '.';
		
	}
	string_playlist = string_playlist.substring(0,string_playlist.length-1);
	$.cookie('playlist', string_playlist, { expires: 9999999, path: '/' });	
	location.reload();
}

function random_text() {
	var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
    var random = '';
	
	for(var i = 0; i < 10; i++) {
		var rand_no = Math.ceil(chars.length*Math.random());
		random = random + chars.substr(rand_no, 1);
	}
	return random;
}
function addtoplaylistmode(id, title) {
	var date = new Date();
    date.setTime(date.getTime() + (10800 * 1000));
	$.cookie('addtoplaylistmode', id, { expires: date, path: '/' });	
	var today = new Date();
	$.cookie('addtoplaylistmodedate', (today.getTime()/1000), { expires: date, path: '/' });	
	alert('Od teraz dodajesz utwory do playlisty "'+title+'".');
}
function edit(id, mode) {
	var playlist = $.cookie('playlist');
	var tracks = new Array();
	tracks = playlist.split('.');
	
	var pos = 0;
	var found = 0;
	for(var i = 0; i < tracks.length; i++) {
		if(tracks[i] == id) {
			pos = i;
			found = 1;
			break;
		}
	}
	
	var ok = 0;
	if(found == 1) {
		var a = tracks[pos];
		if(mode == 0) {
			if(pos+1 <= tracks.length-1) {
				tracks[pos] = tracks[pos+1];
				tracks[pos+1] = a;
				ok = 1;
			}
		}
		else {
			if(pos-1 >= 0) {
				tracks[pos] = tracks[pos-1];
				tracks[pos-1] = a;
				ok = 1;
			}
		}
	}
	if(ok == 1) {
		var string_playlist = "";
		for(var j = 0; j < tracks.length; j++) {
			string_playlist = string_playlist + tracks[j] + '.';
			
		}
		string_playlist = string_playlist.substring(0,string_playlist.length-1);
		$.cookie('playlist', string_playlist, { expires: 9999999, path: '/' });	
		location.reload();
	}
	else {
		return false;
	}
}

function clearplaylist() {
	var r=confirm("Czy napewno chcesz wyczyścić playlistę tymczasową?");
	if (r == true)
	{
		$.cookie('playlist', '', { expires: 9999999, path: '/' });	
		location.reload();
	}
	else {
		return false;	
	}
}

function add_to_favorite(id) {
	$.get("addtofavorite,"+id+".html", function(data){
  		alert(data);
	});
}