var protocol = window.location.protocol + '//';
var wc_common_included = ((typeof(wc_common_included) == 'undefined') ? false : true);
if (typeof ajax_onComplete == 'undefined')
	var ajax_onComplete;

if (wc_common_included == false) {
	wc_common_included = true;
	
	document.write('<scr' + 'ipt type="text/javascript" src="' + protocol + 'wallcann.com/contrib/easyxdm/easyXDM.min.js"></script>');
	
	function load_iframe(containerId, src) {
		$('#' + containerId).append('<div class="roundbox tmp_loading"><img src="' + protocol + 'wallcann.com/images/ajax3.gif" />&nbsp;Loading...</div>');
		var transport = new easyXDM.Socket(/** The configuration */{
			remote: protocol + "wallcann.com/iframe_loader.html?url=" + src,
			swf: protocol + "wallcann.com/contrib/easyxdm/easyxdm.swf",
			container: containerId,
			onMessage: function(message, origin){
				message = JSON.parse(message);
				if (message.height) {
					this.container.getElementsByTagName("iframe")[0].style.height = message.height + "px";	
				}
				if (message.close_popup && message.close_popup == true)
					ajax_popup_close();
				if (message.close_popup_delay && message.close_popup_delay)
					window.setTimeout(function() {
						ajax_popup_close()
					}, 2000);
				if (message.logged_in && message.logged_in == true) {
					logged_in = message.user_id;
					if (ajax_onComplete) {
						if (typeof ajax_onComplete == 'function')
							ajax_onComplete();
						if (typeof ajax_onComplete == 'object' && typeof ajax_onComplete.callback == 'function') {
							ajax_onComplete.callback.call(ajax_onComplete);
						}
					}
				}
				if (message.reload && message.reload == true) {
					href = String(window.top.location.href);
					if (href.charAt(href.length-1) == '#')
						href = href.substring(0, href.length-1);
					window.top.location.href = href;
				}
				if (message.loaded && message.loaded == true)
					jQuery('.tmp_loading').remove();				
			},
			props: {
				style: {
					width: '100%'
				}
			},
			onReady: function() {
				//jQuery('.tmp_loading').remove();
			}
		});			
	}
	
	function ajax_popup_close() {
		jQuery('#popup_backdrop').fadeOut(function() { jQuery(this).remove(); });
		jQuery('#popup_foreground').fadeOut(function() { jQuery(this).remove(); });
	}
	function ajax_popup_open(content) {
		if (jQuery('#popup_backdrop').size()) {
			jQuery('#popup_backdrop').stop().remove();
			jQuery('#popup_foreground').stop().remove();
		}
		jQuery('body').prepend('<div id="popup_backdrop"></div><div id="popup_foreground"></div>');
		jQuery('#popup_foreground').html(content);
		if (jQuery('#popup_foreground').find('iframe').length) {
			jQuery('#popup_foreground').find('iframe').parent('div').prepend('<div class="roundbox tmp_loading"><img src="' + protocol + 'wallcann.com/images/ajax3.gif" />&nbsp;Loading...</div>');
			jQuery('#popup_foreground').find('iframe').load(function() {
				jQuery('.tmp_loading').remove();
			});
		}
		jQuery('#popup_backdrop').fadeTo('normal', 0.4);
		jQuery('#popup_foreground').fadeTo('normal', 1);
	}
	function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				oldonload();
				func();
			}
		}
	}
	function chr(code) {
		return String.fromCharCode(code);
	}
	var check_login_timer = null;
	function cancel_login_status() {
		if (check_login_timer) {
			window.clearTimeout(check_login_timer);
			check_login_timer = false;
		}
	}
	check_login_status = function() {
		check_login_timer = null;		
		check_login();
	}
	check_login = function() {
		return;
		jQuery.get(
			protocol + 'wallcann.com/login_status.php',
			{},
			function(data) {
				if (data.logged_in) {
					logged_in = true;
					ajax_popup_close();
					if (ajax_onComplete) {
						if (typeof ajax_onComplete == 'function')
							ajax_onComplete();
						if (typeof ajax_onComplete == 'object' && typeof ajax_onComplete.callback == 'function') {
							ajax_onComplete.callback.call(ajax_onComplete);
						}
					}
				} else {
					if (check_login_timer !== false)
						check_login_timer = window.setTimeout(check_login, 150);
				}
			},
			'json'
		);
	}
	function ajax_login(onComplete) {
		ajax_onComplete = onComplete;
		if (logged_in) {
			if (onComplete) {
				if (typeof onComplete == 'function')
					onComplete();
				if (typeof onComplete == 'object' && typeof onComplete.callback == 'function')
					onComplete.callback.call(onComplete);
			}
			return;
		}
		ajax_popup_open('<div style="width: 600px; padding: 2em; margin-top: 10%; margin-left: auto; margin-right: auto;" class="darkbox roundbox"><div style="text-align:right;"><a href="#" onclick="cancel_login_status(); ajax_popup_close();return false;">Cancel</a></div><div id="iframe_container"></div></div>');
		load_iframe('iframe_container', 'iframe_login.php');

		//check_login_status();
		jQuery('#popup_foreground input:first').focus();
		jQuery('#popup_foreground input').keypress(function(event) {
			if (event.keyCode == '13')
				jQuery('#popup_foreground input:submit').trigger('click');
		});
		
	}
	
	poll_hash_tag = function() {
		if (typeof jQuery == 'undefined') {
			window.setTimeout(poll_hash_tag, 100);
			return;
		}
		old_hash = hash = window.top.location.hash;
		
		if (/reload=true/.test(hash)) {
			window.top.location.hash = '';
			href = String(window.top.location.href);
			if (href.charAt(href.length-1) == '#')
				href = href.substring(0, href.length-1);
			window.top.location.href = href;
			hash = hash.replace(/\&?reload=true/, '');
		}
		if (/loaded=true/.test(hash)) {
			jQuery('.tmp_loading').remove();
			hash = hash.replace(/\&?loaded=true/, '');
		}
		if (/logged_in=([\d]*)/.test(hash)) {
			var match = /logged_in=([\d]*)/.exec(hash);
			logged_in = match[1];
			hash = hash.replace(/\&?logged_in=([\d]*)/, '');
		}
		
		if (/height=([\d]*)/.test(hash)) {
			var match = /height=([\d]*)/.exec(hash);
			height = parseInt(match[1]);
			hash = hash.replace(/\&?height=([\d]*)/, '');
			iframew = jQuery('#popup_foreground iframe');
		// does iframe have a div on top?
			iframew_container = iframew.parent('div');
			if (iframew_container.length) {
				iframew_container.css('height', height + 'px');
			}
			iframew.css('height', height + 'px');
			iframew.attr('height', height + 'px');
		}
		
		if (hash.indexOf('close_popup=true') !== -1) {
			hash = hash.replace('close_popup=true', '');
			ajax_popup_close();
		}
		
		if (/login_onComplete=true/.test(hash)) {
			if (ajax_onComplete) {
				if (typeof ajax_onComplete == 'function')
					ajax_onComplete();
				if (typeof ajax_onComplete == 'object' && typeof ajax_onComplete.callback == 'function') {
					ajax_onComplete.callback.call(ajax_onComplete);
				}
			}
			hash = hash.replace(/\&?login_onComplete=true/, '');
		}
		if (/^#&+$/.test(hash)) {
			hash = '';
		}
		
		if (hash != old_hash) {
			if (hash != '')
				window.top.location.hash = hash;
			else {
				window.top.location.hash = '';
				delete window.top.location.hash;
			}
		}
			
		window.setTimeout(poll_hash_tag, 100);
	}
	//poll_hash_tag();
}

