// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 28;
// -----------------------------------------------------------------------------

// Constants
var notessimoW = "100%";
var notessimoH = "100%";
var notessimoURL = "files/notessimo";
var notessimoURL2 = "http://notessimo.net/files/notessimo.swf";
var notessimoBG = "#505050";

var lastID = -1;
var rateWindow;
var url5 = '';

// Rollover Button
function hov(loc,cls){
	loc.className = cls;
}

// Share button !
function openShare(id)
{
	var d = document.getElementById('share'+id);
	d.innerHTML = '<!-- AddThis Button BEGIN --><div class="addthis_toolbox addthis_default_style"><a href="http://www.addthis.com/bookmark.php?v=250&amp;username=Starburst" class="addthis_button" style="float: left;" addthis:url="http://notessimo.net/song-'+id+'">Share</a><span class="addthis_separator">|</span><a class="addthis_button_facebook" addthis:url="http://notessimo.net/song-'+id+'"><img src="'+url5+'/social/facebook.png" width="16" height="16" alt="Facebook" /></a><a class="addthis_button_twitter" addthis:url="http://notessimo.net/song-'+id+'"><img src="'+url5+'/social/twitter.png" width="16" height="16" alt="Twitter" /></a><a class="addthis_button_digg" addthis:url="http://notessimo.net/song-'+id+'"><img src="'+url5+'/social/digg.png" width="16" height="16" alt="Digg" /></a><a class="addthis_button_reddit" addthis:url="http://notessimo.net/song-'+id+'"><img src="'+url5+'/social/reddit.png" width="16" height="16" alt="Reddit" /></a><a class="addthis_button_stumbleupon" addthis:url="http://notessimo.net/song-'+id+'"><img src="'+url5+'/social/stumble.png" width="16" height="16" alt="Stumble Upon" /></a><a class="addthis_button_email" addthis:url="http://notessimo.net/song-'+id+'"><img src="'+url5+'/social/email.png" width="16" height="16" alt="Email" /></a></div><!-- AddThis Button END --><div class="embed"><span>URL </span><input type="text" readonly="" onclick="javascript:this.focus();this.select();" value="http://notessimo.net/song-'+id+'" class="inputbox"/></div><div class="embed"><span>Flash HTML </span><input type="text" readonly="" onclick="javascript:this.focus();this.select();" value="&lt;object width=&quot;485&quot; height=&quot;500&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.notessimo.net/Notessimo2.swf&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;param name=&quot;FlashVars&quot; value=&quot;id='+id+'&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.notessimo.net/Notessimo2.swf&quot; type=&quot;application/x-shockwave-flash&quot; FlashVars=&quot;id='+id+'&quot; width=&quot;485&quot; height=&quot;500&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot;&gt;&lt;/embed&gt;&lt;/object&gt;" class="inputbox"/></div><div class="embed"><span>Widget HTML </span><input type="text" readonly="" onclick="javascript:this.focus();this.select();" value="&lt;script type=&quot;text/javascript&quot; src=&quot;http://notessimo.net/widget.js?id='+id+'&quot;&gt;&lt;/script&gt;" class="inputbox"/></div><div class="embed"><span>BBCode </span><input type="text" readonly="" onclick="javascript:this.focus();this.select();" value="[notessimo]'+id+'[/notessimo]" class="inputbox"/></div>';

	d.style.height = "120px";
	
	addthis.button(".addthis_button");
	addthis.toolbox(d);

	d = document.getElementById('ss'+id);
	var olddiv = d.parentNode;
	olddiv.removeChild(d);
}

// Open in a new Window
function openNotessimo(id)
{
	if ( lastID != -1 )
	{
		var d = document.getElementById('notessimo'+lastID);
	  	var olddiv = d.parentNode;
	  	olddiv.removeChild(d);
		
		olddiv.innerHTML = '<div id="n'+lastID+'"><a href="javascript: playNotessimo('+lastID+')">Play</a></div><div class="notessimoOr">or</div><div><a href="javascript: openNotessimo('+lastID+')">Play in a new window</a></div>';
	}
	
	lastID = -1;

	html = url5+'/misc/window.php?id='+id;
	
	w = 650;
	h = 500;
	w += 32;
	h += 96;

	wleft = (screen.width - w) / 2;
	wtop = (screen.height - h) / 2;

	if (wleft < 0) {
		w = screen.width;
		wleft = 0;
	}
	if (wtop < 0) {
		h = screen.height;
		wtop = 0;
	}

	win = window.open(html,'Notessimo','width='+w+',height='+h+',left='+wleft+',top='+wtop+',directories=0,location=0,menubar=0,scrollbars=0,status=0,toolbar=0,resizable=1');
	win.resizeTo(w, h);
	win.moveTo(wleft, wtop);
	win.focus();
}

