/*
 * L A Y O U T
 */
(function($) {
	
	// static constructs
	$.youplanet = $.youplanet || {};
	
	$.youplanet.layout = {
		loader: function(sloader, show, margin) {
			loader(sloader, show, margin || {});
		},
		
		complete: function(jqXHR, textStatus) {
			complete(jqXHR, textStatus);
		}
	};
	
	function loader(sloader, show, margin) {
		sloader = $(sloader);
		
		if(sloader.length) {
			var m = $.extend({ top: 0, right: 0, bottom: 0, left: 0 }, margin || {});
				
			if(show) {
				sloader.css('width', sloader.parent().width() - (m.left + m.right)).
						css('height', sloader.parent().height() - (m.top + m.bottom)).
						css('margin-top', m.top).
						css('margin-left', m.left). 
						show();
			}
			else {
				sloader.hide();
			}
		}
	}
	
	function complete(jqXHR, textStatus) {
		if (textStatus == 'success') {
			var json = system.getHeaderJSON(jqXHR);

			if(json) {
				var notice = null;
				
				if((notice = system.noticeFromJSON(json))) {
					if($('#overlay,#prompt,.overlay').length) {
						$.youplanet.popup.toAlert('#overlay,#prompt,.overlay', notice, 3000);
					}
					else {
						$.youplanet.popup.alert(notice);
					}
				}

				$.each(json, function(i, el) {
					if('parent:' == el[0].substring(0, 7)) {
						if(window.parent.jQuery(el[0].substring(7)).length) {
							window.parent.jQuery(el[0].substring(7)).html(el[1]);
						}
					}
					else {
						if($(el[0]).length) {
							$(el[0]).html(el[1]);
						}
					}
				});
			}
		}
	}
}) 
(jQuery);

