function doRefresh()
{		
	new Effect.SwitchOff(document.getElementById('page'));
	setTimeout('document.location.href = document.location.href;', 1000);
}

function loginToggle()
{
	new Effect.toggle($('loginbar'), 'slide', {duration: 0.5});
}

function langToggle()
{
	new Effect.toggle($('languagebar'), 'slide', {duration: 0.5});
}

function appearEffect(id)
{
	var img = "img_" + $(id);
	new Effect.Appear(img,{duration:.5, from:.4});
}

function openGallery(id)
{
	var target = "img_" + $(id);
	new Effect.Puff(target);	
	setTimeout('document.location.href=\"index.php?page=gallery_view_pictures&id=' + id + '\";', 500);	
}

function zoomPicHover(elm)
{
	elm.src = getFileName(elm.src);
	new Effect.Appear($(elm.id),{duration:.3, from:.2});
}

function getFileName(s)
{
	
	var tempPage = s.substring(s.lastIndexOf('/') + 1,s.length);
	var targetExtensionPoint = tempPage.indexOf(".");
	var shortened = tempPage.substring(0, targetExtensionPoint)
	var ext = tempPage.substring(targetExtensionPoint, tempPage.length)
	var parsedFileName = shortened.indexOf("_") > -1 ? "img/" + tempPage.substring(0, tempPage.indexOf("_")) + ext : "img/" + shortened + "_zoom" + ext;
	
	return parsedFileName;
}

function movePrevious(id, jarjestys, kategoria)
{
	var target = "img_" + $(id);	
	new Effect.Pulsate(target);
	ajaxMovePrevious(id, jarjestys, kategoria);
}


function moveNext(id, jarjestys, kategoria)
{
	var target = "img_" + $(id);	
	new Effect.Pulsate(target);
	ajaxMoveNext(id, jarjestys, kategoria);
}

function ajaxMoveNext(id, jarjestys, kategoria)
{
	new Ajax.Request("ajax_movenext.php", {
			   method: 'post',
			   postBody: "id="+id+"&jarjestys="+jarjestys+"&kategoria="+kategoria,
			   onComplete: showMoveNextResponse
		});
}

function ajaxMovePrevious(id, jarjestys, kategoria)
{
		new Ajax.Request("ajax_moveprevious.php", {
			   method: 'post',
			   postBody: "id="+id+"&jarjestys="+jarjestys+"&kategoria="+kategoria,
			   onComplete: showMovePreviousResponse
		});

}

function showMoveNextResponse(req)
{
	var res=/false/;
	if(!req.responseText.match(res))
   	{
   		setTimeout('document.location.href = document.location.href;', 750);
   	}
   	else
   	{
		document.location.href = document.location.href;
   	}
}

function showMovePreviousResponse(req)
{
	var res=/false/;
	
	if(!req.responseText.match(res))
   	{
   		setTimeout('document.location.href = document.location.href;', 750);
   	}
   	else
   	{
		document.location.href = document.location.href;
   	}
}

function deletePicture(folder, pic, num)
{
	Effect.Shake(document.getElementById(pic));
	
	var msg = "Are you sure you wanna delete the picture: #" + num + "?";
		
	if(confirm(unescape(msg))) {
		new Ajax.Request("ajax_deletepicture.php", {
			   method: "post",
			   postBody: "f="+folder+"&action=deletepicture&pic="+pic,
			   onComplete: showDeletePictureResponse 			   
		});	
	}
	else {
		return false;
	}
}

function showDeletePictureResponse(req)
{
   var res=/false/;
       
   if(!req.responseText.match(res))
   {
   		Effect.SwitchOff($(req.responseText));
   }
   else
   {
		document.location.href = document.location.href;
   }
}

function deleteGallery(folder)
{
	Effect.Shake(document.getElementById("pics"));
	
	var msg = "Are you sure you wanna delete all picture(s) in this gallery?";
	
	if(confirm(unescape(msg))) {
		new Ajax.Request("ajax_deletepicture.php", {
			   method: "post",
			   postBody: "f="+folder+"&action=deletefolder",
			   onComplete: showDeleteFolderResponse 			   
		});	
	}
	else {
		return false;
	}		
}

function showDeleteFolderResponse(req)
{
   var res=/false/;
       
   if(!req.responseText.match(res))
   {
   		Effect.SwitchOff($('pics'));
   		setTimeout('document.location.href = document.location.href;', 1000);
   		
   }
   else
   {
		document.location.href = document.location.href;
   }
}

// ajax delete gallery, response is ok if succeeded
function deleteWholeGallery(id, galleria)
{
	var q = "Are you sure you wanna delete whole gallery with all pictures?";
	var target = "img_" + $(id);
	new Effect.Shake(target);
			
	if(confirm(q))
	{		
		new Ajax.Request("ajax_deletegallery.php", {
			   method: 'post',
			   postBody: "id="+id+"&galleria="+galleria,
			   onComplete: showDeleteWholeGallery
		});
	}
	else {
		return false;
	}
}

function showDeleteWholeGallery(req)
{    
   var res=/false/;
   
   if(!req.responseText.match(res))
   {   	
   		document.location.href = document.location.href;
   }
   else
   {   		
		document.location.href = document.location.href;
   }
}


function doCancel()
{
	document.location.href='index.php?page=news';
}

function doSave()
{
	var theform = document.forms['form1'];
	// set form action
	theform.action = "index.php?page=news_admin&action=save&id=" + getParam('id');
	
	// submit form
	theform.submit();
}

function doDeleteNews(id,header)
{
	if(confirm("Are you sure you wanna delete the news\n" + header + "?")) {
		var theform = document.forms[0];
		theform.action = "index.php?page=news&action=del&id=" + id;
		theform.submit();
	}
	else {
		return false;
	}
}

function getParam(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null ) {
    return "";
  } 
  else {
    return results[1];
  }
}
