/* 
----------------------------------------------------
BANDIT.FM 
http://www.bandit.fm
----------------------------------------------------
CONTENT WIDGETS
Author: Sony BMG Music Australia

----------------------------------------------------
*/

function launchImageManager(filterObjectType, filterId, imMode, imProfile, callbackFunction, defaultLibFolderType) {
	var url =  '/dashboard/imageManager/popup' + '?dbfot=' + filterObjectType + '&dbfoId=' + filterId + '&imMode=' + imMode + '&imProfile=' + imProfile + '&callbackFunction=' + callbackFunction + '&defaultLibFolderType=' + defaultLibFolderType;
	
	genericPopup(url, 730, 560, 'imageManagerWrapper', 'imageManager');
}


function showData(printTargetId) {
	printTargetId.innerHTML = document.getElementById('widgetDataCollector').innerHTML; 
	document.getElementById('widgetDataCollector').innerHTML = '';
	fadeObject(printTargetId,'in');
	evalJS(printTargetId);
}

function readytoPrint(id) {
		printTargetId = document.getElementById(id);
		targetHeight = document.getElementById('widgetDataCollector').offsetHeight;
		setTimeout(function(){ printTargetId.offsetHeight<targetHeight ? readytoPrint(id) : showData(printTargetId); }, 50);
}

function cleardata(id) {
	targetId = document.getElementById(id);
	targetId.innerHTML = dataContainer.innerHTML = '';
	}	
	
function expandForContentWithExpandingDivId(url,linkObject, expandingDivID, scrollDivID) {
	var linkIdArray = linkObject.getAttribute('id').split('_');
	var expandingDivId = expandingDivID;
	var expandingObject = document.getElementById(expandingDivId);
	
	var sectionArray = linkObject.getAttribute('id').split('Item');
	if(scrollDivID) var scrollDiv = document.getElementById('scrollingArea_'+scrollDivID);
	
	if(expandingObject) {
		if(expandingObject.innerHTML=='') {
			originalLabel[linkObject.getAttribute('id')] = linkObject.innerHTML;
			if(expandingObject.getAttribute('class')) originalClass[expandingObject.getAttribute('id')] = expandingObject.getAttribute('class');
			else originalClass[expandingObject.getAttribute('id')] = expandingObject.getAttribute('className');
			expandingObject.style.visibility='visible';
			linkObject.innerHTML = 'Hide';
			
			expandingObject.className += '_expanded';
			getDataToExpand(url,expandingDivId);
			slideit(expandingDivId,dataContainerHeight,1,500);	
			if(scrollDiv) setTimeout(function() { if(scrollDiv.scrollUpdate) scrollDiv.scrollUpdate(); },600);
		} else {
			linkObject.innerHTML = originalLabel[linkObject.getAttribute('id')];
			fadeObject(expandingObject,'out');
			slideit(expandingDivId,expandingObject.offsetHeight,0,500);
			setTimeout(function() { expandingObject.innerHTML = '';
									if(expandingObject.getAttribute('class')) expandingObject.setAttribute('class', originalClass[expandingObject.getAttribute('id')]);
									else expandingObject.setAttribute('className',originalClass[expandingObject.getAttribute('id')]);
									},500);
			if(scrollDiv) setTimeout(function() { if(scrollDiv.scrollUpdate) scrollDiv.scrollUpdate(); },600);
			}
	}
	
}

function findParentScrollerAndUpdate(element) {
	var parentElement = element.parentNode;
	if(parentElement.className.indexOf('flexcroll')>=0) setTimeout(function() { if(parentElement.scrollUpdate()) parentElement.scrollUpdate(); }, 750);
	else {
		if(parentElement.id.indexOf('bodyContent')>=0) return false;
		else findParentScrollerAndUpdate(parentElement);
	}
}

function expandForContent(url,linkObject,scrollDivID,dontChangeLabel) {
	var linkIdArray = linkObject.getAttribute('id').split('_');
	var expandingDivId = linkIdArray[0]+'_expanded';
	var expandingObject = document.getElementById(expandingDivId);
	var sectionArray = linkObject.getAttribute('id').split('Item');
	if(scrollDivID) var scrollDiv = document.getElementById('scrollingArea_'+scrollDivID);
	if (!scrollDiv) findParentScrollerAndUpdate(linkObject);
	
	if(expandingObject) {
		if(expandingObject.innerHTML=='') {
			originalLabel[linkObject.getAttribute('id')] = linkObject.innerHTML;
			if(expandingObject.getAttribute('class')) originalClass[expandingObject.getAttribute('id')] = expandingObject.getAttribute('class');
			else originalClass[expandingObject.getAttribute('id')] = expandingObject.getAttribute('className');
			expandingObject.style.visibility='visible';
			if(!dontChangeLabel) linkObject.innerHTML = 'Hide';
			
			expandingObject.className += '_expanded';
			getDataToExpand(url,expandingDivId);
			slideit(expandingDivId,dataContainerHeight,1,500);	
			if(scrollDiv) setTimeout(function() { if(scrollDiv.scrollUpdate) scrollDiv.scrollUpdate(); },600);
		} else {
			linkObject.innerHTML = originalLabel[linkObject.getAttribute('id')];
			fadeObject(expandingObject,'out');
			slideit(expandingDivId,expandingObject.offsetHeight,0,500);
			setTimeout(function() { expandingObject.innerHTML = '';
									if(expandingObject.getAttribute('class')) expandingObject.setAttribute('class', originalClass[expandingObject.getAttribute('id')]);
									else expandingObject.setAttribute('className',originalClass[expandingObject.getAttribute('id')]);
									},500);
			if(scrollDiv) setTimeout(function() { if(scrollDiv.scrollUpdate) scrollDiv.scrollUpdate(); },600);
			}
	}
	
}

