/**
 * @author omnia
 */ 

var curIndex = 0;
var navItemCount=0;
var map;
var markersArray = [];
var mapPointers = [];
var totalPages;
var curPage = 1;
var singelPaneW = 547;
var totalPages = 2;
function executeHandler(){
$(document).ready(function(){

navigateGoogleList();

var $box = $('div.chk');
$("#Map area,ul.map-tabs li a").click(function() {  
  $box.hide();
  var hash = $(this).attr("rel");  
  $('#'+ hash).fadeIn();
  window.location.hash = hash;
  return false;  
});

$(".chk a.close,.chk a.readmore").click(function() {  
   $box.fadeOut();
   return false;   
});

$("a[rel='external']").attr("target","_blank");
$("a[href='#']").click(function(){
  return false;
});



$('.Keywords').blurHandler();
AssignCSSSelectors();
	
	try {
   if (window.location.href.toLowerCase().indexOf("cmspagemode=edit") > 0) {
   return false;
  }

		fontFix();
 }
	catch(obj){
	 alert(obj.message);
	}
});
$("ul li:last-child").addClass("last-child");
}
function fontFix(){
	Cufon.replace("#header ul li a",{fontFamily: 'gotham-medium'});
 Cufon.replace(".highlights h1",{fontFamily: 'Helvetica-Neue-light'});
 Cufon.replace("h3",{fontFamily: 'Helvetica-Neue-medium'});
}

function initialize() {
  if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("locationMap"));
	  var latlng = new GLatLng(25.798037,55.949936);
	  map.addOverlay(new GMarker(latlng));
	  map.setCenter(new GLatLng(25.798037,55.949936), 6);
	  map.setUIToDefault();
  }
}

// <summary>
//  GoogleMap Load function for the interactive map
// </summary> 
function load() {
 if (GBrowserIsCompatible()){
  map = new GMap2(document.getElementById("locationMap"));
  map.addControl(new GSmallMapControl());
  map.setCenter(new GLatLng(23.574057,58.206055), 7);
		map.setUIToDefault();
		map.setMapType(G_HYBRID_MAP);
  addStaticMarkers();
 }
}

// <summary>
//  Adding Static Markers
// </summary> 
function addStaticMarkers()
{
var MarkerArray = [];
XmlFile = "/Xml/StaticMarkers.xml?54dd";
var listMarkup="";
var htmlText = "";
$(".map-tabs").hide();
$.ajax({
  type: "GET",
  url: XmlFile,
  dataType: 'xml',
  success: function(xml){
   $(xml).find("marker").each(function(index){
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.image = $(this).find("image").text();
    baseIcon.iconSize = new GSize($(this).find("imgwidth").text(),$(this).find("imgheight").text());
    baseIcon.shadowSize = new GSize(16, 17);
    markerOptions = { icon:baseIcon };
    var latlng = new GLatLng($(this).find("lat").text(),$(this).find("long").text());
    var marker = new GMarker(latlng,markerOptions);
    GEvent.addListener(marker, "click", function() {
     map.setCenter(latlng, 12);
    });
	   htmlText = "<div class='info-win'><div class='info-win-wrap'><span class='close-btn' onclick='map.closeExtInfoWindow();'>Hide</span><div><div class='mask'><img src='" + $(this).find("thumbnail").text() +"'  ></div></div><h3>" + $(this).find("heading1").text() +"</h3><h4>" + $(this).find("heading2").text() +"</h4><p>" + LTrim($(this).find("summary").text()) + "<br /><a href='#' class='info-read-more' onclick='openPopup("+index+")'>Read more..</a></p></div></div>"; 
    //map.addOverlay(marker);
	   map.addOverlay(createMarker(latlng, index,htmlText));
	   listMarkup = listMarkup + "<li title="+index+"><h3>"+$(this).find("heading1").text()+"</h3><h4>"+$(this).find("heading2").text()+"</h4></li>";
    mapPointers.push({heading1 :$(this).find("heading1").text(),  heading2: $(this).find("heading2").text(), thumb: $(this).find("thumbnail").text(),summary:$(this).find("summary").text(),latlngObj:latlng,content:$(this).find("content").text(),image:$(this).find("image").text()});				
   });
   $(".map-tabs").html(listMarkup).show();
   highlightMap();
  }
 });
}


