
/** The player buttons in normal state **/
play_buttons = new Image;

/** The player buttons in paused state **/
pause_buttons = new Image;

/** The player buttons in buffered state **/
buffering_buttons = new Image;

/** The image file sources **/
pause_buttons.src = "images/paused.jpg";
buffering_buttons.src = "images/buffering.jpg";
play_buttons.src = "images/buttons.jpg";

/** A reference to the window that plays the full screen video **/
var fullscreen_window = null;

var click_source = "user";

function save_current_playlist() {
	
	var table = get("playlist");
	var row = null;
	
	var data = "";
	
	for(var i = 0; i < table.rows.length; i++) {
		
		row = table.rows[i];
		
		var id = parseInt(row.getAttribute("id"));
		
		if(isNaN(id)) { 	
			continue;
		}
		
		data += id; 
		if(i < table.rows.length - 1) {
			data += "x";
		}
	}
	
	create_cookie("cookie_playlist", data);
}

function update_playlist() {
	
	var table = get("playlist");
	
	for(var i = 0; i < table.rows.length; i++) {
    	table.rows[i].style.background = i % 2 == 1 ? "#EDF5FA" : "#FFFFFF";
    }
    	
    var current_identifier = read_cookie("cookie_identifier");
    if(current_identifier) {
    	
    	var row = get(current_identifier + "_playlist_item_row");
    	if(row) {
    		row.style.background = "#6CFF6C";
    	}
    }
    
    save_current_playlist();
}

function initialize_playlist() {
	
	var l = window.location.href;
	
	/** Only do the following if we are on the main page or the mediaplayer page **/
	if(l.lastIndexOf("mediaplayer") != -1 || l.lastIndexOf("/") == l.length - 1) {
	
		var table = get("playlist");
		var tableDnD = new TableDnD();
		
		tableDnD.init(table);
	
		tableDnD.onDrop = function(table, row) {
    		update_playlist();
		};
	
		update_playlist();
	
		click_current();
	}
}

/** Opens a new popup full screen window **/
function create_fullscreen(time_elapsed) {
	
	var resizable = "fullscreen=yes,resizable=no,";
	var p = "top=0,left=0,screenX=0,screenY=0,";
	var bars = "toolbar=no,titlebar=no,menubar=no,scrollbars=no,status=no,location=no,directories=no";
	var dim = "width=" + screen.width + ",height=" + screen.height; 
	
	create_cookie("cookie_time_elapsed", time_elapsed);
	
	var url = "fullscreen_player.php";
	
	fullscreen_window = window.open(url, "fullscreen", resizable + bars + p + dim);
	
	if(fullscreen_window) {
		fullscreen_window.focus();
	} else {
		window.alert("Please disable your popup blocker and refresh the page.");
	}
}

function reset_time_elapsed() {
	create_cookie("cookie_time_elapsed", "0");
}

/** Plays a video **/
function play(node, artist, title, id, index, source, duration, ctype) {
	
	var time_elapsed = read_cookie("cookie_time_elapsed");
	if(!time_elapsed) {
		time_elapsed = 0;
	}
	
	/** If the click comes from the user, start the video from the beginning **/
	if(click_source == "user") {
		time_elapsed = 0;
	}
	
	click_source = "user";
	
	var identifier = node + "_" + index;
	
	create_cookie("cookie_artist", artist);
	create_cookie("cookie_title", title);
	create_cookie("cookie_link", id);
	create_cookie("cookie_identifier", identifier);
	create_cookie("cookie_id", node);
	create_cookie("cookie_source", source);
	create_cookie("cookie_fullscreen_mode", get_fullscreen_mode());
	create_cookie("cookie_time_elapsed", time_elapsed);
	create_cookie("cookie_type", ctype);
	create_cookie("cookie_mode", "normal");
	
	if(duration) {
		create_cookie("cookie_duration", duration);
	}
	
	var url = "";
	
	var fullscreen_mode = read_cookie("cookie_fullscreen_mode");
	if(fullscreen_mode && fullscreen_mode == "yes") {
		
		/** Show a message in the normal player window **/
		redirect_iframe("player", "fullscreen_message.php");
		
		/** If the fullscreen window does not exist, create it **/
		if(!fullscreen_window) {
			create_fullscreen(time_elapsed);
			
		} else {
					
			/** Build URL containing the new video **/
			url = "fullscreen_player.php";
			
			/** Redirect the fullscreen window to the new video **/
			fullscreen_window.location.href = url;	
		}
		
		/** Request focus for the fullscreen window **/
		fullscreen_window.focus();
		
	} else {
		
		/** If the fullscreen window exists, close it **/
		if(fullscreen_window) {
			fullscreen_window.close();
			fullscreen_window = null;	
		}
		
		url = "";
		
		if(source == "youtube") {
			url = "youtube_player.php";
		}
		
		if(source == "deezer") {
			url = "deezer_player.php";
		}
		
		if(source == "imeem") {
			url = "imeem_player.php";
		}
			
		/** Redirect the normal player window **/	
		redirect_iframe("player", url);
	}
	
	get("comments").src = "comments.php?nid=" + node;
	
	/** Load the video's comments through Ajax **/
	$('#related').load("related.php?nid=" + node);
	
	title = title + " by " + artist;
	
	/** Change the display title and the window title **/
	get("mediatitle").innerHTML = title;
	document.title = title + " | arabiamania.net - the online arabic music channel";
	
	/** Update the row coloring of the playlist **/
	update_playlist();
	
	/** Click the play/pause button **/
	button("pause");
}

