$(document).ready(function()
{
	setInterval("forms()",50);
	intervalPE=setInterval("propertyElementsNormalization()",50);
	intervalNews=setInterval("newsN()",50);
	
	$('body:not(.template-property) #pageimage img').imagesLoaded(news);

	countObInfoSubmitFix=0;
	intervalObInfoSubmitFix=setInterval("obInfoSubmitFix()",100);
});



function forms()
{
	$('form textarea').each(function(){if(!$(this).closest('p.input_element').hasClass('textarea')){$(this).closest('p.input_element').addClass('textarea');}});
	$('form .boxfield').each(function(){if(!$(this).closest('p.input_element').hasClass('multiplec'))	{	$(this).closest('p.input_element').addClass('multiplec');}});
}


function obInfoSubmitFix()
{
	$('#ob_info fieldset,#ob_submitting').appendTo('#ob_form > form');
	if($('#ob_form form fieldset.buttons,#ob_form form fieldset.terms,#ob_form form #ob_submitting').length>0)
	{ 
		countObInfoSubmitFix++;
		if(countObInfoSubmitFix>=3) { clearInterval(intervalObInfoSubmitFix); }
	}
}


function propertyElementsNormalization()
{
	if($('.template-property').length<=0)	{	clearInterval(intervalPE); return false;	}
	
	var selectorCM=$('#availability_calendar_menu');
	var selectorInfo=$('.information');
	var selectorUDPA654=$('.description-udpa-654');
	var selectorUDPA646=$('.description-udpa-646');
	var selectorUDPA645=$('.description-udpa-645');

	if(selectorCM.length<=0)	{	return true;	}
	
	var CMposition=$('#availability_and_price').height()-50-285;	
	selectorCM.css({top:CMposition+'px'});	
	
	var infoPosition=CMposition+415+65;	
	selectorInfo.css({top:infoPosition+'px'});	

	var UDPA654position=infoPosition+selectorInfo.height();	
	selectorUDPA654.css({top:UDPA654position+'px'});	

	var UDPA646position=UDPA654position+selectorUDPA654.height();	
	selectorUDPA646.css({top:UDPA646position+'px'});	

	var UDPA645position=UDPA646position+selectorUDPA646.height();	
	selectorUDPA645.css({top:UDPA645position+'px'});	
}


function newsN()
{
	if($('.template-page').length<=0)	{	clearInterval(intervalNews); return false;	}
	if($('#pageimage img').length<=0)
	{	$('#news').hide();	clearInterval(intervalNews); 	}
}


function news()
{
	if(!this)	{	return false;	}
	$('#news').css({top:170+$(this).height()+'px'});
}




$.fn.imagesLoaded=function(callback)
{
  var elems = this.filter('img'),
      len   = elems.length,
      blank = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
      
  elems.bind('load',function(){
      if (--len <= 0 && this.src !== blank){ callback.call(elems,this); }
  }).each(function(){
     // cached images don't fire load sometimes, so we reset src.
     if (this.complete || this.complete === undefined){
        var src = this.src;
        // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
        // data uri bypasses webkit log warning (thx doug jones)
        this.src = blank;
        this.src = src;
     }  
  }); 

  return this;
};

