/*
 * jQuery postMessage - v0.5 - 9/11/2009
 * http://benalman.com/projects/jquery-postmessage-plugin/
 * 
 * Copyright (c) 2009 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($){var g,d,j=1,a,b=this,f=!1,h="postMessage",e="addEventListener",c,i=b[h]&&!$.browser.opera;$[h]=function(k,l,m){if(!l){return}k=typeof k==="string"?k:$.param(k);m=m||parent;if(i){m[h](k,l.replace(/([^:]+:\/\/[^\/]+).*/,"$1"))}else{if(l){m.location=l.replace(/#.*$/,"")+"#"+(+new Date)+(j++)+"&"+k}}};$.receiveMessage=c=function(l,m,k){if(i){if(l){a&&c();a=function(n){if((typeof m==="string"&&n.origin!==m)||($.isFunction(m)&&m(n.origin)===f)){return f}l(n)}}if(b[e]){b[l?e:"removeEventListener"]("message",a,f)}else{b[l?"attachEvent":"detachEvent"]("onmessage",a)}}else{g&&clearInterval(g);g=null;if(l){k=typeof m==="number"?m:typeof k==="number"?k:100;g=setInterval(function(){var o=document.location.hash,n=/^#?\d+&/;if(o!==d&&n.test(o)){d=o;l({data:o.replace(n,"")})}},k)}}}
	
	$.receiveMessage(function(e){

		var command = getParameterByName(e.data, "command");
		var frame = getParameterByName(e.data, "frameid");
		var params = getParameterByName(e.data, "params"); 
		
		
		if (command == "setheight"){
			var h = Number( getParameterByName(e.data, "height") );

		    if ( !isNaN( h ) && h > 0) {
		      $("#" + frame).height(h);
		     
		       
		     if (params && params != ""){
		      	$("#" + params).height(h);	
		      }
		    }
		}
		else if (command == "closeframe"){
			
			$("#" + frame).remove();
			if (params && params != ""){
	      		$("#" + params).remove();	
	      	}
		}
		else if (command == "imagepopup"){
			var img = decodeURIComponent( getParameterByName(e.data, "img") );
			showImagePopup(img);
		}
		
	  }, 'http://www.reseauurbain.com' );



})(jQuery);


function writeReceiverFrame(src, frameid, w, h){
	src += '#' + encodeURIComponent( document.location.href );
	
	document.write('<iframe id="' + frameid + '" src="' + src + '" width="' + w + '" height="' + h + 
					' scrolling="no" frameborder="0"></iframe>' );
}


function showImagePopup(img){
	
	var preload_image = new Image(); 
    preload_image.src=img; 
	
    //preload_image.onload = function(){
    	content = '<div style="width:550px;height:320px;"><img src="' + img + '" /></div>';
		var b = new Boxy(content, {title: "&nbsp;", show: false, center: true, fixed: false});
		b.center('x').center('y');
		b.show();
    //};
    
	
	return false;
}

function sendImagePopupMessage(_img, _params) {	
	
	var parent_url = decodeURIComponent( document.location.hash.replace( /^#/, '' ) ), link;
	
	$.postMessage({ command:"imagepopup",
					frameid:'',
					img: _img,
		 			params: _params }, 
	 			parent_url, parent );
};

function sendSetHeightMessage(_frameid, heightoffset, _params) {	
	
	if (!heightoffset)
		heightoffset = 0;
	if (!_params)
		_params = "";
	
	var parent_url = decodeURIComponent( document.location.hash.replace( /^#/, '' ) ), link;
	
	
	$.postMessage({ command:"setheight",
					frameid:_frameid,
					height: $('body').outerHeight( true ) + heightoffset,
		 			params: _params }, 
	 			parent_url, parent );
};


function sendCloseFrameMessage(_frameid, _params) {	
	
	
	if (!_params)
		_params = "";
	
	var parent_url = decodeURIComponent( document.location.hash.replace( /^#/, '' ) ), link;
	
	
	$.postMessage({ command:"closeframe",
					frameid:_frameid,
		 			params: _params }, 
	 			parent_url, parent );
};


function getParameterByName( str, name )
{
  //str = "?" + str;
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]?"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( str );
  if( results == null )
    return "";
  else
    return results[1];
}
