﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//browser detection start
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName  = navigator.appName;
var fullVersion  = ''+parseFloat(navigator.appVersion);
var majorVersion = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;

// In MSIE, the true version is after "MSIE" in userAgent
if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
    browserName = "Microsoft Internet Explorer";
    fullVersion = nAgt.substring(verOffset+5);
}
// In Opera, the true version is after "Opera"
else if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
    browserName = "Opera";
    fullVersion = nAgt.substring(verOffset+6);
}
// In Chrome, the true version is after "Chrome"
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
    browserName = "Chrome";
    fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari"
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
    browserName = "Safari";
    fullVersion = nAgt.substring(verOffset+7);
}
// In Firefox, the true version is after "Firefox"
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
    browserName = "Firefox";
    fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) {
    browserName = nAgt.substring(nameOffset,verOffset);
    fullVersion = nAgt.substring(verOffset+1);
    if (browserName.toLowerCase()==browserName.toUpperCase()) {
        browserName = navigator.appName;
    }
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1) fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1) fullVersion=fullVersion.substring(0,ix);

majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
    fullVersion  = ''+parseFloat(navigator.appVersion);
    majorVersion = parseInt(navigator.appVersion,10);
}
//browser detection end


//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({ "opacity": "0.7"});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		$("#popupContact").remove();
		popupStatus = 0;
	}
    $('select').css('visibility','visible');    // FIX IE6  
}

//centering popup
function centerPopup(){
	//request data for centering

    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;

    //fix IE6
    if(browserName == 'Microsoft Internet Explorer' && majorVersion == '6'){
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({ "position": "absolute","top": windowHeight/2-popupHeight/2,"left": windowWidth/2-popupWidth/2});
	//only need force for IE6
	$("#backgroundPopup").css({	"height": windowHeight	});
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("p.map a").click(function(){
        $('body').append(htmlPopUp);
        loadVars($(this));
        AutoClosePopUp();
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
        loadMapa(latitud,longitud,zoom,type);
	});
});

//CLOSING POPUP
function AutoClosePopUp(){
    $('select').css('visibility','hidden');    // FIX IE6
	//Click the x event!
	$("#popupContactClose").click(function(){disablePopup();});
	//Click out event!
    $("#backgroundPopup").click(function(){	disablePopup();	});

    //Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
}


var latitud, longitud, zoom, type = null;

function loadVars(button){
  var title, address;
  latitud  = button.siblings('input[@name=txtLat]').val();
  longitud = button.siblings('input[@name=txtLon]').val();
  zoom = parseInt(button.siblings('input[@name=txtZoom]').val());

  if(button.siblings('input[@name=txtType]') != null){
      type = button.siblings('input[@name=txtType]').val();
  }
  title = button.siblings('input[@name=txtTitle]').val();
  address = button.siblings('input[@name=txtAddress]').val();
  $("#popupContact h2").html(title);
  $("#popupContact p.address").html(address);
}

var htmlPopUp = '';
htmlPopUp += '<div id="popupContact">';
htmlPopUp += '  <a id="popupContactClose">x</a>';
htmlPopUp += '  <div id="mapArea"></div>';
htmlPopUp += '  <h2></h2>';
htmlPopUp += '  <p class="address"></p>';
htmlPopUp += '</div>';
htmlPopUp += '<div id="backgroundPopup"></div>';
