function NewWindow(mypage,myname,w,h,scroll,pos)
{
	var win = null;
	if(pos=="random")
	{
		LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	}
	if(pos=="center")
	{
		LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
	else if((pos!="center" && pos!="random") || pos==null)
	{
		LeftPosition=0;TopPosition=20
	}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win = window.open(mypage,myname,settings);
}

function toggleBox(toolBox)
{
	var box = document.getElementById(toolBox);
	if (box.style.display == 'block')
		box.style.display = 'none';
	else
		box.style.display = 'block';
}

function oldloadVids(m)
{
	document.getElementById("content_frame").src="/templates/sinailive/vidframe.php?offset=0&m=" + m;
}


function loadBasics(m, cat, subcat) {
	document.getElementById("content_frame").src="/templates/sinailive/vidframe.php?&m="+m+"&cat="+cat+"&subcat="+subcat+"&offset=0";
}

function debug()
{
	alert("Moshe");
}


function loadTeacher(name) {
	document.getElementById("content_frame").src="/templates/sinailive/vidframe.php?&m=teacher&name="+name+"&cat=Self&subcat=All&offset=0";
}

function loadHineni() {
	document.getElementById("content_frame").src="/templates/sinailive/vidframe.php?m=hineni&type=Basics&cat=Self&subcat=All&offset=0";
}

function fixMenus(menu)
{
	var menu = document.getElementById(menu);
	var nodes = menu.childNodes;
	var counter = 0;
	for (i in nodes)
	{
		if (nodes[i].tagName == 'LI')
			nodes[i].firstChild.innerHTML = ':: ' + nodes[i].firstChild.innerHTML;
	}
}

function fixBorders()
{
	document.getElementById('mainlevel-nav').firstChild.firstChild.style.borderLeftWidth = '0px';

	fixMenus('mainlevel-teacher');
	fixMenus('mainlevel-basics');
}

function dosearch()
{
	window.location = 'http://www.sinailive.com/index.php?p=video&m=search&keywords=' + document.getElementById('searchTxt').value + '&offset=0';
}

function toggleComments()
{
	var el = document.getElementById('commentBox');
	el.style.display = (el.style.display == 'none' ? 'block' : 'none' );
}

function loadVid(id, args)
{
	var newLoc = "http://www.sinailive.com/index.php?option=com_content&task=view&id=" + id;
	for (arg in args)
		newLoc += "&" + arg + "=" + args[arg];
//	alert(newLoc);
	window.location = newLoc;
}

function clearText(el)
{
	if (el.value == (el.name + '...'))
	{
		el.value = '';
		el.style.color = 'black';
	}
}

function setText(el)
{
	if (el.value == '')
	{
		el.value = el.name + '...';
		el.style.color = 'gray';
	}
}

function toggleTags(curMenu, oldMenu, showTags, hideTags)
{
	document.getElementById(showTags).style.display = 'block';
	curMenu.style.color = 'white';
	curMenu.style.backgroundColor = '#2094bb';
	document.getElementById(hideTags).style.display = 'none';
	document.getElementById(oldMenu).style.color = 'black';
	document.getElementById(oldMenu).style.backgroundColor = 'white';
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function toggleMenu()
{
	var menu = document.getElementById('basics_dropdown');
	if (menu.style.display == 'none')
	{
		menu.style.display = 'block';
		var basicsMenu = document.getElementById('mainnav-Basics');
		var coords = findPos(basicsMenu);
		menu.style.left = (coords[0] - 4) + "px";
	}
	else if (menu.style.display != 'none')
		menu.style.display = 'none';
}

	//TO BE MOVED TO AJAX SELECT id, title FROM `jos_categories` where id > 13
	var sinaiAjax = {
		beginRequest: function(){	
		   // Internet Explorer
		   try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
		   catch(e) {
			  try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
			  catch(oc) { req = null; }
		   }
		   // Mozailla/Safari
		   if (!req && typeof XMLHttpRequest != "undefined") { req = new XMLHttpRequest(); }
		
		   // Call the processChange() function when the page has loaded
		   if (req != null) {
				switch (origin){
					case "processChange":
					req.onreadystatechange = processChange;
					break;
					case "ajaxStatusUpdate":
					req.onreadystatechange = ajaxStatusUpdate;
					break;
					case "ajaxPreviewChapters":
					req.onreadystatechange = ajaxPreviewChapters;
					break;
					case "ajaxStateUpdate":
					req.onreadystatechange = ajaxStateUpdate;
					case "ajaxIncrementUpdate":
					req.onreadystatechange = ajaxIncrementUpdate;			
					break;
					default : alert("Please try again your request did not process!");
				}		 
				req.open("GET", url, true);
				req.send(null);
			}
		},
		delegateRequest: function(oRequest){
			if (req.readyState == 4 && req.status == 200) {
				var callBackInfo = req.responseText;
				params=callBackInfo.split("::").join("");
				previewChapters();
			}
		},
		getE: function(oDiv){
			return document.getElementById(oDiv);
		},
		getItem: function(){
			var items = root.getElementsByTagName("item");
			for (var i = 0 ; i < items.length ; i++) {
				// get one item after another
				var item = items[i];
				// now we have the item object, time to get the contents
				// get the name of the item
				var name = item.getElementsByTagName("name")[0].firstChild.nodeValue;
				// get the quantity
				var quantity = item.getElementsByTagName("quantity")[0].firstChild.nodeValue;
				alert("item #" + i + ": name=" + name + " quantity=" + quantity);
			}		
		},
		insertE: function(newHtml, eleName){
			var newEle = document.createElement('div');
			getE(eleName).appendChild(newEle);
			newEle.innerHTML = newHtml;
		}
	}
	
	var sinaiVideo = {
		constructPlayer : function(videoXml){
			var s1 = new SWFObject("http://www.iwalet.com/swf/mediaplayer.swf","naalehMp","380","385","7");
			s1.addParam("allowfullscreen","true");
			s1.addVariable('autostart','true');
			s1.addVariable('showdigits','true');	
			s1.addVariable('width','380');
			s1.addVariable('height','385');
			s1.addVariable('displayheight','280');
			s1.addVariable('repeat','list');
			s1.addVariable('enablejs', 'true');
			s1.addVariable('playid',vId);
			s1.addVariable('javascriptid','naalehMp');
			s1.addVariable('file',videoXml);	
			s1.write("player1");
		},
		delegateVideo: function(sTitle, videoXml){
			sinaiVideo.changeTitle(sTitle);
			sinaiVideo.constructPlayer(videoXml);
			sinaiVideo.changeTags();
		},
		firstLoad: function(videoXml){
			sinaiVideo.constructPlayer(videoXml);
			
		},
		changeTags: function(sTags){
			//To Be Delegated From Flash
			if(typeof(sTags)=="undefined"){
				var sTags="not loaded yet";
			}
			sinaiAjax.getE("tags").innerHTML = sTags;
		},
		changeTitle: function(sTitle){
			sinaiAjax.getE("title").innerHTML = "<h3>"+sTitle+"</h3>";
		},
		sendEvent: function(typ,prm) { 
			sinaiVideo.thisMovie("naalehMp").sendEvent(typ,prm);
		},
		setAssocText: function(sText){
			sinaiAjax.getE("assocText").innerHTML = sText;
		},
		setTags: function(sTags){
//			sinaiAjax.getE("vTags").innerHTML = sTags;
		},
		thisMovie: function(movieName){
			// This is a javascript handler for the player and is always needed.
			if(navigator.appName.indexOf("Microsoft") != -1) {
				return window[movieName];
			} else {
				return document[movieName];
			}
		}
	}
	
	var sinaiForm = {
		getValues: function(sValue, sTitle){
			var videoXml = 'http://www.iwalet.com/xml/categories/list_'+sValue+'.xml';
			sinaiVideo.delegateVideo(sTitle, videoXml);
		}
	}
		
	window.onload = function(){
		//Default XML for Test
		if(sinaiAjax.getE('player1')){
			var videoXml = 'http://www.iwalet.com/xml/categories/list_'+vCatid+'.xml';		
			sinaiVideo.firstLoad(videoXml);
		}
	}