function exeAfterResponse(functionName,listener) {
	var widgetResponseDiv = document.getElementById(listener);
	if (widgetResponseDiv.addEventListener) { // Firefox & Safari
		widgetResponseDiv.addEventListener('datafound', function() { 
			eval(functionName);
			widgetResponseDiv.removeEventListener('datafound',arguments.callee,true);
		} ,true);
	}
	else if (widgetResponseDiv.attachEvent) {	 // IE
		widgetResponseDiv.attachEvent('onclick', function() { 
		   eval(functionName);
		   widgetResponseDiv.detachEvent('onclick',arguments.callee,false);
		} );
	} 
}

/* NEWS WIDGET */
function fullArticle() {
}

function toggleLayer(on, id,scrollDivID) {
	on==true ? document.getElementById(id).style.display = '' : document.getElementById(id).style.display = 'none';
	if(scrollDivID) {
		var scrollDiv 	 = document.getElementById(scrollDivID);
		setTimeout(function() { if(scrollDiv.scrollUpdate) scrollDiv.scrollUpdate(); },100);
	}
}

function scrollToArticleComments(listener) {
	var widgetResponseDiv = document.getElementById(listener);
	var scrollingDiv = document.getElementById('scrollingArea_newsBlogsGigs');	
	
		if (widgetResponseDiv.addEventListener) { // Firefox & Safari
			widgetResponseDiv.addEventListener('datafound', function() { 
				var scrollToPostObject = document.getElementById('addNewsComment');
				if(scrollingDiv.scrollToElement) scrollingDiv.scrollToElement(scrollToPostObject);
				widgetResponseDiv.removeEventListener('datafound',arguments.callee,true);
			} ,true);
		}
		else if (widgetResponseDiv.attachEvent) {	 // IE
			widgetResponseDiv.attachEvent('onclick', function() { 
			  var scrollToPostObject = document.getElementById('addNewsComment');
			  if(scrollingDiv.scrollToElement) scrollingDiv.scrollToElement(scrollToPostObject);
			  //widgetResponseDiv.detachEvent('onclick',arguments.callee,false);
			} );
		} 
}

function scrollToTop(scrollingDivID) {
	/*  var scrollingDiv = document.getElementById(scrollingDivID);	
	 if (scrollingDiv.addEventListener) { // Firefox & Safari
			scrollingDiv.addEventListener('datafound', function() { 
				 if(scrollingDiv.contentScroll) scrollingDiv.contentScroll(0,0,false);	
				 scrollingDiv.removeEventListener('datafound',arguments.callee,true);
			} ,true);
		}
		else if (scrollingDiv.attachEvent) {	 // IE
			scrollingDiv.attachEvent('onclick', function() { 
			   //if(scrollingDiv.contentScroll) scrollingDiv.contentScroll(0,0,false);	
			   scrollingDiv.detachEvent('onclick',arguments.callee,false);
			} );
		} 
		*/
}

/* !!!! NOT SURE IF I'LL NEED THESE OR NOT....MIGHT BE ABLE TO RE-USE LOGIN BOX FUNCTIONS? */
function emailArticle() {	
	var objBody = document.getElementById('scrollingArea_newsBlogsGigs').parentNode.parentNode;
	createEmailArticleDivs(objBody);
	if(document.getElementById('widgetBgFade')) {
		var widgetBgFadeDiv = document.getElementById('widgetBgFade');
		widgetBgFadeDiv.style.opacity = '0';
		widgetBgFadeDiv.style.filter = 'alpha(opacity=0)';
		widgetBgFadeDiv.style.display = 'block';
		fadeObject(widgetBgFadeDiv,'in',10,80);
	}
		//document.getElementById('widgetBgFade').style.display = 'block';
}

