$(window).load(function() {
	//initialize map
	loadPrevMap();
});

function showZoom(areaObj,back,mapImgDiv,mapImgId,mapName,siteNm){
	var regionId = $(areaObj).attr('id');
	var s = '';
	if (siteNm){
		var sArr = siteNm.split('website=');
		s = sArr[1] +"/";
	}
	$('#map').fadeOut(500, function(){
		$('#map').empty();
		$('#map').empty().css({
			width: '850px',
			height: '414px',
			backgroundImage: 'url('+ _GLOBAL_ROOT +'webImages/home/map/'+ s + regionId +'_zoom.gif)',
			position: 'relative'
		});
		var secMap = document.getElementById(mapImgDiv);		
		var mapId = document.getElementById('wM2');
		
		if (mapId){
			mapId.src = _GLOBAL_ROOT +"webImages/home/map/"+ s + regionId + "_zoom.gif";							
			mapId.useMap = mapName
			mapId.setAttribute('useMap', mapName); 
		}
		$('#map').html(secMap.innerHTML);
		$('#map').fadeIn('slow');
		

		//add back button
		if(back){
			$('<a id="mapback" href="javascript:void(0)"><span>return to world map &gt;</span></a>')
				.appendTo(this)
				.click(function(){loadPrevMap()});
		}
		cvi_map.add(document.getElementById('wM2'),{opacity: 100, noborder: true}); // Initialize the image map to handle mouseovers and clicks	 
	});		
}

function loadPrevMap(){
	
	var isClicked = false; isClickedImage = ''; isClickedArea = '', ac='',bc='',op='',shapeBuffer = '';
	
	// hide the lang popup if shown
	var popDivObj = document.getElementById('popupContent');
	if (popDivObj){
		popDivObj.style.visibility = 'hidden';
  		popDivObj.style.display = 'none';  		
	}
	
			
	$('#map').fadeOut(500, function(){			
		$('#map').empty().css({
			width: '850px',
			height: '414px',
			backgroundImage: 'url('+ _GLOBAL_ROOT +'webImages/home/map/world_map.gif)',
			position: 'relative'
		});
		$('#map').html('<img id="wM" src="'+ _GLOBAL_ROOT +'webImages/home/map/world_map.gif"  alt="map" usemap="#world" width="850" height="414" style="display:block;visibility:visible;"  />');	
		$('#map').fadeIn();		
		$('#popupContent').load('section/pop.html',{},function(){
// 			alert($(this).children($).attr('id'));
// 			$(this).children('a.bullet').each(function(){
// 				alert($(this).attr('id'));			
// 			});
			//$(this).css({visibility:'visible' , display:'block'});			
		});
		cvi_map.add(document.getElementById('wM'),{opacity: 100, noborder: true}); // Initialize the image map to handle mouseovers and clicks	 			
	});
}

function mouseX(evt) {	
	if (evt.pageX) return evt.pageX;
	else if (evt.clientX)
	   return evt.clientX + (document.documentElement.scrollLeft ?
	   document.documentElement.scrollLeft :
	   document.body.scrollLeft);
	else return null;
}
function mouseY(evt) {
	if (evt.pageY) return evt.pageY;
	else if (evt.clientY)
	   return evt.clientY + (document.documentElement.scrollTop ?
	   document.documentElement.scrollTop :
	   document.body.scrollTop);
	else return null;
}

function emptyPopup(){
	var popDivObj = document.getElementById('popupContent');
	if (popDivObj){
		popDivObj.innerHTML = '';	
	}
}

function getMousePos(evt,areaObj,show,fileNm,showLangs,langFileNm){
	
	// empty country name
	var conObj = document.getElementById('conNm');
	if (conObj){
		conObj.innerHTML = '';	
	}
	
	var popDivObj = document.getElementById('popupContent');
	if ((popDivObj) && (show != '1')){
		
		popDivObj.style.visibility = 'hidden';
		popDivObj.style.display = 'none';
		
	}else{
	
		var regionId = $(areaObj).attr('id');
		
		var curX = mouseX(evt);
		var curY = mouseY(evt);
		
		// load the languages if any for all countries of the selected region
		
		var popDivObj = document.getElementById('popupContent');	
		langFileNm = (langFileNm)? langFileNm : 'index/home_languages.php?website=';
	   	$('#popupContent').load(_GLOBAL_ROOT + langFileNm + '&conid=' + regionId + '&f=' + fileNm + '&action=show_languages&showLangs=' + showLangs , {}, function(){
		   	// do nothing
		   	var dragArea = document.getElementById('dragBar');
	 		var dragDiv = document.getElementById('popupContent');
	 		var screenWidth;
	 		if (typeof window.innerWidth != 'undefined'){
		 		screenWidth = window.innerWidth;
		 		screenWidth = screenWidth - 16;
		 		
	 		}else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
		 		screenWidth = document.documentElement.clientWidth;
	 		}else{
				screenWidth = document.getElementsByTagName('body')[0].clientWidth;			
			} 		
	 		Drag.init(dragArea, dragDiv, 0, (screenWidth - 250), 0, null); // Drag only on the dragBar
		});
		
		
		if (popDivObj){
	 		if (show){	 		
		 		popDivObj.style.left = curX + 'px';
	 			popDivObj.style.top = curY + 'px';
	 			popDivObj.style.visibility = 'visible';
	 			popDivObj.style.display = 'block';	 
	 		}else{
		 		popDivObj.style.visibility = 'hidden';
	  			popDivObj.style.display = 'none';
	 		}
		}
	}
}

