jQuery(window).scroll(function () { 
	if(jQuery("#ad-600-500-box").length>0)
		adCenter600500();
});

jQuery(window).resize(function () { 
	if(jQuery("#ad-600-500-box").length>0)
		adCenter600500();
});

jQuery(document).ready(function() {
	jQuery("body").prepend('<div id="ad-600-500-overlay"></div><div id="ad-600-500-box"><div id="ad-600-500-box-relative"><iframe id="ad_iframe" scrolling="no" frameborder="0" width="600" height="500"></iframe><a id="ad-600-500-close" onclick="adClose600500();">Close</a></div></div>');
	adShow600500();
	setTimeout(function(){adClose600500();},(ad_600_500_timeout*1000));
});

function ad600500GetBaseUrl() {
	urlTokens = window.location.href.split('/');
	url = urlTokens[0]+'//'+urlTokens[2];
	return url;
}

function adShow600500() {
	jQuery("#ad_iframe").attr("src",ad600500GetBaseUrl()+'/ads/adAjaxAdInterstitial.dbml?DB_OEM_ID='+ad_600_500_OEM_ID+'&site='+ad_600_500_oemname+'&sz=600x500&zone='+ad_600_500_zone+'&protocol='+ad_600_500_protocol);
	
	jQuery("#ad_iframe").load(function() {
		var firstImageAlt = jQuery("#getAdCall img:first",jQuery(this).contents()).attr("alt");
		if(firstImageAlt=='Click here to find out more!') {
			adClose600500();
		}
		else {
			jQuery("#ad-600-500-overlay").show();
			
			setTimeout(function() {adCenter600500();},1000);
		}		
	});
}

function adClose600500() {
	jQuery("#ad-600-500-box").fadeOut('slow',function() {
		jQuery("#ad-600-500-box").remove();
	});
	jQuery("#ad-600-500-overlay").fadeOut('slow',function() {
		jQuery("#ad-600-500-overlay").remove();
	});	
}

function adCenter600500() {
	var overlay = jQuery("#ad-600-500-overlay");
    var box = jQuery("#ad-600-500-box");
	windowscrollTop = jQuery(window).scrollTop();
	windowH = jQuery(window).height();
	windowW = jQuery(window).width();
	boxH = box.height();
	boxW = box.width();
	boxTop = Math.round((windowH - boxH)/2)+windowscrollTop;
	boxLeft = Math.round((windowW - boxW)/2);

	overlay.css({
		"height":windowH+"px",
		"width":windowW+"px",
		"top":windowscrollTop+"px",
		"left":"0px",
		"display":"block"
	});
	
	box.css({
		"top":boxTop+"px",
		"left":boxLeft+"px",
		"display":"block"
	});
}