// <summary>
//  Opens the info window on hover of the listed items in Map View page
// </summary> 
function highlightMap()
{
var j = 0;
var htmlText;
$(".map-tabs").children().each(function(){
 $(this).click(function(){  
  var pointerIndex = $(this).attr("title");
  var newPointer = getPointerInfo(pointerIndex);
  htmlText = "<div class='info-win'><div class='info-win-wrap'><span class='close-btn' onclick='map.closeExtInfoWindow();'>Hide</span><div ><div class='mask'><img src='" + newPointer.thumb +"'  ></div></div><h3>" + newPointer.heading1 +"</h3><h4>" + newPointer.heading2 +"</h4><p>" + newPointer.summary + "<br /><a href='#' class='info-read-more' onclick='openPopup("+pointerIndex+")' >Read more..</a></p></div></div>"; 
  map.closeExtInfoWindow(); 
  map.setCenter(markersArray[pointerIndex].getLatLng(), 10);
  markersArray[pointerIndex].openExtInfoWindow(map,"extInfoWindow_coolBlues",htmlText,{beakOffset: 1})
  $(".map-tabs li").removeClass("active");
  $(this).addClass("active");
  });
 });
}

function createMarker(lalng,index,htmlText){
 var baseIcon = new GIcon(G_DEFAULT_ICON);
 baseIcon.image = "/Images/Generic/Pointer.png?12";
 baseIcon.iconSize = new GSize(30, 32);
 baseIcon.shadowSize = new GSize(30, 32);
 baseIcon.iconAnchor = new GPoint(16, 17);
 markerOptions = { icon:baseIcon };
 var marker = new GMarker(lalng,markerOptions);
 markersArray.push(marker);
 
 //  Evend handlers
 GEvent.addListener(marker, "mouseover", function() {
  map.closeExtInfoWindow();
  marker.openExtInfoWindow(map,"extInfoWindow_coolBlues",htmlText,{beakOffset: 1});
 });
 GEvent.addListener(marker, "mouseout", function() {
  //map.closeExtInfoWindow();
 });
 GEvent.addListener(marker, "click", function() {
  map.setCenter(latlng, 12);
 });

 
 return marker;
}

// <summary>
//  Trimming the text to best fit inside the info window
// </summary> 
function LTrim(str) {
 var i = str.indexOf(".");
 var len = str.length;
 if (i<=0){
  i=len;
 }
 return str.substring(0,i) + "...";
}

function getPointerInfo(index){
 var pointer = {};
 for (i=0;i<mapPointers.length;i++){
  if (i==index){
   pointer.heading1=mapPointers[i].heading1;
   pointer.heading2=mapPointers[i].heading2;
   pointer.summary=mapPointers[i].summary;
   pointer.thumb=mapPointers[i].thumb;
   pointer.latlngObj=mapPointers[i].latlngObj;
   pointer.content=mapPointers[i].content;
   pointer.image=mapPointers[i].image;
  }
 }
 return pointer;
}

// <summary>
// E-support popup script
// </summary>
function openPopup(index)
{
  var newPointer = getPointerInfo(index);
  var ScrollPos = getPageScroll();
  var PageSizes = getPageSize();
  var TopMargin;
  var PageHeight =  $(document).height();
  var PopupHTML = '<div class="fade" style="height:'+ PageHeight +'px" ></div><div class="popup">' +
    '<div class="wrap1">' +
     '<div class="popup-close" onclick="closePopup();"></div>' +
      '<div class="PopupContent clearfix">';
	   PopupHTML = PopupHTML +  "<h1>" + newPointer.heading1 + "</h1>";
	   PopupHTML = PopupHTML +  "<h2>" + newPointer.heading2 + "</h2>";
	   PopupHTML = PopupHTML + "<div class='overlay-image-wrap'><img src='"+ newPointer.image+"' class='overlay-image'/><a target='_blank' href='/images/map/hi-res/"+(index+1)+".jpg'>Click here to view the full image</a></div>";
       PopupHTML = PopupHTML + newPointer.content;
       PopupHTML = PopupHTML + '</div>' +
     '</div>' +
  '</div>';
 $("body").append(PopupHTML);
 $(".fade").css({opacity:0});
 //$(".Popup").hide();
 $(".popup").css({top: -600});
 $(".fade").animate({opacity: 0.25},{queue:false,duration:200,complete:function(){
  $(".popup").animate({top:	'20%'},{queue:false,duration:400});
 }});
 return false;
}

