/* 
----------------------------------------------------
BANDIT.FM 
http://www.bandit.fm
----------------------------------------------------
ON THIS CHANNEL WIDGET
Author: Sony BMG Music Australia
----------------------------------------------------
*/

if (!Array.indexOf) {
  Array.prototype.indexOf = function (obj, start) {
    for (var i = (start || 0); i < this.length; i++) {
      if (this[i] == obj) {
        return i;
      }
    }
  }
}

function findActiveArtistInList(artistsLinks) {
	for(var x=0;x<artistsLinks.length;x++) {
		if((artistsLinks[x].firstChild.getAttribute('class'))||(artistsLinks[x].firstChild.getAttribute('className'))=='activeArtistFeature')
			return x+1;
	}
}

function getColNo() {
	var colNo = 0;
	if(document.getElementById('featuredArtistCol1').innerHTML!='') colNo++;  
	if(document.getElementById('featuredArtistCol2').innerHTML!='') colNo++;
	if(document.getElementById('featuredArtistCol3').innerHTML!='') colNo++;
	return colNo;
}

var onThisChannelTimeouts = new Array();
var onThisChannelIntervals = new Array();

function initOnThisChannel() {
	var delayTime = 6000;
	var currentCol = 1;
	currentColStart = 1;
	var artistsLinks = getArtistList(currentCol);
	
	for (var i=0; onThisChannelTimeouts && i < onThisChannelTimeouts.length; i++)
	{
		var lt = onThisChannelTimeouts.pop();
		clearTimeout(lt);
	}	
	for (var i=0; onThisChannelIntervals && i < onThisChannelIntervals.length; i++)
	{
		var lt = onThisChannelIntervals.pop();
		clearInterval(lt);
	}

	function changeArtistSelection() {
		var artistsLinks = getArtistList(currentCol);
		if(currentChannelPic==true&&noHoverAction==true) {
			currentChannelPic=false;
			var currentActivePosition = findActiveArtistInList(artistsLinks);
						
			if(currentActivePosition==artistsLinks.length) {
				currentActivePosition=0; // reset to 0 if at end
			if(currentCol<totalColNo) currentCol++; else currentCol=1;
				var artistsLinks = getArtistList(currentCol);
			}							
				
			if(artistsLinks[currentActivePosition]) {
				artistsLinks[currentActivePosition].firstChild.className = 'activeArtistFeature';
			} else { artistsLinks[0].firstChild.className = 'activeArtistFeature'; currentActivePosition=0; currentCol=1; }
			if(currentActivePosition>0) {
				artistsLinks[currentActivePosition-1].firstChild.removeAttribute('class');
				artistsLinks[currentActivePosition-1].firstChild.removeAttribute('className');
			} else {
					if(currentCol>1) {
						var lastCol = getArtistList(currentCol-1);
						lastCol[lastCol.length-1].firstChild.removeAttribute('class');
						lastCol[lastCol.length-1].firstChild.removeAttribute('className');
					} else {
						var lastCol = getArtistList(totalColNo);
						lastCol[lastCol.length-1].firstChild.removeAttribute('class');
						lastCol[lastCol.length-1].firstChild.removeAttribute('className');
					}
				}
			findSWF('onThisChannelFlashSWF').newArtist(artistsLinks[currentActivePosition].firstChild.getAttribute('name'),artistsLinks[currentActivePosition].firstChild.getAttribute('rel'),artistsLinks[currentActivePosition].firstChild.getAttribute('href'));
			//setTimeout(function() { changeArtistSelection() },delayTime);
			
		} //
		else { 		
			var t =	setTimeout(function() { changeArtistSelection() },delayTime); 			
			onThisChannelTimeouts.push(t);
		}
	}
	// END changeArtistSelection();
	
	function addEventsToAllChannelArtists() {
		var columns = new Array();
		columns[0] = getArtistList(1);
		columns[1] = getArtistList(2);
		columns[2] = getArtistList(3);
		
		for(var x=0;x<columns.length;x++) {
			for(var y=0;y<columns[x].length;y++) {
				new artistChannelLink(columns[x][y]);
			}
		}
	}
	
	function artistChannelLink(artistLink) {
		
		artistLink.onmouseover = function() { 
				var timeMouseOver = 0;
				var artistHoverInterval = setInterval(function() { timeMouseOver++; if(timeMouseOver>1) longHoverOK();	} ,600);
				onThisChannelIntervals.push(artistHoverInterval);
				function longHoverOK() {
					findSWF('onThisChannelFlashSWF').newArtist(artistLink.firstChild.getAttribute('name'),artistLink.firstChild.getAttribute('rel'),artistLink.firstChild.getAttribute('href'));
					var disableCurrentInList = getArtistList(currentCol);
					var currentActivePosition = findActiveArtistInList(disableCurrentInList);
					disableCurrentInList[currentActivePosition-1].firstChild.removeAttribute('class');
					disableCurrentInList[currentActivePosition-1].firstChild.removeAttribute('className');
					artistLink.firstChild.className = 'activeArtistFeature';
					noHoverAction = false;
					timeMouseOver = 0;
					if (onThisChannelIntervals && onThisChannelIntervals.length > 0)	onThisChannelIntervals.splice(onThisChannelIntervals.indexOf(artistHoverInterval),0);
					clearInterval(artistHoverInterval); 
					thisColumnID = artistLink.parentNode.parentNode.getAttribute('id');
					currentCol = thisColumnID.charAt(thisColumnID.length-1);
				}
				
				artistLink.onmouseout = function() { 
							noHoverAction = true;
							if (onThisChannelIntervals && onThisChannelIntervals.length > 0) onThisChannelIntervals.splice(onThisChannelIntervals.indexOf(artistHoverInterval),0);
							clearInterval(artistHoverInterval);
							
							if (onThisChannelIntervals && onThisChannelIntervals.length > 0) onThisChannelIntervals.splice(onThisChannelIntervals.indexOf(changeArtistInterval),0);
							clearInterval(changeArtistInterval); // restart artist changing interval			
							changeArtistInterval = setInterval(function() { changeArtistSelection() },delayTime);
							onThisChannelIntervals.push(changeArtistInterval);

						}

				
		}
		
	}
	
	
function changeChannelArtistsPage(e) {	
	if (!e) e = window.event;
	if (e.target) e = e.target; // 'parentNode' is due to Flash element
	else if (e.srcElement) e = e.srcElement;
	if (e.nodeType == 3) // defeat Safari bug
		e = targ.parentNode;
	//var direction = e.getAttribute('id').indexOf('next')>=0 ? 'next' : 'prev'; 
	var browserVersion = navigator.userAgent;
	
	if((browserVersion.indexOf('MSIE')<=0)&&e.parentNode||((browserVersion.indexOf('MSIE')>=0)&&(e=='javascript:link();'))) { // if a button was clicked
	
	if(!e.parentNode.className) { // NOT DISABLED, 'parentNode' added due to use of Flash buttons now
		var colNo = 3;
		var visibleCol1 = document.getElementById('featuredArtistCol1');
		var visibleCol2 = document.getElementById('featuredArtistCol2');
		var visibleCol3 = document.getElementById('featuredArtistCol3');
		var cID = 'channelArtistList';
		if(e.getAttribute('id').indexOf('next')>=0) {
		var newCol1 = document.getElementById(cID+(currentColStart*colNo));
		if(newCol1) var newCol2 = document.getElementById(cID+((currentColStart*colNo)+1));
		if(newCol2) var newCol3 = document.getElementById(cID+((currentColStart*colNo)+2));
		currentColStart++
		} else {
			var newCol1 = document.getElementById(cID+((currentColStart*colNo)-6));
			if(newCol1) var newCol2 = document.getElementById(cID+((currentColStart*colNo)-5));
			if(newCol2) var newCol3 = document.getElementById(cID+((currentColStart*colNo)-4));	
			currentColStart--
		}	
		
		if(document.getElementById(cID+(currentColStart*colNo))) { // show next button if needed
			document.getElementById('onThisChannel_next').removeAttribute('class');
			document.getElementById('onThisChannel_next').removeAttribute('className');
			findSWF('onThisChannel_nextSWF').disabler(1);
		}
		else {
			document.getElementById('onThisChannel_next').setAttribute('class','onThisChannel_disabled');
			document.getElementById('onThisChannel_next').setAttribute('className','onThisChannel_disabled');
			findSWF('onThisChannel_nextSWF').disabler(0);
		}
		
		if(document.getElementById(cID+((currentColStart*colNo)-6))) { // show prev button if needed
				document.getElementById('onThisChannel_prev').removeAttribute('class');
				document.getElementById('onThisChannel_prev').removeAttribute('className');
				findSWF('onThisChannel_prevSWF').disabler(1);
		} else {
				document.getElementById('onThisChannel_prev').setAttribute('class','onThisChannel_disabled');
				document.getElementById('onThisChannel_prev').setAttribute('className','onThisChannel_disabled');
				findSWF('onThisChannel_prevSWF').disabler(0);
		}
			
		if(newCol1) visibleCol1.innerHTML = newCol1.innerHTML;	
		if(newCol2) visibleCol2.innerHTML = newCol2.innerHTML; else visibleCol2.innerHTML = '';
		if(newCol3) visibleCol3.innerHTML = newCol3.innerHTML; else visibleCol3.innerHTML = '';
		addEventsToAllChannelArtists();
		currentCol = 1;
		totalColNo = getColNo();
		changeArtistSelection();
		if (onThisChannelIntervals && onThisChannelIntervals.length > 0) onThisChannelIntervals.splice(onThisChannelIntervals.indexOf(changeArtistInterval),1);
		clearInterval(changeArtistInterval); // restart interval
		changeArtistInterval = setInterval(function() { changeArtistSelection() },delayTime);
		onThisChannelIntervals.push(changeArtistInterval);

	}
} else { // re-initation event simulation
		
		var colNo = 3;
		currentColStart = 0;
		var visibleCol1 = document.getElementById('featuredArtistCol1');
		var visibleCol2 = document.getElementById('featuredArtistCol2');
		var visibleCol3 = document.getElementById('featuredArtistCol3');
		var cID = 'channelArtistList';
				
		var newCol1 = document.getElementById(cID+(currentColStart*colNo));
		if(newCol1) var newCol2 = document.getElementById(cID+((currentColStart*colNo)+1));
		if(newCol2) var newCol3 = document.getElementById(cID+((currentColStart*colNo)+2));
		currentColStart++
		
		// show next button
		document.getElementById('onThisChannel_next').removeAttribute('class');
		document.getElementById('onThisChannel_next').removeAttribute('className');
		findSWF('onThisChannel_nextSWF').disabler(1);
		
		// disable prev button
		document.getElementById('onThisChannel_prev').setAttribute('class','onThisChannel_disabled');
		document.getElementById('onThisChannel_prev').setAttribute('className','onThisChannel_disabled');
		findSWF('onThisChannel_prevSWF').disabler(0);
			
		if(newCol1) visibleCol1.innerHTML = newCol1.innerHTML;	
		if(newCol2) visibleCol2.innerHTML = newCol2.innerHTML; else visibleCol2.innerHTML = '';
		if(newCol3) visibleCol3.innerHTML = newCol3.innerHTML; else visibleCol3.innerHTML = '';
		addEventsToAllChannelArtists();
		currentCol = 1;
		totalColNo = getColNo();
		changeArtistSelection();
		if (onThisChannelIntervals && onThisChannelIntervals.length > 0) onThisChannelIntervals.splice(onThisChannelIntervals.indexOf(changeArtistInterval),1);
		clearInterval(changeArtistInterval); // restart interval
		changeArtistInterval = setInterval(function() { changeArtistSelection() },delayTime);	
		onThisChannelIntervals.push(changeArtistInterval);
	}
}
	// load init 3 columns
	var col1 = document.getElementById('featuredArtistCol1');
	var col2 = document.getElementById('featuredArtistCol2');
	var col3 = document.getElementById('featuredArtistCol3');
	var artistList1 = document.getElementById('channelArtistList0');
	if(artistList1) {
		col1.innerHTML = artistList1.innerHTML;
		col1.getElementsByTagName('a')[0].className = 'activeArtistFeature';
	}
	if(artistList1) {
		var artistList2 = document.getElementById('channelArtistList1');
		if(artistList2) col2.innerHTML = artistList2.innerHTML;
	}
	if(artistList2) {
		var artistList3 = document.getElementById('channelArtistList2');
		if(artistList3) col3.innerHTML = artistList3.innerHTML;
	}
	var totalColNo = getColNo();
	addEventsToAllChannelArtists();
	
	// load flash image loader
	var onthischannelPlayer = new SWFObject("/bandit/flash/channel/onThisChannel.swf", "onThisChannelFlashSWF", "368", "245", "9", "#ffffff");
	onthischannelPlayer.addParam("wmode","transparent");
	onthischannelPlayer.addParam("menu","false");
	onthischannelPlayer.addVariable("artistName",artistsLinks[0].firstChild.getAttribute('name'));
	onthischannelPlayer.addVariable("artistImage",artistsLinks[0].firstChild.getAttribute('rel'));
	onthischannelPlayer.addVariable("artistURL",artistsLinks[0].firstChild.getAttribute('href'));
	/* onthischannelPlayer.addVariable("barColour",globalKeyColour);onthischannelPlayer.write("onThisChannelFlash"); */
	addSingleColourAndRender("barColour", "keyColour", onthischannelPlayer, "onThisChannelFlash");
	
	if(document.getElementById('channelArtistList'+(currentColStart+2))) { // show next button if needed 
		document.getElementById('prevAndNext').style.display='';
		document.getElementById('onThisChannel_next').removeAttribute('class');
		document.getElementById('onThisChannel_next').removeAttribute('className');
	}
	
	//setTimeout(function() { changeArtistSelection(artistsLinks) },delayTime);
	var changeArtistInterval = setInterval(function() { changeArtistSelection() },delayTime);
	onThisChannelIntervals.push(changeArtistInterval);
	
	var aNextButton = document.getElementById('onThisChannel_next');
	var aPrevButton = document.getElementById('onThisChannel_prev');
	var reinit		= document.getElementById('reinitButton');
	
	// add events again
	if(aNextButton&&aPrevButton) {
		if (aNextButton.addEventListener) {
			aNextButton.addEventListener('click', changeChannelArtistsPage,true);
			aPrevButton.addEventListener('click', changeChannelArtistsPage,true);
			reinit.addEventListener('click', function() { changeChannelArtistsPage(true) },true);
		}
		else if(aNextButton.attachEvent) {
			aNextButton.attachEvent('onclick', changeChannelArtistsPage);
			aPrevButton.attachEvent('onclick', changeChannelArtistsPage);
			reinit.attachEvent('onclick', changeChannelArtistsPage);
		}
	}
	putOTCFlashButton('onThisChannel_prev');
	putOTCFlashButton('onThisChannel_next');
}

