//
$detectMacXFF = detectMacXFF();

var gm_client_list = [];
function get_client_list(element){
	var client_list = [];
	$(element).find('[@client_list]').each(function(){
		client_list[client_list.length] = $(this).attr('client_list');																	
	});
	return client_list;
}


$(document).ready(function(){
	gm_client_list = get_client_list('#section_right');						   

	$(".submenu").hover(function(){
		$(this).addClass("submenu_hover");
	},function(){
		$(this).removeClass("submenu_hover");
	}).click(function(){
		var id_page = $(this).attr('id_page');
		overlay_open(id_page);
	});
	
	$('a[@id_page]').attr('href','#').click(function(){
		overlay_open($(this).attr('id_page'));
	});
	

	$('#overlay_close').click(overlay_close);

	$(".flashtext").css('visibility','visible');

	__flashbin.insert('gm_banner',{src:"gm_banner.swf",width:625,height:94,wmode:"transparent"},{gm_banner_text:gm_banner_text});
	
	$(".submenu a.flashtext").each(function(){
		var id_page = $(this).attr('id_page');
		$(this)
			.flashbin({width:548,height:27,wmode:'transparent'},{include:'font/tg.swf',height_resize:'grow','textfield.selectable':false,'callback.click':'overlay_open','callback.param':id_page});
	});
	
	if(detectMacXFF()){
		for(i=0;i<=100;i+=10) MM_preloadImages('img/wt'+(100-i)+'.png');
		$("#page_group_background").css('background-image','url(img/wt70.png)');
		
	}else
		$("#page_group_background").css({'opacity':'0.7','background-color':'#FFFFFF'});
	
	//we need to check this till all flash titles load completely
	if($('.page_group_content').size()){
		var $pgb = 	$("#page_group_background");
		var $pgc =  $('.page_group_content').eq(0);
		var func_count = 40;
		var func = function(){
			$pgb.css('height',$pgc.height()+'px')
			func_count--;
			if(func_count>0) setTimeout(func,250); 
		};
		func();
	}



	if(background_list && background_list.length){
		$('#background').html('<img src="'+background_list[0]+'" border="0" />');
		setTimeout('background_list_change()',5000);	
	}

});


//BEGIN BACKGROUND ROTATION CODE
var background_list_i = 0;
var background_load_set = false;
function background_list_change(){
	new TimerIterate(20,100,0,-10,
		background_set_opacity,
		function(){
			background_list_i++;
			if(background_list_i >= background_list.length) background_list_i = 0;
			if(!background_load_set){
				$('#background img').load(function(){
					new TimerIterate(20,0,100,10,
						background_set_opacity,
						function(){
							setTimeout('background_list_change()',10000);	
						}
					);
				});		
				background_load_set = true;
			}
			$('#background img')[0].src = background_list[background_list_i];										 
		}		
	);
}

function background_set_opacity(i){
	if(detectMacXFF())
		$('#background_shade').css('background-image','url(img/wt'+(100-i)+'.png);' );
	else
		$('#background img').css('opacity', i / 100);	

}
//END BACKGROUND ROTATION CODE


//BEGIN OVERLAY CODE
var id_page_open = 0;
function overlay_open(id_page,overlay_load_skip){
	if(!id_page) return;
	if(id_page_open == id_page) return;
	
	$('.page_group .submenu').css('visibility','hidden');

	if(!overlay_load_skip){
		$('#overlay_content').css('visibility','hidden');
		overlay_load(id_page);
	}

	screen_dim(function(){
		$('#overlay').show();
		$('#overlay_content').css('visibility','visible');	
		if(overlay_load_skip) overlay_flashtext();
	});
	return false;	
}