function closeLangPopup(areaId){
	var isVM = document.namespaces?1:0; 
	var isJG = 0, jg = new Array();
	var canvascheck = document.createElement('canvas');
	var isCV = canvascheck.getContext?1:0;
	
	var popDivObj = document.getElementById('popupContent');
	if (popDivObj){
		popDivObj.style.visibility = 'hidden';
  		popDivObj.style.display = 'none';  	  		
	}
	if ( (areaId) && (_globalCanvas)) {
		var canvas = _globalCanvas;		
		if (canvas){
			if(isVM) {canvas.innerHTML = '';}else 
			if(isJG) {
				jg[z].clear();
			}else if(isCV){
				var context = canvas.getContext("2d");
				context.clearRect(0,0,canvas.width,canvas.height);
			}
			var img = document.getElementById('wM2');
			var obj = document.getElementById(areaId);
			var cav = document.getElementById(img.canvasid);
			
			//alert(img + "==" + obj + "==" + cav);
			if(img && obj && cav) setAreaOut(obj,img.canvasid,img.options['nofade'],img.jg, true);
			var isClicked = false; isClickedImage = ''; isClickedArea = '', ac='',bc='',op='',shapeBuffer = '';
		}
	}

}

function openCountries(tot,selC){
	
	// hide all open divs
	for (var i=1; i <= tot; i++){
		var divObj = document.getElementById('con_' + i);
		if (divObj){
			divObj.style.visibility = 'hidden';
			divObj.style.display = 'none';
		}
		var lnkObj = document.getElementById('clink' + i);
		if (lnkObj){
			lnkObj.className = 'countryLinksN';
		}
	}
	
	// show the div
	var divObj = document.getElementById('con_' + selC);
	if (divObj){
		divObj.style.visibility = 'visible';
		divObj.style.display = 'block';
	}
	
	// hightlight the link
	var lnkObj = document.getElementById('clink' + selC);
	if (lnkObj){
		lnkObj.className = 'countryLinksHover';
	}
	
	
	//$('#expCountries').load(_GLOBAL_ROOT + 'index/home_languages.php?conid=' + regionId + '&f=' + fileNm + '&action=show_countries',{},function(){
		// do nothing	
	//});
}

function setCookie(){
	var langSelected = false;
	var webLink = "";
	var hdnFld = document.getElementById('selected_lang');
	var errFld = document.getElementById('error');
	hdnFld.value = "";
	
	
	$("#dragBar div.langnameClick").each(function(){
		var divClass = $(this).attr('class');				
		var selectedDiv = $(this).children('span');		
		webLink = selectedDiv.html();
		langSelected = true;
		
		
	});
	
	if (langSelected == false){
		$("#langDiv div:first-child")
			.attr("class","langnameClick")
		var firstDiv = $("#langDiv div:first-child");		
		webLink = $(firstDiv).children('span').html();
		
	}
	
	if ((webLink) && (hdnFld)){
		// set the hidden field with the domain value		
		hdnFld.value = webLink;	
		errFld.innerHTML = '&nbsp;';	
	}
}

function selectLang(divObj,mouseEvt){
	if (mouseEvt == 'click'){
		// reset any prev click divs
		$("#dragBar div.langnameClick").each(function(){			
			$(this).attr("class",'langnameN');						
		});		
		divObj.className='langnameClick';
		// every time a new option is selected call set Cookie function
		setCookie();
	}
	if (mouseEvt == 1){	// mouseover
		if (divObj.className=='langnameClick'){
			// do nothing
		}else{
			divObj.className='langnameH';
		}
	}
	if (mouseEvt == 0){	// mouseout	
		// check what previous class was 
		if (divObj.className=='langnameClick'){
			// do nothing
		}else{
			divObj.className='langnameN';
		}
	}	
}

function checkSelection(){
	
	// check how many languages are shown
	var totDivs = $("#langDiv div").length;
	
	// since the above total length also includes line separators we exclude those
	var totLangs = totDivs / 2;
	
	if (totLangs == 1){
		var firstDiv = $("#langDiv div:first-child");		
		$(firstDiv).attr("class","langnameClick");	
		setCookie();
		return true;			
		//webLink = $(firstDiv).children('span').html();		
		//location.href = webLink;
		//return false;
	}else{
	
		// check if atleast one language is selected
		var hdnFld = document.getElementById('selected_lang');
		var errFld = document.getElementById('error');
		
		if (hdnFld.value){
			// do nothing as the value is set. Only hide the error msg
			if (errFld){
				errFld.innerHTML = '&nbsp;';
			}
		}else{
			// show error msg
			if (errFld){
				errFld.innerHTML = 'Please select a language';
				return false;						
			}
		}	
	}
}
