var errFormBG = "#FFC4C4";
var popupWin = null;
function openPopUp(urlPop, properties, title)
{
	NFW = window.open("", 'PopUp' , properties)     
    NFW.blur()
    window.focus()       
    var frameString=""+
					"<html>"+
					"<head>"+
					"<title>"+title+"</title>"+
					"<base href='http://www.djscene.lt/'>"+
					"</head>"+
					"<body topmargin='0' leftmargin='0' rightmargin='0' marginwidth='0' marginheight='0' framespacing='0'>"+
					"<div><img src='"+urlPop+"' border='0'></div>"+
					"</body></html>"
    NFW.document.open();
    NFW.document.write(frameString)
    NFW.document.close()
    NFW.focus()

}

function WindowPopUp(fname, properties)
	{
		if( popupWin && !popupWin.closed )
		{
			popupWin.focus();
		}
		else
		{
			popupWin = window.open(fname, 'PopUp', properties);
		}
	}

function dl(txt) {
   return confirm(txt);
}

function jumpToOpener(url)
{
	try 
	{
		window.opener.document.location.href = url;
	}
	catch (err)
	{
		window.open(url, "_top");
	}
	window.close();
}

function rowOver(object, toClass) {
	object.className = toClass;
}

function rowOverEffect(object) {
  if (object.className == 'moduleRow') object.className = 'moduleRowOver';
  else if (object.className == 'Row1') object.className = 'RowOver1';
  else if (object.className == 'Row2') object.className = 'RowOver2';
}

function rowOutEffect(object) {
  if (object.className == 'moduleRowOver') object.className = 'moduleRow';
  else if (object.className == 'RowOver1') object.className = 'Row1';
  else if (object.className == 'RowOver2') object.className = 'Row2';
}


//Roll Over Image
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  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;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function toggleLayer(whichLayer, valExp, valCol)
{
	if (document.getElementById)
	{
	// this is the way the standards work
	    var style2 = document.getElementById(whichLayer).style;
	    var text2  = document.getElementById(whichLayer+'txt');
	    if (text2.state == "0") 
	    {
	    	style2.display = "";
	    	text2.innerHTML = valExp;
	    	text2.state = "1";
	    } 
	    else
	    {
	    	style2.display = "block";
	    	text2.innerHTML = valCol;
	    	text2.state = "0";	
	    }

	}
	else if (document.all)
	{
	// this is the way old msie versions work
	    var style2 = document.all[whichLayer].style;
	    var text2  = document.all[whichLayer+'txt'];
	    if (text2.state == "0") 
	    {
	    	style2.display = "";
	    	text2.innerHTML = valExp;
	    	text2.state = "1";
	    } 
	    else
	    {
	    	style2.display = "block";
	    	text2.innerHTML = valCol;
	    	text2.state = "0";	
	    }
	}
	else if (document.layers)
	{
	// this is the way nn4 works
	    var style2 = document.layers[whichLayer].style;
	    var text2  = document.layers[whichLayer+'txt'];
	    if (text2.state == "0") 
	    {
	    	style2.display = "";
	    	text2.innerHTML = valExp;
	    	text2.state = "1";
	    } 
	    else
	    {
	    	style2.display = "block";
	    	text2.innerHTML = valCol;
	    	text2.state = "0";	
	    }
	}
}

function getDisplayStyleByTagName(o){
	var n=o.nodeName.toLowerCase();
	return(n=="span"||n=="img"||n=="a")?"inline":"block";
}

function hideDiv(divName){
	var tempDiv=document.getElementById(divName);
	if(!tempDiv){
		return;
	}
	if(tempDiv.style.display=="inline"){
		addClass(tempDiv,"wasinline");
	}else if(tempDiv.style.display=="block"){
		addClass(tempDiv,"wasblock");
	}
	tempDiv.style.display="none";
}

function showDiv(divName){
	var tempDiv=document.getElementById(divName);
	if(!tempDiv){
		return;
	}
	if(hasClass(tempDiv,"wasinline")){
		tempDiv.style.display="inline";
		removeClass(tempDiv,"wasinline");
	}else if(hasClass(tempDiv,"wasblock")){
		tempDiv.style.display="block";
		removeClass(tempDiv,"block");
	}else{
		tempDiv.style.display=getDisplayStyleByTagName(tempDiv);
	}
}

onLoadFunctionList=new Array();

function performOnLoadFunctions()
{
	for(var i=0;i<onLoadFunctionList.length;i++)
	{
		onLoadFunctionList[i]();
	}
}


function removeClass(element,_class)
{
	var upperClass=_class.toUpperCase();
	var remainingClasses=[];
	if(element.className){
		var classes=element.className.split(' ');
		for(var i=0;i<classes.length;i++){
			if(classes[i].toUpperCase()!=upperClass){
				remainingClasses[remainingClasses.length]=classes[i];
			}
		}
		element.className=remainingClasses.join(' ');
	}
}

function addClass(element,_class){
	if(!hasClass(element,_class)){
		element.className+=element.className?(" "+_class):_class;
	}
}

