$(function() {
//pentru expand la makis
$(".activare").click(function() {
	var id = $(this).attr("name");

	if ($("#"+id).css('display') == 'none')
		$("#"+id).show();
	else
		$("#"+id).hide();
});


//dam disable la selectare de text si la click drapta si la ctrl+c pentru stiri
$.extend($.fn.disableTextSelect = function() {
		return this.each(function(){
			if($.browser.mozilla){//Firefox
				$(this).css('MozUserSelect','none');
			}else if($.browser.msie){//IE
				$(this).bind('selectstart',function(){return false;});
			}else{//Opera, etc.
				$(this).mousedown(function(){return false;});
			}
		});
});

$('.stire').disableTextSelect();
$('.stire').bind("contextmenu",function(e){
              return false;
}); 
//end disable
$("a[href$='pdf']:not(:has(p[class='roundedfile'])),a[href$='doc']:not(:has(p[class='roundedfile'])),a[href$='docx']:not(:has(p[class='roundedfile'])),a[href$='xlx']:not(:has(p[class='roundedfile'])),a[href$='xlsx']:not(:has(p[class='roundedfile'])),a[href$='ppt']:not(:has(p[class='roundedfile']))").each(function(){
    $(this).addClass('dlfile');
});

$("ul.sf-menu").superfish();

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"><fieldset><label for="username">Nume utilizator</label><input type="text" name="username" id="username" class="text ui-widget-content ui-corner-all" /><label for="password">Parola</label><input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" /></fieldset></form>');
		}
		$("#loginDialog").dialog({
			bgiframe: true,
			height: 120,
			modal: true,
			resizable: false,
			beforeclose: function(event, ui) { $("#loginDialog").dialog('destroy'); 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);
}

$("#search").keypress(function (e) {
		if (e.which == 13) {
			window.location = "/search/" + $("#search").val() + "/";
		}
});

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

$("#logoutlink").click(function() {
			$.get("/pages/auth.php", { mode:'deauth' }, function(data){ eval(data);
			});
			return false;
		});
		
$("#editpage").click(function() {
		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:685,
			//height:500,
			width:'100%',
			height:650,
			modal: true,
			resizable: false,
			buttons: { "Salveaza": function() { 
							var oEditor = FCKeditorAPI.GetInstance ('FCKeditor1');
							$.post("/pages/edit.php", { mode:'savepage', FCKeditor1:oEditor.GetXHTML(true), id:$("#pageid").val(), title:$("#title").val(), menuentry:$("#menuentry").val(), dependenta:$("#dependenta").val(), cat:$("#cat").val(), tags:$("#tags").val() }); 
						}, 
						"Salveaza si inchide": function() { 
							$("#editareContinut").submit()}, 
						"Sterge pagina": function() { 
					var instanta = document.getElementById('confirmDialog');
					if (instanta == null) {
						var confirmDialog=document.createElement('div');
						confirmDialog.id="confirmDialog";
						confirmDialog.title="Confirmare";
						document.body.appendChild(confirmDialog);
						$("#confirmDialog").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Esti sigur ca vrei sa stergi aceasta pagina definitiv?</p>');
					}
					$("#confirmDialog").dialog({
						resizable: false,
						height:140,
						modal: true,
						buttons: {
							'Sterge': function() {
								$.get("/pages/edit.php", { mode:'deletepage', page:$("#pageid").val() }, function(data){ eval(data);});
								$(this).dialog('destroy');
								document.getElementById('confirmDialog').parentNode.removeChild(document.getElementById('confirmDialog'));
							},
							'Renunta': function() {
								$(this).dialog('destroy');
								document.getElementById('confirmDialog').parentNode.removeChild(document.getElementById('confirmDialog'));
							}
						},
						beforeclose: function(event, ui) { $("#confirmDialog").dialog('destroy'); document.getElementById('confirmDialog').parentNode.removeChild(document.getElementById('confirmDialog')); }
					});
			}},
			beforeclose: function(event, ui) { $("#editDialog").dialog('destroy'); document.getElementById('editDialog').parentNode.removeChild(document.getElementById('editDialog')); }
		});
});

$("#newpage").click(function() {
		var instanta=document.getElementById('newpageDialog');
		if(instanta==null)
		{
		var newpageDialog=document.createElement('div');
		newpageDialog.id="newpageDialog";
		newpageDialog.title="Pagina noua";
		document.body.appendChild(newpageDialog);
		$.get("/pages/edit.php", { mode:'initnewpage', page:document.getElementById('pn').value }, function(data){ $("#newpageDialog").html(data);});
		}
		$("#newpageDialog").dialog({
			bgiframe: true,
			width:'100%', //600
			height: 650,
			modal: true,
			resizable: false,
			buttons: { "Salvare": function() { 
				if ($('#cat').attr('checked')) {
					$.get("/pages/edit.php", { mode:'savenewpage', name:$('#title').val(), mentry:$('#menuentry').val(), tags:$('#tags').val(), depid:document.getElementById('dependenta').value, cat:"1", lang:$('#limba').val() }, function(data){ eval(data);});
				} else {
					$.get("/pages/edit.php", { mode:'savenewpage', name:$('#title').val(), mentry:$('#menuentry').val(), tags:$('#tags').val(), depid:document.getElementById('dependenta').value, lang:$('#limba').val() }, function(data){ eval(data);});
				}
			}, "Renuntare": function() { $("#newpageDialog").dialog('destroy'); document.getElementById('newpageDialog').parentNode.removeChild(document.getElementById('newpageDialog')); } },
			beforeclose: function(event, ui) { document.getElementById('newpageDialog').parentNode.removeChild(document.getElementById('newpageDialog')); }
		});
});

