//
// Copyright (c) 2006. mapanno (http://www.mapanno.com/).
// Under an Attribution, Share Alike License.
//
// * This software contains modified code written by
// * Cody Lindley (http://www.codylindley.com)
// * Under an Attribution, Share Alike License
// * This software is dependant on:
// * JQuery (http://jquery.com/)
// * By John Resig (http://ejohn.org/)
// * Under an Attribution, Share Alike License
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions 
// are met:
//
// * Redistributions of source code must retain the above copyright 
//   notices,this list of conditions and the following disclaimer.
//     
// This software is provided by the copyright holders and contributors
// "as is" and any express or implied warranties, including, but not
// limited to, the implied warranties of merchantability and fitness for
// a particular purpose are disclaimed. In no event shall the copyright
// owner or contributors be liable for any direct, indirect, incidental,
// special, exemplary, or consequential damages (including, but not
// limited to, procurement of substitute goods or services; loss of use,
// data, or profits; or business interruption) however caused and on any
// theory of liability, whether in contract, strict liability, or tort
// (including negligence or otherwise) arising in any way out of the use
// of this software, even if advised of the possibility of such damage.
//

var smMaps = [], smMap = null, smBasePath = 'http://lab.mapanno.com/mapanno/';

function smAppendMask(smScheme) {
 	if (document.getElementById("sm-window") != null)
	$("#sm-window").remove();
	if (document.getElementById("sm-mask") == null)
	$("body").prepend("<iframe id='sm-mask-iframe'></iframe><div id='sm-mask'></div><div id='sm-overlay' class='"+smScheme[2]+"'><div id='sm-base-p'></div><div id='sm-base-o' class='sm-base'>.</div></div>");
	$("body").append("<div id='sm-base-c' class='sm-base'>.</div>");
}

function smAppendContent(smWidth,smSlides,smUrl,smScheme) {
 	smAppendMask(smScheme);
	$("body").append("<div id='sm-window' class='"+smScheme[1]+"' style='width:"+smWidth+"px;'></div>");
	$("#sm-window").append("<div style='width:"+smWidth+"px;'><div id='sm-map-load'>...</div><div id='sm-map-title-nav' class='"+smScheme[0]+"'>" + smSlides + "<a href='"+smUrl+"' alt='Print' title='Print' target='Print'>Print</a>|<a href='' onclick='smRemove(); return false;' title='Close'>Close</a></div></div><div id='sm-content' style='width:"+smWidth+"px;'></div><div id='sm-base-a' class='sm-base'>.</div>");
	smResizeMask(true);
}

function smShowMap(map,smType,smHosted,smSchemeColor) {

	smMap = map;
	var smScheme = smGetScheme(smSchemeColor);
	var smHPad = (smType == "o-frame")?40:0;
	var smVPad = (smType == "o-frame")?120:100;
	var smWidth = smMaps[map][1]+smHPad, smHeight = smMaps[map][2]+smVPad;
	var smUrl = smBasePath+'sm_'+smMaps[map][0]+'.htm', smSlides = "";
	if(smType == 'i-frame'){
	document.getElementById("sm-content-iframe").src = smUrl;
	} else {

	if(smMaps.length > 1){
	smSlides = "Slides ";
	var smCntNum;
			for (smCnt = 0; smCnt < smMaps.length; smCnt++) {
					smCntNum = smCnt+1;
					smSlides +='<a href="" alt="Slide '+smCntNum+'" title="Slide '+smCntNum+'" onclick="javascript:smShowMap(\''+smCnt+'\',\''+smType+'\','+smHosted+',\''+smSchemeColor+'\');return false;">'+smCntNum+'</a>&nbsp;';
			}
	}

	smAppendContent(smWidth,smSlides,smUrl,smScheme);

	if(smType == 'o-ajax'){
	try {
		$.get(smUrl, function(data){				
		$("#sm-map-load").remove();
		data = data.replace(/\<script.*\>\<\/script>/g, "<!-- script removed -->");
		$("#sm-content").html (data);
		smInitMap();
		smResizeMask(false);
		});
	} catch(e) {
		$("#sm-map-load").append("<h3>Load Failed</h3>Possible Reasons:<br/>Internet connection lost;<br/>Blocked by security settings;<br/>Or, file does not exist.");
		smResizeMask(false);
	}
	} else {
		$("#sm-content").append("<iframe id='sm-content-iframe' name='sm-content-iframe' class='"+smScheme[0]+"' src='"+smUrl+"' width='"+smWidth+"' height='"+smHeight+"' scrolling='auto' frameborder='0' onload='smResizeMask(false);smInitMap();'>...</iframe>");
		$("#sm-map-load").remove();
	}
	smAddEvent(window,'resize',smResizeMask,false);
	}
}



function smResizeMask(load) {
	var p1, p2, p3;
	$("#sm-overlay").css("height","100%");
	
	var wScroll = smScroll();
	var wWidth = window.innerWidth || self.innerWidth || (document.documentElement.clientWidth) || document.body.clientWidth;
	var wLeft = wScroll[0] + (wWidth - parseInt($("#sm-window").css("width")))/2;
	wLeft = (wLeft < '10')?'10':wLeft;
	$("#sm-window").css({left: wLeft+"px", top: (wScroll[1] + 20)+"px" });
	
	p1 = smFindPos("sm-base-o");
	p2 = smFindPos("sm-base-c");
	if(load == true) p3 = p2; else p3 = smFindPos("sm-base-a");
	height=(p2[1] > p1[1])?p2[1]:p1[1];
	height=(height > p3[1])?height+60:p3[1]+60;
	width=(p2[0] > p1[0])? p2[0]:p1[0];
	width=(width > p3[0])? width:p3[0];
	if (document.body) {
	var sHeight = document.body.scrollHeight+60;
	if (sHeight > height) height = sHeight;
	sHeight = smVbound + wScroll[1] + 60;
	if (sHeight > height) height = sHeight;
	}
	$("#sm-overlay").css({width:width+"px",height:height+"px"});
	$("#sm-window").css("display","block");
}

function smFindPos(id) {
	var obj = document.getElementById(id)
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function smRemove() {
	$("#sm-window").fadeOut("fast",function(){$('#sm-window,#sm-overlay,#sm-mask-iframe,#sm-mask,#sm-base-c').remove();});
	smRemoveEvent(window,'resize',smResizeMask,false);
	smRemoveEvent(document,'keydown',smKeyNav,true);
	if(smMaps.length > 1) smMaps = [];
	smMap = null; smTag = -1;
}

function smScroll(){
	var yScrolltop, xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}
	wScroll = new Array(xScrollleft,yScrolltop)
	return wScroll;
}
