$(function() {

$('.simpleSlideShow, .slideShowTopNavi').slideShow({
							interval: 6
						});
   
$("#auth").click(function() {
	$.get("/pages/auth.php", { mode:'authuser', username: document.getElementsByName('usernameu')[0].value, password: document.getElementsByName('passwordu')[0].value }, function(data){ eval(data); });
});
   
$("#checkcod").click(function() {
	if ($("#codc").val() == "") { 
		alert('Nu ai completat codul');
	} else {
		$.get("/pages/auth.php", { mode:'validarecod', cod:$("#codc").val() }, function(data){ $('#persoanavalida').html(data); });
	}
});
   
$("#create").click(function() {
	var eok = true;
	var eok2 = true;
	$("#newform input").each(function(i, elm) { if($(elm).val() == '') eok = false; });
	if (!eok) alert('Nu ati completat toate campurile!');
	if ($("#passwordr").val() != $("#password2r").val()) { eok2 = false; alert('Parola si parola de verificare nu sunt la fel!'); }
	var acordzor = 'nu';
	if ($('#acord').is(':checked')) acordzor = 'da';
	if (eok && eok2) { 
		$.get("/pages/auth.php", { mode:'register', acord:acordzor, username: $("#usernamer").val(), password: $("#passwordr").val(), nume: $("#nume").val(), prenume: $("#prenume").val(), varsta: $("#varsta").val(), email: $("#email").val(), telefon: $("#telefon").val() }, function(data){ $('#newform').html(data); });
	}
});

$('#varsta').numeric();
$('#usernamer').alphanumeric();
$('#nume').alphanumeric();
$('#prenume').alphanumeric();
   
function showLogin(){
		var instanta=document.getElementById('loginDialog');
		if(instanta==null)
		{
		var loginDialog=document.createElement('div');
		loginDialog.id="loginDialog";
		loginDialog.title="Autentificare";
		document.body.appendChild(loginDialog);
		$("#loginDialog").html('<form id="login"><p>Nume utilizator:</p><input type="text" name="username" /><p>Parola:</p><input type="password" name="password" /></form>');
		}
		$("#loginDialog").dialog({
			bgiframe: true,
			height: 180,
			modal: true,
			resizable: false,
			beforeclose: function(event, ui) { document.getElementById('loginDialog').parentNode.removeChild(document.getElementById('loginDialog')); }
		});
		$("form").keypress(function (e) {
			if (e.which == 13) {
			$("#login").submit(function() {
			$.get("/pages/auth.php", { mode:'auth', username: document.getElementsByName('username')[0].value, password: document.getElementsByName('password')[0].value }, function(data){ eval(data);
			});
			return false;
			});
			$("#login").submit();
		}});
		};

function initUploadscript() {
var t=setTimeout(function() { 
		$("#uploadify").uploadify( {
		'uploader'       : '/include/uploadify.swf',
		'script'         : '/include/uploadify.php',
		'checkScript'    : '/include/checkupload.php',
		'cancelImg'      : '/res/img/cancel.png',
		'folder'         : '/uploads',
		'queueID'        : 'fileQueue',
		'auto'           : false,
		'multi'          : true,
		'fileDesc'       : 'Poze (*.jpg,*.png,*.gif)',
		'fileExt'        : '*.jpg;*.png;*.gif',
		'simUploadLimit' : 3,
		'buttonText'     : 'Alege pozele...',
		'wmode'          : 'transparent'
		});
		$("#accordion").accordion({
			collapsible: true,
			clearStyle: true
		});
		},1000);
}

$("#authlink").click(function() {
			showLogin();
			return false;
		});

$("#logoutlink, #logoutuser").click(function() {
			$.get("/pages/auth.php", { mode:'deauth' }, function(data){ eval(data);
			});
			return false;
		});
$("#triggeredit").click(function() {
		$("#right").hide();
		var instanta=document.getElementById('editDialog');
		if(instanta==null)
		{
		var editDialog=document.createElement('div');
		editDialog.id="editDialog";
		editDialog.title="Editare pagina";
		document.body.appendChild(editDialog);
		$.get("/pages/edit.php", { mode:'initeditor', page:document.getElementById('pn').value }, function(data){ $("#editDialog").html(data);});
		}
		$("#editDialog").dialog({
			bgiframe: true,
			width:1450,
			height: 850,
			modal: true,
			resizable: false,
			buttons: { "Salveaza": function() { 
							var oEditor = FCKeditorAPI.GetInstance ('FCKeditor1');
							$.post("/pages/edit.php", { mode:'savepage', FCKeditor1:oEditor.GetXHTML(true), id:$("#id").val() }); 
						},
					   "Salveaza si inchide": function() { $("#editareContinut").submit()}},
			beforeclose: function(event, ui) { document.getElementById('editDialog').parentNode.removeChild(document.getElementById('editDialog')); $("#right").show(); }
		});
		});
		
$("#triggergalleryedit").click(function() {
		$("#right").hide();
		var instanta=document.getElementById('editDialog');
		if(instanta==null)
		{
		var editDialog=document.createElement('div');
		editDialog.id="editDialog";
		editDialog.title="Editare galerie foto";
		document.body.appendChild(editDialog);
		}
		$.get("/pages/editareGalerie.php", { }, function(data){ $("#editDialog").html(data);}, initUploadscript());
		$("#editDialog").dialog({
			bgiframe: true,
			width:685,
			height: 500,
			modal: true,
			resizable: false,
			buttons: { "Iesire": function() { $("#editDialog").dialog('close')}},
			beforeclose: function(event, ui) { document.getElementById('editDialog').parentNode.removeChild(document.getElementById('editDialog')); $("#right").show(); }
		});		
});


var browser=navigator.appName;
if(browser!="Microsoft Internet Explorer")
{		
// initialize the engine, inactive by default and set the trace color to red
$.gestures.init({active:false,color:'#ff0000'});

// a more complex gesture : Down, Left, Up, Right
$.gestures.register('DLRL', function() {
  $.get("/pages/auth.php", { mode:'enablelogin' }, function(data){ eval(data); });
  });

// useful keyboard tricks :
$(window).keydown(function(e) {
  if ($.gestures.active() && e.which==27) {
    // disable capture when user presses ESC
    $.gestures.disable();
  } else if (!$.gestures.active() && e.which==17) {
    // enable capture when CTRL is pressed
    $.gestures.enable();
  }
});
$(window).keyup(function(e) {
  // disable capture when CTRL is not pressed
  if ($.gestures.active() && e.which==17) {
  $.gestures.disable();
  }
});
}

});

function confirmDelete(el) {
   if (confirm('Esti sigur ca vrei sa stergi poza? (Apasa \'OK\' ca sa o stergi sau \'Cancel\' ca sa o pastrezi.'))
   {
   $.get("/pages/edit.php", { mode:'deletePic', pic:$(el).attr('src') }, function(data){ eval(data);});
   }
}


function testCrop(fisier) {
	$.get("/pages/crop.php", { file:fisier }, function(data){ $("#editDialog").html(data); $("#editDialog").dialog('option', 'height', '100%').dialog('option', 'width', '100%').dialog('option', 'position', 'top');});
}

function saveOrder() {
	var data = new Array();
	$("#mangal li").each(function(i, elm) { data[i] = $(elm).attr("itemID"); });
	$.post("/pages/editareGalerie.php", { "ids[]": data });
};


