var rb_message_arr=new Array();
rb_message_arr['PROCESSING']='Loading.....';

var last_location='';
var isProcessingURL=false;
var loadedobjects='';

function $i(id){return document.getElementById(id);}
function $n(n){return document.getElementsByName(n);}
function $t(tn){return document.getElementsByTagName(tn);}
function $ptr(ref){return (ref.nodeName.toLowerCase()=='tr')?ref:(ref.parentNode)?$ptr(ref.parentNode):null;}
function $pT(ref,t,c){if(typeof t=='undefined'){return t.parentNode;}if(!ref.parentNode){return null;}if(typeof c=='undefined'){c=0;}if(ref.parentNode.nodeName.toLowerCase()==t.toLowerCase() && c==0){return ref.parentNode;}else if(ref.parentNode.nodeName.toLowerCase()==t.toLowerCase()){c--;return $pT(ref.parentNode,t.toLowerCase(),c);}else{return $pT(ref.parentNode,t.toLowerCase(),c);}}

function show_status(msg){
	if(typeof msg=='undefined'){
		$('#status_msg_div').hide();
		return;
	}
	$('#status_msg_div').html(msg);
	$('#status_msg_div').show();	
	var status_div=document.getElementById('status_msg_div');
	//status_div.style.top=iecompattest().scrollTop+'px';	
	//status_div.style.left = '0px';
}

$.ajaxSetup({
  cache	: false
});

function initAjaxLink(){
	//first clear previous binding
	$(".ajaxlink").unbind('click', parseUrl);
	$('.ajaxlink').click(parseUrl);	
	
	$(".image_link").unbind('click', parseUrl);
	$('.image_link').click(parseUrl);	
	
	$('.inpageLink').click(function() {
          if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')&& location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target|| $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
              var targetOffset = $target.offset().top-15;
              $('html,body').animate({scrollTop: targetOffset}, 3000,'easeOutBounce');
             return false;
         	}
  		} 
	});  
}
     

function parseUrl(){
	//give it a time to change url
	if(isProcessingURL){
		window.location=last_location;
		return false;
	}
	
	isProcessingURL=true;
	setTimeout(function(){		
			document.title=':|: BGCCI :|:';		
	if(location.hash==''){
		isProcessingURL=false;
		return false;
	}
	var arr=location.hash.split('#');
	
	if(arr.length<1){
		isProcessingURL=false;
		return false;
	}
	var hashurl=arr[1];
	if(hashurl==''){
		isProcessingURL=false;
		return;	
	}
	//alert(hashurl)	;
	var par_arr=hashurl.split('/');
	
	window.location=location.hash.replace('.php','');
	var ex_p=(par_arr.length>1)?("&"+par_arr[1]):'';
	var requesturl='parseurl.php?p='+par_arr[0]+ex_p;
	show_status(rb_message_arr['PROCESSING']);
	$.ajax({
		   type		: "GET",
		   url		: requesturl,
		   success	: function(x){
			  document.getElementById('main_body_content').innerHTML=x;
			  if(arr.length>2)
			  	if(typeof arr[2]=='function')
			  		arr[2]();
				else
					eval(arr[2]+'()');
					
			 //Position the page
			 $target = $('[name=topanchor]');
			 if ($target.length) {
				  var targetOffset = $target.offset().top-10;
				  $('html,body').animate({scrollTop: targetOffset}, 1000);
			 }
			 
			  //Re initialize the link for lodaded page
			  initAjaxLink();
			  //Fix Png
			  //correctPNG();
			  show_status();
			  isProcessingURL=false;
			  last_location=location;	
		   },
		   	error:function(xmlHttp, textStatus, errorThrown){
			   show_status();
			   alert('error in communication, error no = '+ xmlHttp.status);
			   isProcessingURL=false;
		   }
		 });					
	},200);
	
}

function Round(num,n){	
	if(isNaN(num))
		return '0.00';
	return Math.round(num * Math.pow(10,n))/Math.pow(10,n);
}



function translate(f){$.ajax({
		   type		: "GET",
		   url		: "transtator.php?f="+f,
		   success	: parseUrl 
		 });				
}


	var modalPop={
	load:function(url,c){
		d = document;
		var ContentDiv;
		if(d.getElementById("modalBox")){
			ContentDiv=d.getElementById("modalBox");
		}else{			//Create Modal Popup
			mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
			mObj.id="modalContainer";
			mObj.style.height = document.documentElement.scrollHeight + "px";
			mObj.style.display="none";
			ContentDiv=d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
			ContentDiv.id = "modalBox";
			
		}
		show_status(rb_message_arr['PROCESSING']);
		$(ContentDiv).load(url,new Date().getTime(),function(){
															 show_status();
															 ContentDiv.style.left = (document.body.clientWidth - ContentDiv.offsetWidth)/2 + "px";
															 //ContentDiv.style.top =(document.body.clientHeight-ContentDiv.offsetHeight)/2;
															 mObj.style.display="";
															 if(typeof c!='undefined'){
																if(typeof c=='function')
																	eval(c)();
																else
																	eval(c);
																return true;
															}
														});
	},
	
	close:function(){
		try{
			document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));	
			document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalBox"));
		}
		catch(exception){
			//Nothing to do 
		}
	}
}

function translate(f){$.ajax({
		   type		: "GET",
		   url		: "transtator.php?f="+f,
		   success	: function(x){
			   if(window.location.hash=="")
			   		window.location="#home";
			   parseUrl();
		   },
		   error:function(xmlHttp, textStatus, errorThrown){
			   show_status();
			   alert('error in communication, error no = '+ xmlHttp.status);
		   }
		 });				
}

function nevigate_menu(d){
	if(typeof d=='undefined'){
		$('#div_bd_content,#div_gr_content').slideUp("slow");
		return;
	}
	ref=document.getElementById(d);
	var hd='';
	if(ref.style.display!='none'){
		$(ref).slideUp("slow");	
		return;
	}
	else{
		$(ref).slideDown('slow');
		if(d=='div_bd_content'){
			hd='#div_gr_content';
		}
		else if(d=='div_gr_content'){
			hd='#div_bd_content';
		}
		$(hd).slideUp("slow");
	}
}
	var modalPop={
	load:function(url,c){
		d = document;
		var ContentDiv;
		if(d.getElementById("modalBox")){
			ContentDiv=d.getElementById("modalBox");
		}else{			//Create Modal Popup
			mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
			mObj.id="modalContainer";
			mObj.style.height = document.documentElement.scrollHeight + "px";
			mObj.style.display="none";
			ContentDiv=d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
			ContentDiv.id = "modalBox";
			
		}
		show_status(rb_message_arr['PROCESSING']);
		$(ContentDiv).load(url,new Date().getTime(),function(){
															 show_status();
															 ContentDiv.style.left = (document.body.clientWidth - ContentDiv.offsetWidth)/2 + "px";
															 //ContentDiv.style.top =(document.body.clientHeight-ContentDiv.offsetHeight)/2;
															 mObj.style.display="";
															 if(typeof c!='undefined'){
																if(typeof c=='function')
																	eval(c)();
																else
																	eval(c);
																return true;
															}
														});
	},
	
	close:function(){
		try{
			document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));	
			document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalBox"));
		}
		catch(exception){
			//Nothing to do 
		}
	}
}