function cancelEmailArticle() {
	var objBody = document.getElementById('scrollingArea_newsBlogsGigs').parentNode.parentNode;
	objBody.removeChild(document.getElementById('emailDiv'));
	var widgetBgFadeDiv = document.getElementById('widgetBgFade');
	if(widgetBgFadeDiv) {
		fadeObject(widgetBgFadeDiv,'out');
		setTimeout(function() { widgetBgFadeDiv.style.opacity = '0.75'; widgetBgFadeDiv.style.filter = 'alpha(opacity=75)'; widgetBgFadeDiv.style.display = 'none'; },500);
	}
}

function alterGigStyles(detailsLink) {
		var selectedGigRow = detailsLink.parentNode.parentNode;
		var linkIdArray = detailsLink.getAttribute('id').split('_');
		var expandingDivId = linkIdArray[0]+'_expanded';
		var expandingDivParent = document.getElementById(expandingDivId).parentNode;
		if(selectedGigRow.getAttribute('class')) {
			var currentRowClass = selectedGigRow.getAttribute('class');
			var currentParentClass = expandingDivParent.getAttribute('class');
			if(!selectedGigRow.getAttribute('class').match('_selected')){ // change classes to highlighted
				selectedGigRow.setAttribute('class', currentRowClass + ' gigItem_selected');
				expandingDivParent.setAttribute('class',currentParentClass+'_expanded');
			} else { // revert to initial class
				selectedGigRow.setAttribute('class', currentRowClass.replace('_selected',''));
				expandingDivParent.setAttribute('class',currentParentClass.replace('_expanded',''));
			}
		}
		else {
			var currentRowClass = selectedGigRow.getAttribute('className');
			var currentParentClass = expandingDivParent.getAttribute('className');
			if(!selectedGigRow.getAttribute('className').match('_selected')){ // change classes to highlighted
				selectedGigRow.setAttribute('className', currentRowClass + ' gigItem_selected');
				expandingDivParent.setAttribute('className',currentParentClass+'_expanded');
			} else { // revert to initial class
				selectedGigRow.setAttribute('className', currentRowClass.replace('_selected',''));
				expandingDivParent.setAttribute('className',currentParentClass.replace('_expanded',''));
			}
		}
}


function createEmailArticleDivs(objBody) {
	if (document.getElementById) {
		 var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}
	if (x) {
		 x.onreadystatechange = function() {
			if (x.readyState == 4 && x.status == 200) {
			  objEmaildiv.innerHTML = x.responseText;
			  objBody.appendChild(objEmaildiv);
			  //document.getElementById('loginEmailInput').focus();
			}
		 }
		 
		var objEmaildiv = document.createElement("div");
		objEmaildiv.setAttribute('id','emailDiv');
		 
		 x.open("GET", '/bandit/contentWidget_email.html', true);
		 x.send(null);
	  }	
}



/* BLOG PIC ENLARGER */
var lastBlogDiv;
function prepareEnlarger(element,scrollToID,widgetName,ifLast) {
	hs.outlineType = null;
	if(document.getElementById('widgetBgFade')) {
		document.getElementById('widgetBgFade').style.display = 'block';
		document.getElementById('widgetBgFade').style.visibility = 'visible';
	}
	if(widgetName=='blog') {
		/*
		var scrollingDiv = document.getElementById('scrollingArea_blogWidget');
		var scrollToPostObject = document.getElementById(scrollToID);
		if(scrollingDiv.scrollToElement) scrollingDiv.scrollToElement(scrollToPostObject);
		if(ifLast) {
			element.getElementsByTagName('div')[0].style.display = 'none';
			lastBlogDiv = element;
		}
		hs.expand(element, { targetY: 'contentWidget_'+widgetName+' 0px' });
		*/
		hs.expand(element, { anchor: 'top right' });	} else if(widgetName=='news') {
			/* var scrollingDiv = document.getElementById('scrollingArea_newsBlogsGigs');
			var scrollToPostObject = document.getElementById(scrollToID);
			if(scrollingDiv.scrollToElement) scrollingDiv.scrollToElement(scrollToPostObject);
			*/
			hs.expand(element, { anchor: 'top left' });
		}
	return false;
}

var originalLabel = new Array;
var originalClass = new Array;

/* COMMENTS WIDGET */
function postComment() {
	var widgetBg = document.getElementById('widgetBgFade_comments');
	if(widgetBg) {
		widgetBg.style.display = 'block';
	 	fadeObject(widgetBg,'in',20,80);
	}
	
}

function updateHiddenField(text, safariHiddenID) {
	if(safariHiddenID) document.getElementById(safariHiddenID).value=text;
}

function initScrollerNow(scrollerID) {
	CSBfleXcroll(scrollerID);
}

