//________________Pre-amble_____________________________________________
//Latest Version: 0.40 | Updated: 21/3/2012
//Coded by: Mohd Amir Shah, Design & Multimedia Department
//______________________________________________________________________
$(document).ready(function(){  
    var detective = new Detector();
    var status1 = detective.detect('Traditional Arabic');
    var status2 = detective.detect('Andalus');
    var status3 = detective.detect('Simplified Arabic');
	if (status1 != true || status2 != true || status3 != true){ //exec if either one font not available
		//alert(status);
		centerPopup();
		loadPopup();
	}
	$("#backgroundPopup").click(function(){  
		disablePopup();  
	});  
	$(document).keypress(function(e){  //Press Escape event!  
		if(e.keyCode==27 && popupStatus==1){  
		disablePopup();  
		}  
	});
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	$("#dload").click(function(){
		disablePopup();
	});
}); 

var popupStatus = 0;  //init popup
function loadPopup(){  
    if(popupStatus==0){  
    $("#backgroundPopup").css({"opacity": "0.7"});  
    $("#backgroundPopup").fadeIn("slow");  
    $("#popupContact").fadeIn("slow");  
    popupStatus = 1;  
   }  
}

function disablePopup(){  //disables popup only if it is enabled 
if(popupStatus==1){  
	$("#backgroundPopup").fadeOut("slow");  
	$("#popupContact").fadeOut("slow");  
	popupStatus = 0;  
	}  
}

function centerPopup(){  //request data for centering
  var windowWidth = 0, windowHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    	windowWidth = window.innerWidth;
    	windowHeight = window.innerHeight;
 	 } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
   	 	windowWidth = document.documentElement.clientWidth;
   	 	windowHeight = document.documentElement.clientHeight;
  	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    	windowWidth = document.body.clientWidth;
   	 	windowHeight = document.body.clientHeight;
 	 }
	//var windowWidth = document.documentElement.clientWidth;  
	//var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $("#popupContact").height();  
	var popupWidth = $("#popupContact").width();  
	$("#popupContact").css({  "position": "absolute",  "top": windowHeight/2-popupHeight/2,  "left": windowWidth/2-popupWidth/2});  //centering  
	$("#backgroundPopup").css({"height": windowHeight});  //only need force for IE6  
}  
	function alertSize() {
    var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}
var Detector = function(){//start comparison
	var baseFonts = ['Traditional Arabic','Andalus','Simplified Arabic'];	
	var testString = "mmmmmmmmmmlli";
	var testSize = '72px';
	var h = document.getElementsByTagName("body")[0];
	var s = document.createElement("span");
	s.style.fontSize   = testSize;			
	s.innerHTML        = testString;
	var defaultWidth   = {};
	var defaultHeight  = {};
	for(var index in baseFonts) {
		//get the default width for the three base fonts
		s.style.fontFamily = baseFonts[index];
		h.appendChild(s);
		defaultWidth[baseFonts[index]]   = s.offsetWidth;  //width for the default font
		defaultHeight[baseFonts[index]]  = s.offsetHeight; //height for the defualt font
		h.removeChild(s);
	}
	function detect(font) {
		var detected = false;
		for(var index in baseFonts) {
			s.style.fontFamily = font + ',' + baseFonts[index];	// name of the font along with the base font for fallback.
			h.appendChild(s);
			var matched = (s.offsetWidth != defaultWidth[baseFonts[index]] || s.offsetHeight != defaultHeight[baseFonts[index]]);
			h.removeChild(s);
			detected = detected || matched;
		}
		return detected;
	}
	this.detect = detect;
};