//********* GetElementByClass *********//
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

//********* Rollover *********//
var array_image_hover = getElementsByClass('rollover');
var array_preload_image = new Array();
var preload_image;

// Preload images
for (x in array_image_hover){
	preload_image= new Image(); 
	preload_image.src=array_image_hover[x].src.replace('_off','_on'); 
	array_preload_image.push(preload_image); 
}

// Add rollover event
for (x in array_image_hover){
	array_image_hover[x].onmouseover = function(){this.src = this.src.replace('_off','_on');}
	array_image_hover[x].onmouseout = function(){this.src = this.src.replace('_on','_off');}
}

//********* Animation *********//
var timer = new Array();
var delayedcall;
var first_click = true;
var field_focus;

if(run_animation!=false){
var run_animation = true;
}

// Add animation event
document.getElementById('um_lk_account').onclick = function(){reset_click(this);animate(this,'height',180,30);}
document.getElementById('um_lk_news').onclick = function(){reset_click(this);animate(this,'height',207,30);}
document.getElementById('um_lk_games').onclick = function(){reset_click(this);animate(this,'height',373,30);}


// Add clear event 
document.getElementById('um_lk_account').onmouseover = function(){clearTimeout(delayedcall);}
document.getElementById('um_ct_account').onmouseover = function(){clearTimeout(delayedcall);}
document.getElementById('um_lk_news').onmouseover = function(){clearTimeout(delayedcall);}
document.getElementById('um_ct_news').onmouseover = function(){clearTimeout(delayedcall);}
document.getElementById('um_lk_games').onmouseover = function(){clearTimeout(delayedcall);}
document.getElementById('um_ct_games').onmouseover = function(){clearTimeout(delayedcall);}

// Add event for the rollout
document.getElementById('um_lk_account').onmouseout = function(){timer_delay(document.getElementById('um_ct_account'), 10000);}
document.getElementById('um_ct_account').onmouseout = function(){timer_delay(this, 1000);}
document.getElementById('um_lk_news').onmouseout = function(){timer_delay(document.getElementById('um_ct_news'), 500);}
document.getElementById('um_ct_news').onmouseout = function(){timer_delay(this, 500);}
document.getElementById('um_lk_games').onmouseout = function(){timer_delay(document.getElementById('um_ct_games'), 500);}
document.getElementById('um_ct_games').onmouseout = function(){timer_delay(this, 500);}


function animate(obj,elm,duration,fps,aback) {

	var backup = obj;
	
	// Detect if obj parametre is the DIV
	if(obj.id.match(/lk/i)){
		obj = document.getElementById(obj.id.replace('lk','ct'));
	}
	// Close the DIV if open
	if(obj.style.visibility == "visible"){
		// Switch the button to OFF
 document.getElementById(obj.id.replace('ct','lk')).getElementsByTagName("img")[0].src=document.getElementById(obj.id.replace('ct','lk')).getElementsByTagName("img")[0].src.replace('_on','_off');
    aback = true;
	}
	
	var current_height = obj.clientHeight;
	
	//Animation code
	if(run_animation == true){
		clearall();
		if(!duration) duration = 1000;
		if(!fps)      fps      = 20;
		
		if(!aback){
  	  // if opening, prepare object for animation
  	  obj.style.height = 0;
    	obj.style.visibility = "visible";
    	// animate from 0 to object height
    	begin = 0;
    	end = parseFloat(current_height);
  	}else{
  	  // animate from object height to 0
    	begin = parseFloat(current_height);
    	end = 0;
  	}
  	
		duration  = parseFloat(duration);
		fps       = parseFloat(fps);
		
		var change      = end-begin;
		var interval    = Math.ceil(1000/fps);
		var totalframes = Math.ceil(duration/interval);
		var step        = change/totalframes;
	
	
		for(i=1;i<= totalframes;i++) {
			(function() {
				var frame=i;
				function inner() {
					var increase=easeQuad(begin,change,totalframes,frame);
					unit=(elm=='opacity') ? '' : 'px';
					if(window.attachEvent && !unit) { 
						increase*=100; 
						obj.style.zoom = 1;
						obj.style.filter = "alpha(opacity=" + increase + ")";
					} else {
						obj.style[elm]  = increase+unit; 
					}
					
					if(aback && obj.style[elm] == '0px'){
					  obj.style.visibility = "hidden";
					  obj.style[elm] = begin + unit;
					}
					
			   }
			   timer[i] = setTimeout(inner,interval*frame);
			})();
		}
	}else{
	  if(!aback){
	    obj.style.visibility = 'visible';
	  } else {
	    obj.style.visibility = 'hidden';
	  }
	}
}

