var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

function nw(ext) { window.open(ext); return false; }
function go(u) { document.location.href = u; return false; }

function NTChecker(code)
{
	new Ajax.Request('/KeepAlive.html?code='+code, {
		method: 'get',
		onSuccess: function(transport)
		{
			var chk = transport.responseText.evalJSON();
			
			if(chk['SessionLeft'] < 0)
				document.location.href="/pLogout.html"
		}
	});
}

function dateFormat(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();

    if (month < 10) {
        month = '0' + month;
    }

    if (day < 10) {
        day = '0' + day;
    }
    return year+'-'+month+'-'+day;
}

function getCoords()
{

	var coords = [
		[570, 110],	// Blekinge län
		[385, 115],	// Dalarnas län
		[530, 185],	// Gotlands län
		[370, 155],	// Gävleborgs län
		[540, 60],	// Hallands län
		[300, 120],	// Jämtlands län
		[515, 95],	// Jönköpings län
		[535, 130],	// Kalmar län
		[550, 95],	// Kronobergs län
		[160, 220],	// Norrbottens län
		[580, 70],	// Skåne län
		[450, 180],	// Stockholms län
		[460, 150],	// Södermanlands län
		[425, 175],	// Uppsala län
		[425, 80],	// Värmlands län
		[240, 190],	// Västerbottens län
		[295, 180],	// Västernorrlands län
		[435, 145],	// Västmanlands län
		[490, 70],	// Västra Götaland
		[445, 120],	// Örebro län
		[484, 126]	// Östergötlands län
	];

	return coords;
}

function Dot(x, y)
{
	$('#dot'+x+y).attr("src","/_gfx/dot2.png");
}

function HideDot(x, y)
{
	$('#dot'+x+y).attr("src","/_gfx/dot.png");
}

function setFNHeight()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
//	window.alert( 'Width = ' + myWidth + '\nHeight = ' + myHeight );
	
	$('#fnContent').css({height: myHeight + 'px'});
}

