var g_synctimer = null;

/* get a pararm value from the url */
function getUrlParm(parm, value) {
   if (!parm) return '';
   seperator = '&';
   parm += '=';
   
   
   /* search this doc */
   var s = value;
   var i = s.indexOf(parm,0);

   /* found parm, now get the value */
   if (i != -1) {
      /* move the index to the end of the keyword */
      i += parm.length;
      if (i == s.length) return '';
      
      /* find the ampersand */
      var ii = s.indexOf(seperator, i);
      if (ii == -1) ii = s.length;
      
      /* return the value */
      return s.substring(i, ii);
   }
   else 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('.html',0) != -1) ||
		    (image.indexOf('.swf', 0) != -1));	
}

function AdSync (imgUrl, imgHref) {
	abaDebug ("AdSync: url:" + imgUrl + "<br>href:" + imgHref);
	ClearAdSyncTile();

	var adHtml = "";
	
	
	var is_default = (imgUrl == defaultImage);
	var theFrame = document.getElementById ("TILE_FRAME");
	theFrame.contentWindow.document.open();	
	if (theFrame) {
		//close then re-open the content frame if it was already opened to clear contents, this fixes a problem with the contents of the frame not clearing in firefox
		theFrame.contentWindow.document.close();
		theFrame.contentWindow.document.open();	
		if (is_default) 
		{ 
			theFrame.setAttribute("src", imgUrl);
			window.status = window.status + 'adsync: ' + imgUrl + ' ' + imgHref;

		} 
		//support for adsync images of .html 
		if (imgUrl.indexOf('.html') != -1) {
			imgUrl = 'http://v3.player.abacast.com/banners/dynamic_300x250.php?url=' + imgUrl;
			abaDebug("new imgUrl: " + imgUrl);
			theFrame.setAttribute("src", imgUrl);;
		}
		if (imgUrl.indexOf('.swf') != -1) {
			abaDebug("writing flash code");
			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>';
			theFrame.contentWindow.document.write (adHtml);
			abaDebug(adHtml);
			SetAdSyncTimer();					
		}
		else 
		{
			abaDebug("writing standard img code");
			adHtml  = '<a href="'+imgHref+'" target="_blank">';
			adHtml += '<img src="'+imgUrl+'" width="300" height="250" border="0" />';
			adHtml += '</a>';
			abaDebug ('setting image href ' + adHtml);
			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();
	}
	else {
		abaDebug("cannot access frame");
	}
}


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", "title=http://www.abacast.com&image=images/leader.gif")', 5000);
