//RSS Parser Module Functions  -- Begin
function RSS2Enclosure(encElement) {
	if (encElement == null)
	{
		this.url = null;
		this.length = null;
		this.type = null;
	}
	else
	{
		this.url = encElement.getAttribute("url");
		this.length = encElement.getAttribute("length");
		this.type = encElement.getAttribute("type");
	}
}

function RSS2Guid(guidElement) {
	if (guidElement == null)
	{
		this.isPermaLink = null;
		this.value = null;
	}
	else
	{
		this.isPermaLink = guidElement.getAttribute("isPermaLink");
		this.value = guidElement.childNodes[0].nodeValue;
	}
}

function RSS2Source(souElement) {
	if (souElement == null)
	{
		this.url = null;
		this.value = null;
	}
	else
	{
		this.url = souElement.getAttribute("url");
		this.value = souElement.childNodes[0].nodeValue;
	}
}


function RSS2Item(itemxml) {
	
	this.title;
	this.link;
	this.description;


	this.author;
	this.comments;
	this.pubDate;

	
	this.category;
	this.enclosure;
	this.guid;
	this.source;

	var properties = new Array("title", "link", "description", "author", "comments", "pubDate");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = itemxml.getElementsByTagName(properties[i])[0];
		if (tmpElement != null)
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	this.category = new RSS2Category(itemxml.getElementsByTagName("category")[0]);
	this.enclosure = new RSS2Enclosure(itemxml.getElementsByTagName("enclosure")[0]);
	this.guid = new RSS2Guid(itemxml.getElementsByTagName("guid")[0]);
	this.source = new RSS2Source(itemxml.getElementsByTagName("source")[0]);
}


function RSS2Category(catElement) {
	if (catElement == null)
	{
		this.domain = null;
		this.value = null;
	}
	else
	{
		this.domain = catElement.getAttribute("domain");
		this.value = catElement.childNodes[0].nodeValue;
	}
}


function RSS2Image(imgElement) {
	if (imgElement == null)
	{
	this.url = null;
	this.link = null;
	this.width = null;
	this.height = null;
	this.description = null;
	}
	else
	{
		imgAttribs = new Array("url","title","link","width","height","description");
		for (var i=0; i<imgAttribs.length; i++)
			if (imgElement.getAttribute(imgAttribs[i]) != null)
				eval("this."+imgAttribs[i]+"=imgElement.getAttribute("+imgAttribs[i]+")");
	}
}


function RSS2Channel(rssxml) {
	
	this.title;
	this.link;
	this.description;
	
	this.items = new Array();
	
	this.language;
	this.copyright;
	this.managingEditor;
	this.webMaster;
	this.pubDate;
	this.lastBuildDate;
	this.generator;
	this.docs;
	this.ttl;
	this.rating;

	
	this.category;
	this.image;

	var chanElement = rssxml.getElementsByTagName("channel")[0];
	var itemElements = rssxml.getElementsByTagName("item");

	for (var i=0; i<itemElements.length; i++)
	{
		Item = new RSS2Item(itemElements[i]);
		this.items.push(Item);
	
	}

	var properties = new Array("title", "link", "description", "language", "copyright", "managingEditor", "webMaster", "pubDate", "lastBuildDate", "generator", "docs", "ttl", "rating");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = chanElement.getElementsByTagName(properties[i])[0];
		if (tmpElement!= null)
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	this.category = new RSS2Category(chanElement.getElementsByTagName("category")[0]);
	this.image = new RSS2Image(chanElement.getElementsByTagName("image")[0]);
}
function processRSS(rssxml) {
	RSS = new RSS2Channel(rssxml);
	showRSS(RSS);
}