function fbs_click(url)
{
	if(url)
		u = 'http://www.nojestorget.se' + url;
	else
		u=location.href;

	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

function getUserInfo()
{
	if (checkIt('konqueror'))
	{
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser = "Safari"
	else if (checkIt('omniweb')) browser = "OmniWeb"
	else if (checkIt('opera')) browser = "Opera"
	else if (checkIt('webtv')) browser = "WebTV";
	else if (checkIt('icab')) browser = "iCab"
	else if (checkIt('msie')) browser = "Internet Explorer"
	else if	(checkIt('firefox')) browser = "Firefox"
	else if (!checkIt('compatible'))
	{
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS)
	{
		if (checkIt('linux')) OS = "Linux";
		else if (checkIt('x11')) OS = "Unix";
		else if (checkIt('mac')) OS = "Mac"
		else if (checkIt('win')) OS = "Windows"
		else OS = "an unknown operating system";
	}
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

//--------------------------
//Disabled due to deprecation in WebKit
/*

function getObject( obj )
{
	// step 1
	if ( document.getElementById )
	{
		obj = document.getElementById( obj );
	}
	// step 2
	else if ( document.all )
	{
		obj = document.all.item( obj );
	}
	//step 3
	else
	{
		obj = null;
	}

	//step 4
	return obj;
}

function moveObject( obj, e )
{
	// step 1
	var tempX = 0;
	var tempY = 0;
	var offsetX = 30;
	var offsetY = 5;
	var objHolder = obj;

	// step 2
	obj = getObject( obj );
	if (obj==null)
		return;

	// step 3
	if (document.all)
	{
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	}
	else
	{
		tempX = e.pageX;
		tempY = e.pageY;
	}

	// step 4
	if (tempX < 0)
	{
		tempX = 0;
	}

	if (tempY < 0)
	{
		tempY = 0;
	}

	// step 5
	obj.style.top  = (tempY + offsetY) + 'px';
	obj.style.left = (tempX + offsetX) + 'px';

	// step 6
	displayObject( objHolder, true );
}

function displayObject( obj, show )
{
	// step 1
	obj = getObject( obj );
	if (obj==null)
		return;

	// step 2
	obj.style.position = 'absolute';
	obj.style.display = show ? 'block' : 'none';
	obj.style.visibility = show ? 'visible' : 'hidden';
}

function hideObject( obj )
{
	var objHolder = obj;

	obj = getObject( obj );
	if (obj==null)
		return;
	
	displayObject( objHolder , false );
}
*/

function showCookies()
{
	obj = getObject('CookieLayer');
		
	if(obj == null)
		return;
			
	obj.style.display = 'block';
	obj.style.visibility = 'visible';
}

function CenterPopup(ref,w,h,s)
{
	var xpos, ypos;
	if (ref.window.document.body)
	{
		xpos = Math.round(ref.window.screenLeft + (ref.window.document.body.clientWidth / 2) - ((w + 5) / 2));
		ypos = Math.round(ref.window.screenTop + (ref.window.document.body.clientHeight / 2) - ((h + 25) / 2));
	} else {
		xpos = 150;
		ypos = 150;
	}
	
	xpos = xpos + Math.round(Math.random()*200-100);
	ypos = ypos + Math.round(Math.random()*200-100);
	
	return 'scrollbars=' + s + ',statusbar=0,resizable=0,width=' + w + ',height=' + h + ',left=' + xpos + ',top=' + ypos;
}

function PopupWindow(url, w, h, s)
{
	win = window.open(url,'', CenterPopup(top,w,h,s));

	if(win==null) alert('Please activate popupwindows');
	win.focus();
}
function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
   return IsNumber;
}

function rand(num1, num2)
{
	num1 = parseInt(num1);
	num2 = parseInt(num2);
	if(num1 >= num2)
	{
		alert("Number 2 should be greater than Number 1");
		return false;
	}
	else
	{
		var generator = Math.random()*(num2-num1);
		generator = Math.floor(num1+generator);
	}
	
	return generator;
}

function randomNum(numDigits)
{
	var num='';
	if (numDigits <= 0)
		return 0;

	for (i=1; i <= numDigits; i++)
	{
		if(i == 1)
		{
			num += rand(1, 9);
		} else {
			num += rand(0,9);
		}
	}
	return num;
}

function G(el) { return document.getElementById(el); }

function isInt (str)
{
	var i = parseInt (str);

	if (isNaN (i))
		return false;

	i = i . toString ();
	if (i != str)
		return false;

	return i;
}

function dw(id)
{
	PopupWindow('/popup/mp3/'+id, 400, 400);
}

function nyhetsbrev(id)
{
	PopupWindow('nyhetsbrev.php?' + id, 350, 100)
}

function sf()
{
	G('frmSearch').q.focus();
	
//	var html = document.getElementById('NTCounter').innerHTML;

//	document.getElementById('NTCounter').innerHTML = html + '<img src="/_lib/uniqueVisitorCounter.php" alt="" width="100" height="15" />';
}

function taf(SID, ID)
{
	document.location.href = '/TipAFriend_' + SID + '_' + ID + '.html';
}

function as(_p, _g)
{
	return;
}
function clk()
{
	var frm = G('frmSearch');
	var q = frm.q;
	var btnG = frm.btnG;
	btnG.disabled = true;

	if(q.length > 1)
	{
		while (q.substring(0,1) == ' ') q = q.substring(1);
		while (q.substring(q.length-1,q.length) == ' ') q = q.substring(0,q.length-1);
		document.frmSearch.q.value = q;
	}
	
	if((!IsNumeric(q)) && (q.value.length < 3))
	{
		alert('Skningen mste best av mer n 3 tecken');
		btnG.disabled = false;
		return false;
	}
	
	frm.submit();
	return true;
}

function bs(_p, _g)
{
	return;
}

function qs(el)
{
	var frm = G('frmSearch');
	if (window.RegExp && window.encodeURIComponent)
	{
		var ue=el.href;
		var qe=encodeURIComponent(frm.q.value);
		
		if(ue.indexOf("q=")!=-1)
		{
			el.href=ue.replace(new RegExp("q=[^&$]*"),"q="+qe);
		} else {
			el.href=ue+"&q="+qe;
		}
	}
	return 1;
}

//////////////////////////////////// Flash player

var detectableWithVB = false;
var pluginFound = false;

function canDetectPlugins() {
    if( detectableWithVB || (navigator.plugins && navigator.plugins.length > 0) ) {
	return true;
    } else {
	return false;
    }
}
function detectFlash() {
    pluginFound = detectPlugin('Shockwave','Flash'); 
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
    }
    // check for redirection
    return pluginFound;
}
function detectDirector() { 
    pluginFound = detectPlugin('Shockwave','Director'); 
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('SWCtl.SWCtl.1');
    }
    // check for redirection
    return pluginFound;
}
function detectQuickTime() {
    pluginFound = detectPlugin('QuickTime');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectQuickTimeActiveXControl();
    }
    return pluginFound;
}
function detectReal() {
    pluginFound = detectPlugin('RealPlayer');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = (detectActiveXControl('rmocx.RealPlayer G2 Control') ||
		       detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
		       detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
    }	
    return pluginFound;
}
function detectWindowsMedia() {
    pluginFound = detectPlugin('Windows Media');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');
    }
    return pluginFound;
}
function detectPlugin() {
    // allow for multiple checks in a single pass
    var daPlugins = detectPlugin.arguments;
	
	
    // consider pluginFound to be false until proven true
    var pluginFound = false;
    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) {
	var pluginsArrayLength = navigator.plugins.length;
	// for each plugin...
	
	for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
	    // loop through all desired names and check each against the current plugin name
	    var numFound = 0;
	    for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
		// if desired plugin name is found in either plugin name or description
		if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
		    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
		    // this name was found
		    numFound++;
		}   
	    }
	    // now that we have checked all the required names against this one plugin,
	    // if the number we found matches the total number provided then we were successful
	    if(numFound == daPlugins.length) {
		pluginFound = true;
		// if we've found the plugin, we can stop looking through at the rest of the plugins
		break;
	    }
	}
    }
    return pluginFound;
} // detectPlugin

