var allGalleries = false;
var allParents = {};

$().ready(function() {
	var url = document.location.href.replace(/^(.*?)idorablo.hu.*/,"$1idorablo.hu/");

	$j = $;

	// validate signup form on keyup and submit

	$("#regForm").validate({

		rules: {
			regUser: {
				required: true,
				minLength: 3
			},
			regPass1: {
				required: true,
				minLength: 5
			},
			regPass2: {
				required: true,
				minLength: 5,
				equalTo: "#regPass1"
			},
			regEmail: {
				required: true,
				email: true
			},
			regName: {required: true},
			regNick: {required: true },
			regAgree: { required: true	}
		},
		messages: {
			regUser: {
				required: "Kérlek adj meg egy felhasználónevet!",
				minLength: "A választott felhasználónévnek legalább 3 karakter-hosszúságúnak kell lennie."
			},
			regPass1: {
				required: "Kérlek adj meg egy jelszót!",
				minLength: "A választott jelszónak legalább 5 karakter-hosszúságúnak kell lennie."
			},
			regPass2: {
				required: "Kérlek add meg mégegyszer a választott jelszót, hogy ellenőrizhessük",
				equalTo: "A két jelszó nem azonos."
			},
			regEmail: {
				required: "Kérlek add meg email címed!",
				email: "Kérlek valós emailt adj meg, amire elküldhetjük a regisztráláshoz szükséges adatokat."
			},
			regName:{
				 required: "Kérlek add meg a teljes neved!"
			},
			regNick: "Kérlek add meg a beceneved/nicked amin szeretnéd, hogy szólítsanak!.",
			regAgree: "Az oldalra csak a szabályokat elfogadók regisztrálhatnak"
		}
	});

	$('#uplAddRow').bind("click",function(){
   $('#upPic').clone().insertAfter("#upPic");
 	});



	$.ImageBox.init({

		loaderSRC: '/resources/images/loading.gif',
		closeHTML: '<img src="/resources/images/close.jpg" />',
		textImage: 'Kép',
		textImageFrom: ' - '

	});

/*	if($('#pictures').get().length){
	$('#galleries tbody tr').bind("click",function(){
			var gid = $(this).children(".name").attr("id");
		 $.getJSON("/admin/galleries/pictures/gallery/all/"+gid,
		 	function(json){placeImages(json,"all");}	);
			$('#pictures .tabs h4').removeClass('active');
	});
	$('#allpix').bind("click",function(){
		 $.getJSON("/admin/galleries/pictures/user/all/"+$('#activeuser').attr("value"),
		 	function(json){placeImages(json,"all");}	);
			$('#pictures .tabs h4').removeClass('active');
			$('#galleries tbody tr').removeClass('active');
			$('#allpix').addClass('active');
	});
	$('#unsignedpix').bind("click",function(){
		 $.getJSON("/admin/galleries/pictures/user/unsigned/"+$('#activeuser').attr("value"),
		 	function(json){placeImages(json,"unsigned");}	);
			$('#pictures .tabs h4').removeClass('active');
			$('#galleries tbody tr').removeClass('active');
			$('#unsignedpix').addClass('active');
	});
	$('#signedpix').bind("click",function(){
		 $.getJSON("/admin/galleries/pictures/user/signed/"+$('#activeuser').attr("value"),
		 	function(json){placeImages(json,"signed");}	);
			$('#pictures .tabs h4').removeClass('active');
			$('#galleries tbody tr').removeClass('active');
			$('#signedpix').addClass('active');
	});
	}
	if($('#files').get().length){
	$('#categories tbody tr').bind("click",function(){
			var gid = $(this).children(".name").attr("id");
		 $.getJSON("/admin/files/files/category/all/"+gid,
		 	function(json){placeFiles(json,"all");}	);
			$('#files .tabs h4').removeClass('active');
	});
	$('#allfiles').bind("click",function(){
		 $.getJSON("/admin/files/files/user/all/"+$('#activeuser').attr("value"),
		 	function(json){placeFiles(json,"all");}	);
			$('#files .tabs h4').removeClass('active');
			$('#categories tbody tr').removeClass('active');
			$('#allfiles').addClass('active');
	});
	$('#unsignedfiles').bind("click",function(){
		 $.getJSON("/admin/files/files/user/unsigned/"+$('#activeuser').attr("value"),
		 	function(json){placeFiles(json,"unsigned");}	);
			$('#files .tabs h4').removeClass('active');
			$('#categories tbody tr').removeClass('active');
			$('#unsignedfiles').addClass('active');
	});
	$('#signedfiles').bind("click",function(){
		 $.getJSON("/admin/files/files/user/signed/"+$('#activeuser').attr("value"),
		 	function(json){placeFiles(json,"signed");}	);
			$('#files .tabs h4').removeClass('active');
			$('#categories tbody tr').removeClass('active');
			$('#signedfiles').addClass('active');
	});
	}*/


	$('#msgBody').bind("keyup",function(){
		if($('#msgBody').val().length >150){
			$('#msgBody').val($('#msgBody').val().substr(0,150));

		}

	});

	if($('textarea.wymeditor').get().length){
		 $('.wymeditor').wymeditor({

      //options
      lang: 'en',
      //classes panel
      /*aClassesItems: [
        {'name': 'date', 'title': 'PARA: Date', 'expr': 'p'},
        {'name': 'hidden-note', 'title': 'PARA: Hidden note',
         'expr': 'p[@class!="important"]'},
        {'name': 'important', 'title': 'PARA: Important',
         'expr': 'p[@class!="hidden-note"]'},
        {'name': 'border', 'title': 'IMG: Border', 'expr': 'img'},
        {'name': 'special', 'title': 'LIST: Special', 'expr': 'ul, ol'}
      ],

      //editor css values for visual feedback
      aEditorCss: [
        {'name': '.hidden-note',
         'css': 'color: #999; border: 2px solid #ccc;'},
        {'name': '.border', 'css': 'border: 4px solid #ccc;'},
        {'name': '.date',
         'css': 'background-color: #ff9; border: 2px solid #ee9;'},
        {'name': '.important',
         'css': 'color: red; font-weight: bold; border: 2px solid red;'},
        {'name': '.special',
         'css': 'background-color: #fc9; border: 2px solid red;'}
      ],

      //dialogs css values
      aDialogCss: [
        {'name': '.hidden-note',
         'css': 'display: none;'},
        {'name': '.border', 'css': 'border: 1px solid #ccc;'},
        {'name': '.date',
         'css': 'color: #ccf;'},
        {'name': '.important',
         'css': 'color: red; font-weight: bold;'},
        {'name': '.special',
         'css': 'color: green;'}
      ],*/

      //function called when WYMeditor instance is ready
      //wym is the WYMeditor instance
      postInit: function(wym) {

        //set the editor's HTML value
        wym.html('');

        //set the status bar value
        wym.status('&nbsp;');

        //activate 'tidy' plugin
        //which cleans up the HTML
        var wymtidy = wym.tidy({

          sUrl:          "wymeditor/tags/0.3-b1/wymeditor/plugins/tidy/tidy.php",
          sButtonHtml:   "<li class='wym_tools_tidy'>"
                       + "<a name='CleanUp' href='#'"
                       + " style='background-image:"
                       + " url(wymeditor/tags/0.3-b1/wymeditor/plugins/tidy/wand.png)'>"
                       + "Clean up HTML"
                       + "<\/a><\/li>"
        });
        wymtidy.init();

        //activate 'hovertools' plugin
        //which gives more feedback to the user
        wym.hovertools();

      },

      //function called when dialog is ready
      //wym is the WYMeditor instance
      //wdw is the dialog's window
      postInitDialog: function(wym,wdw) {

        var body = wdw.document.body;

        //add a select box populated with predefined values to the dialog

        var sSelectLink = "<div class='row row-indent'>"
        + "<select class='wym_select_link'>"
        + "<option selected value='Idorablo http://www.idorablo.hu/'>"
        + "Idorablo<\/option>"
        + "<\/select>"
        + "<input class='wym_choose' type='button'"
        + " value='{Choose}' />"
        + "<\/div>";

        $(body)
          .filter('.wym_dialog_link').find('fieldset').eq(0)
          .prepend(wym.replaceStrings(sSelectLink));

        $(body)
          .find('.wym_choose')
          .click(function() {

            var sVal = $(body).find('.wym_select_link').val();

            $(body)
              .find('.wym_href')
              .val(sVal.substring(sVal.lastIndexOf(' ') + 1));
            $(body)
              .find('.wym_title')
              .val(sVal.substr(0, sVal.lastIndexOf(' ')));
          });

      }
    });


	}

	$('#selectInverse').click( function(){

		$('#usermanage :checkbox').each(function(){

			if($(this).attr("checked")){
				$(this).removeAttr("checked");
			}else{
				$(this).attr("checked","checked");
			}

		});

	});

	$('#usermanage tr td').click(function(){

		if($(':checkbox',this).length == 0){

			_cb = $(':checkbox',$(this).parent());

			if(_cb.attr("checked")){
				_cb.removeAttr("checked");
			}else{
				_cb.attr("checked","checked");
			}

		}

	});

	$('#newslist').change(function(){

		var _s = $(this);

		document.location.href = url + 'admin/pageditor/new/'+_s.val();

	});


	$('.dirtree.manage li.c').click(function(){


		if($(this).hasClass('selected')){

			$(this).removeClass('selected');

			deselectDirectory();

		}else{

			$('.dirtree li.c').removeClass('selected');

			$(this).addClass('selected');

			selectDirectory(this);

		}
		return false;

	});


	$('.dirtree.manage li.c > a').click(function(){

/*		if($(this).hasClass('selected')){

			$(this).removeClass('selected');

			deselectDirectory();

		}else{

			$('.dirtree li.c').removeClass('selected');

			$(this).addClass('selected');

			selectDirectory(this);

		}
*/		return false;

	});




	$('.tree > li.p').bind("click",function(){

		_galleryId = $('i',$(this)).text();

		$('.dirtree').addClass('offscreen');

		$('.dirtree',$(this)).removeClass('offscreen');

		deselectDirectory();

		$("#selectedGalleryId").attr("rel",$('i',_galleryId));

		$("#inpSelectedGalleryId").val(_galleryId);
		$("#inpSelectedGalleryParent").val(_galleryId);

		return false;

	});

	$('.tree .moveup').click(function(){

    document.location.href = url+'admin/uploaded/manage/move/u/'+$(this).parent().prev('u').text();
		return false;

	});

	$('.tree .movedown').click(function(){

		document.location.href = url+'admin/uploaded/manage/move/d/'+$(this).parent().prev('u').text();
		return false;

	});

	$('#moveToGal').change(function(){

	  if($(this).val())this.form.submit();

	});

	$('#selectGal').change(function(){

	  document.location.href = url+'admin/uploaded/edit/gal/'+$(this).val();

	});

	$('#selectType').change(function(){

	  $('#usermanage tbody tr').addClass('offscreen');

	  var type = $(this).val();

	  $('#usermanage tbody tr.'+type).removeClass('offscreen');


	});


	$('td a.play').click(function(){

	  td = $(this).parent();
    tr = td.parent();
    tbody = tr.parent();
    file = $(this).attr('rel');


	  $('#flash',tbody).remove()

      tr.after('<tr><td colspan="4" id="flash"><a href="http://www.macromedia.com/go/getflashplayer">Get Flash</a> to see this player.</td></tr>');

      tr.after(
       '<script type="text/javascript">'+
        'var s1 = new SWFObject("http://www.idorablo.hu/modules/flvplayer/phpsflvplayer.swf","iwflash","320","260","9", "#FFFFFF");'+
        's1.useExpressInstall("http://www.idorablo.hu/modules/flvplayer/playerProductInstall.swf");'+
        's1.setAttribute( "xiRedirectUrl", window.location.href );'+
        's1.addVariable("file","'+file+'/.flv");'+
        's1.addVariable("streamscript","http://www.idorablo.hu/page/flvprovider.php");'+
        's1.addVariable("bufferlength","1.2");'+
        's1.addVariable("displaywidth","320");'+
        's1.addVariable("displayheight","240");'+
        's1.addVariable("autostart","false");'+
        's1.addVariable("showdigits","true");'+
        's1.addParam("allowfullscreen","true");'+
        's1.addVariable("showfsbutton","true");'+
        's1.write("flash");'+
      '</script>'

      );

	});



	$('td a.playmp3').click(function(){

	  td = $(this).parent();
    tr = td.parent();
    tbody = tr.parent();
    file = $(this).attr('rel');


	  $('#player3',tbody).remove()

      tr.after('<tr><td colspan="4" id="player3"><a href="http://www.macromedia.com/go/getflashplayer">Get Flash</a> to see this player.</td></tr>');

      tr.after(
       '<script type="text/javascript">'+
        'var s3 = new SWFObject("http://www.idorablo.hu/modules/mp3player/mp3player.swf", "line", "520", "20", "7");'+
        's3.addVariable("file","'+file+'/.mp3");'+
        's3.addVariable("repeat","true");'+
        's3.addVariable("showdigits","false");'+
        's3.addVariable("showdownload","true");'+
        's3.addVariable("width","520");'+
        's3.addVariable("height","20");'+
        's3.write("player3");'+
      '</script>'

      );

	});



	var ltie7 = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);
	if(ltie7){
		$('.ngeencontainer.messageboard.open').css({
			floatStyle: 'right',
			width: '150px',
			margin: '0 0 0 2px'
		});
		$('.ngeencontainer.messageboard.open textarea').css({
			width: '147px'
		});
	}
});



