$(document).ready( function() {

  $('a.fancy').fancybox({
    'hideOnContentClick': false,
    'frameWidth': 600,
    'frameHeight': 450,
    'callbackOnShow': function(){$('#c_text').focus();}
  });
  

  $('.tooltip').wTooltip({
    className: "wtooltip",
    style: false
  });


  $('.colorpick').ColorPicker({
    onSubmit: function(hsb, hex, rgb, el) {
  		$(el).val('#' + hex);
  		$(el).ColorPickerHide();
        $('#preview-' + $(el).attr('id')).css('background-color', '#' + hex);

  	},
  	onBeforeShow: function () {
  		$(this).ColorPickerSetColor(this.value);
  	}
  });
  


  $('#hide-int-info').click(function() {
    $.ajax({
      url: './?hide=hide_int_info',
      cache: false,
      success: function(html){
        $('#int-info').hide(750);
      }
    });
  });
  $('#hide-config-info').click(function() {
    $.ajax({
      url: './?hide=hide_conf_info',
      cache: false,
      success: function(html){
        $('#config-info').hide(750);
      }
    });
  });
  $('#lang-img').click(function() {$('#lang-select').slideToggle(500);});
  $('#chmod-help-toggle').click(function() {$('#install-list').slideToggle(750);});
  $('#p-tags-viewer').click(function() {
    $('#tags-in').slideToggle(750);
    $('#new_tag').val('');
    $('#new_tag').focus();
  });
  $('#p-info-viewer').click(function() {$('#info-in').slideToggle(750);});
  $('#p-settings-viewer').click(function() {$('#settings-in').slideToggle(750);});
  $('#page-key-viewer').click(function() {$('#page-key').slideToggle(750);});
  $('#post-full-story-viewer').click(function() {$('#full-story-wrap').slideToggle(750);});

  $('#post-img-add-cat').click(function() {
    $('#post-add-cat').show(500);
    $('#add_cat').focus();
  });
  $('#post-add-cat-hide').click(function() {$('#post-add-cat').hide(500);});
  
  $('#smiles-toggle-short_story').click(function() {$('#simple-format-short_story').slideToggle();});
  $('#simple-format-short_story img').click(function() {$('#simple-format-short_story').slideToggle();});
  $('#smiles-toggle-full_story').click(function() {$('#simple-format-full_story').slideToggle();});
  $('#simple-format-full_story img').click(function() {$('#simple-format-full_story').slideToggle();});
  $('#smiles-toggle-text').click(function() {$('#simple-format-text').slideToggle();});
  $('#simple-format-text img').click(function() {$('#simple-format-text').slideToggle();});
  $('#personal-info-toggle').click(function() {
    $('#personal-info').slideToggle(1000);
    $('#contact-info').slideToggle(1000);
    $('#other-info').slideToggle(1000);
  });
  $('#admin-info-toggle').click(function() {$('#admin-info').slideToggle(1000);});
  $('#view-comment-info').click(function() {$('#comment-info').slideToggle(750);});
  $('#filter-viewer').click(function() {$('\.filter').slideToggle(750);});
  $('#comments_bb1').click(function() {$('#bb-buttons-viewer').show(750);});
  $('#comments_bb2').click(function() {
    $('#bb-buttons-viewer').hide(750);
    $('#bb-buttons').hide(750);
  });
  $('#bb-buttons-viewer').click(function() {$('#bb-buttons').slideToggle(750);});
  $('#page-pass-on').click(function() {
    $('#page-pass').show();
    $('#pass').focus();
  });
  $('#page-pass-off').click(function() {$('#page-pass').hide();});
  $('#imgupload-starter').click(function() {$('#imgupload').fileUploadStart();});
  
  $("input.file-link").focus(function(){
    this.select();
});
  
  $('#email').focus(function() {$('#profile-old-pass').show();});
  $('#pass1').focus(function() {
    $('#profile-old-pass').show();
    $('#tr-pass2').show();
  });
  
  $('#tmpl-title-toggle').click(function() {$('#tmpl-title').slideToggle(450);});
  $('#tmpl-links-toggle').click(function() {$('#tmpl-links').slideToggle(450);});
  $('#tmpl-author-toggle').click(function() {$('#tmpl-author').slideToggle(450);});
  $('#tmpl-comments-toggle').click(function() {$('#tmpl-comments').slideToggle(450);});
  $('#tmpl-avatar-toggle').click(function() {$('#tmpl-avatar').slideToggle(450);});
  $('#tmpl-cat-toggle').click(function() {$('#tmpl-cat').slideToggle(450);});
  $('#tmpl-image-toggle').click(function() {$('#tmpl-image').slideToggle(450);});
  $('#tmpl-tags-toggle').click(function() {$('#tmpl-tags').slideToggle(450);});
  $('#tmpl-date-toggle').click(function() {$('#tmpl-date').slideToggle(450);});
  $('#tmpl-text-toggle').click(function() {$('#tmpl-text').slideToggle(450);});
  $('#tmpl-time-toggle').click(function() {$('#tmpl-time').slideToggle(450);});

  $('#tmpl_group1').click(function() {$('#group_mode1').attr('checked', 'checked');});
  $('#tmpl_group2').click(function() {$('#group_mode2').attr('checked', 'checked');});
  $('#group_mode2').click(function() {$('#tmpl_group2').focus();});
  
  
  
  
  if (document.location.hash) {
    $(document.location.hash).css('background', '#ffff93');
  }







  // Manage click events
  $('a.ajaxcall').click(function(e) {

    e.preventDefault();

    var link = $(this).attr('href');
    
    
    // Function getUrlVars()
    // @Source: http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
    function getUrlVars() {
      var vars = [], hash;
      var hashes = link.slice(link.indexOf('?') + 1).split('&');
      for(var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
      }
      return vars;
    }
    
    var msg = getUrlVars()['msg'];
    var status = getUrlVars()['s'];
    var comm_id = getUrlVars()['c'];
    var from = getUrlVars()['from'];
    
    $('#c' + comm_id + ' td \.comment_status').replaceWith('<span class="comment_status"><img src="./stuff/img/ajax-loader.gif" alt="" /><br /></span>');
    
    // ajax call
    $.ajax({
      url: link + '&ajaxcall',
      cache: false,
      success: function(html){
        if (from == 'trash' || status == '0') {
          if (from == 'index') {
            $('#c' + comm_id + ' td').css({'background-color' : '#df5757'});
            setTimeout('$("#c' + comm_id + ' td").hide();', 250);
          }
          else if (from == 'post') {
            $('#c-' + comm_id).hide(300);
          }
          else {
            var trashint = parseInt($("#trash_count").html(),10)
            var commint = parseInt($("#comm_count").html(),10)
            if (status == '0') trashint++;
            else trashint--;
            commint--;
            $("#trash_count").html(trashint);
            $("#comm_count").html(commint);
            $('#c' + comm_id + ' td').hide(300);
          }
        }
        else {
          if (status == '1') {
            $('#c' + comm_id + ' td \.comment_status').replaceWith('<span class="comment_status status_' + status + '"><img src="./stuff/img/icons/ok.png" alt="" /> ' + msg + '<br /></span>');
          }
          else {
            $('#c' + comm_id + ' td \.comment_status').replaceWith('<span class="comment_status status_' + status + '"><img src="./stuff/img/icons/exclamation.png" alt="" /> ' + msg + '<br /></span>');
          }

          $('#c' + comm_id + ' td p\.process-links').hide();
          $('#c' + comm_id + ' td input\.checkbox').attr('checked', false);
        }
      }
    });

  });

  
  
  
  
  
  /* Add category via ajax */
  $('#catajax').click(function() {
    if ($('#add_cat').val() != '') {
      $.ajax({
        url: './mn-categories.php?action=ajaxcall&cat_name=' + $('#add_cat').val(),
        cache: false,
        success: function(html){
          $('#cat').append(html);
          $('#post-img-add-cat').attr('src', './stuff/img/icons/tick.png');
        }
      });
      $('#add_cat').val('');
      $('#post-add-cat').hide(500);
    }
    else {
      $('#add_cat').focus();
    }
  });

  $('#add_cat').keypress(function(e) {
    var k = e.keyCode || e.which;
    if (k == 13 && $('#add_cat').val() != '') {
      e.preventDefault();
    	$.ajax({
        url: './mn-categories.php?action=ajaxcall&cat_name=' + $('#add_cat').val(),
        cache: false,
        success: function(html){
          $('#cat').append(html);
          $('#post-img-add-cat').attr('src', './stuff/img/icons/tick.png');
        }
      });
      $('#add_cat').val('');
      $('#post-add-cat').hide(500);
    }
    else {
      $('#add_cat').focus();
    }
  });
  
  
  
  
  /* Add tag via ajax */
  $('#tagajax').click(function() {
    $.ajax({
      url: './mn-tags.php?action=ajaxcall&tag_name=' + $('#new_tag').val(),
      cache: false,
      success: function(html){
        $('\.tags').append(html);
      }
    });
    $('#new_tag').val('');
    $('#new_tag').focus();
  });

  $('#new_tag').keypress(function(e) {
    var k = e.keyCode || e.which;
    if (k == 13) {
      e.preventDefault();
    	$.ajax({
        url: './mn-tags.php?action=ajaxcall&tag_name=' + $('#new_tag').val(),
        cache: false,
        success: function(html){
          $('\.tags').append(html);
        }
      });
      $('#new_tag').val('');
      $('#new_tag').focus();
    }
  });



  $('#select-all').click(function(){
		var checked_status = this.checked;
		$('\.input-tag').each(function(){
			this.checked = checked_status;
		});
	});

});
