//Video Controller
var user_logged_on = 0;

function sendEvent(swf,typ,prm) { 
	thisMovie(swf).sendEvent(typ,prm);
	itemData(swf,prm);
};
function getUpdate(typ,pr1,pr2,swf) {};
function thisMovie(swf) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[swf];
  } else {
    return document[swf];
  }
};

function getUpdate(typ,pr1,pr2,swf) { 
	if(typ == 'item') {
		itemData('moovplay', pr1);	
	}
};

function loadFile(swf,obj) { 
  thisMovie(swf).loadFile(obj);
  getLength('moovplay','');
};

function addItem(swf,obj,idx) { 
  thisMovie(swf).addItem(obj,idx); 
};

function getLength(swf,idx) { 
 	 var len = thisMovie(swf).getLength();
	 if (len == 1) {
		singleList(len,idx); 
	 }
	 else {
 	 playList(len,idx);
	 }
};

function itemData(swf,idx) { 
  	var obj = thisMovie(swf).itemData(idx);
	var txt = "";
	for(var i in obj) {
		//alert(i + ':::' + obj[i]);
		if (i == 'title') {
			document.getElementById('artist_song_update').innerHTML = obj['title'];
		}
		if (i == 'link') {
			document.getElementById('lyric_artist_name').innerHTML = obj['link'];
		}
		if (i == 'author') {
			document.getElementById('filename_placeholder').value = obj['author'];
		}
	}
	getLength('moovplay',idx);
}

function singleList(LEN,idx) {
	var plArray = new Array();
	var obj;
	var item_place = 0;
	var pl_nowplaying = (idx);
	for(var i=0; i<=LEN; i++) {
		item_place = (i + 1);
		obj = thisMovie('moovplay').itemData(i);
		for(var r in obj) { 
			//txt += r+": "+obj[r]+"\n";
			if (r == 'author' && i == pl_nowplaying) {
				//test += i+":"+obj[r]+"|";
				plArray[i] = '<table cellspacing="0" cellpadding="0"><tr><td class="pl_item_cursor" onclick="javascript:sendEvent(\'moovplay\',\'playitem\','+i+');"><div class="pl_item_container"><div class="pl_item_picnum"><div class="pl_item_num">'+item_place+'.</div><div class="pl_item_pic"><img src="http://www.ohwvideos.com/images/screenshots/100/'+obj['author']+'.jpg" /></div></div><div class="pl_item_title">'+obj['title']+'</div></div></td></tr></table>';
				sendViewer(obj['author']);
			}
		}
	}
	document.getElementById('playlist_placeholder').innerHTML = plArray;
}

function playList(LEN,idx) {
	var plArray = new Array();
	var obj;
	var item_place = 0;
	var pl_nowplaying = (idx);
	for(var i=0; i<=LEN; i++) {
		item_place = (i + 1);
		obj = thisMovie('moovplay').itemData(i);
		for(var r in obj) {
			//txt += r+": "+obj[r]+"\n";
			if (r == 'author' && i != pl_nowplaying) {
				//test += i+":"+obj[r]+"|";
				plArray[i] = '<table cellspacing="0" cellpadding="0"><tr><td class="pl_item_cursor" onclick="javascript:sendEvent(\'moovplay\',\'playitem\','+i+');"><div class="pl_item_container"><div class="pl_item_picnum"><div class="pl_item_num">'+item_place+'.</div><div class="pl_item_pic"><img src="http://www.ohwvideos.com/images/screenshots/100/'+obj['author']+'.jpg" /></div></div><div class="pl_item_title">'+obj['title']+'</div></div></td></tr></table>';
			}
			if (r == 'author' && i == pl_nowplaying) {
				//test += i+":"+obj[r]+"|";
				plArray[i] = '<table cellspacing="0" cellpadding="0"><tr><td class="pl_item_cursor" onclick="javascript:sendEvent(\'moovplay\',\'playitem\','+i+');"><div class="pl_item_container"><div class="pl_item_picnum"><div class="pl_item_num">'+item_place+'.</div><div class="pl_item_pic"><div class="pl_item_nowplaying"></div></div></div><div class="pl_item_title">'+obj['title']+'</div></div></td></tr></table>';
				sendViewer(obj['author']);
			}
		}
	}
	plChopper(idx, plArray, LEN);
}