/** Plays a song in-play **/
function inplay(node, artist, title, id, source, duration, ctype) {
	
	create_cookie("cookie_artist", artist);
	create_cookie("cookie_title", title);
	create_cookie("cookie_link", id);
	create_cookie("cookie_id", node);
	create_cookie("cookie_source", source);
	create_cookie("cookie_type", ctype);
	create_cookie("cookie_duration", duration);
	create_cookie("cookie_mode", "inplay");
		
	var url = "";
		
	if(source == "youtube") {
		url = "youtube_player.php";
	}
		
	if(source == "deezer") {
		url = "deezer_player.php";
	}
		
	if(source == "imeem") {
		url = "imeem_player.php";
	}	
	
	title = title + " by " + artist;
	get("inplay_title").innerHTML = title;
	
	open_inplay(url);
}

/** Returns the "player" object used to play files **/
function get_player() {
	
	var plugin = null;
	var iframe = null;
	
	if(fullscreen_window) {
		
		iframe = fullscreen_window.frames.player;
		
	} else iframe = window.frames.player;
	
	var source = get_source();
	
	if(source == "youtube") {
		plugin = iframe.document.getElementById("myytplayer");
	}
	
	if(source == "deezer") {
		plugin = iframe.document.getElementById("mediaspace");
	}
	
	if(source == "imeem") {
		plugin = iframe.document.getElementById("mediaspace");
	}
	
	return plugin;
}

/** Starts or pauses the player depending on which state it is in **/
function play_resume_video() {
	
	var buttons = get("buttons");
	
	if(buttons.src == play_buttons.src) {
		resume_video();
		return;
	} 
	
	if(buttons.src == pause_buttons.src) {
		pause_video();
		return;
	}
}

/** Pauses the player **/
function pause_video() {
	
	var player = get_player();
	
	if(player) {
		
		var source = get_source();
		
		if(source == "youtube") {
			player.pauseVideo();
		}
		
		if(source == "deezer") {
			// do nothing
		}
		
		if(source == "imeem") {
			player.innerHTML = "";
		}
		
		button("play");
	}
}

/** Starts the player after it has been paused **/
function resume_video() {
	
	var player = get_player();
	
	if(player) {
		
		var source = get_source();
		
		if(source == "youtube") {
			player.playVideo();
		}
		
		if(source == "deezer") {
			click_current();
		}
			
		if(source == "imeem") {
			click_current();
		}
			
		button("pause");
		
	} else {
		
		/** If nothing is currently playing, play the first item **/	
		click_current();
		button("pause");
	}
}

/** Stops the player **/
function stop_video() {
	
	var player = get_player();
	if(player) {
		
		var source = get_source();
		
		if(source == "youtube") {
			player.stopVideo();
			player.clearVideo();
		}
		
		if(source == "deezer") {
			// do nothing
		}
		
		if(source == "imeem") {
			// do nothing
		}
	}
	
	button("play");
}

