/***
 * index.js
 *		File that stores Javascript functions for the main page
 **/

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;
//document.onClick = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

var activeLayer = document.getElementById( "gHolder" ) ;


// Main function to retrieve mouse x-y pos.s

function setLayer( obj )
{
	if( activeLayer )
		activeLayer.style.top = "0px";	
	activeLayer = obj ;
	
}

function resetLayer( obj )
{
	if( activeLayer )
		activeLayer.style.top = "0px";	
}

function getMouseXY(e) {

if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
//  document.Show.MouseX.value = tempX
//  document.Show.MouseY.value = tempY
  
  var sw = false;
  

  holder_w = parseInt( document.getElementById( "gHolder" ).style.width.split("px",1) );
//  holder_l = parseInt( document.getElementById( "gHolder" ).style.left.split("px",1) );
	holder_l = 100;
//  holder_t = parseInt(document.getElementById( "gHolder" ).style.top.split("px",1));
  holder_t = 690;
  holder_h = parseInt(document.getElementById( "gHolder" ).style.height.split("px",1));

//  holder_l = 8 ;

//	if( tempX < 200 ) alert( "l: " + holder_l + "; " +  "t: " + holder_t + "; " +  "w: " + holder_w + "; " +  "h: " + holder_h + "; " );

  if( ( tempX - holder_l > 0 ) && ( holder_l + holder_w - tempX > 0 ) )
  	sw = true ;
  
  
  if( ( tempY > holder_t ) && (tempY < holder_t + holder_h ) && sw )
  {
	  
	  holder_dif = tempY - holder_t ;
	  holder_p = Math.round( holder_dif * 100 / holder_h ) ;

	  
	  layer_p = holder_p;
	  layer_h = parseInt(activeLayer.style.height.split("px",1));
	  
	  layer_t = layer_p * ( layer_h - holder_h ) / 100 ;

//	  if( tempY < 270 ) alert("l_t:"+layer_h);

//	  alert( layer_t );
	  activeLayer.style.top =  Math.round( -1 * layer_t )  ;
  }
  
  return true
}


/*
var featured_active = 'Video' ;
var featured_active_key = 0 ;

var popular_active = 'Video' ;
var popular_active_key = 0 ;

var latest_active = 'Video' ;
var latest_active_key = 0 ;
*/

//alert(featured_active_key);


var isLoading = false ;

//  ############################################################## FEATURED ADDITIONS

// function called when user clicks on a Featured Menu Button
function featuredMenuClicked( key, title )
{
	if( !isLoading )
	{
		for( i=0; i<4; i++)
		{
			featureItemContent = '        <div id="featuredItem" >        	<div class="title">                <div class="titleLeft">                	<h1><a href="#">Loading title ...</a></h1>                    <div class="titleDesc"> 0 views</div>                </div>                <div class="titleRight">Comments: <span class="white">0</span></div>            </div>            <div class="content">           	  <div class="pic90"></div>                <div class="picText">Loading description ...</div>            	<div class="picBtn"><a href="#">&nbsp;</a></div>            </div>        </div>';
			eval( 'obj=document.getElementById( \'featuredItem_'+i+ '\' );');
			obj.innerHTML = featureItemContent ;
			
		}
		document.getElementById( 'featuredMenuItem_' + key ).innerHTML = '<div class="selected">'+title+'</div>';
		document.getElementById( 'featuredMenuItem_' + featured_active_key ).innerHTML = '<a class="link" onclick="javascript:featuredMenuClicked('+featured_active_key+', \''+featured_active+'\');">'+featured_active+'</a>' ;
		featured_active_key = key;
		featured_active = title;
		
		featuredItemsCall( key );
	}
}

/***
 *	Functie ce afiseaza noile contenturi din featured additions
 **/
function featuredItemsShow()
{ 
	var z=0;
	//alert( ajax.response );
	eval( 'var ar = ' + ajax.response + ';' ) ;
	for ( x in ar )
	{
		y = ar[x] ;
		//alert(y.key );
		if( y.key )
		{
		featureItemContent = '        <div id="featuredItem" >        	<div class="title">                <div class="titleLeft">                	<h1><a href="'+root_path+'show/'+y.key+'/'+y.file+'">'+y.title+'</a></h1>                    <div class="titleDesc">'+y.views+' views</div>                </div>                <div class="titleRight">Comments: <span class="white">'+y.comments+'</span></div>            </div>            <div class="content">           	  <div class="pic90"><a href="'+root_path+'show/'+y.key+'/'+y.file+'"><img src="'+root_path+y.pic+'" width="90" height="67" border="0"></a></div>                <div class="picText">'+y.desc+'</div>            	<div class="picBtn"><a href="'+root_path+'show/'+y.key+'/'+y.file+'">View now !</a></div>            </div>        </div>';
		eval( 'obj=document.getElementById( \'featuredItem_'+x+ '\' );');
		obj.innerHTML = featureItemContent ;
		}
	}
	isLoading = false ;
}