// Here we write out the VBScript block for MSIE Windows
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<script language="VBscript">');

    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('\'and the following function handles QuickTime');
    document.writeln('Function detectQuickTimeActiveXControl()');
    document.writeln('  on error resume next');
    document.writeln('  detectQuickTimeActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('    detectQuickTimeActiveXControl = False');
    document.writeln('    hasQuickTimeChecker = false');
    document.writeln('    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
    document.writeln('    If IsObject(hasQuickTimeChecker) Then');
    document.writeln('      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
    document.writeln('        detectQuickTimeActiveXControl = True');
    document.writeln('      End If');
    document.writeln('    End If');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('</scr' + 'ipt>');
}

// Statistik script slut
function mp3player(id, auto)
{
	var url = 'http://cdn.nojestorget.se/mp3/'+ id +'.mp3';
	var margin = 'margin: 0px; padding: 0px;';

	if(auto)
		var flash = '&autoplay=1'
	else	
		var flash = '';

	var flashvars = { mp3: url + flash, width: 284, height: 20 };
	var params = { menu: "false", scale: "scale", salign: "lt", quality: "best", wmode: "transparent", configxml: "/_lib/player_mp3_maxi.xml", bgcolor: "#ffffff" };
	var attributes = { id: "mp3player", name: "mp3player" };
	swfobject.embedSWF("/assets/flash/player_mp3_maxi.swf", "mp3player", "284", "20", "10", '', flashvars, params, attributes);
}

function setHeight()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
//	window.alert( 'Width = ' + myWidth + '\nHeight = ' + myHeight );
	
	G('adNTLeft').style.height = myHeight + 'px';
	
	if(G('adNTRight'))
		G('adNTRight').style.height = myHeight + 'px';
}

function artist(link)
{
	var ArtistID = isInt(link);

	if(ArtistID == 0)
		document.location.href = '.'+link;
	else
		document.location.href = './' + ArtistID + '_untitled.html';
}


/////////////////////////////

function writePrint()
{
	if(window.print)
	{
		document.write('<input type="button" value="Skriv ut sidan" class="button" onclick="javascript:window.print();" />');
	} else {
		document.write('&nbsp;');
	}
}

function CleanArtists()
{
	if (!confirm('Sker p att du vill radera alla dina sparade artister?')) { return false; }
	
	document.location.href = './CleanArtist_wipe.html';
}

function sDM(year, month, day)
{
	if(month < 10)
		month = '0' + month;

	if(day < 10)
		day = '0' + day

	document.frmDate.eventdate.value = year + '-' + month + '-' + day;
	hideObject('divDateSelection');
}

