/*
 * webdevRefinery Critiq - Slave JS
 * October 24, 2010
 * Tom Frost
 *
 * Group critiquing engine, slave-side javascript
 */
if (!console)
	var console = {log:function(){}};

/*
 * a backwards compatable implementation of postMessage
 * by Josh Fraser (joshfraser.com)
 * released under the Apache 2.0 license.
 */
var XD=function(){var interval_id,last_hash,cache_bust=1,attached_callback,window=this;return{postMessage:function(message,target_url,target){if(!target_url){return}target=target||parent;if(window['postMessage']){target['postMessage'](message,target_url.replace(/([^:]+:\/\/[^\/]+).*/,'$1'))}else if(target_url){target.location=target_url.replace(/#.*$/,'')+'#'+(+new Date)+(cache_bust++)+'&'+message}},receiveMessage:function(callback,source_origin){if(window['postMessage']){if(callback){attached_callback=function(e){if((typeof source_origin==='string'&&e.origin!==source_origin)||(Object.prototype.toString.call(source_origin)==="[object Function]"&&source_origin(e.origin)===!1)){return!1}callback(e)}}if(window['addEventListener']){window[callback?'addEventListener':'removeEventListener']('message',attached_callback,!1)}else{window[callback?'attachEvent':'detachEvent']('onmessage',attached_callback)}}else{interval_id&&clearInterval(interval_id);interval_id=null;if(callback){interval_id=setInterval(function(){var hash=document.location.hash,re=/^#?\d+&/;if(hash!==last_hash&&re.test(hash)){last_hash=hash;callback({data:hash.replace(re,'')})}},100)}}}}}();

var Critiq = function() {
	var parentURL = decodeURIComponent(location.hash).substr(1);
	if (parent && parentURL.indexOf('http') === 0) {
	
		var attached = false;
		var loaded = false;
	
		// Find the domain
		var target;
		var idx = parentURL.indexOf('//') + 2;
		idx = parentURL.indexOf('/', idx);
		if (idx === -1)
			target = parentURL;
		else
			target = parentURL.substr(0, idx);
		
		// Attach the onload
		if (window['addEventListener'])
			window.addEventListener("load", sendSize);
		else if (window['attachEvent'])
			window.attachEvent("onload", sendSize);
		else
			window.onload = sendSize;
	
		// Attach the receiver
		XD.receiveMessage(function(message) {
			console.log('To Child from Parent: ' + message.data);
			if (message.data == "critiq:attach" && !attached) {
				if (window['addEventListener'])
					window.addEventListener("scroll", sendScrollPos);
				else if (window['attachEvent'])
					window.attachEvent("onscroll", sendScrollPos);
				else
					window.onscroll = sendScrollPos;
				attached = true;
				if (loaded)
					sendSize();
			}
		}, target);
	
		function sendSize() {
			loaded = true;
			if (attached) {
				sendToParent('critiq:{"action":"report","pagex":' +
					getPageWidth() + ',"pagey":' + getPageHeight() + ',"viewx":' +
					getViewportWidth() + ',"viewy":' + getViewportHeight() + '}');
			}
		}
	
		function sendScrollPos() {
			sendToParent('critiq:{"action":"scroll","xpos":' +
				getLeftEdge() + ',"ypos":' + getTopEdge() + '}')
		}
	
		function sendToParent(message) {
			XD.postMessage(message, target, parent.window);
		}
	
		// Size/Scroll functions based on 
		// http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
		function getViewportWidth() {
			return filterResults(
				window.innerWidth ? window.innerWidth : 0,
				document.documentElement ? document.documentElement.clientWidth : 0,
				document.body ? document.body.clientWidth : 0
			);
		}
		function getViewportHeight() {
			return filterResults(
				window.innerHeight ? window.innerHeight : 0,
				document.documentElement ? document.documentElement.clientHeight : 0,
				document.body ? document.body.clientHeight : 0
			);
		}
		function getPageWidth() {
			return Math.max(document.body.scrollWidth, document.body.offsetWidth);
		}
		function getPageHeight() {
			return Math.max(document.body.scrollHeight, document.body.offsetHeight);
		}
		function getLeftEdge() {
			return filterResults(
				window.pageXOffset ? window.pageXOffset : 0,
				document.documentElement ? document.documentElement.scrollLeft : 0,
				document.body ? document.body.scrollLeft : 0
			);
		}
		function getTopEdge() {
			return filterResults(
				window.pageYOffset ? window.pageYOffset : 0,
				document.documentElement ? document.documentElement.scrollTop : 0,
				document.body ? document.body.scrollTop : 0
			);
		}
		function filterResults(n_win, n_docel, n_body) {
			var n_result = n_win ? n_win : 0;
			if (n_docel && (!n_result || (n_result > n_docel)))
				n_result = n_docel;
			return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
		}
	
		sendToParent('critiq:{"action":"init"}');
	}
}();
