//var tb_pathToImage = "http://dev.unlockingonline.com/images/loader.gif";
var tb_pathToImage = "/images/loader.gif";


//on page load call tb_init
$(document).ready(function(){   
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
});



//Showing of the loaded window
function my_onload(data){
$("#TB_load").remove();
/*$("#TB_window").animate({
	opacity: 'show'
	}, 300);
*/	
$("#TB_window").fadeIn(300);
tb_position();
}


//Open popup window
function my_popup(modal, arg, model, network_id){
	
var arg = arg || '';
	try {		
		switch(modal){
			case 1: 
			case 'payment_maintenance':
				TB_WIDTH=710;				
				break;
			default:
				TB_WIDTH=450;
				break;
			}		
	
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});			
			$("html").css("overflow","hidden");			
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}
		$("#TB_window").html('<div class="window" id="mypopup-content"></div>');		
		tb_position();		
		$("#mypopup-content").css("width", TB_WIDTH+"px");
		
		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#TB_load').show();//show loader

		var req="/popup_order_ajax.php?window="+modal;
		if(arg != '')req=req+"&order_id="+arg;	
						 						
		$("#mypopup-content").load(req, my_onload);
		
		if(tb_detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}		

	
	} catch(e) {
		//nothing here
		alert("ERROR in Javascript code!");
	}

}


//helper functions below

function tb_remove() {
	$("#TB_error").remove();	
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut(300,function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});


	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_position() {
var height  = document.getElementById('TB_window').offsetHeight;	
$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	if ( !(typeof document.body.style.maxHeight === "undefined")) { // take away IE6
		$("#TB_window").css({marginTop: '-' + parseInt((height / 2),10) + 'px', width: TB_WIDTH + 'px'});		
	}
}

function tb_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}


/**********************************************************************************************************/
/*									ORDER PROCESSING FUNCTIONS						           			  */