function hasClass(element,_className){
	if(!element){
		return;
	}
	var upperClass=_className.toUpperCase();
	if(element.className){
		var classes=element.className.split(' ');
		for(var i=0;i<classes.length;i++){
			if(classes[i].toUpperCase()==upperClass){
				return true;
			}
		}
	}
	return false;
}



function sprintf()
{
	if (!arguments || arguments.length < 1 || !RegExp)
	{
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str))
	{
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
		
		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%')
		{
			subst = '%';
		}
		else
		{
			numSubstitutions++;
			if (numSubstitutions >= arguments.length)
			{
				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
			       if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
			  else if (pPad) pad = pPad;
			var justifyRight = true;
			       if (pJustify && pJustify === "-") justifyRight = false;
			var minLength = -1;
			       if (pMinLength) minLength = parseInt(pMinLength);
			var precision = -1;
			       if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
			       if (pType == 'b') subst = parseInt(param).toString(2);
			  else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
			  else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
			  else if (pType == 'u') subst = Math.abs(param);
			  else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
			  else if (pType == 'o') subst = parseInt(param).toString(8);
			  else if (pType == 's') subst = param;
			  else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
			  else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}

function DrawRate(element)
{
	/*
	var elName 	= 'vote_' + String(element);
	var elObj	= document.getElementById(elName);
	var imgEl	= eval('rate' + (element < 0 ? 'm' + String((element*-1)) : String(element)));
	elObj.src = imgEl.src;
	*/
	var to = (element > 0 ? element : (element*-1));
	for (var i=1; i<= to; i++)
	{
		var elName 	= 'vote_' + String((element > 0 ? i : (i*-1)));
		var elObj	= document.getElementById(elName);
		var imgEl	= eval('rate' + String((element > 0 ? i : 'm' + String(i))));
		elObj.src = imgEl.src;
	}
	
}

function ClearRate()
{
	for (var i=-5; i<= 5; i++)
	{
		if (i != 0)
		{
			var elName 	= 'vote_' + String(i);
			var elObj	= document.getElementById(elName);
			elObj.src = rate0.src;
		}
	}
}

function changeStyle(cell, style) {
	if (obj = (document.getElementById(cell))) 
	{
		if (style == 'error') obj.style.color = '#FF0000';
		else obj.style.color = '';
	}
}

function keepTop( id ) {
	//firefox
	if (document.getElementById && !document.all) {
		var scroll = (window.pageYOffset);
	} else {
		var scroll = (window.body.scrollTop);
	}
	
	if (obj = document.getElementById(id))
	{
		obj.style.top = (scroll+150) + 'px';
	}
}


// AJAX //
var http_request = false;

function makePOSTRequest(url, parameters, AjaxTarget) {
	document.getElementById(AjaxTarget).innerHTML = '<div style="font-size:100%; font-weight:bold; color:#FFF; background-color:#FF3C00; padding:5px;">Loading...</div>';
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = function() { ResponseRaw(http_request, AjaxTarget); };
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function ResponseRaw(http_request, AjaxTarget) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			result = http_request.responseText;
			document.getElementById(AjaxTarget).innerHTML = '<div>'+result+'</div>';
		} else {
			alert('There was a problem with the request.');
		}
	}
}

function addMyEvent(path, item) {
      var poststr = "action=user_add&event=" + item;
      makePOSTRequest(path, poststr, "whos_going_"+item);
}

function removeMyEvent(path, item) {
      var poststr = "action=user_remove&event=" + item;
      makePOSTRequest(path, poststr, "whos_going_"+item);
}

function saveMyPicture(path, item, img) {
      var poststr = "action=save_my_picture&id="+item+"&image="+img;

      Element.hide('hoverNav');
      Element.hide('prevLink');
      Element.hide('nextLink');
      Element.hide('userImageNav');
      
      var myAjax = new Ajax.Updater(
					{success: 'caption'}, 
					path, 
					{
						method: 'post', 
						parameters: poststr,
						evalScripts: true,
						onCreate: $('caption').innerHTML = '<div style="font-size:100%; font-weight:bold; color:#FFF; background-color:#FF3C00; padding:5px;">Loading...</div>'
					});
      
      
}

function startDownload(path, item) {
      var poststr = "do=download&process=ajax&id="+item;
      
      var myAjax = new Ajax.Updater(
					{success: 'download-button'}, 
					'media/download', 
					{
						method: 'post', 
						parameters: poststr,
						evalScripts: true,
						onCreate: $('download-button').innerHTML = '<div style="font-size:100%; font-weight:bold; color:#FFF; background-color:#FF3C00; padding:5px;">Loading...</div>'
					});      
}

function toggleLogin()
{
	
	if($j('#login-form').css('display')=='none')
	{
		setfocus=0;
	}
	else
	{
		setfocus=1;
	}
	if($j.browser.safari)
	{
		$j('#login-form').toggle();
	}
	else
	{
		$j('#login-form').animate({opacity:'toggle'},'normal',function(){if(setfocus==0){$j('#login_email').focus();}});
	}
	return false;
}