function initInnerScroller(listener,scrollerID,withTimeout) {
	var widgetResponseDiv = document.getElementById(listener);
	if (widgetResponseDiv.addEventListener) { // Firefox & Safari
		widgetResponseDiv.addEventListener('datafound', function() { 
			CSBfleXcroll(scrollerID);
			widgetResponseDiv.removeEventListener('datafound',arguments.callee,true);
		} ,true);
	}
	else if (widgetResponseDiv.attachEvent) {	 // IE
		   widgetResponseDiv.attachEvent('onclick', function(e) {
			var scrollInit = false;												 
		   var doUntilInit = setInterval(function() { 
												  if(document.getElementById(scrollerID)) {
												  	CSBfleXcroll(scrollerID); 
													scrollInit = true;
												  }
												  if (scrollInit==true) clearInterval(doUntilInit); 
												  },10);
		   widgetResponseDiv.detachEvent('onclick',arguments.callee,false);
		} );
 	} 
	//var functionName = "CSBfleXcroll('"+scrollerID+"');";
	//exeAfterResponse(functionName,listener);
	
}

function initSelectboxes(listener) {
	var widgetResponseDiv = document.getElementById(listener);
		if (widgetResponseDiv.addEventListener) { // Firefox & Safari
			widgetResponseDiv.addEventListener('datafound', function() { 													 
				initIESelects();
				widgetResponseDiv.removeEventListener('datafound',arguments.callee,true);
			} ,true);
		}
		else if (widgetResponseDiv.attachEvent) {	 // IE
			widgetResponseDiv.attachEvent('onclick', function() { 
			   initIESelects();
			  widgetResponseDiv.detachEvent('onclick',arguments.callee,false);
			} );
		} 
}

function initBanditRecommendsAfterAjax(listener) {
	var widgetResponseDiv = document.getElementById(listener);
	if (widgetResponseDiv.addEventListener) { // Firefox & Safari
		widgetResponseDiv.addEventListener('datafound', function() { 
			initBanditRecommends();
			widgetResponseDiv.removeEventListener('datafound',arguments.callee,true);
		} ,true);
	}
	else if (widgetResponseDiv.attachEvent) {	 // IE
		widgetResponseDiv.attachEvent('onclick', function() { 
			initBanditRecommends();
			widgetResponseDiv.detachEvent('onclick',arguments.callee,false);
		} );
	} 
}

function initBanditRecommends() {
	var refreshVal = Math.floor(Math.random()*1000);
	var widgetResponseDiv = document.getElementById('widgetResponse_banditRecommends_page1');
	if(widgetResponseDiv) {
		if (widgetResponseDiv.addEventListener) { // Firefox & Safari
			widgetResponseDiv.addEventListener('datafound', function() { 
				new initHozScroller('banditRecommends');
				widgetResponseDiv.removeEventListener('datafound',arguments.callee,true);
			} ,true);
		}
		else if (widgetResponseDiv.attachEvent) {	 // IE
			widgetResponseDiv.attachEvent('onclick', function() { 
			   new initHozScroller('banditRecommends');
			   widgetResponseDiv.detachEvent('onclick',arguments.callee,false);
			} );
		} 
		getWidget(sliderBaseURL['banditRecommends']+'&rfsh='+refreshVal,'widgetResponse_banditRecommends_page1');
	}
}


/* WIN */
function winFlashLoader() {	
	var compHeadline 	= document.getElementById('winFlashText').innerHTML;
	var compImg 		= document.getElementById('winFlashImg').innerHTML;
	var onthischannelPlayer = new SWFObject("/bandit/flash/artist/win.swf", "winFlashSWF", "368", "245", "7", "#ffffff");
	onthischannelPlayer.addParam("wmode","transparent");
	onthischannelPlayer.addVariable("compHeadline", compHeadline);
	onthischannelPlayer.addVariable("artistImage",compImg);
	onthischannelPlayer.addVariable("barColour",globalKeyColour);
	onthischannelPlayer.write('winFlash');   
}

function initWinFlash(listener) {	
	var widgetResponseDiv = document.getElementById(listener);
		if (widgetResponseDiv.addEventListener) { // Firefox & Safari
			widgetResponseDiv.addEventListener('datafound', function() { 
				winFlashLoader();
				widgetResponseDiv.removeEventListener('datafound',arguments.callee,true);
			} ,true);
		}
		else if (widgetResponseDiv.attachEvent) {	 // IE
			widgetResponseDiv.attachEvent('onclick', function() { 
			   winFlashLoader();
			   widgetResponseDiv.detachEvent('onclick',arguments.callee,false);
			} );
		} 
	
}

/* BILLBOARD */
function defaultBillboard(side) {
	if(side=='L') {
		var billboardPlayer = new SWFObject("/bandit/flash/common/billboardDefaultL.swf", "headerFlashSWF", "368", "440", "7", "#ffffff");
		
	} else var billboardPlayer = new SWFObject("/bandit/flash/common/billboardDefault.swf", "headerFlashSWF", "480", "440", "7", "#ffffff");
	billboardPlayer.addParam("wmode","transparent");
	if(document.getElementById('billboardDefault')) addColoursAndRender(billboardPlayer,"billboardDefault"); 
	
}

