// JavaScript Document
var currentState = "NONE"; 
var previousState = "NONE"; 
 
var player = null;
function playerReady(thePlayer) {
	player = document.getElementById(thePlayer.id);
	addListeners();
}
 
 
function addListeners() {
	if (player) { 
		player.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners()",100);
	}
}
 
 
function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 
 
	var tmp = document.getElementById("stat");
	if (tmp) { 
		tmp.innerHTML = "current state: " + currentState + 
		"<br>previous state: " + previousState; 
	}
 
	if ((currentState == "COMPLETED")&&(previousState == "PLAYING")) {
	// add your reciter here
		//document.location.href="http://www.longtailvideo.com/players/jw-flv-player/"; 
		
		addVerse();
		
		var selectedNode = "txt"+document.getElementById("verse").value;
		selx(selectedNode);

		loadFile();
		//alert("pass");
	}
	else
		if ((currentState == "COMPLETED")&&(previousState == "PAUSED")) {
		// add your reciter here
			//document.location.href="http://www.longtailvideo.com/players/jw-flv-player/"; 
			
			addVerse();
			
			var selectedNode = "txt"+document.getElementById("verse").value;
			selx(selectedNode);
			loadFile();
			//alert("pass");
		}

}
 


function createPlayer() {
	
	readCookie("style");
	
	var fileURI = getFileURI();
	changeFont();
	
	var flashvars = {
		//backcolor:"0x445044",
		//frontcolor:"0xE9EDD2",
		//lightcolor:"0xE9EDD2",
		file: fileURI , 
		autostart:"true"
		//bufferlength:"30"
	}
 
	var params = {
		allowfullscreen:"false", 
		allowscriptaccess:"always",
		wmode:"transparent"
	}
 
	var attributes = {
		id:"player1",  
		name:"player1"
	}
 	selectedx("txt1", 1);
	swfobject.embedSWF("player.swf", "placeholder1", "194", "19", "9.0.115", false, flashvars, params, attributes);
}

// ---------------------------- my own ---------------------------------

function loadFile() {

	var theFile = getFileURI();
	
	player.sendEvent('LOAD', theFile);
}


function setURL() {
 
			//document.getElementById('url').innerHTML = document.getElementById('url').href = getFileURI();
			 
			//loadFile('player1', {file: getFileURI()});
			
			//recite();

		}

function recite() {
 
			//sendEvent('player1', 'playpause');
 
		}
		
function completed() {
 
 			document.location.href="http://www.iqra.com.hk/";
 
		}
		
function changeFont(){
	var id = parseInt(document.getElementById("changer").value);
	var fontClass = "aya"+id;
	var txtId = "txt"+document.getElementById('verse').value;
	if(txtId != "txt1")
	scrollDivDown(txtId);
	
	document.getElementById("holder").setAttribute("class",fontClass);
	
	//alert(fontClass);
}

// *** Cookies functions to save and retrive cookies of style ***

var style_cookie_name = "style" ;
var style_cookie_duration = 30 ;

// *** END OF CUSTOMISABLE SECTION ***

function switch_style (css_title)
{
// You may use this script on your site free of charge provided
// you do not remote this notice or the URL below. Script from
// http://www.thesitewizard.com/javascripts/change-style-sheets.shtml
  var i, link_tag ;
  for (i = 0, link_tag = document.getElementsByTagName("link"); i < link_tag.length ; i++ ) {
    if ((link_tag[i].rel.indexOf("stylesheet") != -1) && link_tag[i].title) {
      link_tag[i].disabled = true ;
      if (link_tag[i].title == css_title) {
        link_tag[i].disabled = false ;
      }
    }
	
    createCookie( style_cookie_name, css_title, style_cookie_duration );
  }
}

// *** Cookies functions to save and retrive cookies of style ***

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) switch_style(c.substring(nameEQ.length,c.length));
	}
	return null;
}

function readFontCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) setFontSize(c.substring(nameEQ.length,c.length));
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}