var CTRL = false;
var SHIFT = false;

var Message = {
  show: function(text,status){
    $('#message').text(text);
    if(status){
      switch(status){
        case "error":
          $('#message').addClass('state-error');
          break;
      }
    }
    $('#loading').hide();
    $('#message').fadeIn();
  },
  hide: function(){
    $('#loading').hide();
  }
};

var Loading = {
  show: function(){
    $('#loading').show();
    $('#message').hide();
  },
  hide: function(){
    $('#loading').hide();
  }
}

var Dialog = {
  dialog_container: $('#zorn-dialog'),
  origWidth: 0,
  origHeight: 0,
  show: function(title,html_text,options){
    if(options){
      if(options.height){
        this.origHeight = options.height;
        this.setHeight(options.height);
      }
      if(options.width){
        this.origWidth = options.width;
        this.setWidth(options.width);
      }
    }
    $('#zorn-dialog .title').text(title);
    $('#zorn-dialog .content').html(html_text);
    $('#fuzz_bg').height($(document).height());
    $('#fuzz_bg').show();
    $('#zorn-dialog').fadeIn();
    setTimeout(function(){$('#zorn-dialog input[type=text]:first').focus();},100);
  },
  hide: function(){
    $('#fuzz_bg').hide()
    $('#zorn-dialog').fadeOut();
  },
  setHeight: function(newheight){
    $(this.dialog_container).height(newheight);
    var margintop = (newheight/2 > $(window).height()/2) ? ($(window).height()/2-50) : newheight/2;
    $(this.dialog_container).css('marginTop','-'+margintop+'px');
  },
  setWidth: function(newwidth){
    $(this.dialog_container).width(newwidth);
    $(this.dialog_container).css('marginLeft','-'+newwidth/2+'px');
  }
}

var DateTime = {
	day: '',
	month: '',
	year: '',
	hours: '',
	mins: '',
	secs: '',
	init: function(){
		d = new Date();
		day = d.getDay().toString();
		if (day.length < 2) 
			day = '0' + day;
		this.day = day;
		month = d.getMonth().toString();
		if (month.length < 2) 
			month = '0' + month;
		this.month = month;
		this.year = d.getFullYear();
		hours = d.getHours().toString();
		if (hours.length < 2) 
			hours = '0' + hours;
		this.hours = hours;
		mins = d.getMinutes().toString();
		if (mins.length < 2) 
			mins = '0' + mins;
		this.mins = mins;
        secs = d.getSeconds().toString();
        if (secs.length < 2) 
            secs = '0' + secs;
        this.secs = secs;
	},
	time: function(){
		this.init();
		return this.hours + ':' + this.mins + ':' + this.secs;
	},
	date: function(){
		this.init();
		return this.day + '.' + this.month + '.' + this.year;
	},
	datetime: function(){
		this.init();
        return this.day + '.' + this.month + '.' + this.year +
		       ' ' + 
		       this.hours + ':' + this.mins + ':' + this.secs;
	}
}

jQuery.fn.zorntabs = function(options){
  var tabs = $('<ul/>').addClass('zorntabs');
  var counter = 0;
  $(this).find('h1').each(function(){
    $(tabs).append('<li id="'+counter+'">'+$(this).text()+'</li>');
    $($(this).next('div')).attr('id','tab_'+counter)
                        .addClass('zorntab')
                        .hide();
    $(this).remove();
    counter++;
  });
  $(this).before(tabs);
  
  var active_tab = options.active || 0;
  
  $('#tab_'+active_tab).show();
  $('li#'+active_tab).addClass('selected');
  
  // EVENT HANDLING
  $(tabs).children('li').click(function(){
    $('.zorntab').hide();
    $('#tab_'+$(this).attr('id')).show();
    $(tabs).children('li').removeClass('selected');
    $(this).addClass('selected');
  });
}
