var g_synctimer = null;

function getUrlParm (parm, url)
{
	var a1 = url.split('&');
	for (i = 0; i < a1.length; i++)
	{
		var s = a1[i].split("=");
		if (s.length > 1) 
		{
			if (String(s[0]).toLowerCase() == parm.toLowerCase()) return unescape(s[1]);
		}
	}
	return '';
}

function isImage (image)
{
	return true;
	return ((image.indexOf('.gif', 0) != -1) || 
	        (image.indexOf('.jpg', 0) != -1) || 
	        (image.indexOf('.png', 0) != -1) || 
		    (image.indexOf('.jpeg', 0) != -1)||	
		    (image.indexOf('.swf', 0) != -1));	
}

function AdSync (imgUrl, imgHref) {
	ClearAdSyncTile();

	var adHtml = "";
	
	var is_default = (imgUrl == defaultImage);
	var theFrame = null;
	
	try {
		theFrame = document.getElementById ("TILE_FRAME");			
	} catch (e) {
		alert ('exception in getElementById: ' + e);
	}
	if (theFrame != null) {		
		if (is_default) 
		{ 
			theFrame.setAttribute("src", imgUrl);
			window.status = window.status + 'adsync: ' + imgUrl + ' ' + imgHref;
		} 
		else
		{
			theFrame.setAttribute("src", imgUrl);
			window.status = window.status + 'adsync: ' + imgUrl + ' ' + imgHref;
		}
		
		/*
		
		if (imgUrl.indexOf('.swf') != -1) {
			adHtml = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="468" height="60">';
          	adHtml += '<param name="movie" value="' + imgUrl + '" />';
          	adHtml += '<embed src="' + imgUrl + '" bgcolor="#646464" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="intervalCheck=20"/>';
          	adHtml += '</embed>';
        	adHtml += '</object>';
			theFrame.contentWindow.document.open();
			theFrame.contentWindow.document.write (adHtml);
			SetAdSyncTimer();					
		}
		else 
		{
			adHtml  = '<a href="'+imgHref+'" target="_blank">';
			adHtml += '<img src="'+imgUrl+'" width="468" height="60" border="0" />';
			adHtml += '</a>';
			
			theFrame.contentWindow.document.open();
			theFrame.contentWindow.document.write (adHtml);
			SetAdSyncTimer();
		}
		*/
		
		//
		// if we're not setting the default ad, we need to put the default ad back up in 90 seconds.
		//
		if (!is_default) SetAdSyncTimer();
	}
}


function AbacastAdSync (type, value) {

	if (type != 'data') {
		window.status = type + ' value: ' + value + ' ' + window.status;
		//return;
	}

	var href    = '';
	var artist  = getUrlParm ("artist",  value);
	var album   = getUrlParm ("album",   value);
	var title   = getUrlParm ("title",   value);
	var image   = getUrlParm ("picture", value);
				
	// if it looks like ad-content, use it.
	if (image != "" && isImage(image)) {
		if (title.indexOf ('http://', 0) == -1)
		{
			href = 'http://' + title;
		}
		else
		{
			href = title;
		}
	}
	else
	{
		// set default image and href
		image = defaultImage;
		href  = defaultHref;
	}
	
	AdSync (image, href);
}


function SetAdSyncTimer ()
{
	ClearAdSyncTile();
	g_synctimer = setTimeout ("ResetAdSyncTile ()", 90000);
	Status ('SetAdSyncTimer');
}

function ClearAdSyncTile()
{
	if (g_synctimer) clearTimeout (g_synctimer)
	Status ('ClearAdSyncTile');
}
		
function ResetAdSyncTile ()
{
	AdSync (defaultImage, defaultHref);
	Status ('ResetAdSyncTile');
}

function Status (str)
{
	window.status = str + ' | ' + window.status;
}

//setTimeout ("AdSync('http://www1.abacast.com/demo/regent/player/images/leader.gif','http://www.abacast.com')", 10000);
//setTimeout ('AbacastAdSync("data", "picture=http://www.abacast.com&image=images/leader.gif")', 5000);
//setTimeout ('AbacastAdSync("data", "picture=http://www.abacast.com&image=images/leader.gif")', 10000);

