//For the mash up effect 
//For the back ground effect

var smMaps = [], smMap = null, smBasePath = 'http://www.rlwcword.org/', smEditing = false;


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  style='height:5px' 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;height:0px'></div>");
	$("#sm-window").append("<div style='height:5px' bgcolor:'transparent' > </div><div style='width:"+smWidth+"px;'><div id='sm-map-load'>...</div><div id='sm-map-title-nav' class='"+smScheme[0]+"'>" + smSlides + "<a href='' onclick='smRemove(); return false;' title='Back'>Back</a></div></div>	<div id='sm-content' style='width:"+smWidth+"px;'></div><div id='sm-base-a' class='sm-base'>.</div>");
	smResizeMask(true);
}

//var browserVer=parseInt(navigator.appVersion); 
// var loc= window.parent.location;
//  if (loc != "http://www.rlwcword.org/frmHome.htm")
// {
//    var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
//    if(!ie7)
//    alert("Detected browser Internet Explorer 6 (IE6)! please upgrade your browser to IE7 or higher");
// }

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

	smMap = map;
	var smScheme = smGetScheme(smSchemeColor);
	var smHPad = (smType == "o-frame")?40:1;
	var smVPad = (smType == "o-frame")?120:100;
	var smWidth = smMaps[map][1]+smHPad, smHeight = smMaps[map][2]+smVPad;
	var smUrl = smBasePath+smMaps[map][0], smSlides = "";
	if(smType == 'i-frame'){
	document.getElementById("sm-content-iframe").src = smUrl;
	} else {
	
	if(smHosted){
	smBasePath = 'http://www.rlwcword.org/';
	
	}
	
	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;';
					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 {
		//$("#sm-content").load(smUrl, function(){
		$.get(smUrl, function(data){				
		$("#sm-map-load").remove();
		data = data.replace(/\<script.*\>\<\/script>/g, "<!-- script removed -->");
		var smImageReg = /(src=\").*images\/(.{11})\//g;
		data = data.replace(smImageReg,"$1"+smBasePath+"images/$2/");
		$("#sm-content").html (data);
		smInitMap();
		smResizeMask(false);
	});
	} catch(e) {
		$("#sm-map-load").append("<h3>Load Failed</h3>- connection lost or, file does not exist.<h2>...</h2>");
		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();
	}
	//$(window).bind('resize',smResizeMask);
	smAddEvent(window,'resize',smResizeMask,false);
	}
}



function smResizeMask(load) {

	if (document.getElementById("sm-window") != null) {
	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", display:"block" });

	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;
	height=(height > p3[1])?height+10:p3[1]+10;
	width=(p2[0] > p1[0])? p2[0]:p1[0];
	width=(width > p3[0])? width:p3[0];
	if (document.body) {
	var sHeight = document.body.scrollHeight+80;
	var sHeight = 10;
	if (sHeight > height) height = sHeight;
	sHeight = smVbound + wScroll[1] + 80;
	sHeight = smVbound + wScroll[1] ;
	if (sHeight > height) height = sHeight;	
	}
	$("#sm-overlay").css({width:width+"px",height:height+"px"});

	if(smEditing) {	
		$("#sm-editor-toolbar").css({left: wLeft+"px", top: (wScroll[1] + 40)+"px" });
		$("#sm-editor-toolbar-ghost").css({left: wLeft+"px", top: (wScroll[1] + 40)+"px" });	
	} 
	}
	else {
	//$(window).unbind('resize',smResizeMask);
	smRemoveEvent(window,'resize',smResizeMask,false);
	}
}

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);
	//$(window).unbind('resize',smResizeMask);
	smRemoveEvent(document,'keydown',smKeyNav,true);
	if(smMaps.length > 1) smMaps = [];
	smMap = null; smTag = -1;
	try { GUnload(); }
	catch(e){
		//gmap not loaded
	}
}

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;
}