var schmapPlugin = {
	proxy_address : '',
	multi_scrn_name : false, 
	$ : function(str) {
		return document.getElementById(str);
	},
	// preload the spinner
	loadSpinner : function () {
		var img = new Image();
		img.src = 'http://schmap.it/plugins/schnap/spinner.gif';
	},
	isIE : function () {
		if (navigator.appName.match('Microsoft') || navigator.userAgent.match('MSIE'))
			return true;
		else return false;
	},
	isIE8 : function () {
		var rv = -1; 
		if (navigator.appName == 'Microsoft Internet Explorer') {
			var ua = navigator.userAgent;
			var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
			if (re.exec(ua) != null)
				rv = parseFloat(RegExp.$1);
		}
		if (rv == 8) return true; 
		else return false			
	},
	openPlugin : function(scrn_name, proxy_location, content_type) {
		if (!scrn_name || !proxy_location || !content_type) {
			alert('openPlugin : parameters missing');
			return;
		}
		schmapPlugin.setProxyLocation(proxy_location);
		// set the adcode
		schmapPlugin.setAdcode(proxy_location);
		
		var arr = scrn_name.split('|'), length = 0;	
		for (i=0; i<arr.length; i++) {if (arr[i]!='') length++;}	
		if (scrn_name.match(/\|/gi) && length > 1) 	
			schmapPlugin.multi_scrn_name = true; 	
		else 										
			schmapPlugin.multi_scrn_name = false; 	
		if(content_type == 'map')
			schmapPlugin.addMap(scrn_name);
		else if(content_type == 'photos') {
			schmapPlugin.addPhotos(scrn_name);
		}
	},
	// Avoid cross-sub-domain bug if the proxy address contain 'www.' and not the host of the document (and opposite)
	setProxyLocation : function(proxy) {
		var host = document.location.host.toLowerCase();
		if (host.match('www') && (!proxy.match('www') && !proxy.match('WWW'))) {
			var arr = proxy.split('http://'), buf = 'http://www.';
			for (i=0; i<arr.length; i++) {buf += arr[i];}		
			proxy = buf;	
		}		
		else if (!host.match('www') && (proxy.match('www') || proxy.match('WWW'))) {
			var arr = [], buf = '';
			if (proxy.match('www'))
				arr = proxy.split('www.')
			else
				arr = proxy.split('WWW.')
			for (i=0; i<arr.length; i++) 
				{buf += arr[i];}		
			proxy = buf;		
		}	
		schmapPlugin.proxy_address = proxy;			
	},
	setAdcode : function(proxy) {
		proxy = proxy.toLowerCase();
		if (!schmapPlugin.adcode || !proxy.match(/schmap.it/gi)) {
			if (proxy.match('www'))
				tmp = proxy.split('//www.')[1];			
			else 
				tmp = proxy.split('//')[1];	
			tmp = tmp.split('/')[0];
			tmp = tmp.split('.'); 
			schmapPlugin.adcode = tmp[tmp.length-2]+tmp[tmp.length-1];
		}
	},
	loaded : function() {
		if (schmapPlugin.$('popup_schmap') && schmapPlugin.$('spinner_schmap') && (schmapPlugin.$('iframe_schmap') || schmapPlugin.$('iframe_schnap'))) {
			schmapPlugin.$('popup_schmap').style.display = 'block';
			if (schmapPlugin.$('iframe_schmap'))
				schmapPlugin.$('iframe_schmap').style.display = 'block';
			else if (schmapPlugin.$('iframe_schnap'))
				schmapPlugin.$('iframe_schnap').style.display = 'block';
			schmapPlugin.$('spinner_schmap').parentNode.removeChild(schmapPlugin.$('spinner_schmap'));
		}
	},
	getWindowDimensions : function () {
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	  }
	  return {height:myHeight, width:myWidth};
	},
	addOverlay : function (map){
		if (schmapPlugin.$('popup_schmap') && schmapPlugin.$('popup_schmap').length) return;
		var filter = document.createElement('div'); filter.id='filter_schmap';
		filter.style.display = 'block'; 	filter.style.position = 'absolute'; 
		filter.style.top = '0px'; 			filter.style.zIndex = '300'; 
		var width = '', height = ''; 
		if (schmapPlugin.isIE()) {
			width = document.body.clientWidth;
			height1 = document.body.clientHeight;
			height2 = document.body.scrollHeight;
			height = (height1>height2?height1:height2);				
		}
		else {
			width1 = document.documentElement.scrollWidth; 	
			height1 = document.documentElement.scrollHeight;
			width2 = document.documentElement.clientWidth; 	
			height2 = document.documentElement.clientHeight;  
			width = (width1>width2?width1:width2);
			height = (height1>height2?height1:height2);
		}
		filter.style.width = width+'px';	filter.style.height = height+'px';
		filter.style.opacity = '0.6'; 		filter.style.filter = 'alpha(opacity=60)';
		filter.style.background = 'black';	filter.style.left = 0;		
		document.body.appendChild(filter);
		
		var spinner = document.createElement('img'); 
		spinner.id='spinner_schmap';		spinner.src = 'http://schmap.it/plugins/schnap/spinner.gif';
		spinner.style.display = 'block'; 	spinner.style.position = 'absolute'; 
		spinner.style.margin = 'auto';		spinner.style.zIndex = '301'; 
		if (schmapPlugin.isIE() && !schmapPlugin.isIE8()) 
			spinner.style.top = '149px';
		else
			spinner.style.top = '143px';
		spinner.style.left = ((schmapPlugin.getWindowDimensions().width/2)-30)+'px'; 	
		document.body.appendChild(spinner);
		
		var popup = document.createElement('div'); popup.id='popup_schmap';
		popup.style.display = 'none'; 		popup.style.position = 'absolute'; 		
		popup.style.top = '100px';
		if (map) {			
			popup.style.left = ((schmapPlugin.getWindowDimensions().width/2)-337)+'px';  
		}
		else {
			popup.style.left = ((schmapPlugin.getWindowDimensions().width/2)-250)+'px';  
		}
		if (schmapPlugin.isIE() && !schmapPlugin.isIE8()) {
			popup.style.height = 144+'px'; 
			if (map) 
				popup.style.width = 674+'px';
			else
				popup.style.width = 456+'px'; 
		}
		else if (schmapPlugin.isIE8()) {
			popup.style.height = 154+'px'; 
			if (map) 
				popup.style.width = 674+'px';
			else
				popup.style.width = 456+'px';		
		}
		else {	
			popup.style.height = 153+'px'; 	
			if (map) 
				popup.style.width = 674+'px';
			else
				popup.style.width = 451+'px'; 
		}
		popup.style.zIndex = '400';			popup.style.background = 'white'; 
		popup.style.border = 'solid 1px';	
		document.body.appendChild(popup);
	},
	addMap : function (screen_name) {
		if(schmapPlugin.$('iframe_schmap') && schmapPlugin.$('iframe_schmap').length)return;
		schmapPlugin.addOverlay(true);
		var iframe = document.createElement('iframe'); iframe.style.display='none';
		iframe.id='iframe_schmap'; 		iframe.name='iframe_schmap';
		iframe.style.height = '100%'; 	iframe.style.width = '100%';
		iframe.scrolling = "no"; 		iframe.style.border = 'none';
		iframe.style.zIndex = '500';	iframe.frameBorder = 0;
		iframe.src = 'http://www.schmap.it/plugins/schmap/schmap_iframe.shtml#u='+screen_name+'&p='+schmapPlugin.proxy_address+'&adcode='+schmapPlugin.adcode;
		schmapPlugin.$('popup_schmap').appendChild(iframe);	
	},
	addPhotos : function (screen_name){	
		if(schmapPlugin.$('iframe_schnap') && schmapPlugin.$('iframe_schnap').length)return;
		schmapPlugin.addOverlay();
		var iframe = document.createElement('iframe'); iframe.style.display='none';
		iframe.id='iframe_schnap'; 		iframe.name='iframe_schnap';
		iframe.style.height = '100%'; 	iframe.style.width = '100%';
		iframe.scrolling = "no"; 		iframe.style.border = 'none';
		iframe.style.zIndex = '500';	iframe.frameBorder = 0;
		iframe.src = 'http://www.schmap.it/plugins/schnap/schnap_iframe.html#u='+screen_name+'&p='+schmapPlugin.proxy_address+'&adcode='+schmapPlugin.adcode; 
		schmapPlugin.$('popup_schmap').appendChild(iframe);
	},
	getOverlayHeight : function () {
		height = 0;
		if (schmapPlugin.isIE()) {
			height1 = document.body.clientHeight;
			height2 = document.body.scrollHeight;
			height = (height1>height2?height1:height2);				
		}
		else {
			height1 = document.documentElement.scrollHeight;
			height2 = document.documentElement.clientHeight; 
			height = (height1>height2?height1:height2);
		}
		return height;
	},
	resize : function (size) {
		var start = (schmapPlugin.multi_scrn_name)?376:338; 
		var step = 32; 
		if (schmapPlugin.isIE()) {
			if (size === '0' || size === "0")
				start = (schmapPlugin.multi_scrn_name)?412:380; 
			else
				start = (schmapPlugin.multi_scrn_name)?389:357;
			schmapPlugin.$('popup_schmap').style.width = 455+'px'; 	
		}
		size = start+(step*(parseInt(size))); 		
		schmapPlugin.$('popup_schmap').style.height = size+'px';
		schmapPlugin.$('filter_schmap').style.height = schmapPlugin.getOverlayHeight()+'px';
	},
	resizeMap : function(size) {
		if (size == 'big') {
			if (navigator.userAgent.match('MSIE 6'))
				size = 626;
			else if (navigator.userAgent.match('MSIE 7'))
				size = 653;
			else if (schmapPlugin.isIE8())
				size = 635;
			else
				size = 620;
		}		
		else if (size == 'small') {
			if (navigator.userAgent.match('MSIE 6'))
				size = 530;
			else if (navigator.userAgent.match('MSIE 7'))
				size = 556;
			else if (schmapPlugin.isIE8())
				size = 544;
			else
				size = 529;
		}		
		schmapPlugin.$('popup_schmap').style.height = size+'px';
		schmapPlugin.$('filter_schmap').style.height = schmapPlugin.getOverlayHeight()+'px';
	},
	closeDiv : function () {	
		document.body.removeChild(schmapPlugin.$('filter_schmap'));
		document.body.removeChild(schmapPlugin.$('popup_schmap'));
	},
	complete : function (url) {
		pluginCallback("http://schnap.it/"+url);
		schmapPlugin.closeDiv();
	},
	completeSchmap : function (url) {
		pluginCallback("http://schmap.it/"+url);
		schmapPlugin.closeDiv();
	}
};
// preload the spinner
schmapPlugin.loadSpinner();
function openPlugin(x, y, z) {
	schmapPlugin.openPlugin(x, y, z);
}

