



function getSite(wp_sites,site){
	
	jQuery(wp_sites).each(function(e,d){
		if(site == d[0]){
			site = d[1];
		}
	});
 return site;
}

jQuery(document).ready(function() {
	
	// resize iframe
//	pm({
//		  target: window.frames["example2"],
//		  type:"message2", 
//		  data:{hello:"world"}, 
//		  success: function(data) {
//		    //console.log(JSON.stringify(data));
//		    var height = (data.height + 10)+'px';
//		    var width = (data.width+ 10)+'px';
//		    jQuery('#example2').closest('div').height(height).width(width);
//		    }
//		});
	



	function test(id){


		//console.log(id);
		
	  pm({
	  target: window.frames[id],
	  type:"message2", 
	  data:{hello:"world"}, 
	  success: function(data) {
	    //console.log(JSON.stringify(data));
	    var height = (data.height)+'px';
	    var width = (data.width)+'px';
	    var url = data.url;
	    //console.log(window.location.href);
	    var reloadUrl = 'http://' + parseUri(window.location.href).authority+parseUri(window.location.href).path + '#' + parseUri(url).relative;
	    //console.log(reloadUrl);
	    window.location.href = reloadUrl;
	    jQuery('#'+id).closest('div').height(height).width(width);
	    }
	});
	}

	function callIframe(div, callback) {
	    var url = jQuery('.iframe').attr('src');
	    var id = jQuery('.iframe').attr('id');
	    
	    var baseUrl = 'http://' + parseUri(url).authority + parseUri(url).path;

	    
	    var path = parseUri(window.location.href).relative.split('#',1) + '#';
	    var newUrl = baseUrl + parseUri(window.location.href).relative.substring((path.length));
	    

	    //console.log(newUrl);
	    
	    jQuery('iframe#'+id).attr('src', newUrl);

	    jQuery('iframe#'+id).load(function() 
	    {

	        callback(id);
	    });
	}





	callIframe(jQuery('.iframe'), test);


	//
	

	jQuery("html").live("myLiveEvent", function(e, myName, myValue) {
		  //console.log('live event');
			jQuery('table.eshop').remove();
			jQuery('div.eshopdetails').remove();
		});

	jQuery('.wordpress-post').each(function(){
		
		
		site = jQuery(this).attr('url');
		post_id = jQuery(this).attr('post');
		id = jQuery(this).attr('id');
		var wpSite = getSite(wp_sites,site);
		
		//console.log(wpSite);

		var div_id = jQuery(this).parent().attr('id');
		
		//var blog = WP.open('http://pablocantero.com/blog');
		var blog = WP.open('http://'+wpSite);
		// find needs to be found ;-)

		blog.posts().findByPostId(post_id, function(post){
		//jQuery('#'+id).html(post.content);
		
			jQuery('#'+div_id).append('<div>'+post.content+'</div>').trigger("myLiveEvent");
			//console.log(post.content);
		
		}); 
		
	});



});
