//var agent=navigator.userAgent.toLowerCase();
//var is_iphone = (agent.indexOf('iphone') != -1);
//if ((is_iphone) && !(parent.ignore_iphone)) { document.location='/beta/iphone/'; }
function getQuerystring(key, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}
dojo.require("dojo.fx");
dojo.require("dojo.date");
dojo.require("dijit.Dialog");
dojo.require("dijit.form.TextBox");  
dojo.require("dijit.form.Button");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.layout.TabContainer");
dojo.require("dojo.cookie");
dojo.require("dojox.image.Lightbox");

  var currentAnimation;
  var currentData = null;
  var resizingTimer = null;
  function resize()
  {	  
 
		if(currentAnimation){		  
      	return;//do not interrupt a running animation
    	}
		console.log("resize");
  	  dojo.style("fadeBoxShop", "opacity", "0");
	  dojo.style("checkMeOut", "opacity", "0");
	  if (resizingTimer != null)
	  {
		clearTimeout(resizingTimer);  
	  }	
	  
  	resizingTimer = setTimeout('doResize()',500);
  }
  function doResize()
  {
	  showSong();
	  showCheckMeOut(true);  
  }
  
  function showSong()
  {

	  	resizingTimer = null;
	  	document.getElementById('animDiv').style.position = 'absolute';

		//determine mainDiv x
		var mainDivCord = dojo.coords(document.getElementById('mainDiv'));
		var x = mainDivCord.l + mainDivCord.w + 3;

		var slideDown = dojo.fx.slideTo({node: "animDiv",duration: 1000, left: x, top: 0});


	  if(currentAnimation && currentAnimation.status() != "stopped"){
		  
      	return;//do not interrupt a running animation
    	}

		currentAnimation = slideDown; 

	if (currentAnimation) {

     //Play the animation. Without this call, it will not run.
      dojo.connect(currentAnimation, "onEnd", function()
		{

			//document.getElementById('animDiv').style.position = 'fixed';
			//show check it out popup
			var x = mainDivCord.l + mainDivCord.w + 3;
			var bottom = dojo.coords(document.getElementById('animDiv')).h;
			var positionFader = dojo.fx.slideTo({node: "fadeBox",duration: 10, left: x, top: bottom});
			var fadeIn = dojo.fadeIn({node: "fadeBox",duration: 1000});
			
			document.getElementById('fadeBox').style.position = 'absolute';
			
			if (currentData.MP3URL !='')
			{
				var fader = dojo.fx.chain([positionFader, fadeIn]);
				 dojo.connect(fader, "onEnd", function(){
					//document.getElementById('fadeBox').style.position = 'fixed';
				});
				fader.play();
			}	

			currentAnimation = null;
			
	     });
	  
		  if (isCurrentSongValid())
		  {
			 currentAnimation.play();
		  }
	  
    }
  }
  function slideLeft(callsetvalue)
  { 
  	    dojo.style("fadeBox", "opacity", "0");
  //determine mainDiv x
		var mainDivCord = dojo.coords(document.getElementById('mainDiv'));
	  var slideUp = dojo.fx.slideTo({node: "animDiv",duration: 500,  left: mainDivCord.l + mainDivCord.w + 3, top: -500});
	  if(currentAnimation && currentAnimation.status() != "stopped"){
      return;//do not interrupt a running animation
    	}
		currentAnimation = slideUp; 
	if (currentAnimation) {
      //Play the animation. Without this call, it will not run.
      dojo.connect(currentAnimation, "onEnd", function(){
        currentAnimation = null;
		if(callsetvalue)
		{
			setValues();
			showSong();
		}
      });
      currentAnimation.play();
    }
  }