function plChopper(START, plArray, LEN) {
	var plShow = new Array();
	var counter = 0;
	var FINISH = (START + 4);
	
	if (START == 0) {
		for(var i=START; i<=FINISH; i++) {
			if (plArray[i] == 'undefined') {
				 break;
			}
			plShow += plArray[i];
			counter++;
		}
	}
	else {
		for(var i=(START-1); i<=(FINISH-1); i++) {
			if (plArray[i] == 'undefined') {
				 break;
			}
			plShow += plArray[i];
			counter++;
		}
	}
	document.getElementById('playlist_placeholder').innerHTML = plShow;
}

function loadUserPL(TAB,LOGGEDON) {
	if (document.getElementById('currentplaylistholder_opener').value != 1) {
		hideCurrentPlayList();
	}
	tabClicked(TAB);
	if (TAB == 'pl_tab_yourplaylist') {
		if (LOGGEDON == '0') {
			if (document.getElementById('logged_in_watcher').value == 0) {
				document.getElementById('playlist_placeholder').innerHTML = '<table cellspacing="0" cellpadding="0"><tr><td><div class=\'pl_login_container\'><table><tr><td>Please log in to create and edit your personal playlist.</td></tr><tr><td height=\"10\"><p class=\'pl_login_wrong\' id=\'login_incorrect_placeholder\'></p></td></tr><tr><td align=\'center\'><input type=\"text\" name=\"loguser\" id=\"loguser\" value=\"Username\" onfocus=\"fieldFocus(\'loguser\')\" onblur=\"fieldBlur(\'loguser\')\"></td></tr><tr><td align=\'center\'><input type=\"password\" name=\"logpass\" id=\"logpass\" value=\"Password\" onfocus=\"fieldFocus(\'logpass\')\" onblur=\"fieldBlur(\'logpass\')\"></td></tr><tr><td><input type=\"submit\" name=\"submit\" value=\"Log In\" onclick=\"javascript:ajaxFunc(\'login.php\',\'login\');\" />&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:signUp();\">Sign Up</a></td></tr></table></div></td></tr></table>';
			}else{
				ajaxFunc('loadUPL.php', 'loadUPL');	
			}
		}
		else {
			ajaxFunc('loadUPL.php', 'loadUPL');	
		}
	}else if (TAB == 'pl_tab_favorites') {
		document.getElementById('playlist_placeholder').innerHTML = '<table><tr><td class=\'pl_login_container\'>Coming Soon</td></td></table>';
	}
}

function hideCurrentPlayList() {
	//alert(document.getElementById('currentplaylistholder_opener').value);
	//if (document.getElementById('currentplaylistholder_opener').value == 0) {
		document.getElementById('CPL_holder').value = document.getElementById('playlist_placeholder').innerHTML;
		document.getElementById('currentplaylistholder').value = document.getElementById('playlist_placeholder').innerHTML;
		document.getElementById('playlist_placeholder').innerHTML = '';	
		document.getElementById('currentplaylistholder_opener').value = 1;
	//}	
}
function showCurrentPlayList() {
	document.getElementById('playlist_placeholder').innerHTML = '';
	document.getElementById('playlist_placeholder').innerHTML = document.getElementById('CPL_holder').value;
	document.getElementById('currentplaylistholder_opener').value = 0;
	tabClicked('CPL');
}

function fieldFocus(NAME) {
	var curText = document.getElementById(NAME).value;
	if (curText == 'Username') {
		document.getElementById(NAME).value = '';			
	}
	if (curText == 'Password') {
		document.getElementById(NAME).value = '';			
	}
}

function fieldBlur(NAME) {
	var curText = document.getElementById(NAME).value;
	var curLength = curText.length;
	if (curLength == 0) {
		if (NAME == 'loguser') {
			document.getElementById(NAME).value = 'Username';
		}
		if (NAME == 'logpass') {
			document.getElementById(NAME).value = 'Password';
		}
	}
}

function playUPL() {
	ajaxFunc('playUPL.php', 'playUPL');	
}

//========================== AJAX FUNCTION ================================//

var xmlHttp

