var active = null;
var browser=navigator.appName;
var currentTime;

var agent = navigator.userAgent.toLowerCase(); 
var isMac = (agent.indexOf("mac") != -1) ? true : false;
var isMSIE = (navigator.appVersion.indexOf("MSIE")!=-1) ? true : false;

toCamelCase=function (s) {
	for (var exp = /-([a-z])/; exp.test(s); s=s.replace(exp, RegExp.$1.toUpperCase())) {
	}
	return s;
}

function submitEmail()
{

	var thisEmail = document.getElementById('email').value;

	window.location.href = "join.php?email="+thisEmail;

}

function submitMobile()
{

	var thisEmail = document.getElementById('email').value;
	
	var thisMobile = document.getElementById('phone_mobile').value;
	
	window.location.href = "join.php?email="+thisEmail+"&mobileNumber="+thisMobile;

}

var divBioHeight;
var initialBioHeight;
var bioExpanded = "false";

var divVidHeight;
var initialVidHeight;
var vidsExpanded = "false";

function toggleBio(obj) {
	var el = document.getElementById(obj);
	if (bioExpanded == "true") {
		el.style.height = initialBioHeight + "px";
		document.getElementById('toggleLink').innerHTML = "More...";
		bioExpanded = "false";
	}
	else {
		el.style.height = divBioHeight + "px";
		document.getElementById('toggleLink').innerHTML = "Less...";
		bioExpanded = "true";
	}
}

function toggleVideos(obj) {
	var el = document.getElementById(obj);
	if (vidsExpanded == "true") {
		el.style.height = initialVidHeight + "px";
		document.getElementById('toggleVideoLink').innerHTML = "More...";
		vidsExpanded = "false";
	}
	else {
		el.style.height = divVidHeight + "px";
		document.getElementById('toggleVideoLink').innerHTML = "Less...";
		vidsExpanded = "true";
	}
}
				
function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
	thefield.value = ""
} 