function showRSS(RSS) {
	
	
	var lPan = document.getElementById("listPanel");
	lPan.innerHTML="";
	

	document.getElementById("leftBorder").style.background = "transparent url('images/lbor.png') no-repeat";	
	document.getElementById("rightBorder").style.background = "transparent url('images/rbor.png') no-repeat";	
	
	for (var i=0; i<6; i++)	
	{
		var rItem = RSS.items[i];	
		var floatAdd = "";
			
						myDIV= "<div class='album' style='float:left'><div class='albumInfo'>                   <div onclick=\" clickReport('"+rItem.link.replace(/^\s\s*/, '').replace(/\s\s*$/, '')+"?fref="+fRefCode+"',true) \" class='albumInfoImage' style=\"margin-left: -2px; \">       <img style='position: relative;' src="+parseImage(rItem.description)+" width=48 height=46 />   "+operaFix+"            </div>                     <div class='albumInfoTitle' ><div style='cursor: pointer;' class='player2' onclick='clickReport(\""+rItem.link.replace(/^\s\s*/, '').replace(/\s\s*$/, '')+"?fref="+fRefCode+"\",true);'><img src='images/dwn.png' /></div><div class='player' style='cursor: pointer;' onclick='clickReport(\""+parseM3U(rItem.description,true)+"?fref="+fRefCode+"\",true);'><img src='images/playbtn.png' /></div><div id='albTit"+i+"' class='truncate' onclick=\" clickReport('"+rItem.link.replace(/^\s\s*/, '').replace(/\s\s*$/, '')+"?fref="+fRefCode+"',true) \">"+formatTitle(rItem.title,i)+"</div></div></div></div>";											
			
		
		lPan.innerHTML += myDIV;
	}
		
	truncateTitles();
	
	return true;
}

function getRSS(myrss) {
	if(!myrss) {
		var rssURL=getURLParam("feed");
		if(!rssURL) alert('No feed provided!');
	}
	else {
		rssURL = myrss;
	}
	
	if (window.ActiveXObject)
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		xhr = new XMLHttpRequest();
	else
		alert("not supported");

	
	
   xhr.open("GET",rssURL,true);

	xhr.setRequestHeader("Cache-Control", "no-cache");
	xhr.setRequestHeader("Pragma", "no-cache");
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4)
		{
			if (xhr.status == 200)
			{
				if (xhr.responseText != null)
					processRSS(xhr.responseXML);
				else
				{
					showPromPanel();
					//alert("Failed to receive RSS file from the server - file not found.");
					return false;
				}
			}
			else
				showPromPanel(); 
		}
		
	}

	
	xhr.send(null);
	
}

//RSS Parser Module Functions  -- End 

//Function startUp is loading when the body of index.html ends is loaded.
function startUp(){	
	
	var browser=navigator.appName;
		
		if(browser=='Opera') { //Opera browser alignment fixes
			 operaFix = "<div style='width: 48px; height: 46px; position: relative;margin-top: -46px; background: transparent url(\"images/round.png\");' /> </div>";
			 document.getElementById('rightBorder').style.left = '293';
			 document.getElementById('albumsList').style.margin = '-3px 0 0 36px';
		}
		else { //Internet Explorer browser alignment fixes
			 operaFix = "<!--[if IE]>  <div style='width: 48px; height: 46px; position: relative;margin-top: -50px;background: transparent url(\"images/round.png\");   '> </div>  <![endif]-->  <![if !IE]>   <div style='width: 48px; height: 46px; position: relative;margin-top: -46px; background: transparent url(\"images/round.png\");' /> </div>    <![endif]> ";
		}	
	
	var defG = getURLParam('defaultGenre');
	if(!defG) defG='classic';
	
	document.getElementById(defG).selected = true;
	
	
	var promTxt = getURLParam('promotionText'); 
	var promUrl = getURLParam('promotionURL'); //Getting the promotion links and title from URL Parameters added to iframe source
	
	if(typeof(promTxt)!='undefined' && typeof(promUrl)!='undefined') {
		
		var promLink="<a style='cursor:pointer;float:right;' onclick='clickReport(\""+promUrl+"?fref="+fRefCode+"\",true);' >"+promTxt+"</a>";  // Adding the affiliate code (from iFrame source) to the promotional link
		var lf = document.getElementById('leftFooter');				
		lf.innerHTML = promLink;		
	}
	
	getRSS(document.getElementById(defG).value);
	
}



//When GO button from the footer is clicked goToPromotion() function is called
function goToPromotion(){	
	var promUrl = getURLParam('promotionURL')+"?fref="+fRefCode;	
	clickReport(promUrl,true);
}



//When feed is no loaded showPromPanel() function is called
function showPromPanel() {
	var lPan = document.getElementById("listPanel");
	var promTxt = getURLParam('promotionText');
	var promUrl = getURLParam('promotionURL');
	lPan.innerHTML="<li style='padding-bottom:10px;'><a href='"+promUrl+"' style='color:#254B7A;font-family:Arial;font-size:11px;font-weight:bold;text-decoration:none;'>"+promTxt+"</a></li>";
	
	document.getElementById("leftBorder").style.background = "transparent url('images/lborsm.png') no-repeat";	
	document.getElementById("rightBorder").style.background = "transparent url('images/rborsm.png') no-repeat";	
	
}