function ajaxFunc(FILE,TODO) {
	document.getElementById('todow').value = TODO;
	if (TODO == 'login') {
		var ua = document.getElementById('loguser').value;
		var ub = document.getElementById('logpass').value;
	}else if (TODO == 'videocount') {
		var ua = document.getElementById('current_video').value;
		var ub = 0;
	}else if (TODO == 'lyrics') {
		var ua = document.getElementById('filename_placeholder').value;
		var ub = 0;
	}else if (TODO == 'addtopl') {
		var ua = document.getElementById('current_video').value;
		var ub = 0;
	}else if (TODO == 'rempl') {
		var ua = document.getElementById('filename_placeholder').value;
		var ub = 0;
	}else if (TODO == 'changeorder') {
		var ua = document.getElementById('filename_placeholder').value;
		var ub = document.getElementById('change_pl_order').value;
	}else if (TODO == 'signup') {
		var ua = document.getElementById('loguser').value;
		var ub = document.getElementById('logpass').value;
	}else{
		var ua = 0;
		var ub = 0;
	}
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	var url="http://www.ohwvideos.com/ajax/";
	url=url+FILE;
	url=url+"?ua="+ua;
	url=url+"&ub="+ub;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	}

function stateChanged() { 
	if (xmlHttp.readyState==4) { 
		var server_response=xmlHttp.responseText;
		TODO = document.getElementById('todow').value;
		if (TODO == 'login') {
			if (server_response == 'on') {
				document.getElementById('playlist_placeholder').innerHTML = '';
				document.getElementById('logged_in_watcher').value = '1';
				ajaxFunc('loadUPL.php', 'loadUPL');
			}else{
				document.getElementById('login_incorrect_placeholder').innerHTML = 'Incorrect Login Credentials';
				document.getElementById('loguser').value = 'Username';
				document.getElementById('logpass').value = 'password';
			}
		}else if (TODO == 'loadUPL') {
			document.getElementById('playlist_placeholder').innerHTML = server_response;
			document.getElementById('UPL_placeholder').value = server_response;
		}else if (TODO == 'playUPL') {
			loadFile('moovplay', {file:'http://www.ohwvideos.com/vids/playUPL.php'});
			//document.getElementById('currentplaylistholder').value = document.getElementById('playlist_placeholder').value;
			tabClicked('CPL')
		}else if (TODO == 'logout') {
			if (server_response == 'loggedout') {
				alert('You have been logged out.');
				window.location.href = 'http://www.ohwvideos.com';
			}
		}else if (TODO == 'videocount') {
			ajaxFunc('getlyrics.php','lyrics');
		}else if (TODO == 'lyrics') {
			document.getElementById('lyric_lyrics').innerHTML = server_response;	
		}else if (TODO == 'addtopl') {
			ajaxFunc('loadUPL.php', 'loadUPL');
		}else if (TODO == 'rempl') {
			ajaxFunc('loadUPL.php', 'loadUPL');	
		}else if (TODO == 'changeorder') {
			ajaxFunc('loadUPL.php', 'loadUPL');
		}else if (TODO == 'signup') {
			if (server_response == 'true') {
				document.getElementById('logged_in_watcher').value = 1;
				ajaxFunc('loadUPL.php', 'loadUPL');
			}else if (server_response == 'duplicate') {
				document.getElementById('login_incorrect_placeholder').innerHTML = 'Username Already Taken';	
			}else if (server_response == 'password') {
				document.getElementById('login_incorrect_placeholder').innerHTML = 'Alpha-Numeric Passwords Only';
			}else if (server_response == 'username') {
				document.getElementById('login_incorrect_placeholder').innerHTML = 'Alpha-Numeric Usernames Only';	
			}
		}else if (TODO == 'indexlogin') {
			if (server_response == 'error') {
				document.getElementById('indexloginerror').innerHTML = 'Incorrect Username or Password.';	
			}
			if (server_response == 'dupuname') {
				document.getElementById('indexloginerror').innerHTML = 'Username Already Taken.	';
			}
			if (server_response == 'sweet') {
				window.location.href = 'http://www.ohwvideos.com/vidwatcherwithplaylist.php';	
			}
		}
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e) {
  		// Internet Explorer
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e) {
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
	return xmlHttp;
}

//=================== TAB SETTINGS =================================//
function tabColor(TAB, SWITCH) {
	if (TAB == 'Start') {
		document.getElementById('CPL').style.background = '#fdf791';
	}else{
		if (SWITCH == 'on') {
			document.getElementById(TAB).style.background = '#fdf791';
		}else{
			document.getElementById(TAB).style.background = '#56ee56';
			var currentTab = document.getElementById('current_tab').value;
			document.getElementById(currentTab).style.background = '#fdf791';
		}
	}
}

function tabClicked(TAB) {
	if (TAB == 'CPL') {
		document.getElementById('CPL').style.background = '#fdf791';
		document.getElementById('UPL').style.background = '#56ee56';
		document.getElementById('FAV').style.background = '#56ee56';
		document.getElementById('current_tab').value = 'CPL';
	}else if (TAB == 'pl_tab_yourplaylist') {
		document.getElementById('CPL').style.background = '#56ee56';
		document.getElementById('UPL').style.background = '#fdf791';
		document.getElementById('FAV').style.background = '#56ee56';
		document.getElementById('current_tab').value = 'UPL';
	}else if (TAB == 'pl_tab_favorites') {
		document.getElementById('CPL').style.background = '#56ee56';
		document.getElementById('UPL').style.background = '#56ee56';
		document.getElementById('FAV').style.background = '#fdf791';
		document.getElementById('current_tab').value = 'FAV';
	}
}

function sendViewer(VIDEO, ARTIST) {
	document.getElementById('current_artist').value = ARTIST;
	document.getElementById('current_video').value = VIDEO;
	ajaxFunc('videocount.php', 'videocount');
}

//================================= Add stuff to playlist ========================= //
function playlistAdd() {
	if (document.getElementById('logged_in_watcher').value == 1) {
		ajaxFunc('addtopl.php', 'addtopl');
		tabClicked('pl_tab_yourplaylist');
	}else{
		loadUserPL('pl_tab_yourplaylist', '0');	
	}
}

function remPL(FILENAME) {
	document.getElementById('filename_placeholder').value = FILENAME;
	ajaxFunc('rempl.php', 'rempl');	
}

function changePLOrder(FILENAME,ORDER) {
	document.getElementById('filename_placeholder').value = FILENAME;
	document.getElementById('change_pl_order').value = ORDER;
	ajaxFunc('changePLOrder.php', 'changeorder');
}

//================================ Sign Up ====================================== //
function signUp() {
	document.getElementById('playlist_placeholder').innerHTML = '<table cellspacing="0" cellpadding="0"><tr><td><div class=\'pl_login_container\'><table><tr><td>Create a username and begin creating YOUR playlist today.</td></tr><tr><td height=\"10\"><p class=\'pl_login_wrong\' id=\'login_incorrect_placeholder\'></p></td></tr><tr><td align=\'center\'><input type=\"text\" name=\"loguser\" id=\"loguser\" value=\"Username\" onfocus=\"fieldFocus(\'loguser\')\" onblur=\"fieldBlur(\'loguser\')\"></td></tr><tr><td align=\'center\'><input type=\"text\" name=\"logpass\" id=\"logpass\" value=\"Password\" onfocus=\"fieldFocus(\'logpass\')\" onblur=\"fieldBlur(\'logpass\')\"></td></tr><tr><td><input type=\"submit\" name=\"submit\" value=\"Create\" onclick=\"javascript:ajaxFunc(\'signup.php\',\'signup\');\" /></td></tr></table></div></td></tr></table>';
}

function loginSignup(TASK) {
	var uc = document.getElementById('loguser').value;
	var ud = document.getElementById('logpass').value;
	var location_base = 'loginsignupprocess.php';
	if (TASK == 'signup') {
		var ajaxEnding = location_base+"?uc="+uc+"&ud="+ud+"&pro=signup&";	
		ajaxFunc(ajaxEnding, 'indexlogin');
	}else if (TASK == 'login') {
		var ajaxEnding = location_base+"?uc="+uc+"&ud="+ud+"&pro=login&";
		ajaxFunc(ajaxEnding, 'indexlogin');
	}
}

function alphaNumCheck(FIELD,ERROR) {
	var regex=/^[0-9A-Za-z]+$/;
	alert('woot');
	if (document.getElementById(FIELD).value.length() > 0) {
		if (!regex.test(document.getElementById(FIELD).value)) {
			var fieldValue = document.getElementById(FIELD).value;
			fieldValue_len = fieldValue.length();
			fieldValue = fieldValue.substr(0,(fieldValue_len-1));
			document.getElementById(FIELD).value = 'fieldValue';
			document.getElementById(ERROR).innerHTML = 'Invalid Character';
		}else{
			document.getElementById(ERROR).innerHTML = '';
		}
	}
}