//returns the boolean result of whether the URL referst to a valid media file
function isAudioFile(fileURL){

	start = fileURL.lastIndexOf('.');                  //find the last period character in the string

	   start++                                       //the first character of the extension is the one after the .
	   
	   extension = fileURL.substring(start, fileURL.length).toLowerCase();  //force the rest of the string to lower case
	   /*
	   if ((extension == "rm") || 
	       (extension == "ram")
	   ){ 
		return true;

	   }else if( (extension == "wma") || 
	       (extension == "asf") ||
	       (extension == "wm") ||
	       (fileURL.indexOf('mms://') != -1)
	   ){  
		return true;
	   }else */
	
	   if(extension == "mp3"){                                                 
		return true;
	   }else if(extension == "m3u"){                                                 
		return true;
	   }

	return false;
}

//this function will find all <a> tags containing a valid media file URL
//and replace their href with our javavscript function playFile(URL)
function editAudioLinks(){
	

	for (i=0;i<document.links.length;i++) {
		if( isAudioFile(document.links[i].href) ){
			if(document.links[i].rel != 'nopopup'){
				document.links[i].target='';
				document.links[i].href = "javascript: popNewWindow('http://wfiu.indiana.edu/player.php?myMediaURL=" + document.links[i].href + "')";
				document.links[i].onclick = '';
				//var this_url = document.links[i].href;
				//document.links[i].onclick = function(this_url){ alert(this_url);popNewWindow('http://wfiu.indiana.edu/player.php?myMediaURL=' + this_url); };
				//document.links[i].href = '#';
			}
			//document.getElementById('out').innerHTML =  document.getElementById('out').innerHTML + "*. - "  + document.links[i].href  + "<br>";
		}
	}

}



//**********************************************************************8
//***************************NEW SECTION*********************************
//**********************************************************************8



// popup window function, based on original code by James Craig
var sUserAgent = navigator.userAgent.toLowerCase();
var isOp = (sUserAgent.indexOf('opera')!=-1)?true:false;

function popUp(oAnchor,sProps,sWindow){
    var sUrl = '';
    if(typeof(oAnchor)=='string') sUrl = oAnchor;
    else {
        if(oAnchor.getAttribute) sUrl = oAnchor.getAttribute('href');
        if(sUrl=='') sUrl = oAnchor.href;
    }
    if(sUrl=='') return true;
    var sWindowName = sWindow?sWindow:'nprPopup';
    if(!sProps) sProps = null;
    if(sUrl) var oPopup = window.open(sUrl,sWindowName,sProps);
    if(oPopup && !isOp) oPopup.focus();
    return (oPopup)?false:true;
}

function popNewWindow(newURL)
{
    if(newURL)
    {
        popUp(newURL,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=340,height=400','player');    
    }
}


function writeAd(zoneid){
   var m3_u = (location.protocol=='https:'?'https://wfiu.org/Openads/www/delivery/ajs.php':'http://wfiu.org/Openads/www/delivery/ajs.php');
   var m3_r = Math.floor(Math.random()*99999999999);
   if (!document.MAX_used) document.MAX_used = ',';
   document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
   document.write ("?zoneid="+zoneid+"&amp;target=_blank&amp;block=1");
   document.write ('&amp;cb=' + m3_r);
   if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
   document.write ("&amp;loc=" + escape(window.location));
   if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
   if (document.context) document.write ("&context=" + escape(document.context));
   if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
   document.write ("'><\/scr"+"ipt>");
}



// ******* jquery ********

$(document).ready(function(){
    $(".podPress_content").each(function( intIndex ){


	var first = $(this).find("a:eq(1)");
	var second = $(this).find("a:last");
	var url = $(second).attr('href');
	first.attr('onclick', 'asdkfj');
	first.attr('href', url);
	second.attr('rel', 'nopopup');
	
/*
	first.text('Download');
	second.text('Play in Popup');

	$(second).after($(first).clone());
	$(first).after($(second).clone());
	$(second).remove();
	$(first).remove();
*/

    });

    editAudioLinks();
    $(".sidebar_med_rect").before("<div class='sponsor-text above'>Support for WFIU is provided by:</div>");
    $(".sidebar_med_rect").after("<div class='sponsor-text below'><a href='http://wfiu.org/support.php'>Become a WFIU Sponsor&nbsp;&raquo;</a></div>");
  });