function clearall(){
	// Clear every timer on the page
	for (i=0; i<timer.length;i++){window.clearTimeout(timer[i]);}
}

function reset_click(obj){
	// Detect if obj parametre is the DIV
	if(obj.id.match(/lk/i)){
		ctobj = document.getElementById(obj.id.replace('lk','ct'));
	}
	
	// Put every other DIV visibility to hidden
  var array_checkbox = getElementsByClass('ctbox');
  for (x in array_checkbox){
if(array_checkbox[x].style){
	if(first_click) {
		array_checkbox[x].style.display="block";
		array_checkbox[x].style.visibility="hidden";
	} else {

    if (array_checkbox[x] != ctobj){array_checkbox[x].style.visibility="hidden";}
}
}   
  }
 first_click = false;
 
	// Put every button to OFF state
	for (x in array_image_hover){
		if(array_image_hover[x].src){
			array_image_hover[x].src = array_image_hover[x].src.replace('_on','_off');
		}
	}
	
	// Remove the mouseout event for the button clicked
	obj.getElementsByTagName("img")[0].onmouseout='';
	
	// Put the on state to the button clicked
	obj.getElementsByTagName("img")[0].src=obj.getElementsByTagName("img")[0].src.replace('_off','_on');
}

function animate_back(obj){
	clearall();
	// Closing animation function call
	if(obj.clientHeight > 0) {
		animate(obj,'height',150,60, true);
	}
}

function easeQuad(begin,change,totalframes,frame) {
	// Easeout equation
	return (change*frame/totalframes)*(frame/totalframes)+begin;
}

function timer_delay(obj, timeout_var){

	delayedcall = setTimeout(function(){
		// Closing animation function call
		animate_back(obj);
		// Put all image to OFF state
		for (x in array_image_hover){
			if(array_image_hover[x].src){
				array_image_hover[x].src = array_image_hover[x].src.replace('_on','_off');
			}
		}
		// Add the rollover mouseout event
		for (x in array_image_hover){
			if(array_image_hover[x].src){
				array_image_hover[x].onmouseout = function(){this.src = this.src.replace('_on','_off');}
			}
		}
	}, timeout_var);
}
function switchGame(gameId) {
	if(thisMovie("FuncomFlashBanner")){thisMovie("FuncomFlashBanner").jsSwitchGame(gameId);}else{
        	document.location.href = "/scene/" + gameId + "/1";
	}
}
function switchGameThumbnail(id) {

	$("#featured img").each(function(){$(this).attr("src", $(this).attr("src").replace("_sel", ""));});

	// Change image to selected state
	var current_game_scene = id;
	var src = $("#featured ."+id).attr("src");
	src = src.replace("_ov", "");
	var ext = src.substring(src.length-4,src.length);
	sel = src.replace(ext, "_sel"+ext);
	$("#featured ."+id).attr("src", sel);
	
}
function toggle_over_state(obj) {
	var match = obj.attr("src").match("_ov");
	if(match==null){
		match = obj.attr("src").match("_sel");
		if(match==null){
			var ext = obj.attr("src").substring(obj.attr("src").length-4,obj.attr("src").length);
			ov = obj.attr("src").replace(ext, "_ov"+ext);
			obj.attr("src", ov);
		}
	}else{
		ov = obj.attr("src").replace("_ov", "");
		obj.attr("src", ov);
	}
}
$(document).ready(function() {
	$(".fc-rollover").each(function(){
		$(this).mouseover(function() {toggle_over_state($(this));});
		$(this).mouseout(function() {toggle_over_state($(this));});
	});
});