$("#newnews").click(function() {
		var instanta=document.getElementById('newnewsDialog');
		if(instanta==null)
		{
		var newpageDialog=document.createElement('div');
		newpageDialog.id="newnewsDialog";
		newpageDialog.title="Stire noua";
		document.body.appendChild(newpageDialog);
		$.get("/pages/edit.php", { mode:'initnewnews' }, function(data){ $("#newnewsDialog").html(data);});
		}
		$("#newnewsDialog").dialog({
			bgiframe: true,
			width: '100%', //670,
			height: 700, //440,
			modal: true,
			resizable: false,
			buttons: { "Salvare": function() { 
				var oEditor = FCKeditorAPI.GetInstance ('FCKeditor1');
							$.post("/pages/edit.php", { mode:'savenewnews', FCKeditor1:oEditor.GetXHTML(true), title:$("#title").val(), date:$("#date").val(), lang:$("#limba").val() }, function(data) { eval(data); }); 
			}, "Renuntare": function() { $("#newnewsDialog").dialog('destroy'); document.getElementById('newnewsDialog').parentNode.removeChild(document.getElementById('newnewsDialog')); } },
			beforeclose: function(event, ui) { document.getElementById('newnewsDialog').parentNode.removeChild(document.getElementById('newnewsDialog')); }
		});
});

$("#editnews").click(function() {
		var instanta = document.getElementById('editDialog');
		if (instanta == null) {
			var editDialog=document.createElement('div');
			editDialog.id="editDialog";
			editDialog.title="Editare stire";
			document.body.appendChild(editDialog);
			$.get("/pages/edit.php", { mode:'initeditnews', nid:document.getElementById('sid').value }, function(data){ $("#editDialog").html(data);});
		}
		$("#editDialog").dialog({
			bgiframe: true,
			//width:685,
			//height:500,
			width: '100%', //670,
			height:700, //440,
			modal: true,
			resizable: false,
			buttons: { "Salveaza": function() { 
							var oEditor = FCKeditorAPI.GetInstance ('FCKeditor1');
							$.post("/pages/edit.php", { mode:'savenews', FCKeditor1:oEditor.GetXHTML(true), nid:$("#nid").val(), title:$("#title").val(), date:$("#date").val() }, function(data) { eval(data); }); 
						}, 
						"Sterge stirea": function() { 
					var instanta = document.getElementById('confirmDialog');
					if (instanta == null) {
						var confirmDialog=document.createElement('div');
						confirmDialog.id="confirmDialog";
						confirmDialog.title="Confirmare";
						document.body.appendChild(confirmDialog);
						$("#confirmDialog").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Esti sigur ca vrei sa stergi aceasta stire definitiv?</p>');
					}
					$("#confirmDialog").dialog({
						resizable: false,
						height:140,
						modal: true,
						buttons: {
							'Sterge': function() {
								$.get("/pages/edit.php", { mode:'deletestire', sid:$("#nid").val() }, function(data){ eval(data);});
								$(this).dialog('destroy');
								document.getElementById('confirmDialog').parentNode.removeChild(document.getElementById('confirmDialog'));
							},
							'Renunta': function() {
								$(this).dialog('destroy');
								document.getElementById('confirmDialog').parentNode.removeChild(document.getElementById('confirmDialog'));
							}
						},
						beforeclose: function(event, ui) { $("#confirmDialog").dialog('destroy'); document.getElementById('confirmDialog').parentNode.removeChild(document.getElementById('confirmDialog')); }
					});
			}},
			beforeclose: function(event, ui) { $("#editDialog").dialog('destroy'); document.getElementById('editDialog').parentNode.removeChild(document.getElementById('editDialog')); }
		});
});

});


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 confirmDeleteMeniu(tip,id) {
   var cesastergi='';
   switch(tip)
   {
   case "cat":
   cesastergi='categoria';
   break;
   case "prod":
   cesastergi='produsul';
   break;
   case "descr":
   cesastergi='descrierea';
   break;
   }
   if (confirm('Esti sigur ca vrei sa stergi '+cesastergi+'? (Apasa \'OK\' ca sa o stergi sau \'Cancel\' ca sa o pastrezi.'))
   {
   $.get("pages/edit.php", { mode:'delete'+cesastergi, id:id }, function(data) { location.reload(); });
   }
}


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 });
};

function disableCtrlKeyCombination(e)
{
        //list all CTRL + key combinations you want to disable
        var forbiddenKeys = new Array('a', 'n', 'c', 'x', 'j');
        var key;
        var isCtrl;

        if(window.event)
        {
                key = window.event.keyCode;     //IE
                if(window.event.ctrlKey)
                        isCtrl = true;
                else
                        isCtrl = false;
        }
        else
        {
                key = e.which;     //firefox
                if(e.ctrlKey)
                        isCtrl = true;
                else
                        isCtrl = false;
        }

        //if ctrl is pressed check if other key is in forbidenKeys array
        if(isCtrl)
        {
                for(i=0; i<forbiddenKeys.length; i++)
                {
                        //case-insensitive comparation
                        if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
                        {
                                alert('Combinatia de taste CTRL + ' + String.fromCharCode(key) +' a fost dezactivata pentru acest site.');
                                return false;
                        }
                }
        }
        return true;
}


function expand(id) {
	var obj = document.getElementById(id);
	if (obj.style.display == 'none') { obj.style.display = ''; }
	else { obj.style.display = 'none'; }
}

