function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "";
}
$.fn.equals = function(compareTo) {
        if (!compareTo || !compareTo.length || this.length!=compareTo.length)
{
                return false;
        }
        for (var i=0; i<this.length; i++) {
                if (this[i]!==compareTo[i]) {
                        return false;
                }
        }
        return true;
} 
function alphanumeric(alphane)
{
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
		  {
		  }
		else	{
                         
			 return false;
		  }
 		}
 return true;
}

$(window).load(function() {
	$("#home img").fadeIn(2000, function () {
		$(".navigation").slideDown(1000);
	});
	if(getCookie('lang')=="en") $('.be').hide();
	else $('.en').hide();
	if($("input[name='faces']").is(':checked')) { $(".faces").show();$(".workshop").show(); } //show all if faces checked
	else //faces not checked
	{
		$(".faces").hide();
		if($("input[name='workshop']").is(':checked')) $(".workshop").show();
		else $(".workshop").hide();
	}
	if(!$("input[name='workshop']").is(':checked') && !$("input[name='faces']").is(':checked')) $(".faces.workshop").hide();	
});

$(document).ready(function() {
	$("#flag").click( function () {
		if(getCookie('lang')=="en") // was english
		{
			$(this).attr("src","/img/en.png");
			$(this).attr("title","Change to English");
			$('.en').fadeOut(600, function() {
				$('.en').hide();
				$('.be').fadeIn(600);
			});
			setCookie('lang','be',30);
		}
		else //was dutch
		{
			$(this).attr("src","/img/be.jpg");
			$(this).attr("title","Change to Dutch");
			$('.be').fadeOut(600, function() {
				$('.be').hide();
				$('.en').fadeIn(600);
			});
			setCookie('lang','en',30);
		}
	});
	
	$("input[name='workshop']").click(function () {
		if($("input[name='faces']").is(':checked')) { $(".faces").show();$(".workshop").show(); } //show all if faces checked
		else //faces not checked
		{
			$(".faces").hide();
			if($("input[name='workshop']").is(':checked')) $(".workshop").show();
			else $(".workshop").hide();
		}
		if(!$("input[name='workshop']").is(':checked') && !$("input[name='faces']").is(':checked')) $(".faces.workshop").hide();
	});
	
	$("input[name='faces']").click(function () {
		if($("input[name='faces']").is(':checked')) { $(".faces").show();$(".workshop").show(); } //show all if faces checked
		else //faces not checked
		{
			$(".faces").hide();
			if($("input[name='workshop']").is(':checked')) $(".workshop").show();
			else $(".workshop").hide();
		}
		if(!$("input[name='workshop']").is(':checked') && !$("input[name='faces']").is(':checked')) $(".faces.workshop").hide();
	});
	$("#add").click( function () {
		temp = $("input[name='submit']").parent().parent().html();
		$("input[name='submit']").parent().html('<td  colspan="3"><input name="file[]" type="file" /></td><td></td></tr><tr>'+temp);
	});
	$("#logout").click( function () {
		setCookie('password','',30);
		window.location.href = '/';
	});
	$("a.confirm").click( function () {
		if(confirm("Are you sure you want to "+$(this).attr('id')+"?"))
			window.location.href = $(this).attr('href');
		else return false; 
	});
	$("a.addmodel").click( function () {
		var result = window.prompt("Please enter folder name for the new model, e.g. jovana.\n(Only alphanumeric characters allowed, i.e. a-z and 0-9)","")
		if(result!=null)
		{
			if(alphanumeric(result))
			{
				window.location.href = $(this).attr('href')+"&model="+result;
				return false;
			}
			else
			{
				alert('ERROR: Folder names can only contain letters and numbers!');
				return false;
			}
		}
		else return false; 
	});
	$("#save").click( function () {
		$("#submit").click();
		return false;
	});
	$("#additem").click( function () {
		$('.edit table').append('<tr><td><input type="text" name="attribute[]" value="" /></td><td><input type="text" name="value[]" value="" /></td><td><a href="javascript:void(0);" class="movedown"><img src="/img/down.gif" title="Move Down" /></a><a href="javascript:void(0);" class="moveup"><img src="/img/up.gif" title="Move Up" /></a><a href="javascript:void(0);" class="deleteitem"><img src="/img/delete.png" title="Delete Item" /></a></td></tr>');
		return false;
	});
	$("#addline").click( function () {
		$('.edit table').append('<tr><td colspan="2"><input type="text" name="attribute[]" value="" style="display: none;" /><input type="text" name="value[]" value="" /></td><td><a href="javascript:void(0);" class="movedown"><img src="/img/down.gif" title="Move Down" /></a><a href="javascript:void(0);" class="moveup"><img src="/img/up.gif" title="Move Up" /></a><a href="javascript:void(0);" class="deleteitem"><img src="/img/delete.png" title="Delete Item" /></a></td></tr>');
		return false;
	});
	$(".deleteitem").click( function () {
		$(this).parent().parent().html('');
		return false;
	});
	$(".moveup").click( function () {
		selected = $(this).parent().parent();
		if(!selected.equals(selected.parent().children(":nth-child(2)")))
			selected.after(selected.prev());
		return false;
	});
	$(".movedown").click( function () {
		selected = $(this).parent().parent();
		if(!selected.equals(selected.parent().children(":last")))
			selected.before(selected.next());
		return false;
	});
	$(".deletefile").click( function () {
		file = $(this).attr('id'); 
		$.ajax({
			type: "GET",
			url: "index.php",
			data: 	"deletefile="+file+"&garbage="+((new Date()).getTime()),
			success: function(html){
			}
		});
		$(this).fadeOut(300);
		file = file.replace(/[^a-zA-Z0-9]/g,"");
		$("img."+file).fadeOut(300);
		$("p."+file).fadeOut(300);
	});
});

$(function() {
	var settings = { containerResizeSpeed: 350 };
	$('#gallery a').lightBox(settings);
	$('#photos a').lightBox(settings);
	$('.photos a').lightBox(settings);
	$('#profile a').lightBox(settings);
	if(getCookie('lang')=="en") $('.be').hide();
	else $('.en').hide();
});