function GetXMLHttpObject()
{
      var oXMLHttp = null;

      try
      {
            oXMLHttp = new ActiveXObject("MSXML2.XMLHTTP");
      }
      catch (E)
      {
            try
            {
                  oXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (E)
            {
              oXMLHttp = null;
            }
    }

      if ((oXMLHttp == null) && (typeof(XMLHttpRequest) != 'undefined'))
      {
            oXMLHttp = new XMLHttpRequest();
      }

      return oXMLHttp;
}

function loadArtistVideo(videoId, videoTitle)
{


	var responseText = "<table width=\"500\" height=\"411\" cellspacing=\"0\" cellpadding=\"10\">";
	responseText += "<tr valign=\"top\">";
	responseText += "<td style=\"background-image: url('images/video_bg_1x409.jpg'); background-repeat: repeat-x; border-width: 1px; border-color: #d1d1d1; border-style:solid;\">";
	responseText += "<embed src=\"http://services.brightcove.com/services/viewer/federated_f8/933423656\" bgcolor=\"#FFFFFF\" flashVars=\"playerId=933423656&viewerSecureGatewayURL=https://console.brightcove.com/services/amfgateway&servicesURL=http://services.brightcove.com/services&cdnURL=http://admin.brightcove.com&domain=embed&autoStart=true&videoId="+videoId+"\" base=\"http://admin.brightcove.com\" name=\"flashObj\" width=\"480\" height=\"395\" seamlesstabbing=\"false\" type=\"application/x-shockwave-flash\" swLiveConnect=\"true\" pluginspage=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\"></embed>";
	responseText += "<br /><br />";
	responseText += "<span id=\"videoTitle\">"+videoTitle+"</span>";
	responseText += "</td>";
	responseText += "</tr>";
	responseText += "</table>";
	document.getElementById('videoPlayer').innerHTML = responseText;
}

function sortVideos(filter, query)
{

	var oXMLHttp = GetXMLHttpObject();

	if (query == null)
	{
	//	query = document.getElementById('searchQuery').value;
	}

	
	  var url="php/allVideos.php";
	  switch(browser)
	  {
	  	case "Netscape":
	  		break;
	  		
	  	default:
	  		query = Utf8.encode(query);
	  		break;	  	
	  }
	  url=url+"?query="+query;
	  url=url+"&filter="+filter;
	  url=url+"&browser="+browser;

	switch(filter)
	{
		
		case "all" : 
		//	document.getElementById('searchQuery').value = "";
		//	document.getElementById('videoHeader').innerHTML = "<img src=\"images/videos_allVideos.gif\" width=\"150\" height=\"14\" />";
			var responseText = "<table width=\"901\" cellspacing=\"10\" cellpadding=\"0\">\n";
			responseText += "\t<tr valign=\"top\" colspan=\"3\">\n";
			responseText += "\t<td>\n";
			responseText += "\t\t<div id=\"videoSortNoLink\">Generating Master List...</div>\n";
			responseText += "\t</td>\n";
			responseText += "\t</tr>\n";
			responseText += "\t</table>\n";
		//	document.getElementById('videoTable').innerHTML = responseText;
			break;
		
		case "bytitle" :
		case "listSearch" :
		//	document.getElementById('videoHeader').innerHTML = "<img src=\"images/videos_allVideos.gif\" width=\"150\" height=\"14\" />";
			var responseText = "<table width=\"901\" cellspacing=\"10\" cellpadding=\"0\">\n";
			responseText += "\t<tr valign=\"top\" colspan=\"3\">\n";
			responseText += "\t<td>\n";
			switch(browser)
			{
				case "Netscape":
					break;

				default:
					query = Utf8.decode(query);
					break;	  	
	  		}
			responseText += "\t\t<div id=\"videoSortNoLink\">Currently searching: "+ query +"</div>\n";
			responseText += "\t</td>\n";
			responseText += "\t</tr>\n";
			responseText += "\t</table>\n";
		//	document.getElementById('videoTable').innerHTML = responseText;
			break;
			
		case "alpha" :
		//	document.getElementById('videoHeader').innerHTML = "<img src=\"images/videos_allVideos.gif\" width=\"150\" height=\"14\" />";
			var responseText = "<table width=\"901\" cellspacing=\"10\" cellpadding=\"0\">\n";
			responseText += "\t<tr valign=\"top\" colspan=\"3\">\n";
			responseText += "\t<td>\n";
			responseText += "\t\t<div id=\"videoSortNoLink\">Currently searching all artist beginning with : "+ query +"</div>\n";
			responseText += "\t</td>\n";
			responseText += "\t</tr>\n";
			responseText += "\t</table>\n";
		//	document.getElementById('videoTable').innerHTML = responseText;
			break;
	}
	
	oXMLHttp.open("GET", url, true);

	oXMLHttp.onreadystatechange = function()
	    {
		  if (oXMLHttp.readyState == 4)
		  {
		//	document.getElementById('videoTable').innerHTML = oXMLHttp.responseText;
		  }
	    }

	oXMLHttp.send(null);
}



function stristr(haystack, needle) {

    var pos = 0;
 
    pos = haystack.toLowerCase().indexOf( needle.toLowerCase() );
    
    if( pos == -1 ){
        return false;
    } else{
        return true;
    }
}

//
getStyle=function (el, style) {
	if (!document.getElementById) {
		return;
	}
	var value = el.style[toCamelCase(style)];
	if (!value) {
		if (document.defaultView) {
			value = document.defaultView.getComputedStyle(el, "").getPropertyValue(style);
		} else if (el.currentStyle) {
			value = el.currentStyle[toCamelCase(style)];
		}
	}
	return value;
}

getElement=function(id){	
	if (!document.getElementById){
	 return;
	}
	//
	if (document.getElementById){
		// this is the way the standards work
		var el = document.getElementById(id);
	}else if (document.all){
		// this is the way old msie versions work
		var  el = document.all[id];
	}else if (document.layers){
		// this is the way nn4 works
		var el = document.layers[id];
	}
	return el;
}

function tableRollover(obj) {
	document.getElementById(obj).style.backgroundColor='#db0038';
}

function tableRollout(obj) {
	document.getElementById(obj).style.backgroundColor='transparent';
}

toggleDisplay=function(el){
	el.style.display = el.style.display ? "" : "block";
}

replaceAnchors=function(){
	if (document.getElementById) {
		var atags = document.getElementsByTagName("A");
		for (var i = 0; i<atags.length; i++) {
			var ca = atags[i];
			var index=ca.href.indexOf("#");
			var hash=ca.href.substring(index+1);
			if (index>-1 &&	hash.length>0) {
				ca.link=hash;
				ca.onclick = function() {	
					var b = getElement('content');
					b.scrollTop = getElement(this.link).offsetTop;
					return false;
				};
			}
		}
	}
}

function placePlayer(sDivId, previewUrl, iteration)
{
      var oXMLHttp = GetXMLHttpObject();

	  var url="php/placeRingtonePlayer.php";
	  url=url+"?previewUrl="+previewUrl;
	  url=url+"&iteration="+iteration;
	  url=url+"&browser="+browser;

      oXMLHttp.open("GET", url, true);

      oXMLHttp.onreadystatechange = function()
            {
                  if (oXMLHttp.readyState == 4)
                  {
                        document.getElementById(sDivId).innerHTML = oXMLHttp.responseText;
                  }
            }

      oXMLHttp.send(null);
}

function setPlaying(id){

	objectPlaying = getElement(id);
}

function stopAudio(id)
{

	if(objectPlaying != getElement(id))
	{
		objectPlaying.stopAudio();
	}
}

function swapTerritory(terr){

	territory = terr;
	
	if (section == "news")
	{
		switchContent('news');
	}
	
	if (section == "contest")
	{
		switchContent('contest');
	}
	
	
}

switchContent=function(id){	

	//turn off last active
	if(active){
		document.images[oldSection].src = items[oldSection].off.src;
		toggleDisplay(active);
	}
	
	switch(id)
	{
		case "news":
			
			id = "news"+territory;	
			active=getElement(id);
			toggleDisplay(active);
			section = oldSection = "news";
			document.images[section].src = items[section].on.src;
			break;

		case "contest":

			id = "contest"+territory;	
			active=getElement(id);
			toggleDisplay(active);
			section = oldSection = "contest";
			document.images[section].src = items[section].on.src;
			break;

		default:

			active=getElement(id);
			toggleDisplay(active);
			section = oldSection = id;
			document.images[section].src = items[section].on.src;
			break
	}
	
	//var img=(getElement('headerimg'))
	//img.src="images/header-"+id+".jpg"

	//trackEvent(window.location.host, "/"+id+".ajaxPage", id, '');
	
	var contentDiv = getElement('content');
	contentDiv.scrollTop = 0;
}

//
getQueryVariable = function (variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i<vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}

var Utf8 = {

	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}

function startScrollingDetector(){
	setInterval("scrollingDetector()", 250);
}

function scrollingDetector(){
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		scrollValue = document.body.scrollTop;
	} else {
		scrollValue = window.pageYOffset;
	}
	
	if(scrollValue > 500)
	{
		toggleAlphaNav('show');
	} else {
		toggleAlphaNav('hide');
	}
	
	moveDiv();
}