/** Returns the elapsed time of a video in seconds **/
function get_time_elapsed() {

	var player = get_player();
	
	if(player) {
		
		var source = get_source();
		
		if(source == "youtube") {
			return player.getCurrentTime();	
		}
		
		if(source == "deezer") {
			return 0;
		}
		
		if(source == "imeem") {
			return 0;
		}
	}
	
	return 0;
}

/** Plays the track before the current one on the playlist **/
function previous_video() {

	var table = get("playlist");

	var current_identifier = read_cookie("cookie_identifier");
	var row = get(current_identifier + "_playlist_item_row");
	
	/** If no valid identifier exists, default to first index of playlist **/
	if(!row) {
	
		var first = table.rows[0];
		
		var id = first.getAttribute("id");
		var split = id.split("_");
		
		click_identifier(split[0] + "_" + split[1]);
		
		return;
	}
	
	var current_index = row.rowIndex;
	var previous_index = current_index == 0 ? table.rows.length - 1 : parseInt(current_index) - 1;
	
	var previous_row = table.rows[previous_index];
	var split = previous_row.getAttribute("id").split("_");
	
	var previous_identifier = split[0] + "_" + split[1];
	click_identifier_as_user(previous_identifier);
}

/** Plays the track after the current one on the playlist **/
function next_video() {

	var table = get("playlist");

	var current_identifier = read_cookie("cookie_identifier");
	var row = get(current_identifier + "_playlist_item_row");

	/** If no valid identifier exists, default to first index of playlist **/
	if(!row) {
		
		var first = table.rows[0];
		
		var id = first.getAttribute("id");
		var split = id.split("_");
		
		click_identifier(split[0] + "_" + split[1]);
		
		return;
	}
	
	var current_index = row.rowIndex;
	var next_index = current_index == table.rows.length - 1 ? 0 : parseInt(current_index) + 1;
	
	var next_row = table.rows[next_index];
	var split = next_row.getAttribute("id").split("_");
	
	var next_identifier = split[0] + "_" + split[1];
	click_identifier_as_user(next_identifier);
}

/** Toggles the full screen mode **/
function change_fullscreen_mode() {
	
	/** If nothing is playing, do nothing **/
	var player = get_player();
	if(!player) {
		return;
	}
	
	var new_fullscreen_mode = "";
	
	/** Get the current fullscreen mode from a cookie **/
	var current_fullscreen_mode = read_cookie("cookie_fullscreen_mode");
	if(!current_fullscreen_mode) {
		current_fullscreen_mode = "no";	
	}
	
	if(current_fullscreen_mode == "yes") {
		new_fullscreen_mode = "no";
	}
	
	if(current_fullscreen_mode == "no") {
		new_fullscreen_mode = "yes";
	}
	
	create_cookie("cookie_fullscreen_mode", new_fullscreen_mode);
	create_cookie("cookie_time_elapsed", "" + get_time_elapsed());
	
	click_current();
}

/** Toggles the quality of the video **/
function change_quality() {
	
	var current_quality = read_cookie("cookie_quality");
	if(!current_quality) {
		current_quality = "low";
	}
	
	var new_quality = "";
	
	if(current_quality == "low") {
		
		new_quality = "high";
		
		get("high_quality_link").className = "qlink_selected";
		get("low_quality_link").className = "qlink_unselected";
	}
	
	if(current_quality == "high") {
		
		new_quality = "low";
		
		get("low_quality_link").className = "qlink_selected";
		get("high_quality_link").className = "qlink_unselected";
	}
	
	create_cookie("cookie_quality", new_quality);
	create_cookie("cookie_time_elapsed", "" + get_time_elapsed());
	
	click_current();	
}

function button(mode) {
	
	var buttons = get("buttons");
	
	if(mode == "pause") {
		buttons.src = pause_buttons.src;
	}
	
	if(mode == "play") {
		buttons.src = play_buttons.src;
	}
	
	if(mode == "buffering") {
		buttons.src = buffering_buttons.src;
	}
	
	if(mode == "stopping") {
		buttons.src = buffering_buttons.src;
	}
}

/** Plays the currently selected clip **/
function click_current() {
	
	var table = get("playlist");
	
	var current_identifier = read_cookie("cookie_identifier");
	var row = get(current_identifier + "_playlist_item_row");

	if(!row) {
		var first = table.rows[0];
		
		var id = first.getAttribute("id");
		var split = id.split("_");
		
		current_identifier = split[0] + "_" + split[1];	
	}
	
	click_identifier(current_identifier);
}

