/*
 *	@author Isaac Rivera & Brian Adams
 *	@version 1.0
 *	@date 12/15/2005
 */
try {
	document.domain = 'aol.com';
} catch(err){}

var g_MotifId = null;
var g_expandOnLoad = false;
var currentItem = null;
var newPlayerInstance = true;
var defaultBanner = 'http://ad.doubleclick.net/adj/tw.aol.us.ban/mus_music_videos;chan=music;sz=300x60;ord=1114012113?';
var interval = null;
var banner_locator_frame = null;

// DART JSF
function registerMotifObjId(id) {
	g_MotifId = id;
	if (g_expandOnLoad) {
		window.setTimeout(function() {
				try {
					window.expandMotif();
				} catch(err){}
			}, 100);
		g_expandOnLoad = false;
	}
}
function expandMotif() {
	if (window.frames && window.frames.ac_frame && window.frames.ac_frame.expandMotif) {
		window.frames.ac_frame.expandMotif(g_MotifId);
	}
}
function collapseMotif() {
	if (window.frames && window.frames.ac_frame && window.frames.ac_frame.collapseMotif) {
		window.frames.ac_frame.collapseMotif(g_MotifId);
   }
}
function startUpdates() {
	stopUpdates();
	window.interval = setInterval(function() {
			try {
				window.updatePlayer();
			} catch(err){}
		}, 500);
}

function stopUpdates() {
	clearInterval(window.interval);
}

function updatePlayer() {
	var info = FUGUHandle.getPollingInfo();
	if (info) {
		// get current item
		var item = info.currentItem;
		if (item) {
			// check for new item
			if (item != currentItem) {
				// check and see if we need to request an ad banner
				if (newPlayerInstance && (item.curtain == null)) {
					setDefaultAd(item);
               	}
				newPlayerInstance = false;
				// check for ad curtain commands
				if (item.curtain) {
					var curtain = item.curtain;
					// load new curtain
					if (curtain.tagURL) {
						if (curtain.expand) {
							g_expandOnLoad = true;
                       }
						setDCad(curtain.tagURL);
					}
					// expand existing curtain
					else if (curtain.expand) {
						expandMotif();
					}
				} else {
					// collapse curtain
					collapseMotif(); 
               }
				// update current item
				currentItem = item;
			}
		}
	}
}
function setDCad (url) {
	if (window.frames && window.frames.ac_frame) {
		window.frames.ac_frame.location.href = 'http://cdn.channel.aol.com/ch_tv/ac.html?dcurl=' + escape(url);
	}
}
function writeTheAdCurtain(left, top) {
	document.write("<div id=\"IZKcurtainDiv\" style=\"position:absolute; top:" + top + "px; left:" + left + "px; background-color:transparent;\"><iframe id=\"ac_frame\" name=\"ac_frame\" tabindex=\"1\" width=\"300\" height=\"250\" border=\"0\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" style=\"overflow:hidden; background-color:transparent; display:none;\" scrolling=\"no\"></iframe><script language=\"javascript\">document.getElementById(\"ac_frame\").allowTransparency=true;</script></div>");
}

/* @param item - A playlist js item. */
setDefaultAd = function(item) {
		var cpid = item.cpid;
		var pmmsid = "";
		if(cpid != null) {
			var cpid_elements = cpid.split(":");
			var new_url = null;
			switch(cpid_elements[0]) {
				case "pmmsid" :
					new_url = "http://us.video.aol.com/banner.adp?pmmsid=" + cpid_elements[1] + "&referer=" + top.location.href;
					break;
				case "pls" :
					new_url = "http://us.video.aol.com/banner.adp?pls=" + cpid_elements[1] + "&referer=" + top.location.href;
					break;
				default :
					new_url = "http://us.video.aol.com/banner.adp?pmmsid=&referer=" + top.location.href;
					break;
			}
			if(new_url) {
				if(window.banner_locator_frame) {
					loadIntoFrame(window.banner_locator_frame, new_url);
				} else {
					window.banner_locator_frame = top.document.createElement("iframe");
					window.banner_locator_frame.id = "banner_locator_frame";
					window.banner_locator_frame.width = 1;
					window.banner_locator_frame.height = 1;
					window.banner_locator_frame.style.position = "absolute";
					window.banner_locator_frame.style.top = "-1000px";
					window.banner_locator_frame.style.left = "-1000px";
					window.banner_locator_frame.visibility = false;
					window.banner_locator_frame.src = new_url;
					window.document.getElementById("IZKflashDalaiDiv").parentNode.appendChild(window.banner_locator_frame);
				}
			}
		}
	}
	
	/** load_into_frame */
	loadIntoFrame = function(i_frame, url) {
		try {
			if(typeof(i_frame.contentWindow.location) != "undefined") {
				if(i_frame.contentWindow.location.href == url 
						&& typeof(i_frame.contentWindow.location.reload) != "undefined") {
					i_frame.contentWindow.location.reload(true);
				} else {
					i_frame.contentWindow.location.href = url;
				}
			} else if(typeof(i_frame.contentDocument.location) != "undefined") {
				if(i_frame.contentDocument.location.href == url 
						&& typeof(i_frame.contentDocument.location.reload) != "undefined") {
					i_frame.contentDocument.location.reload(true);
				} else {
					i_frame.contentDocument.location.href = url;
				}
			} else if(typeof(i_frame.location) != "undefined") {
				if(i_frame.location.href == url 
						&& typeof(i_frame.location.reload) != "undefined") {
					i_frame.location.reload(true);
				} else {
					i_frame.location.href = url;
				}
			} else {
				i_frame.src = url;
			}
		} catch(e) {}
	};