function toggleAlphaNav(state){
	el = document.getElementById('footerAlphaNav');
	
	if(state == "hide")
	{
	 	el.style.display = "";
	} else {
		el.style.display = "block";
	}
}

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}


function moveDiv() {

	pageSize = getPageSize();

	thisY = 0;
	var pageScroll;

	if (self.pageYOffset) {
		pageScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		pageScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		pageScroll = document.body.scrollTop;
	}

	 document.getElementById('footerAlphaNav').style.top = pageSize[3] + pageScroll - 25 +"px";

}

function launchRadio()
{
	
	var url = "http://myplay.com/audio_player/columbia/";
	
	var options = "menubar=0,toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=0";

	launch(url, "download", 850, 450, options);
}

function findObj(n, d) { 
	var p,i,x;  

	if(!d) d=document; 

	if((p=n.indexOf("?"))>0&&parent.frames.length) 
	{
		d=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}

	if(!(x=d[n])&&d.all) x=d.all[n]; 

	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];

	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);

	if(!x && d.getElementById) x=d.getElementById(n); 

	return x;
}


var oXMLHttpTwitter;

function GetTwitterXMLHttpObject()
{
     oXMLHttpTwitter = null;

      try
      {
            oXMLHttpTwitter = new ActiveXObject("MSXML2.XMLHTTP");
      }
      catch (E)
      {
            try
            {
                  oXMLHttpTwitter = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (E)
            {
              oXMLHttpTwitter = null;
            }
    }

      if ((oXMLHttpTwitter == null) && (typeof(XMLHttpRequest) != 'undefined'))
      {
            oXMLHttpTwitter = new XMLHttpRequest();
      }

      return oXMLHttpTwitter;
}


var tweetObj = new Object;

function loadTwitter()
{

	if(oXMLHttpTwitter)
	{
		oXMLHttpTwitter.abort();
	}

	oXMLHttpTwitter = GetTwitterXMLHttpObject();

	var url = "php/twitter.php";

	oXMLHttpTwitter.open("POST", url, true);

	oXMLHttpTwitter.onreadystatechange = function()
	    {
		  if (oXMLHttpTwitter.readyState == 4)
		  {
			var JSONObject = new Object;

			var response = oXMLHttpTwitter.responseText;

			JSONObject = JSON.parse(response);

			tweetObj = JSONObject.Tweets;

			loadTweet(0);
		  }
	    }

	oXMLHttpTwitter.send(null);

}

var tweetCount = 0;

function loadTweet(count)
{

	var totalTweets = tweetObj.length;

	if(count >= totalTweets)
	{
		tweetCount = 0;
	} else {
		tweetCount++;
	}

	fadeInOut('in', tweetCount);

}

var c=0;
var dir2 = "out"
var tweetCount = 0;

function fadeInOut(dir, count){

	var obj;
	var speed=3;

	var obj = document.getElementById('twitterDiv');

	var tweetText = '<span id="twitter"><a href="' + tweetObj[count].link[0] +'" target="_blank">' + tweetObj[count].title + '</a> - Posted: '+ tweetObj[count].date +'</span>';

	if(obj.filters) {
		obj.style.filter='alpha(opacity='+c+')';
	} else {
		obj.style.opacity=c/100;
	}

	if(dir=='in') {
		dir1='in'
		c++;
	} else {
		dir1='out';
		c--;
	}

	if(c>100){
		c=100;
		setTimeout('fadeInOut(dir2, tweetCount)',5000);
		return;
	}

	if(c<0){
		c=0;
		count++;
		loadTweet(count);
		return;
	}

	obj.innerHTML = tweetText;
	setTimeout('fadeInOut(dir1, tweetCount)',10)
}

function launch(url, name, width, height, options)
	{
	if (!name)
		{
		name = "window_" + Math.floor(1000 * Math.random());
		}
	
	if (!width)
		{
		width = screen.availWidth;
		}
		
	if (!height)
		{
		height = screen.availHeight;
		}
	
	if (!options)
		{
		options = "menubar=1,toolbar=1,location=1,directories=1,status=1,scrollbars=1,resizable=1";
		}
	
	width	= Math.min(width, screen.availWidth);
	height	= Math.min(height, screen.availHeight);
	var x	= Math.max(0, 0.5 * (screen.availWidth - width));
	var y	= Math.max(0, 0.5 * (screen.availHeight - height));

	var config = "";
	config += "left=" + x + ",top=" + y + ",";
	config += "screenX=" + x + ",screenY=" + y + ",";
	config += "width=" + width + ",height=" + height;
	config += "," + options;

	var win = window.open(url, name, config);
	if (win && window.focus)
		{
		win.focus();
		}
	}
	