//****************************************************************************************
//Narnoo.com Client Video Light box
//Version 2.2 Updated 10/11/09
//info@narnoo.com
//Developed by: James Wells
//****************************************************************************************
var alreadyrunflag=0 //flag to indicate whether target function has already been run

if (document.addEventListener)
  document.addEventListener("DOMContentLoaded", function(){alreadyrunflag=1; rules(); }, false)
else if (document.all && !window.opera){
  document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>')
  var contentloadtag=document.getElementById("contentloadtag")
  contentloadtag.onreadystatechange=function(){
    if (this.readyState=="complete"){
      alreadyrunflag=1
      rules()    
	  }
  }
}

window.onload=function(){
  setTimeout("if (!alreadyrunflag){rules();}", 0)
}

//********************************
//Injects the CSS into the webpage
//********************************
function rules(){
var headID = document.getElementsByTagName("head")[0];         
var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.href = 'http://www.moa.narnoo.com/css/narnoovideo-box.css';
headID.appendChild(cssNode);
}
//********************************
//Set variables
//********************************
var vid_id;
var vid;
var user;
var user_id;
var webpage;
var urlpage;
//setup skin..
var noooverlay_window;
var noolight_window;
var noowindow_close;
var noowindow_box;
//fadein
var noobackground;
var noobox;
var windowWidth; 
var windowHeight;
var leftSpace; 
var topSpace;
var leftMarginWidth;
var topMarginHeight;
var scrOfY=0;

//**********************************
//Get Page Height
//**********************************

function narnooVideo(user,vid,webpage){
user_id=user;
vid_id=vid;
urlpage=webpage;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		
		
		if( typeof( window.pageYOffset ) == 'number' ) {
			 //Netscape compliant
			scrOfY = window.pageYOffset;
			} else if( document.body && ( document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			} else if( document.documentElement && ( document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			}
				
				
				totalHeight = windowHeight + scrOfY;
				leftSpace = ((windowWidth - 543) / 2);
				topSpace = ((totalHeight - 379) / 2);
				leftMarginWidth = ((leftSpace /windowWidth)*100);
				topMarginHeight = ((topSpace /windowHeight)*100);
				
				setnarnooWindow();
		
}

//**********************************
//Set Window DOM
//**********************************
noooverlay_window = document.createElement("div");
noolight_window = document.createElement("div");
noowindow_close = document.createElement("div");
noowindow_box = document.createElement("div");

noooverlay_window.id = "noolayout";
noolight_window.id = "noowindow-box";
noowindow_close.id = "noowinclose-box";
noowindow_box.id = "noomedia-box";
noowindow_close.setAttribute("onclick","deactivatevideo()");
noowindow_close.style.cursor = 'pointer';

function setnarnooWindow(){

document.body.appendChild(noooverlay_window);
document.body.appendChild(noolight_window);
noolight_window.appendChild(noowindow_close);
noolight_window.appendChild(noowindow_box);
initvideoFade();

}
//********************************
//Starts Fade In
//********************************

function initvideoFade() {
	noobackground = document.getElementById("noolayout");
	noobackground.style.display = "block";
 	noobackground.style.visibility = "visible";
	noobackground.style.height=totalHeight+"px";
	
	for (var i=0;i<8;i++)
		setTimeout("setOpacity("+i+")",30*i);
	
	setVisiblevideoElements();
}

function setOpacity(value)
{
	noobackground.style.opacity = value/10;
	noobackground.style.filter = "alpha(opacity="+value*10+")";
}
//********************************
//Displays media contents
//********************************
function setVisiblevideoElements()
{
	
	//noobox = document.getElementById('noowindow-box');
	document.getElementById("noowindow-box").style.display="block";
	document.getElementById("noowindow-box").style.visibility="visible";
	document.getElementById("noowindow-box").style.left=leftMarginWidth+"%";
	document.getElementById("noowindow-box").style.top=topMarginHeight+"%";
	loadVideo();
	
}
//********************************
//Loads the video media
//********************************
function loadVideo()
{
	//Set video in iFrame.
	noowindow_box.innerHTML = "<iframe width=\"543px\" height=\"328px\" scrolling=\"no\" frameborder=\"0\" src=\"http://www.vmedia.narnoo.com/pvideo.php?id="+user_id+"&vid="+vid_id+"&page="+urlpage+"\"></iframe>";
	//Set Close Window.
	noowindow_close.innerHTML="<table width=\"543\" height=\"18\" ><tr class=\"nooclose\"><td align=\"right\"><a href=\"javascript:deactivatevideo()\">X Close Window</a></td></tr></table>";
	
}
//********************************
//Deactivates Lightbox
//********************************
function deactivatevideo(){
 
  var p = document.body;	
  
  noolight_window.removeChild(noowindow_box);
  noolight_window.removeChild(noowindow_close);
  p.removeChild(noolight_window);
  p.removeChild(noooverlay_window);
  
  noobackground.style.opacity = '0';
  noobackground.style.filter = 'alpha(opacity=0)';
  noobackground.style.display = 'none';
  noobackground.style.visibility = 'hidden';
  noowindow_box.innerHTML = "";
  noowindow_close.innerHTML = "";
}