// Play Notessimo
function writeNotessimo(id){
	var flashvars = {};
	if (id != 0) {
		flashvars.id = id;
	}
	var params = {};
	var attributes = {};
		attributes.id = "notessimo";
		attributes.name = "notessimo";
	swfobject.embedSWF("/files/notessimo.swf", "notessimoSWF", "100%", "100%", "9.0.0", "/files/expressInstall.swf", flashvars, params, attributes);
}

// Play Notessimo
function playNotessimo(id){
	// Kill all other SWF
	if ( lastID != -1 )
	{
		var d = document.getElementById('notessimo'+lastID);
	  	var olddiv = d.parentNode;
	  	olddiv.removeChild(d);
		
		olddiv.innerHTML = '<div id="n'+lastID+'"><a href="javascript: playNotessimo('+lastID+')">Play</a></div><div class="notessimoOr">or</div><div><a href="javascript: openNotessimo('+lastID+')">Play in a new window</a></div>';
	}
	lastID = id;
	
	// Add the new SWF
	var flashvars = {};
	if (id != 0) {
		flashvars.id = id;
		flashvars.autoPlay = true;
	}
	var params = {};
	var attributes = {};
		attributes.id = "notessimo" + id;
		attributes.name = "notessimo" + id;
	swfobject.embedSWF(url5+"/files/notessimo.swf", "n" + id, "485", "500", "9.0.0", url5+"/files/expressInstall.swf", flashvars, params, attributes);
}

// Rate a song !
function updateRating(type, power){
	switch(type) {
		case 1:
			vote = parseInt(document.getElementById("rate").value);
			nrating = parseInt(document.getElementById("nrating").innerHTML);
			document.getElementById("nrating").innerHTML = nrating+1;
			document.getElementById("ratingf").innerHTML = ((p_rating+vote)/((n_rating+power)*5)*5).toPrecision(3);
			alert(voted_1+" "+vote+", "+voted_2+" !");
			break;
		default:
			alert(vote_once+" !");
			break;
	}
}

function rate(id) {
	rateWindow = window.open("update/rate.php?rate="+document.getElementById("rate").value+"&id="+id, "", "width=1,height=1");
}

function getkey(e)
{
	if (window.event)
		return window.event.keyCode;
	else if (e)
		return e.which;
	else
		return null;
}

function goodchars(e, goods)
{
	var key, keychar;
	key = getkey(e);
	if (key == null) return true;

	// get character
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	goods = goods.toLowerCase();

	// check goodkeys
	if (goods.indexOf(keychar) != -1)
		return true;

	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
   		return true;

	// else return false
	return false;
}

function num(e) {
	return goodchars(e, '0123456789');
}

function changeEmbed(id) {
	document.getElementById("embed_code").value = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="Notessimo'+id+'" width="'+document.getElementById("embed_w").value+'" height="'+document.getElementById("embed_h").value+'" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"><param name="movie" value="'+notessimoURL2+'" /><param name="quality" value="high" /><param name="bgcolor" value="'+notessimoBG+'" /><param name="allowScriptAccess" value="sameDomain" /><param name="FlashVars" value="id='+id+'"><embed src="'+notessimoURL2+'" quality="high" bgcolor="'+notessimoBG+'" width="'+document.getElementById("embed_w").value+'" height="'+document.getElementById("embed_h").value+'" name="Notessimo'+id+'" align="middle" play="true" loop="false" quality="high" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" FlashVars="id='+id+'" pluginspage="http://www.adobe.com/go/getflashplayer"></embed></object>';
}

function changeWH(id) {
	switch (document.getElementById("scale").value) {
		case "Huge":
			document.getElementById("embed_w").value = 800;
			document.getElementById("embed_h").value = 600;
			break;
		case "Big":
			document.getElementById("embed_w").value = 586;
			document.getElementById("embed_h").value = 530;
			break;
		case "Normal":
			document.getElementById("embed_w").value = 485;
			document.getElementById("embed_h").value = 500;
			break;
		case "Small":
			document.getElementById("embed_w").value = 468;
			document.getElementById("embed_h").value = 450;
			break;
		case "Tiny":
			document.getElementById("embed_w").value = 300;
			document.getElementById("embed_h").value = 350;
			break;
	}
	changeEmbed(id);
}