function featuredItemsCall( key )
{
	isLoading = true;
	ajax.requestFile = root_path + 'application/ajax/www/featuredItems.php?key=' + key +'&lang=' + site_lang;	// Specifying which file to get
	ajax.onCompletion = featuredItemsShow;	// Specify function that will be executed after file has been found
	ajax.runAJAX();		// Execute AJAX function		
}


//  ############################################################## POPULAR ADDITIONS

// function called when user clicks on a Popular Menu Button
function popularMenuClicked( key, title )
{
	if( !isLoading )
	{
	
		for( i=0; i<8; i++)
		{
			popularItemContent = '<div id="popularItem">                	<div class="pic90"></div>                    <div class="picText">Loading ...</div>                    <div class="picDesc">0 views</div>                </div>';
			eval( 'obj=document.getElementById( \'popularItem_'+i+ '\' );');
			obj.innerHTML = popularItemContent ;
			
		}
		document.getElementById( 'popularMenuItem_' + key ).innerHTML = '<div class="btn_selected">'+title+'</div>';
		document.getElementById( 'popularMenuItem_' + popular_active_key ).innerHTML = '<a onclick="javascript:popularMenuClicked('+popular_active_key+', \''+popular_active+'\');">'+popular_active+'</a>' ;
		popular_active_key = key;
		popular_active = title;
		
		popularItemsCall( key );
	}
	
}

/***
 *	Functie ce afiseaza noile contenturi din featured additions
 **/
function popularItemsShow()
{ 
	var z=0;
	//alert( ajax.response );
	eval( 'var ar = ' + ajax.response + ';' ) ;
	for ( x in ar )
	{
		y = ar[x] ;
		//alert(y.key );
		if( y.key )
		{
		popularItemContent = '<div id="popularItem">                	<div class="pic90"><a href="'+root_path+'show/'+y.key+'/'+y.file+'"><img src="'+root_path+y.pic+'" width="90" height="67" border="0"></a></div>                    <div class="picText">'+y.title+'</div>                    <div class="picDesc">'+y.views+' views</div>                </div>';
		eval( 'obj=document.getElementById( \'popularItem_'+x+ '\' );');
		obj.innerHTML = popularItemContent ;
		}
	}
	isLoading = false ;
}


function popularItemsCall( key )
{
	isLoading = true;	
	ajax.requestFile = root_path + 'application/ajax/www/popularItems.php?key=' + key +'&lang=' + site_lang;
	ajax.onCompletion = popularItemsShow;	// Specify function that will be executed after file has been found
	ajax.runAJAX();		// Execute AJAX function		
}


//  ############################################################## LATEST ADDITIONS

// function called when user clicks on a Latest Menu Button
function latestMenuClicked( key, title )
{
	if( !isLoading )
	{
		
		for( i=0; i<21; i++)
		{
			latestItemContent = '<div id="latestItem" align="center"><div class="pic120"></div><a href="#">0 views</a></div>';
			eval( 'obj=document.getElementById( \'latestItem_'+i+ '\' );');
			obj.innerHTML = latestItemContent ;
			
		}
		document.getElementById( 'latestMenuItem_' + key ).innerHTML = '<div class="btn_selected">'+title+'</div>';
		document.getElementById( 'latestMenuItem_' + latest_active_key ).innerHTML = '<a onclick="javascript:latestMenuClicked('+latest_active_key+', \''+latest_active+'\');">'+latest_active+'</a>' ;
		latest_active_key = key;
		latest_active = title;
		
		latestItemsCall( key );
	}
}

/***
 *	Functie ce afiseaza noile contenturi din featured additions
 **/
function latestItemsShow()
{ 
	var z=0;
	//alert( ajax.response );
	eval( 'var ar = ' + ajax.response + ';' ) ;
	for ( x in ar )
	{
		y = ar[x] ;
		//alert( x );
		if( ( y.key ) && (x<21))
		{		
		latestItemContent = '<div id="latestItem" align="center"><div class="pic120"><a href="'+root_path+'show/'+y.key+'/'+y.file+'"><img src="'+root_path+y.pic+'" width="120" height="90" border="0" onMouseOver="return overlib(\'<b>'+y.title+'</b><br>'+y.comments+' comments\',ol_width=180,ol_height=20,ol_offsetx=-90);" onMouseOut="return nd();"/></a></div><a href="'+root_path+'show/'+y.key+'/'+y.file+'">'+y.views+' views</a></div>';
		eval( 'obj=document.getElementById( \'latestItem_'+x+ '\' );');
		obj.innerHTML = latestItemContent ;
		}
		if( x>0 )
			z=x;
	}
	//alert(z);
	if(z<20)
	{
		i=parseInt(z)+1;
		while( i<21 )
		{
			eval( 'obj=document.getElementById( \'latestItem_'+i+ '\' );');
			obj.innerHTML = '' ;			
			i++;
		}
	}
	isLoading = false ;
	
}


function latestItemsCall( key )
{
	isLoading = true;	
	ajax.requestFile = root_path + 'application/ajax/www/latestItems.php?key=' + key +'&lang=' + site_lang;
	ajax.onCompletion = latestItemsShow;	// Specify function that will be executed after file has been found
	ajax.runAJAX();		// Execute AJAX function		
}