function unquoteXml (xmlStr) {
	xmlStr = xmlStr.replace("&amp;", "&");
	xmlStr = xmlStr.replace("&lt;", "<");
	xmlStr = xmlStr.replace("&gt;", ">");
	xmlStr = xmlStr.replace("&quot;", "\"");
	xmlStr = xmlStr.replace("&apos;", "'");
	return xmlStr;
}
  function setValues()
  {
	  	document.getElementById('animDiv').style.visibility = 'visible';
		document.getElementById('buymp3').innerHTML = '';
		document.getElementById('buycd').innerHTML = '';
		if(currentData.MedImgUrl == '')
		{
			document.getElementById('albumart').src =  '/img/nocd.jpg';
		}
		else
		{
			document.getElementById('albumartlink').href = '/buysongredirect.html?redirect=' + currentData.MP3URL;
			document.getElementById('albumart').src = currentData.MedImgUrl;  
		}
		document.getElementById('artist').innerHTML = unquoteXml(currentData.artist);
		document.getElementById('album').innerHTML = unquoteXml(currentData.album);
		document.getElementById('song').innerHTML = unquoteXml(currentData.song);
		if(currentData.MP3URL != '')
		{
			document.getElementById('buymp3').innerHTML = '<a href="' + '/buysongredirect.html?redirect=' + currentData.MP3URL + '" target="_blank">Buy MP3</a>';
		}
		if(currentData.CDURL != '')
		{
			document.getElementById('buycd').innerHTML = '<a href="' + '/buysongredirect.html?redirect=' + currentData.CDURL + '" target="_blank">Buy CD</a>';
		}
  }
  
//RUN onload
  function getSongInfo( ) {
	  
	  showCheckMeOut(true);
	  checkForSong();
	  setInterval('checkForSong()',5000);
	
}

function showCheckMeOut(show)
{
	if (show)
		{
			//show check it out popup
			var shopLink = dojo.coords(document.getElementById('shopLink'));
			var top = shopLink.y + shopLink.h;
			var positionFader = dojo.fx.slideTo({node: "checkMeOut",duration: 10, left: shopLink.x, top: top});
			var fadeIn = dojo.fadeIn({node: "checkMeOut",duration: 10});
	
			var fader = dojo.fx.chain([positionFader, fadeIn]);
			 dojo.connect(fader, "onEnd", function(){
				
			});
		 	fader.play();
		}
		else
		{
			dojo.fadeOut({node: "checkMeOut",duration: 10}).play();
		}
}

function checkForSong()
{

    dojo.xhrGet({
      url:"/songinfo.json", handleAs:"json",
	  preventCache: true,
      load: function(data){
		  
		  		var newsong = false;
		 		var songover = false;
				if ((currentData == null) || (currentData.songid != data.songid))
				{
					currentData = data;	
					newsong = true;
				}
					
					songover = !isCurrentSongValid();

					if(newsong && !songover)
					{
						slideLeft(true);
					}
					else if(songover)
					{	
						slideLeft(false);	
					}
      }
    });	
}

function isCurrentSongValid()
{
	if (currentData == null)
	{
		return false;	
	}
	//parse the timestamp and duration
	var timestamp = dojo.date.stamp.fromISOString(currentData.timestamp);
	var now = new Date();
	var dif = dojo.date.difference(timestamp,now,"second");
	if (dif > currentData.duration)
	{
		//song is over
		return false;
	}
	else
	{
		return true;	
	}
}
function runOnce(){	
      //set the fadebox to opacity 0 
	  dojo.style("fadeBox", "opacity", "0");
  	  dojo.style("fadeBoxShop", "opacity", "0");
	  dojo.style("checkMeOut", "opacity", "0");
	  
	}
	
	function showShopBox(show)
	{
		if (show)
		{
			showCheckMeOut(false);
			//show check it out popup
			var shopLink = dojo.coords(document.getElementById('shopLink'));
			var top = shopLink.y + shopLink.h;
			var positionFader = dojo.fx.slideTo({node: "fadeBoxShop",duration: 10, left: shopLink.x, top: top});
			var fadeIn = dojo.fadeIn({node: "fadeBoxShop",duration: 1000});
	
			var fader = dojo.fx.chain([positionFader, fadeIn]);
			 dojo.connect(fader, "onEnd", function(){
				
			});
		 	fader.play();
			setTimeout('showShopBox(' + false + ')',30000);
		}
		else
		{
			dojo.fadeOut({node: "fadeBoxShop",duration: 500}).play();
			showCheckMeOut(true);
		}
		
	}
	
function ShowSignUpAtLogin()
{
	if (dojo.cookie("showSignUpOnLoad") != "false")
	{
		document.getElementById('donotshowdiv').style.visibility = 'visible';	
		dijit.byId('dialog1').show(); 
	}
}
	
function SetDoNotShowCookie()
{
	dojo.cookie("showSignUpOnLoad","false", { expires: 365 })
	dijit.byId('dialog1').hide(); 
}
  