function changePlayWH() {
	switch (document.getElementById("play_scale").value) {
		case "Huge":
			document.getElementById("play_w").value = 1300;
			document.getElementById("play_h").value = 850;
		break;
		case "Big":
			document.getElementById("play_w").value = 1000;
			document.getElementById("play_h").value = 720;
		break;
		case "Normal":
			document.getElementById("play_w").value = 724;
			document.getElementById("play_h").value = 530;
		break;
		case "Small":
			document.getElementById("play_w").value = 586;
			document.getElementById("play_h").value = 530;
		break;
		case "Tiny":
			document.getElementById("play_w").value = 468;
			document.getElementById("play_h").value = 450;
		break;
	}
}

function add_ad(name) {
	var ad_hidden = document.getElementById("ad_"+name+"_hidden");
	var ad_show = document.getElementById("ad_"+name);
	ad_show.innerHTML = "";
	ad_show.appendChild(ad_hidden);
	ad_hidden.className = "show";
}

function loadNotessimo() {
	window.onbeforeunload = confirmExit;
}	

function confirmExit() {
	return str_exit+".";
}

function $(v,o) { return((typeof(o)=='object'?o:document).getElementById(v)); }
function $S(o) { return((typeof(o)=='object'?o:$(o)).style); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function abPos(o) { var o=(typeof(o)=='object'?o:$(o)), z={X:0,Y:0}; while(o!=null) { z.X+=o.offsetLeft; z.Y+=o.offsetTop; o=o.offsetParent; }; return(z); }
function XY(e,v) { var o=agent('msie')?{'X':event.clientX+document.body.scrollLeft,'Y':event.clientY+document.body.scrollTop}:{'X':e.pageX,'Y':e.pageY}; return(v?o[v]:o); }

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

star={};
var stat;

star.mouse = function(e,o) 
{ 
	if ( star.stop || isNaN(star.stop) ) 
	{ 
		star.num=o.id.substr(4);
		
		if ( star.lock.indexOf( star.num ) == -1 && star.num != -1 )
		{
			star.stop=0;
		
			document.onmousemove = function( e ) 
			{ 
				var n=star.num;
			
				if ( star.lock.indexOf( n ) == -1 && star.num != -1 )
				{
					var p=abPos($('star'+n)), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y;
					
					if ( oX<1 || oX>star.maxW || oY<0 || oY>star.maxH ) 
					{ 
						star.stop=1; star.revert(); 
					}
					else 
					{
						// Round to 20%
						oX = Math.ceil(oX / star.maxW * 5) * (star.maxW / 5);
						
						$S('starCur'+n).width=oX+'px';
					}
				}
				else
				{
					star.stop=1;
					document.onmousemove='';
				}
			};
		}
	} 
};

star.update=function(e,o) 
{ 
	var n=star.num;
	
	if ( star.lock.indexOf( n ) == -1 && star.num != -1 )
	{
		var p=abPos($('star'+n)), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y; star.num=o.id.substr(4);
		
		if ( oX<1 || oX>star.maxW || oY<0 || oY>star.maxH ) 
		{ 
			// Nothing...
		}
		else 
		{
			var power = 1;
			var p_rating = Math.ceil(oX / star.maxW * 5);
		
			// Round to 20%
			star.lock.push( n );
			jx.load( url5+'/update/rate.php?id=' + star.num + '&rate=' + p_rating, function( data )
			{
				// Do nothing :P
			});
			
			// Update HTML
			$('starCur'+n).className = "curr2";
			
			nPoints = parseInt(document.getElementById("rate"+n).title.split("|")[0]);
			nPower = parseInt(document.getElementById("rate"+n).title.split("|")[1]);
			nrating = parseInt(document.getElementById("vote"+n).innerHTML);
			
			if ( nrating+1 > 1 )
			{
				document.getElementById("vote"+n).innerHTML = nrating+1 + " votes";
			}
			else
			{
				document.getElementById("vote"+n).innerHTML = nrating+1 + " vote";
			}
			
			document.getElementById("rate"+n).innerHTML = ((nPoints+p_rating)/((nPower+power)*5)*5).toPrecision(3);
		}
	}
};

star.revert=function() 
{ 
	var n=star.num, v=parseInt($('starCur'+n).title);
	
	$S('starCur'+n).width=Math.round(v*star.maxW/100)+'px';

	document.onmousemove='';
};