// JavaScript Document

var timeout       = 500;
var closetimer		= 0;
var ddmenuitem    = 0;
var ddmenulink    = 0;
var aColor        = null;
var pColor        = null;

function jsddm_open() {	
  jsddm_canceltimer();
	jsddm_close();
	$('div.top_menu').css('background-color', pColor);
	$(this).css('background-color', aColor);
	ddmenuitem = $('#sub_'+$(this).find('a').eq(0).attr('id'));
	ddmenuitem.css({visibility: 'visible', display: 'block'});
}
function jsddm_sopen() {	
  jsddm_canceltimer();
	jsddm_close();
	$('div.top_menu').css('background-color', pColor);
	$('#menu_'+$(this).attr('rel')).parent().parent().css('background-color', aColor);
	ddmenuitem = $(this);
	ddmenuitem.css({visibility: 'visible', display: 'block'});
}

function jsddm_close() {	
	$('div.top_menu').css('background-color', pColor);
	$('#active_menu').css('background-color', aColor);
  if (ddmenuitem) ddmenuitem.css({visibility: 'hidden', display: 'none'});
}

function jsddm_timer()
{	closetimer = window.setTimeout(jsddm_close, timeout*4);}
function jsddm_stimer()
{	closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{	if(closetimer) {	
    window.clearTimeout(closetimer);
		closetimer = null;
  }
}

$(document).ready(function(){
  aColor = $('#top_sub_menu').css('background-color');
  pColor = $('#passive_menu').css('background-color');

  $('div.top_menu').bind('mouseover', jsddm_open);
  $('div.top_menu').bind('mouseout', jsddm_timer);
  $('div.sub_menu').bind('mouseover', jsddm_sopen);
  $('div.sub_menu').bind('mouseout', jsddm_stimer);
  
  $('input.rules').change(function() {
    if ($(this).attr('checked')) {
      $('#tr_frm_sbmt').css('visibility', 'visible');
    } else {
      $('#tr_frm_sbmt').css('visibility', 'hidden');
    }
  });
  
  $('#partition').change(function(){
    var id = $(this).val();
    if (id < 1) {
      $('#fandoms').load('ssajax.php', {'do': 'partition', 'id': 0, 'code': 'top sicret'});
      $('#fandoms').attr('disabled', true);
      $('select.perss').attr('disabled', true);
      $('#frm_sbmt').attr('disabled', true);
    } else {
      $('#fandoms').load('ssajax.php', {'do': 'partition', 'id': id, 'code': 'top sicret'}, function(){
        if ($('#fandoms > option').length > 1) {
          $('#fandoms').removeAttr('disabled');
          $('select.perss').attr('disabled', true);
          $('#frm_sbmt').removeAttr('disabled');
        } else {
          $('#fandoms').attr('disabled', true);
          $('#fandoms').trigger('change');
        }    
      });
    }
  });
  
  $('#fandoms').change(function(){
    var id = $(this).val();
    $('select.perss').each(function(){
      var rel = $(this).attr('rel');
      $(this).load('ssajax.php', {'do': 'fandom', 'id': id, 'code': 'top sicret', 'add': rel}, function(){
        $(this).removeAttr('disabled');
      });
    });
    $('#frm_sbmt').removeAttr('disabled');
  });
  
  $('#nf_partition').change(function(){
    var id = $(this).val();
    if (id < 1) {
      $('#nf_fandoms').load('ssajax.php', {'do': 'partition', 'id': 0, 'code': 'top sicret'});
      $('#nf_fandoms').attr('disabled', true);
      $('select.perss').attr('disabled', true);
      $('#frm_sbmt').attr('disabled', true);
    } else {
      $('#nf_fandoms').load('ssajax.php', {'do': 'partition', 'id': id, 'code': 'top sicret'}, function(){
        if ($('#nf_fandoms > option').length > 1) {
          $('#nf_fandoms').removeAttr('disabled');
          $('select.perss').attr('disabled', true);
          $('#frm_sbmt').attr('disabled', true);
        } else {
          $('#nf_fandoms').attr('disabled', true);
          $('#nf_fandoms').trigger('change');
        }    
      });
    }
  });
  
  $('#nf_fandoms').change(function(){
    var id = $(this).val();
    $('select.perss').each(function(){
      $(this).load('ssajax.php', {'do': 'newfic', 'id': id, 'code': 'top sicret'}, function(){
        $(this).removeAttr('disabled');
      });
    });
    $('#frm_sbmt').removeAttr('disabled');
  });
  
  $('select.perss').change(function(){
    if ($(this).val() < 0) {
      $("span[name='"+$(this).attr('name')+"_other']").css('display', '');
    } else {
      $("span[name='"+$(this).attr('name')+"_other']").css('display', 'none');
    }
  });

  var menuHeight = 0;
  $('div.top_info').each(function() {
    var cHeight;
    if ($.browser.msie && $.browser.version < 7) {
      cHeight = 150;
    } else {
      cHeight = $(this).innerHeight();
    }
    if (cHeight > menuHeight) menuHeight = cHeight;
  });  
  $('div.info_cnt').css('height', (menuHeight-14));
  $('div.top_info').css('height', menuHeight);
  $('#tbl_informer').css('height', menuHeight);
  
  $('#addFav').jFav();
});