// <summary>
// E-support popup script
// </summary>
function openModelWindow(targetid)
{
  var ScrollPos = getPageScroll();
  var PageSizes = getPageSize();
  var TopMargin;
  var PageHeight =  $(document).height();
  var PopupHTML = '<div class="fade" style="height:'+ PageHeight +'px" ></div><div class="popup">' +
    '<div class="wrap1">' +
     '<div class="popup-close" onclick="closePopup();"></div>' +
      '<div class="PopupContent clearfix">';
       PopupHTML = PopupHTML + $(targetid).html();
       PopupHTML = PopupHTML + '</div>' +
     '</div>' +
  '</div>';
 $("body").append(PopupHTML);
 $(".fade").css({opacity:0});
 $(".popup").css({top: -600});
 $(".fade").animate({opacity: 0.25},{queue:false,duration:200,complete:function(){
  $(".popup").animate({top:ScrollPos[1]+150},{queue:false,duration:400});
 }});
 return false;
}

function initializePopup(){
 $(".model-popup").click(function(){
  var targetId= "#" + $(this).attr("rel");
  openModelWindow(targetId);
		return false;
 });
}

// <summary>
//  
// </summary>


function closePopup(){
 $(".popup").animate({top:	-800},{queue:false,duration:400,complete:function(){
   $(".fade").animate({opacity: 0},{queue:false,duration:200,complete:function(){
    $(".popup").remove();
    $(".fade").remove();
  }});
 }});
}


//Array Return an array with x,y page scroll values.
		function getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;
			}
			arrayPageScroll = new Array(xScroll,yScroll)
			return arrayPageScroll;
		}

// <summary>
// @return Array Return an array with page width, height and window width, height
// </summary>
		function getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth;
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else {
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){
				pageWidth = xScroll;
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
			return arrayPageSize;
		};
		
		

function AssignCSSSelectors()
{
	if(jQuery.browser.msie)
	{
		$("li:first-child").addClass("FirstChild");
		$("li:last-child").addClass("LastChild");
		$("td:last").addClass("LastChild");
  $("tr:first").addClass("FirstChild");
	}
}


//Add Onblur/Onfocus behaviour to a Textbox 
$.fn.blurHandler = function(){
    return this.focus(function(){
        if ($(this).hasClass("noBlur")) 
            return false;
        if (this.value == this.defaultValue) {
            this.value = "";
        }
    }).blur(function(){
        if (!this.value.length) {
            this.value = this.defaultValue;
        }
    });
}


function animatePane(pageNo){
	var paneW = -((pageNo-1) * singelPaneW);
	//console.log(paneW);
	$(".map-tabs").animate({left:paneW},{queue:false,duration:400,complete:function(){}})
}

function navigateGoogleList(){
	if (totalPages>1) $(".right-btn").show(); 
	curPage =1;
 $(".left-btn").click(function(){	
		curPage= curPage-1;
		if (curPage<=1){
			curPage=1;
			$(".left-btn").hide(); 
		}
		$(".right-btn").show(); 
		animatePane(curPage);
		return false;
	});
	$(".right-btn").click(function(){
		curPage= curPage+1;
		
		if (curPage>=totalPages){
			curPage=totalPages;
			$(".right-btn").hide(); 
		}
		animatePane(curPage);
		$(".left-btn").show(); 
		return false;
	});
}