function flashPicLoaded(isLoaded) {
	currentChannelPic = isLoaded;
}

function getArtistList(colNo) {
	var colId = 'featuredArtistCol' + ((colNo*1));
		
	var artistList = document.getElementById(colId);
		
	if(artistList) {
		var artistsLinks = artistList.getElementsByTagName('li');	
		return artistsLinks;
	} else return false;
}

function putOTCFlashButton(buttonID) {
	var fplayer = new SWFObject("/bandit/flash/channel/onThisChannelButton.swf", buttonID+"SWF", "22", "22", "7", "#ffffff");
	fplayer.addVariable("id",buttonID);
	// add custom colours
	if(flashColoursSet==false) setFlashWidgetColours();
	if(!widgetColours) var widgetColours = getFlashWidgetColours();
	fplayer.addVariable("colours", widgetColours);
	fplayer.addParam("wmode","transparent");
	fplayer.write(buttonID);
}

function changePageFromFlash(dir) {
	var eventTarget = dir=='prev' ? document.getElementById('onThisChannel_prev') : document.getElementById('onThisChannel_next')
	if( document.createEventObject ) eventTarget.fireEvent('onclick');
}

function reinitOTC() {
		/* var prevAndNextDiv = document.getElementById('prevAndNext');	
		prevAndNextDiv.innerHTML = '';
		var newPrev = document.createElement('a');
		newPrev.id = 'onThisChannel_prev';
		newPrev.href = 'javascript:link();';
		
		var newNext = document.createElement('a');
		newNext.id = 'onThisChannel_next';
		newNext.href = 'javascript:link();';
		prevAndNextDiv.appendChild(newPrev);
		prevAndNextDiv.appendChild(newNext);
		//putOTCFlashButton('onThisChannel_prev');
		//putOTCFlashButton('onThisChannel_next');
	*/		
		var reinit		= document.getElementById('reinitButton');

		if(document.createEventObject) reinit.fireEvent('onclick');
		else { // Opera
				var evObj = document.createEvent('MouseEvents');
				evObj.initEvent( 'click', true, true );
				reinit.dispatchEvent(evObj); 
			}		
	}
	