function defaultBillboardGeneric(listener) {
	//document.getElementById('idWidgetContainer_newsBlogsGigs').className = 'contentPageBg';
	var billboardPlayer = new SWFObject("/bandit/flash/common/billboardGenericPage.swf", "headerFlashSWF", "488", "440", "7", "#ffffff");
	billboardPlayer.addParam("wmode","transparent");
	billboardPlayer.addVariable("searchURL",escape("/search/home?searchTerms="+escapedArtistName+"&searchType=1&submitState=11&findSimilar=true"));
	if(document.getElementById('billboardDefault')) addColoursAndRender(billboardPlayer,"billboardDefault"); 
}

function noSpecialChars(artistName) {
	var newArtistName = '';
	for(x=0;x<artistName.length;x++) {
		switch(artistName.charCodeAt(x)) {
			case 230: newArtistName += 'ae'; break;  // æ
			case 233:
			case 232: newArtistName += 'e'; break;  // é,è
			case 243: newArtistName += 'o'; break;  // ó
			case 248: newArtistName += 'o'; break;  // ø
			case 246: newArtistName += 'o'; break;  // ö
			case 252: newArtistName += 'u'; break;  // ü
			case  36: newArtistName += 's'; break;  // $
			default: newArtistName += artistName.charAt(x); break;
		}
	}
	return newArtistName;
}

var currentChannelPic = false;
var noHoverAction = true;
var currentColStart;

var currentChannelPic = false;

var noHoverAction = true;

var currentColStart;

/* GENERIC POPUP */

function genericPopup(url, width, height, wrapperId, popupId, wrapperClass, popupClass, optionalCallbackFunction) {	
	var objBody = document.getElementsByTagName("body").item(0);
	var objHtml = document.getElementsByTagName("html").item(0);
	genericCreatePopupDivs(objBody,url,width,height,wrapperId,popupId, wrapperClass, popupClass ,optionalCallbackFunction);
	fadeGenericPopupWindow('in',wrapperId, 0);
}



function genericCreatePopupDivs(objBody,url,width,height,wrapperId,popupId, wrapperClass, popupClass, optionalCallbackFunction) {
	if (document.getElementById) {
		var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}
	if (x) {
		 x.onreadystatechange = function() {
			if (x.readyState == 4 && x.status == 200) {
			  var response = x.responseText;
			  
			  if(response.trim() == 'loginRequired')
			  {
			  	  fadeGenericPopupWindow('out',wrapperId,0);	
			  	  login();
			  }
			  else
			  {
			  		objLogindiv.innerHTML = response;
			  		objBody.appendChild(objLogindiv); 
					if (optionalCallbackFunction) eval(optionalCallbackFunction); 

					  var shadowAdjustment = 15;
					  var browserVersion = navigator.userAgent;
					  if(browserVersion.indexOf('MSIE')>=0) { shadowAdjustment = 15; }
					 if(browserVersion.indexOf('Firefox')<0) evalJS(objLogindiv); 
					  if(document.getElementById('popupShadow')) document.getElementById('popupShadow').style.width = divWidth + 'px';
					  if(document.getElementById('popupLeftShadow')) document.getElementById('popupLeftShadow').style.height = divHeight-shadowAdjustment + 'px';
					  if(document.getElementById('popupRightShadow')) document.getElementById('popupRightShadow').style.height = divHeight-shadowAdjustment + 'px';
					  //scroll(0,0);
					}
				}
		 }

		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id', wrapperId);
		wrapperClass ? objOverlay.className =  wrapperClass : objOverlay.className =  'genericPopupWrapper';
		var windowSizeY = document.body.clientHeight;
		objOverlay.style.height = windowSizeY + 'px';
		objOverlay.style.display = 'block';

		objBody.appendChild(objOverlay);		
		var objLogindiv = document.createElement("div");
		objLogindiv.setAttribute('id',popupId);
		popupClass ? objLogindiv.className =  popupClass : objLogindiv.className =  'genericPopupClass';
		var divWidth  = width;
		var divHeight = height;
		var windowSizeX = document.documentElement.clientWidth;
		var windowSizeY = document.documentElement.clientHeight;
		var currentScrollPos = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
		objLogindiv.style.top = (currentScrollPos+40) + 'px';
		objLogindiv.style.left = (windowSizeX/2)-(divWidth/2)+'px'; 
		x.open("GET", url, true);
		x.send(null);
	  }	
}

function fadeGenericPopupWindow(fadeType,wrapperId,fader) {
	fadeWindow(wrapperId,fadeType,fader,20);
}

