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 ((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 = document.getElementById ("TILE_FRAME");
	//abaDebug ("ADYSNC EVENT: About to edit frame before: " + theFrame.src);

	var checkDoc = theFrame.contentDocument;
	if (checkDoc == undefined || checkDoc == null) checkDoc = theFrame.contentWindow.document;

	checkDoc.open();	
	abaDebug ("ADYSNC EVENT: About to edit frame after: " + theFrame.src);
	//abaDebug ("ADYSNC EVENT: About to edit frame checkDoc: " + checkDoc.title);

	if (checkDoc) {
		if (is_default) 
		{ 
			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="300" height="250">';
          	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>';
			abaDebug ("ADYSNC EVENT: About to add SWF HTML: " + adHtml);
			checkDoc.write (adHtml);
			SetAdSyncTimer();					
		}
		else 
		{
			adHtml  = '<a href="'+imgHref+'" target="_blank">';
			adHtml += '<img src="'+imgUrl+'" width="300" height="250" border="0" />';
			adHtml += '</a>';
			abaDebug ("ADYSNC EVENT: About to add IMG HTML: " + adHtml);
			checkDoc.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;
		}
		//abaDebug ("ADYSNC EVENT: Calling AdSync href: " + href);
	}
	else
	{
		// set default image and href
		image = defaultImage;
		href  = defaultHref;
	}

	//abaDebug ("ABACASTADYSNC EVENT: Calling AdSync (href + img): " + href + " - " + image);
	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", "title=http://www.abacast.com&image=images/leader.gif")', 5000);