function parseImage(myDesc){ //Getting the image source from the album's content
			
			var imgInd = myDesc.indexOf("src=\"");
			var restStr = myDesc.substring(imgInd,myDesc.length);
				imgInd = restStr.indexOf(">");
			var realStr = restStr.substring(5,imgInd-1);		
		
			return realStr;
}

function formatTitle(tit,divId){ //Getting the artist's title from the album's content
	var tInd = tit.indexOf(" -");
	var cutTitle = tit.substring(0,tInd);		
	return cutTitle;
}

function truncateTitles() { //Truncating the title to fit in two rows
	for(i=0;i<6;i++){
		var currTit = document.getElementById("albTit"+i);
		var insideText = currTit.innerHTML;
		
			if(currTit.offsetHeight  > maxHeight){				
				currTit.innerHTML = '';				
				var one='';
				var two='';
				var cnt = 0;
				while(currTit.offsetHeight < maxHeight){					
					two = one;					
					one += insideText.charAt(cnt);					
					currTit.innerHTML = one;					
					cnt ++;					
				}
				one ="";				
				for(ns=0;ns<two.length-3;ns++){
					one += two.charAt(ns);
				}				
				currTit.innerHTML = one+"...";			
			}
			
	}
}

function changeFeed() { //When a new genre is selected from the drop down menu changeFeed() function is called	
	var theFeed = document.getElementById("albumsList").value;
	var theFeedID = document.getElementById("albumsList").selectedIndex;	
	var selID = document.getElementById("albumsList").options[theFeedID].id;
	clickReport(realFeeds[selID],false);
	getRSS(theFeed);
}

function parseM3U(part) { //Getting the m3u file source from the album's content	
	var tmpInd = part.indexOf("</a><br>");	
	var aStr  = part.substring(tmpInd+8,part.length);	
	tmpInd = aStr.indexOf("</a><br>");	
	aStr = aStr.substring(tmpInd+8,aStr.length);	
	aStr = aStr.substring(0,aStr.indexOf("\">"));	
	aStr = aStr.substring(16,aStr.length)	;
	return aStr;	
}

function getURLParam(strParamName){ //This function parses the URL parameters provided by the iFrame source and returns the value of the required parameter.
  var strReturn = "";
  var strHref = window.location.href;
 
  if ( strHref.indexOf("?") > -1 ){ 
  	  var strQueryString = strHref.substr(strHref.indexOf("?"));    	 
      var aQueryString = strQueryString.split("&");
      for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){     		
			      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){					      		    
			        var aParam = aQueryString[iParam].split("=");			        
			        strReturn = aParam[1];
			        break;
			      }    		
    }
  }
 
  return unescape(strReturn);
}

var operaFix = "";  //Opera browser alignment fix variable 

var realFeeds = new Array();  //This array contains the real eMusic Feeds so they can be 'recorded' when a Feed is selected
realFeeds['classic'] = "http://www.emusic.com/rss/0/b/-dbm/a/0-0/1200000453/0.html";
realFeeds['jazz'] = "http://www.emusic.com/rss/0/b/-dbm/a/0-0/1200000348/0.html";
realFeeds['world'] = "http://www.emusic.com/rss/0/b/-dbm/a/0-0/1200000324/0.html";
realFeeds['indie'] = "http://www.emusic.com/rss/0/b/-dbm/a/0-0/1200000296/0.rss";
realFeeds['alter'] = "http://www.emusic.com/rss/0/b/-dbm/a/0-0/1200000300/0.html";
realFeeds['pop'] = "http://www.emusic.com/rss/0/b/-dbm/a/0-0/1200000277+151/0.html";


var fRefCode = getURLParam("affiliateCode");  //The affiliate code gotten from URL parameter provided by the iFrame source
var artId = getURLParam("artId");  //The artist ID gotten from URL parameter provided by the iFrame source
var artTitle = getURLParam("artTitle"); //The artist Title gotten from URL parameter provided by the iFrame source
var secId = getURLParam("secId"); //The section ID gotten from URL parameter provided by the iFrame source
var secName = getURLParam("secName"); //The section name gotten from URL parameter provided by the iFrame source
var secPos = getURLParam("secPos"); //The section position gotten from URL parameter provided by the iFrame source
var channel = getURLParam("channel"); //The channel gotten from URL parameter provided by the iFrame source
var maxHeight = 35;
var xhr; // XmlHttp object loading the rss feeds