function closeGenericPopupWindow(wrapperId,popupId) {
	var objBody = document.getElementsByTagName("body").item(0);
	var objPopup = document.getElementById(wrapperId);
	var popup = document.getElementById(popupId);
	if(popup) {
		popup.innerHTML = '';
		objBody.removeChild(popup);
	}
	else alert('JavaScript Error: Specified popupID in closeGenericPopupWindow does not exist');
	fadeGenericPopupWindow('out',wrapperId,80);
}


function genericFlashPopup(url, width, height, wrapperId, popupId, wrapperClass, popupClass, optionalCallbackFunction) {
	var objBody = document.getElementsByTagName("body").item(0);
	var objHtml = document.getElementsByTagName("html").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id', wrapperId);
	wrapperClass ? objOverlay.className =  wrapperClass : objOverlay.className =  'genericPopupWrapper';
	var windowSizeY = document.body.clientHeight;
	objOverlay.style.height = windowSizeY + 'px';
	objOverlay.style.display = 'block';
	
	objBody.appendChild(objOverlay);		
	var objLogindiv = document.createElement("div");
	objLogindiv.setAttribute('id',popupId);
	popupClass ? objLogindiv.className =  popupClass : objLogindiv.className =  'genericPopupClass';
	var divWidth  = width;
	var divHeight = height;
	var windowSizeX = document.documentElement.clientWidth;
	var windowSizeY = document.documentElement.clientHeight;
	var currentScrollPos = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
	objLogindiv.style.top = (currentScrollPos+40) + 'px';
	objLogindiv.style.left = (windowSizeX/2)-(divWidth/2)+'px'; 
	objBody.appendChild(objLogindiv); 
	if(optionalCallbackFunction) eval(optionalCallbackFunction);
	
	var fplayer = new SWFObject(url, url+'SWF', width, height, "7", "#ffffff");
	fplayer.addParam("wmode","transparent");
	fplayer.addVariable("configURL","/srv/playlistConfig.do");
	addColoursAndRender(fplayer,popupId);
}

function closeGenericFlashPopup(wrapperId, popupId) {
	var objBody = document.getElementsByTagName("body").item(0);
	var objPopup = document.getElementById(wrapperId);
	var popup = document.getElementById(popupId);
	if(popup) {
		popup.innerHTML = '';
		objBody.removeChild(popup);
	}
	else alert('JavaScript Error: Specified popupID in closeGenericPopupWindow does not exist');
	fadeGenericPopupWindow('out',wrapperId,80);
}

/* CONNECTIONS WIDGET */
function toggleConnnectionAZ(ifLeft) {
	if(ifLeft=='L') var azList = document.getElementById('connectionsSortAZ_L');
	else var azList = document.getElementById('connectionsSortAZ');
	if(azList.style.visibility=='visible') fadeObject(azList,'out');
	else { azList.style.visibility='visible'; fadeObject(azList,'in'); }
}


function toggleConnectionSort(button) {
	// revert tab that was previously active
	var linkArray = document.getElementById('connectionsSort').getElementsByTagName('a');
	for(var x=0;x<linkArray.length;x++) {
		if (linkArray[x].className=='selected') linkArray[x].className=''; 
	}
	button.className='selected';
	var azList = document.getElementById('connectionsSortAZ');
	if ((azList != null) && (azList.style.visibility=='visible')) fadeObject(azList,'out');
}

/* MODALS */
function killModal() {
	var objBody = document.getElementsByTagName("body").item(0);
	if (document.getElementById('imModalWrapper'))
		objBody.removeChild(document.getElementById('imModalWrapper'));
	if (document.getElementById('imModal'))
		objBody.removeChild(document.getElementById('imModal'));
}