var gm_id_page_return = 0;
var gm_client_list_overlay = [];
function overlay_load(id_page,id_project,id_page_return){
	id_page_open = id_page;
	//get next and previous client
	var client_list = gm_client_list_overlay.length ? gm_client_list_overlay : gm_client_list;
	var id_page_next = 0;
	var id_page_prev = 0;
	if(client_list.length){
		for(var i=0;i<client_list.length;i++){
			if(client_list[i] == id_page){
				id_page_prev = i>0 ? client_list[i-1] : client_list[client_list.length-1];
				id_page_next = i<client_list.length-1 ?  client_list[i+1] : client_list[0];
				break;
			}
		}		
	};
	$('#overlay_content').html('').load('index.php',{id_page:id_page,id_project:id_project,id_page_return:id_page_return,id_page_prev:id_page_prev,id_page_next:id_page_next,page_view:1},function(data){
		overlay_flashtext();
		gm_id_page_return = $('#overlay_content').find('[@id_page_return]').attr('id_page_return');
		var client_list = get_client_list('#overlay_content');
		if(client_list.length) 	gm_client_list_overlay = client_list;
	});			
	return false;
}


function overlay_flashtext(){
	$("#overlay_content h1.flashtext")
		.css('visibility','visible')
		.flashbin({width:660,height:28},{include:'font/tg.swf',height_resize:'grow'});	
}

function overlay_close(){
	if(gm_id_page_return && gm_id_page_return != 'undefined'){
		overlay_load(gm_id_page_return);
	}else{
		$('#overlay_content').html('');		
		$('#overlay').hide();
	
		$('.page_group .submenu').css('visibility','visible');
	
		screen_undim();	
		gm_client_list_overlay = []
		id_page_open = 0;
	}
	return false;
}

function screen_dim(callback){
	if($('#__dimScreen').size() > 0) return;
	
	var $dim = $('<div></div>').attr({
		id: '__dimScreen'
	}).css({
		position: 'absolute'
		,height: $(document).height() + 'px'
		,left: '0px'
		,top: '0px'
		,width: $(document).width() + 'px'
		,zIndex: 999
	}).appendTo(document.body);
	
	new TimerIterate(10,0,80,40,
		function(i){			 
			screen_dim_opacity($dim,i);
		},
		callback
	);	
}


function screen_undim(){
	var $dim = $('#__dimScreen');
	new TimerIterate(10,80,0,-40,
		function(i){			 
			screen_dim_opacity($dim,i);
		},
		function(){
			$dim.remove();
			if(typeof callback == 'function') callback();									 
		}		
	);	
}

function screen_dim_opacity($dim,i){
	if($detectMacXFF)
		$dim.css('background-image','url(img/bt'+i+'.png)' );
	else 
		//$dim.css({'background':'#5e7f73','opacity': i / 100});#1e382c
		//$dim.css('background-image','url(img/bt'+i+'.png)' );
		$dim.css({'background':'#5e7f73','opacity': i / 100});
}

//END OVERLAY CODE


//BEGIN SUBMENU CODE
function submenu_move(btn,dir){
	var $page_group = $(btn).parents('.page_group');
	var $page_group_next = dir>0  ? $page_group.next('.page_group') : $page_group.prev('.page_group');
	
	$("#page_group_background").css('height',$page_group_next.find('.page_group_content').height()+'px');
	
	$page_group.css('top','-1000px');
	$page_group_next.css('top','auto');

}
//END SUBMENU CODE

//Util functions 
function TimerIterate(interval,iteration_start,iteration_end,iteration_step,callback_iteration,callback_end){
	this.interval = interval;
	this.iteration = iteration_start;
	this.iteration_end= iteration_end;
	this.iteration_step= iteration_step;	
	this.callback_iteration = callback_iteration;
	this.callback_end = callback_end;
	this.iterate();	
}

TimerIterate.prototype.iterate = function(){
	if(
	   (this.iteration_step >= 0 && this.iteration <= this.iteration_end)
		||
	   (this.iteration_step < 0 && this.iteration >= this.iteration_end)
	 ){
		if(this.callback_iteration)
			(this.callback_iteration)(this.iteration);
		if(this.iteration == this.iteration_end){
			if(this.callback_end) (this.callback_end)();
		}
		this.iteration += this.iteration_step;
		var ti = this;
		setTimeout(function(){ti.iterate();},this.interval);
	}else{
		
	}		
}



function detectIEPngFix(){
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	return ( (version >= 5.5) && (version <=6)  && document.body.filters);
}

function detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}



// MM
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
