domDone = false;

jQuery.fn.valign = function(mode)
{
	this.each(function(){
		var elm = $(this);
		var parent = $(this).parent();
		var delta = parent.innerHeight(true) - elm.outerHeight(true);
		elm.css('margin-top',  delta + 'px');
		//console.log(delta, this);
		/*
		*/
		//parent.height(parent.height() - delta);
	});
};

jQuery.fn.syncHeight = function(mode, minHeight)
{
	if(typeof mode == 'undefined')
		mode = 'max';
	mode = mode.toLowerCase();
	var comp = function(a,b){ return a>b; }
	if(mode == 'min')
		comp = function(a,b){ return a<b; };
	var value = null;
	
	if(typeof minHeight != 'undefined' && minHeight > 0)
    value = minHeight;
		
	this
    .each(function()
    {
      if(value == null || comp($(this).height(), value))
        value = $(this).height();
    }
    )
	.height(value)
	.css('height', value+"px")
	.css('height', value+"px !important");
}

jQuery.fn._getImagesOfElement = function()
{
  rgxExtractBgImg = /url\(['"]?(.*?)['"]?\)/;
  var items = $(this).filter('IMG');
  var bgImage = $(this).attr('background-image');
  var match = null;
  var that = this;
  var img = null;
  
  $(this).each(function(){  
    if(typeof bgImage !== 'undefined' && (match = rgxExtractBgImg.match(bgImage)) !== null && match[1])
    {
      img = $('<img />').attr('src', bgImage);
      items.add(img);
    }
  });
  return items;
}

jQuery.fn.gfxReady = function(callbackSingle, callbackAll)
{
  var images = $(this).add($('*', this))._getImagesOfElement();
  
  if(images.length < 1)
  {
    if(callbackSingle)
      callbackSingle.call(this);
    if(callbackAll)
      callbackAll.call(this);
   
  }
  
  var count = images.length;
  var that = this;
  var loadCB = function(){
    $(this).addClass('loaded');
    if(jQuery.isFunction(callbackSingle))
      callbackSingle.apply(this, arguments);
    if(--count < 1 && jQuery.isFunction(callbackAll))
      callbackAll.apply(that, arguments);
  };
  
  images.each(function(){
    if($(this).hasClass('loaded') || this.complete)
      loadCB.apply(this);
    else
      $(this).load(loadCB);
  });
};

function domReady()
{
  $(this).gfxReady(null,domReadyImages);
	$("a.fb").fancybox({ transitionIn: 'elastic', transitionOut: 'elastic', titleShow:false});
}

function domReadyImages()
{
  if(domDone)
    return;
	$('.collogo').syncHeight();
	//$('.colheader').syncHeight();
	$('.col .content').syncHeight('max', 370);
	$('.valignb').valign('b');
	$('#waiting').fadeOut();
	domDone = true;
}
jQuery(domReady);