function popModal(modalTxt,width,height,modalTitle,buttons,confirmLogic) {
	var objBody = document.getElementsByTagName("body").item(0);

	var objOverlay = document.createElement("div");
	objOverlay.id = 'imModalWrapper';
	objOverlay.className =  'modalWrapperClass';
	var windowSizeY = document.body.clientHeight;
	objOverlay.style.height = windowSizeY + 'px';
	objOverlay.style.display = 'block';
	objBody.appendChild(objOverlay);
	
	var objModal = document.createElement("div");
	objModal.id = 'imModal';
	objModal.className =  'modalClass';
	objModal.style.width = width;
	
	var titleBar = document.createElement("div");
	titleBar.id = 'imModalTitleBar';
	titleBar.className = 'modalTitleBar keyColour';
	titleBar.innerHTML = modalTitle;
	objModal.appendChild(titleBar);
	
	var modalBody = document.createElement("div");
	modalBody.id = 'modalBody';
	var modalMsg = document.createElement("div");
	modalMsg.id = 'modalMsg';
	modalMsg.innerHTML = modalTxt;
	modalBody.appendChild(modalMsg);
	
	var buttonsWrapper = document.createElement("div");
	
	if(buttons=='yesNo') {
		buttonsWrapper.className = 'modalYesNoButtons';
		var yesBtn = document.createElement("a");
		yesBtn.className = 'modalOptionButton keyColourBg';
		yesBtn.id = 'yesBtn';
		yesBtn.innerHTML = 'Yes';
		yesBtn.href = 'javascript:link();'
		yesBtn.setAttribute('onclick','eval('+confirmLogic+')');
		var noBtn = document.createElement("a");
		noBtn.className = 'modalOptionButton keyColourBg';
		noBtn.id = 'noBtn';
		noBtn.innerHTML = 'No';
		noBtn.href = 'javascript:link();'
		noBtn.setAttribute('onclick','killModal();');
		buttonsWrapper.appendChild(yesBtn);
		buttonsWrapper.appendChild(noBtn);
	 
	} else {
		buttonsWrapper.className = 'modalGenericButton';
		var genericBtn = document.createElement("a");
		genericBtn.className = 'modalOptionButton keyColourBg';
		genericBtn.innerHTML = 'OK';
		genericBtn.href = 'javascript:link();'
		genericBtn.setAttribute('onclick','killModal();');
		buttonsWrapper.appendChild(genericBtn);
	}
	
	buttonsWrapper.innerHTML += "<div class='cssClear'></div>";
	modalBody.appendChild(buttonsWrapper);
	objModal.appendChild(modalBody);
	
	var divWidth  = width;
	var divHeight = height;
	var windowSizeX = document.documentElement.clientWidth;
	var windowSizeY = document.documentElement.clientHeight;
	var currentScrollPos = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
	objModal.style.top = (currentScrollPos+180) + 'px';
	objModal.style.left = (windowSizeX/2)-(divWidth/2)+'px';
	objBody.appendChild(objModal);	
}

var banditRecData = null;
function setBanditRecommends(data)
{
	banditRecData = data;
}

function getBanditRecommends()
{
	return banditRecData;
}

function preparePreview() {
	var cover = document.getElementById('noClick');
	var windowSizeY = document.body.clientHeight;
	var windowSizeX = document.body.clientWidth;
	if(cover) {
		cover.style.height = windowSizeY + 'px';
		cover.style.width  = windowSizeX + 'px';
	}
}


function getNewsDeepLink(qs)
{
	s = window.location + '';
	pos = s.indexOf("?");
	if (pos >= 0)
		s = s.substring(0,pos);
	s = s + "?" + escape(qs) + "#news";

	var block = document.getElementById("newsArticleGetLink");
	var extblock = document.getElementById("newsArticle");
	block.style.width = extblock.scrollWidth + "px";
	block.style.height = extblock.scrollHeight + "px";
	var height = 470;
	if (extblock.offsetHeight > height)
		height += ((extblock.offsetHeight - 470) * 2);
	var obj = new SWFObject("/bandit/flash/getlink.swf", "getLinkSWF", extblock.scrollWidth, height , "7", "#ffffff");
	obj.addParam("wmode","transparent");
	obj.addVariable("getString",s);
	addColoursAndRender(obj,'newsArticleGetLink');

	/*
	if (document.getElementById("innerNewsArticleGetLink") == null)
	{
		var innerDiv = document.createElement("div");
		innerDiv.setAttribute('id', 'innerNewsArticleGetLink');
		block.appendChild(innerDiv);
	}
	if(flashColoursSet==false) setFlashWidgetColours(); 
	var flashvars = {getString: s, "colours": widgetColours};
	var params = {wmode: "transparent"};
	swfobject.embedSWF("/bandit/flash/getlink.swf", "innerNewsArticleGetLink", ""+extblock.scrollWidth, ""+height , "9.0.0", null, flashvars, params);    
	*/
}
function removeGetURLflash()
{
	ua = navigator.userAgent;
	ua = ua.toLowerCase();
	if ((ua.indexOf("safari") >= 0) && (ua.indexOf("chrome") < 0))
		setTimeout("removeGetURLflashAsync()",100);
	else
		removeGetURLflashAsync()
}

function removeGetURLflashAsync()
{
	var block = document.getElementById("newsArticleGetLink");
	//block.innerHTML = "";
	block.removeChild(block.childNodes[0]);
	//swfobject.removeSWF("innerNewsArticleGetLink");    
	block.style.width = 0;
	block.style.height = 0;
}




/**
	Update the connection status displayed to the user
**/
function updateConnectionStatusText(newStatusText)
{
	if (document.getElementById('connectStatus'))
	{
		document.getElementById('connectStatus').innerHTML = newStatusText;
	}
}

/**
	Update the next command to be sent to the connectToPage() function from the connection form.
	This is used after a successful connection/disconnection request
**/
function updateNextConnectionCommand(newConnectionCommand)
{
	if (document.getElementById('connectionStateId'))
	{
		document.getElementById('connectionStateId').value = newConnectionCommand;
	}
}

