// JavaScript Document

document.aw_lib = {};
document.aw_data = {};
document.aw_data.global_hide = [];

var err_log = 1;

function init( panel, rootEl ) {
  if(!rootEl) rootEl = document;
	$(document).ready( function() {
		if ( typeof panel == 'undefined') log('Error: Undefined panel!');
		else _init2(panel, this);
	});
}

function _init2 ( panel, rootEl) {
	var panels = panel.split(',');
	for (var i = 0; i < panels.length; i++)
  {
		var pan = $.trim(panels[i]);
		_init3(pan, rootEl);
	}
}

function _init3 ( panel, rootEl ) {
  var panelName = panel.replace(/-/g, '_');
  $("." + panel, rootEl).each ( function() {
    var component = new document.aw_lib[panelName](this, rootEl);
    document.aw_data[this.id] = component;
 }); 
}

log = function log( text ) {
	if(err_log) $("<div class = 'log'>" + text + "</div>").appendTo('body');
}

function repAjaxAnchors(el)
{
  if(!el.repAjaxAnchors)
  {
  	$("a.aw-ajax", el).each(function(){
    	var link = $(this).attr("href");
    	var request = $.ajax({url: link, async: false});
    	$(this).replaceWith(request.responseText);
   	});
   	el.repAjaxAnchors = true;
  }
}

$(document).click(function(){
  var a = document.aw_data.global_hide;
  for(var i=0; i<a.length; i++) $(a[i]).hide();
  a.length=0;
});
