/*SWFObject v2.0 rc4 <http://code.google.com/p/swfobject/>Copyright (c) 2007 Geoff Stearns, Michael Williams, and Bobby van der Sluis.This software is released under the MIT License <http://www.opensource.org/licenses/mit-license.php>*/
var swfobject=function(){var X="undefined",P="object",a="visibility:visible",e="visibility:hidden",B="Shockwave Flash",h="ShockwaveFlash.ShockwaveFlash",V="application/x-shockwave-flash",K="SWFObjectExprInst",G=window,g=document,N=navigator,f=[],H=[],Q=null,L=null,S=false,C=false;var Y=function(){var l=typeof g.getElementById!=X&&typeof g.getElementsByTagName!=X&&typeof g.createElement!=X&&typeof g.appendChild!=X&&typeof g.replaceChild!=X&&typeof g.removeChild!=X&&typeof g.cloneNode!=X,t=[0,0,0],n=null;if(typeof N.plugins!=X&&typeof N.plugins[B]==P){n=N.plugins[B].description;if(n){n=n.replace(/^.*\s+(\S+\s+\S+$)/,"$1");t[0]=parseInt(n.replace(/^(.*)\..*$/,"$1"),10);t[1]=parseInt(n.replace(/^.*\.(.*)\s.*$/,"$1"),10);t[2]=/r/.test(n)?parseInt(n.replace(/^.*r(.*)$/,"$1"),10):0}}else{if(typeof G.ActiveXObject!=X){var o=null,s=false;try{o=new ActiveXObject(h+".7")}catch(k){try{o=new ActiveXObject(h+".6");t=[6,0,21];o.AllowScriptAccess="always"}catch(k){if(t[0]==6){s=true}}if(!s){try{o=new ActiveXObject(h)}catch(k){}}}if(!s&&o){try{n=o.GetVariable("$version");if(n){n=n.split(" ")[1].split(",");t=[parseInt(n[0],10),parseInt(n[1],10),parseInt(n[2],10)]}}catch(k){}}}}var v=N.userAgent.toLowerCase(),j=N.platform.toLowerCase(),r=/webkit/.test(v)?parseFloat(v.replace(/^.*webkit\/(\d+(\.\d+)?).*$/,"$1")):false,i=false,q=j?/win/.test(j):/win/.test(v),m=j?/mac/.test(j):/mac/.test(v);/*@cc_on i=true;@if(@_win32)q=true;@elif(@_mac)m=true;@end@*/return{w3cdom:l,pv:t,webkit:r,ie:i,win:q,mac:m}}();var d=function(){if(!Y.w3cdom){return }J(I);if(Y.ie&&Y.win){try{g.write("<script id=__ie_ondomload defer=true src=//:><\/script>");var i=b("__ie_ondomload");if(i){i.onreadystatechange=function(){if(this.readyState=="complete"){this.parentNode.removeChild(this);U()}}}}catch(j){}}if(Y.webkit&&typeof g.readyState!=X){Q=setInterval(function(){if(/loaded|complete/.test(g.readyState)){U()}},10)}if(typeof g.addEventListener!=X){g.addEventListener("DOMContentLoaded",U,null)}M(U)}();function U(){if(S){return }if(Y.ie&&Y.win){var m=W("span");try{var l=g.getElementsByTagName("body")[0].appendChild(m);l.parentNode.removeChild(l)}catch(n){return }}S=true;if(Q){clearInterval(Q);Q=null}var j=f.length;for(var k=0;k<j;k++){f[k]()}}function J(i){if(S){i()}else{f[f.length]=i}}function M(j){if(typeof G.addEventListener!=X){G.addEventListener("load",j,false)}else{if(typeof g.addEventListener!=X){g.addEventListener("load",j,false)}else{if(typeof G.attachEvent!=X){G.attachEvent("onload",j)}else{if(typeof G.onload=="function"){var i=G.onload;G.onload=function(){i();j()}}else{G.onload=j}}}}}function I(){var l=H.length;for(var j=0;j<l;j++){var m=H[j].id;if(Y.pv[0]>0){var k=b(m);if(k){H[j].width=k.getAttribute("width")?k.getAttribute("width"):"0";H[j].height=k.getAttribute("height")?k.getAttribute("height"):"0";if(O(H[j].swfVersion)){if(Y.webkit&&Y.webkit<312){T(k)}}else{if(H[j].expressInstall&&!C&&O("6.0.65")&&(Y.win||Y.mac)){D(H[j])}else{c(k)}}}}A("#"+m,a)}}function T(m){var k=m.getElementsByTagName(P)[0];if(k){var p=W("embed"),r=k.attributes;if(r){var o=r.length;for(var n=0;n<o;n++){if(r[n].nodeName.toLowerCase()=="data"){p.setAttribute("src",r[n].nodeValue)}else{p.setAttribute(r[n].nodeName,r[n].nodeValue)}}}var q=k.childNodes;if(q){var s=q.length;for(var l=0;l<s;l++){if(q[l].nodeType==1&&q[l].nodeName.toLowerCase()=="param"){p.setAttribute(q[l].getAttribute("name"),q[l].getAttribute("value"))}}}m.parentNode.replaceChild(p,m)}}function F(i){if(Y.ie&&Y.win&&O("8.0.0")){G.attachEvent("onunload",function(){var k=b(i);for(var j in k){if(typeof k[j]=="function"){k[j]=function(){}}}k.parentNode.removeChild(k)})}}function D(j){C=true;var o=b(j.id);if(o){if(j.altContentId){var l=b(j.altContentId);if(l){L=l}}else{L=Z(o)}if(!(/%$/.test(j.width))&&parseInt(j.width,10)<310){j.width="310"}if(!(/%$/.test(j.height))&&parseInt(j.height,10)<137){j.height="137"}g.title=g.title.slice(0,47)+" - Flash Player Installation";var n=Y.ie&&Y.win?"ActiveX":"PlugIn",k=g.title,m="MMredirectURL="+G.location+"&MMplayerType="+n+"&MMdoctitle="+k,p=j.id;if(Y.ie&&Y.win&&o.readyState!=4){var i=W("div");p+="SWFObjectNew";i.setAttribute("id",p);o.parentNode.insertBefore(i,o);o.style.display="none";G.attachEvent("onload",function(){o.parentNode.removeChild(o)})}R({data:j.expressInstall,id:K,width:j.width,height:j.height},{flashvars:m},p)}}function c(j){if(Y.ie&&Y.win&&j.readyState!=4){var i=W("div");j.parentNode.insertBefore(i,j);i.parentNode.replaceChild(Z(j),i);j.style.display="none";G.attachEvent("onload",function(){j.parentNode.removeChild(j)})}else{j.parentNode.replaceChild(Z(j),j)}}function Z(n){var m=W("div");if(Y.win&&Y.ie){m.innerHTML=n.innerHTML}else{var k=n.getElementsByTagName(P)[0];if(k){var o=k.childNodes;if(o){var j=o.length;for(var l=0;l<j;l++){if(!(o[l].nodeType==1&&o[l].nodeName.toLowerCase()=="param")&&!(o[l].nodeType==8)){m.appendChild(o[l].cloneNode(true))}}}}}return m}function R(AE,AC,q){var p,t=b(q);if(typeof AE.id==X){AE.id=q}if(Y.ie&&Y.win){var AD="";for(var z in AE){if(AE[z]!=Object.prototype[z]){if(z=="data"){AC.movie=AE[z]}else{if(z.toLowerCase()=="styleclass"){AD+=' class="'+AE[z]+'"'}else{if(z!="classid"){AD+=" "+z+'="'+AE[z]+'"'}}}}}var AB="";for(var y in AC){if(AC[y]!=Object.prototype[y]){AB+='<param name="'+y+'" value="'+AC[y]+'" />'}}t.outerHTML='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+AD+">"+AB+"</object>";F(AE.id);p=b(AE.id)}else{if(Y.webkit&&Y.webkit<312){var AA=W("embed");AA.setAttribute("type",V);for(var x in AE){if(AE[x]!=Object.prototype[x]){if(x=="data"){AA.setAttribute("src",AE[x])}else{if(x.toLowerCase()=="styleclass"){AA.setAttribute("class",AE[x])}else{if(x!="classid"){AA.setAttribute(x,AE[x])}}}}}for(var w in AC){if(AC[w]!=Object.prototype[w]){if(w!="movie"){AA.setAttribute(w,AC[w])}}}t.parentNode.replaceChild(AA,t);p=AA}else{var s=W(P);s.setAttribute("type",V);for(var v in AE){if(AE[v]!=Object.prototype[v]){if(v.toLowerCase()=="styleclass"){s.setAttribute("class",AE[v])}else{if(v!="classid"){s.setAttribute(v,AE[v])}}}}for(var u in AC){if(AC[u]!=Object.prototype[u]&&u!="movie"){E(s,u,AC[u])}}t.parentNode.replaceChild(s,t);p=s}}return p}function E(k,i,j){var l=W("param");l.setAttribute("name",i);l.setAttribute("value",j);k.appendChild(l)}function b(i){return g.getElementById(i)}function W(i){return g.createElement(i)}function O(k){var j=Y.pv,i=k.split(".");i[0]=parseInt(i[0],10);i[1]=parseInt(i[1],10);i[2]=parseInt(i[2],10);return(j[0]>i[0]||(j[0]==i[0]&&j[1]>i[1])||(j[0]==i[0]&&j[1]==i[1]&&j[2]>=i[2]))?true:false}function A(m,j){if(Y.ie&&Y.mac){return }var l=g.getElementsByTagName("head")[0],k=W("style");k.setAttribute("type","text/css");k.setAttribute("media","screen");if(!(Y.ie&&Y.win)&&typeof g.createTextNode!=X){k.appendChild(g.createTextNode(m+" {"+j+"}"))}l.appendChild(k);if(Y.ie&&Y.win&&typeof g.styleSheets!=X&&g.styleSheets.length>0){var i=g.styleSheets[g.styleSheets.length-1];if(typeof i.addRule==P){i.addRule(m,j)}}}return{registerObject:function(l,i,k){if(!Y.w3cdom||!l||!i){return }var j={};j.id=l;j.swfVersion=i;j.expressInstall=k?k:false;H[H.length]=j;A("#"+l,e)},getObjectById:function(l){var i=null;if(Y.w3cdom&&S){var j=b(l);if(j){var k=j.getElementsByTagName(P)[0];if(!k||(k&&typeof j.SetVariable!=X)){i=j}else{if(typeof k.SetVariable!=X){i=k}}}}return i},embedSWF:function(n,u,r,t,j,m,k,p,s){if(!Y.w3cdom||!n||!u||!r||!t||!j){return }r+="";t+="";if(O(j)){A("#"+u,e);var q=(typeof s==P)?s:{};q.data=n;q.width=r;q.height=t;var o=(typeof p==P)?p:{};if(typeof k==P){for(var l in k){if(k[l]!=Object.prototype[l]){if(typeof o.flashvars!=X){o.flashvars+="&"+l+"="+k[l]}else{o.flashvars=l+"="+k[l]}}}}J(function(){R(q,o,u);A("#"+u,a)})}else{if(m&&!C&&O("6.0.65")&&(Y.win||Y.mac)){A("#"+u,e);J(function(){var i={};i.id=i.altContentId=u;i.width=r;i.height=t;i.expressInstall=m;D(i);A("#"+u,a)})}}},getFlashPlayerVersion:function(){return{major:Y.pv[0],minor:Y.pv[1],release:Y.pv[2]}},hasFlashPlayerVersion:O,createSWF:function(k,j,i){if(Y.w3cdom&&S){return R(k,j,i)}else{return undefined}},createCSS:function(j,i){if(Y.w3cdom){A(j,i)}},addDomLoadEvent:J,addLoadEvent:M,getQueryParamValue:function(m){var l=g.location.search||g.location.hash;if(m==null){return l}if(l){var k=l.substring(1).split("&");for(var j=0;j<k.length;j++){if(k[j].substring(0,k[j].indexOf("="))==m){return k[j].substring((k[j].indexOf("=")+1))}}}return""},expressInstallCallback:function(){if(C&&L){var i=b(K);if(i){i.parentNode.replaceChild(L,i);L=null;C=false}}}}}();

	function make_time(seconds)
	{
		var miliseconds = seconds * 1000;
		var time = new Date(miliseconds);
		
		//tmp  = (((time.getHours()-1) < 10) ? '0' : '') + (time.getHours()-1);
		tmp  = ((time.getMinutes() < 10) ?  '0' : ':') + time.getMinutes();
		tmp += ((time.getSeconds() < 10) ? ':0' : ':') + time.getSeconds();
	
		return tmp;
	}	
		
	

	/*function show()
	{
		var elem = document.getElementById('subtuber_wrapper').getElementsByTagName('div')[0];
		
		if (ytplayer.getPlayerState() == 1)
		{
			var func = t[make_time(Math.floor(getCurrentTime()))];

			if (func !== undefined)
			{
				time = func.replace('|', '<br />');
				
				elem.innerHTML = '		<pre class="p8">' + time + 
								 '</pre><pre class="p7">' + time +
								 '</pre><pre class="p6">' + time +
								 '</pre><pre class="p5">' + time +
								 '</pre><pre class="p4">' + time +
								 '</pre><pre class="p3">' + time +
								 '</pre><pre class="p2">' + time +
								 '</pre><pre class="p1">' + time +
								 '</pre><pre class="p0">' + time + '</pre>';
				life = 0;
			}
			else
			{
				//life++;
				life = life + 0.1;
				if (life > max_sec)
				{
					life = 0;
					elem.innerHTML = '';
				}
			}
		}

		setTimeout('show()', 100);
	}*/
	
	
	
	
	function show()
	{
		var elem = document.getElementById('subtuber_wrapper').getElementsByTagName('div')[0];
		
		if (ytplayer.getPlayerState() == 1)
		{
			var func = t[make_time(Math.floor(getCurrentTime()))];

			if (func !== undefined)
			{
				elem.innerHTML = '<pre style="font:bold 18px arial,sans-serif;color:#fff;text-align:center;display:inline;">' + func.replace('|', '<br />') + '</pre>';
				life = 0;
			}
			else
			{
				//life++;
				life = life + 0.1;
				if (life > max_sec)
				{
					life = 0;
					elem.innerHTML = '';
				}
			}
		}

		setTimeout('show()', 100);
	}
	

	
	
	function time()
	{
		return/[\d:]{8}/.exec(Date())
	}
		
	function getCurrentTime() {
		if (ytplayer) {
			return ytplayer.getCurrentTime();
		}
	}
	
	function getPlayerState() {
		if (ytplayer) {
			return ytplayer.getPlayerState();
		}
	}	
	
	function getVideoUrl() {
		if (ytplayer) {
			return ytplayer.getVideoUrl();
		}
	}
	
	function play() {
		if (ytplayer) {
			ytplayer.playVideo();
		}
	}

	function pause() {
		if (ytplayer) {
			ytplayer.pauseVideo();
		}
	}
	
	function create_text_element()
	{
  		var div = document.createElement('div');
		div.setAttribute('class', 'sub');
		
		div.style.position = 'absolute';
		div.style.width = '470px';
		div.style.left = '0';
		div.style.bottom = '32px';
		div.style.textAlign = 'center';
		
  		//var p = document.createElement('pre');		
		//var txtNode = document.createTextNode('Sample text');
		//p.setAttribute('style', 'color:green;');
		//p.style.color = '#fff';
		//p.style.background = 'green';
		//p.style.textAlign = 'center';
		//p.style.font = 'bold 16px arial, sans-serif;';
		//p.style.margin = '-10px 0 0 0';
		
		//div.appendChild(txtNode);
		//div.appendChild(p);
		document.getElementById('subtuber_wrapper').appendChild(div);
	}
	

	
	function preview()
	{
		var text = document.getElementById('subtitles').value;
		var tab = text.split("\n");
		t = [];
		
		for (var i in tab)
		{
			var line = tab[i].split(' ');
			t[line[0]] = tab[i].replace(line[0] + ' ', '');
		}
	}
	
	
	
	function create_add_menu()
	{
		var menu = document.getElementById('subtitles_menu');
		
		/*menu.style.position = 'absolute';
		menu.style.width = '150px';
		menu.style.height = '100px';
		menu.style.left = '-50px';
		menu.style.top = '0';
		menu.style.background = '#FFFBEF';*/
	}
	
	
	/*function preview_add_time()
	{
		var textarea = document.getElementById('subtitles');
		var lines = textarea.value.split("\n");
		var current_line = lines.pop();
		
		new_line = '';
		
		if (current_line.length > 0)
		{
			new_line = "\n";
		}
		
		pause();
		
		textarea.value += new_line + make_time(Math.floor(getCurrentTime())) + ' ';
		textarea.focus();
		set_caret_position(textarea, textarea.value.length);
	}*/
	
	
	function preview_add_time()
	{
		var button = document.getElementById('add_video_menu').getElementsByTagName('a')[0].firstChild;
		var state = getPlayerState();

		/* getPlayerState() => unstarted (-1), 
							ended (0), 
							playing (1), 
							paused (2), 
							buffering (3), 
							video cued (5)
		*/
		
		//alert(state);
							
		// if is playing
		
		switch (state)
		{
			case -1:
				button.nodeValue = 'Play';
				play();
				break;

			// ended
			case 0:
				button.nodeValue = 'Play again';
				break;

			// playing
			case 1:
				button.nodeValue = 'Pause';
				break;
			
			// paused
			case 2:
				button.nodeValue = 'Play';
				break;
				
			// buffering
			case 3:
				button.nodeValue = 'Add time';
				break;
				
			// video cued
			case 5:
				button.nodeValue = 'Play';
				break;
		}
		
		
		
		/*if ((state == 1) || (state == -1) || (state == 5))
		{
			pause();			
			button = 'Add time';

			var textarea = document.getElementById('subtitles');
			var lines = textarea.value.split("\n");
			var current_line = lines.pop();
			
			new_line = '';
			
			if (current_line.length > 0)
			{
				new_line = "\n";
			}
			
			textarea.value += new_line + make_time(Math.floor(getCurrentTime())) + ' ';
			textarea.focus();
			set_caret_position(textarea, textarea.value.length);
		}
		else
		{
			 // is paused
			if (getPlayerState() == 2)
			{
				alert('pause');
				//play();
				button = 'Play';
			}
		}*/
	}
	
	/*function preview_add_time()
	{
		var button = document.getElementById('add_video_menu').getElementsByTagName('a')[0].firstChild.nodeValue;
		var state = getPlayerState();

		 getPlayerState() => unstarted (-1), 
							ended (0), 
							playing (1), 
							paused (2), 
							buffering (3), 
							video cued (5)
		
		
		//alert(state);
							
		// if is playing
		if ((state == 1) || (state == -1) || (state == 5))
		{
			pause();			
			button = 'Add time';

			var textarea = document.getElementById('subtitles');
			var lines = textarea.value.split("\n");
			var current_line = lines.pop();
			
			new_line = '';
			
			if (current_line.length > 0)
			{
				new_line = "\n";
			}
			
			textarea.value += new_line + make_time(Math.floor(getCurrentTime())) + ' ';
			textarea.focus();
			set_caret_position(textarea, textarea.value.length);
		}
		else
		{
			 // is paused
			if (getPlayerState() == 2)
			{
				alert('pause');
				//play();
				button = 'Play';
			}
		}
	}*/
	
	
	function set_caret_position(ctrl, pos)
	{
		if (ctrl.setSelectionRange)
		{
			ctrl.focus();
			ctrl.setSelectionRange(pos,pos);
		}
		else if (ctrl.createTextRange) 
		{
			var range = ctrl.createTextRange();
			range.collapse(true);
			range.moveEnd('character', pos);
			range.moveStart('character', pos);
			range.select();
		}
	}
	
	
	function check_user_name(user)
	{
		var http = false;

		if (navigator.appName == "Microsoft Internet Explorer")
		{
			http = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else
		{
			http = new XMLHttpRequest();
		}

		http.abort();
		http.open('GET', 'http://localhost/subtubes2.5/index.php/auth/check_user_name_exists/' + user, true);
		http.onreadystatechange=function()
		{
			if (http.readyState == 4)
			{
				document.getElementById('check_username').innerHTML = http.responseText;
				return false;
			}
		}
		http.send(null);
		return false;
	}
	
	
	
	function register_validation()
	{
		var username  = document.getElementById('user_name').value;
		
		var password  = document.getElementById('password').value;
		var password2 = document.getElementById('password_confirm').value;
		
		var email	  = document.getElementById('email').value;
		var email2	  = document.getElementById('email_confirm').value;
		
		var day		  = document.getElementById('day').value;
		var month	  = document.getElementById('month').value;		
		var year	  = document.getElementById('year').value;

		var country   = document.getElementById('country').value;
		var captcha   = document.getElementById('captcha').value;
		var accept	  = document.getElementById('accept_terms');
		
		var result = '';
		var date = new Date();
		
		if (username.length < 6)
			result += 'Username field must contain at least 6 characters.' + "\n";
			
		if (password.length < 6)
			result += 'Password field must contain at least 6 characters.' + "\n";
			
		if ((password != password2) || (password2.length < 6))
			result += 'Password confirm and pasword must be the same.' + "\n";
			
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if (reg.test(email) == false)
			result += 'E-mail address is not valid.' + "\n";
			
		if ((email != email2) || (email2.length < 6))
			result += 'E-mail confirm and e-mail must be the same.' + "\n";

		if (!(!isNaN(day) & (day >= 1) & (day <= 31)))
			result += 'You must select your day of birth.' + "\n";
			
		if (!(month.match(/^\d+$/) & (month >= 1) & (month <= 12)))
			result += 'You must select your month of birth.' + "\n";
			
		if (!(year.match(/^\d+$/) & (year >= (date.getFullYear() - 100)) & (year <= date.getFullYear())))
			result += 'You must select your year of birth.' + "\n";
			
		if (!(!isNaN(country) & (country >= 1) & (country <= 239)))
			result += 'You must select your country.' + "\n";
			
		if (captcha.length != 6)
			result += 'Captcha must contain 6 characters.' + "\n";
			
		if (!accept.checked)
			result += 'You must accept Subyo terms and privacy policy.' + "\n";
		
		if (result.length > 0)
		{
			alert(result);
			return false;
		}
	}
	
	function table_ruler()
	{
		var tables = document.getElementsByTagName('table');
		
		for (i=0; i<=tables.length; i++)
		{
			if (tables[i].className == 'ruler')
			{
				var trs = tables[i].getElementsByTagName('tr');

				for (j=0; j<=tables.length; j++)
				{
					trs[j].onmouseover = function()
					{
						this.className = 'ruled';
						return false;
					}
					
					trs[j].onmouseout = function()
					{
						this.className = '';
						return false;
					}
				}
			}
		}
	}
	
	window.onload = function()
	{
		//table_ruler();
	}
	
	
	
	
	