/**
	Process a connection request using values in the connection form.
**/
function connectToPage() {

	var connectToPageForm = document.getElementById('connectToPageFormId');

	postAjaxForm('/misc/connectPage', connectToPageForm, 'handleConnectThisPageResponse')
}

function handleConnectThisPageResponse(ajaxResponse)
{
	if (ajaxResponse)
	{
		if (ajaxResponse.responseCode == 'ConnectedSuccessfully')
		{
			flushConnectionWidgetClientCache();
			findSWF('connectButtonSWF').connect();
			updateConnectionStatusText('Connected');
			updateNextConnectionCommand('DisconnectPage');
		}
		else if (ajaxResponse.responseCode == 'ConnectedSuccessfullyButPendingConfirmation')
		{
			findSWF('connectButtonSWF').connect();
			updateConnectionStatusText('Connected (Awaiting Approval)');
			updateNextConnectionCommand('DisconnectPage');
		}
		else if (ajaxResponse.responseCode == 'DisconnectedSuccessfully')
		{
			flushConnectionWidgetClientCache();
			findSWF('connectButtonSWF').disconnect();
			updateConnectionStatusText('Not Connected');
			updateNextConnectionCommand('ConnectPage');
		}
		else if (ajaxResponse.responseCode == 'ConnectionUnsuccessful')
		{
			alert('connection unsuccessful');x
			updateConnectionStatusText('Error');
		}
		else if (ajaxResponse.responseCode == 'NotLoggedIn')
		{
			login();
		}
		else
		{
			alert('Error State. Statuses = ' + ajaxResponse.responseCode )
			updateConnectionStatusText('Error');
		}
	}
}

/**
	Flush the cache for the appropriate widget i.e. Artist/Channel
**/
function flushConnectionWidgetClientCache()
{
	if (document.getElementById('connectToPageFormId'))
	{
		if (document.getElementById('connectToPageFormId').fArtistId)
		{
			evictWidget(38); // Flush the artists widget (since there was an artist id provided)
		}
		else
		{
			evictWidget(37); // Flush the channels widget
		}
	}
}

/*
// May no longer be needed. Could not find a ref in JSP or JS files. LT 16/12/2009
function selectAll(connections) {
	var checkboxes = document.getElementById(connections).getElementsByTagName('input');
	var checked = 0;
	for(x=0;x<checkboxes.length;x++) if(checkboxes[x].checked) checked++;
	
	if(checked<checkboxes.length) for(x=0;x<checkboxes.length;x++) checkboxes[x].checked=true;
	else for(x=0;x<checkboxes.length;x++) checkboxes[x].checked=false;
}*/

/**
	About Page Support Widget. Append territory ID to support text
**/
function addTerritoryIdToSupportForm()
{
	var msgElement = document.getElementById('supportMsg');
	if (msgElement && msgElement.value.trim.length > 0)
	{
		// If we haven't already added the territory ID
		if (msgElement.value.indexOf('[tid=') < 0)
		{
			msgElement.value += '[tid=<bean:write name="banditSessionBag" property="viewingTerritory.territoryId"/>]'
		}
	}
}

/**
	Navigate to a url and change the browsing context
**/
function updateBrowsingContextAndNavigate(objectId, objectType, destination) 
{
	var objBody = document.getElementsByTagName("body").item(0);
	var existingBrowsingContextWrapper = document.getElementById('browsingContextSubmissionFormWrapper');
	if (existingBrowsingContextWrapper)
		objBody.removeChild(existingBrowsingContextWrapper);
	
	// Add wrapper to body
	var bcWrapper = document.createElement("div");
	bcWrapper.id = 'browsingContextSubmissionFormWrapper';
	bcWrapper.style.display = 'none';
	objBody.appendChild(bcWrapper);
	
	// Add form element to wrapper
	var bcForm = document.createElement("form");
	bcForm.action = '/srv/dashboard/changeCtx.do';
	bcForm.name = 'updateBrowsingContextForm';
	bcWrapper.appendChild(bcForm);
	
	// Add objectId field to form
	var objectIdHidden = document.createElement("input");
	objectIdHidden.value = objectId;
	objectIdHidden.name = 'dbfoId';
	bcForm.appendChild(objectIdHidden);

	// Add objectType field to form
	var objectTypeHidden = document.createElement("input");
	objectTypeHidden.value = objectType;
	objectTypeHidden.name = 'dbfot';
	bcForm.appendChild(objectTypeHidden);
	
	// Add destination field to form
	var destinationHidden = document.createElement("input");
	destinationHidden.value = destination;
	destinationHidden.name = 'cbcdo';
	bcForm.appendChild(destinationHidden);
	
	// Submit form
	bcForm.submit();
}