// define abc video package
if (typeof com == "undefined") {
	com = {};
}
if (typeof com.abc == "undefined") {
	com.abc = {};
}
if (typeof com.abc.ads == "undefined") {
	com.abc.ads = {
		url: 'http://oscars.com/Adserver?CallDown&AdTypes=[ADTYPE];&url=[TARGET]',
		targetUrl: null,
		refreshableAds: [],

		addRefreshableAd: function(element,adType) {
			var found = false;
			
			if (!adType) adType = 'Rectangles';
			
			// check that we don't already have this one
			for (var i=0; i<this.refreshableAds.length; i++) {
				if (this.refreshableAds[i].element == element) {
					found = true;
					break;
				}
			}
			
			if (found) return false;
			
			this.refreshableAds.push(new com.abc.ads.RefreshableAd(element,adType));
			
			return true;
		},
		
		getRefreshableAds: function() {
			return this.refreshableAds;
		},
		
		getTarget: function() {
			this.targetUrl = window.location.pathname + ((window.location.search != '?pn=index') ? window.location.search : '');
		},
		
		refreshAds: function() {
			for (var i=0; i<this.refreshableAds.length; i++) {
				this.refreshableAds[i].refresh(this.url,this.targetUrl);
			}
		}
	};
}

com.abc.ads.getTarget();

com.abc.ads.RefreshableAd = new Class.create();
com.abc.ads.RefreshableAd.prototype = {
	refreshing: false,
	adType: null,
	iframe: null,
	id: null,
	
	initialize: function(element,adType,options) {
		this.element = $(element);
		this.adType = adType;
		this.refreshed = false;
		
		this.id = this.element.id.substring(this.element.id.indexOf('_') + 1);

		this.inlineAd = $('inlineAd_' + this.id);
		this.iframe = $('iframeAd_' + this.id);
		this.iframeWrapper = $('iframeWrapper_' + this.id);

		if (!element || !adType) return null;
		
		// default options
		this.options = {};
		Object.extend(this.options, options || {});
	},
	
	refresh: function(url,target) {
		this.iframeWrapper.removeClassName('hidden');
		if (this.inlineAd !=null){
			this.inlineAd.addClassName('hidden');
		}
		
		// check that we aren't already refreshing
		if (!this.stillLoading()){
			this.iframe.src = this.getURL(url,target);
		}
	},
	
	getURL : function(url,target) {
		var newUrl = url.replace('[TARGET]',target);
		newUrl = newUrl.replace('[ADTYPE]',this.adType);

		return newUrl + this.timeStamp();
	},
	
	stillLoading: function() {
		try {
			var doc = this.iframe.contentWindow || this.iframe.contentDocument;
			
			if (doc.document) doc = doc.document;
			if (document.getElementsByTagName("body")[0] != null || document.body != null) {
				return false;
			}
		} catch(e) {
			return true;
		}
		return true;
	},
	
	timeStamp: function() {
		return new Date().getTime();
	}
}