/*
 * PriceRunner v2.0 - PriceRunner script magic
 * Copyright (c) 2008 Michael Poijes, Giulio Mola
 */
var pr_ad_check_delay=100;
var pr_ad_check_maxTime=2000;

function pr_getIframeBody(iFrame){
	var innerDocument=iFrame[0].contentDocument?iFrame[0].contentDocument:iFrame[0].contentWindow.document;
	return innerDocument.body
}

function pr_isIframeVisible(iFrameBody){
	return iFrameBody.scrollHeight>30
}

function pr_resizeIframe(iFrame,iFrameBody){
	var newHeight=iFrameBody.scrollHeight;
	iFrame.height(newHeight<0?0:newHeight);
	iFrame.width(iFrameBody.scrollWidth)
}

function pr_showIframe(iFrame,iFrameBody,removeClass){
	iFrame.parents(".adWrapper").removeClass(removeClass);
	jQuery(iFrameBody).css("visibility","visible");
	jQuery("embed",iFrameBody).attr("wmode","opaque")
}

function pr_resizeAdCallback(iFrame,iFrameBody,removeClass,totalTime){
	if(pr_isIframeVisible(iFrameBody)){
		pr_resizeIframe(iFrame,iFrameBody);
		pr_showIframe(iFrame,iFrameBody,removeClass)
	} else {
		if(totalTime<pr_ad_check_maxTime){
			setTimeout(function(){
 				pr_resizeAdCallback(iFrame,iFrameBody,removeClass,totalTime+pr_ad_check_delay)
			},pr_ad_check_delay)
		}
	}
}

function pr_resizeAd(adIFrame,removeClass){
	var iFrameBody=pr_getIframeBody(adIFrame);
	pr_resizeAdCallback(adIFrame,iFrameBody,removeClass,0)
}

function pr_initiateAdIframe(element,width,height,hiddenResizeClass){
	var anchor=element.children("a:first");
	var url=anchor.attr("href");
	if(!url){return }

	var iframe=jQuery("<iframe>").addClass("adIframe").attr("src",url).attr("width",width).attr("height",height).attr("frameBorder","0").attr("scrolling","no").attr("marginwidth","0").attr("marginheight","0");

	if(hiddenResizeClass){
		iframe.load(function(){pr_resizeAd(jQuery(this),hiddenResizeClass)})
	}
	anchor.replaceWith(iframe)
}

jQuery(document).ready(function($){

	for(var i=1;i<=3;i++){
		var ad=$("#rightAdSpot"+i);
		if(ad.hasClass("external")){
			pr_initiateAdIframe(ad,"160","600","invisible")
		} else{
			pr_initiateAdIframe(ad,"1","1","invisible")
		}
	}
}
);