/** Emulates a click on the link contained in the cell whose id is index **/
function click_identifier(identifier) {
	
	var row = get(identifier + "_playlist_item_row");
	if(!row) {
		return;
	}
	
	var link = identifier + "_playlist_item_link";
	
	click_source = "auto";
	get(link).onclick();
}

/** Emulates a click on the link contained in the cell whose id is index **/
function click_identifier_as_user(identifier) {
	
	var row = get(identifier + "_playlist_item_row");
	if(!row) {
		return;
	}
	
	var link = identifier + "_playlist_item_link";
	
	click_source = "user";
	get(link).onclick();
}

function remove_playlist_item(node, index) {

	var identifier = node + "_" + index;
	var row_index = get(identifier + "_playlist_item_row").rowIndex;
	
	var table = get("playlist");
	table.deleteRow(row_index);
	
	update_playlist();
}

function revert(identifier) {
	hide(identifier + "_message");
}

function please_login(text, mode) {
	
	var login = "<span id='login_button' class='errormessage'><a href='user/login'>Login</a></span>";
	var register = "<span id='login_button' class='errormessage'><a href='user/register'>Register</a></span>";
	
	var buttons = "<div style='text-align: center'>" + login + "&nbsp;&nbsp;" + register + "</div><br/>";
	text = "<div style='text-align: center;'>" + text + "</div>";
	
	display_error_message(text + "<br/><br/>" + buttons, 10000, mode);		
}

function add_to_playlist(node, index) {

	var identifier = node + "_" + index; 

	var message = identifier + "_message"; 
	
	var data = read_cookie("cookie_playlist");
	
	if(data && data != "") {
		
		var items = data.split("x");
		if(items.length > 15 && !is_logged_in()) {
	
			var text = "Please log in or register to add more videos!";
			please_login(text, "normal");
						
			return;
		}
		
		data = "";
		for(var i = 0; i < items.length; i++) { 
			data += items[i] + "x";
		}
		data += node;
		
		create_cookie("cookie_playlist", data);
		
	} else {
		create_cookie("cookie_playlist", node);
	}
	
	get(message).innerHTML = "Added to <a href='http://www.arabiamania.net'>playlist</a>!";
	get(message).className = "statusmessage";
	
	show(message);
	
	setTimeout("revert('" + identifier + "');", 4000);
}

function inplay_add_to_playlist() {
	
	var node = read_cookie("cookie_id");
	var data = read_cookie("cookie_playlist");
	
	if(data && data != "") {
		
		var items = data.split("x");
		if(items.length > 15 && !is_logged_in()) {
	
			var text = "Please log in or register to add more videos!";
			please_login(text, "inplay");
	
			return;
		}
		
		data = "";
		for(var i = 0; i < items.length; i++) { 
			data += items[i] + "x";
		}
		data += node;
		
		create_cookie("cookie_playlist", data);
		
	} else {
		create_cookie("cookie_playlist", node);
	}
	
	var content = "<div style='text-align: center;'>Added to <a href='http://www.arabiamania.net'>playlist</a>!</div>";
	display_centered_message("message", content, "statusmessage", "inplay", 4000);
}

/** Add a node from the list of related videos to the current playlist **/
function add_related_to_playlist(node, index) {
	
	add_to_playlist(node, index);
	create_cookie("cookie_update_playlist", "true");
	$('#playlist_div').load("show_playlist.php");
	
	update_playlist();
}

function open_playlist(data) {
	
	create_cookie("cookie_playlist", data);
	create_cookie("cookie_index", "0");
	
	window.location.href = "http://www.arabiamania.net";
}

function quick_playlist(artist) {

	create_cookie("cookie_quick_playlist", artist);
	window.location.href = "http://www.arabiamania.net";
}

function get_source() {
	return read_cookie("cookie_source");
}

function get_fullscreen_mode() {
	
	var mode = read_cookie("cookie_fullscreen_mode");
	
	if(!mode) {
		return "no";
	}
	
	return mode;
}

function new_random_playlist() {
	
	create_cookie("cookie_playlist", "");
	window.location.href = "http://www.arabiamania.net";
	
}