var links = {
  clicks: function() {
    $('.links').click(function(event) {
      el = event.target;
      el = $(el).parents('.link');
      el = $(el).children('.linkhead')[0];
      el = $(el).children('a')[0];
      location.href = el.href;
    });
  }
}


var dialog = {
  create: function() {
    $('#dialog').dialog({
      autoOpen: false,
      bgiframe: true,
      height: 140,
      modal: true
    });
  }
}

var ajaxloading = {
  show: function() {
    $('#dialog').dialog('open');
    $('#dialog').html('<img src="/images/ajax-loader.gif" alt="Loading..." />');
  },
  close: function() {
    $('#dialog').html('');
    $('#dialog').dialog('close');
  }
}


/* share */
var share = {
  boxAnim: function() {
    $('#share_icons span.email').click(function() {
      if($('#share_email_content').hasClass('closed')) {
        share.openBox();
      }
      else {
        share.closeBox();
      }
    });
  },
  openBox: function() {
    $('#share_email_content').removeClass('closed').animate({
      'height': 194
    });
  },
  closeBox: function() {
    $('#share_email_content').addClass('closed').animate({
      'height': 0
    })
  }
}

var iwiwShare = {
  init: function() {
    $('#share_icons .iwiw').click(function(e) { iwiwShare.click(e) });
  },

  click: function(e) {
    e.preventDefault();

    u = location.href;
    t = document.title;

    window.open('http://iwiw.hu/pages/share/share.jsp?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'iwiwshare','toolbar=0,status=0,location=1, width=650,height=600,scrollbars=1');
    return false;
  }
}

var twitterShare = {
  init: function() {
    $('#share_icons .twitter').click(function(e) { twitterShare.click(e) });
  },

  click: function(e) {
    e.preventDefault();

    window.open('http://twitter.com/home?status='+encodeURIComponent(document.title)+' '+encodeURIComponent(location.href));
    return false;
  }
}


var emailShare = {
  errormsg: function() {
    $('#share_email_content .error').each(function () {
      if($(this).hasClass('invisible')) {
      }
      else {
        $(this).addClass('invisible');
      }
    });
  },
  newSendAjax: function() {
    $('#share_email_content .button_send').click(function() {
      $('#share_email_content').ajaxForm({
          dataType:  'json',
          success:   emailShare.newSend
      });
    });
  },
  newSend: function(data) {
    emailShare.errormsg();
    if (data.type == 0)
    {
      for(i=0; i<data.message.length; i++) {
        $('#error_'+data.message[i].id).removeClass('invisible');
        $('#error_'+data.message[i].id).html(data.message[i].msg);
      }
    }
    else if (data.type == 1)
    {
      $('#share_email_done').removeClass('invisible');
      $('#share_email_done').html(data.msg);
    }
    $('#share_email_content').css({
      height: $('#share_email_content fieldset').height() + 20
    });
  }
}


var fontSizeChanger = fsc = {
  org_main_fs : null,
  main_fs     : null,
  main_fs_unit: null,

  org_el_fs   : [],
  el_fs       : [],
  el_fs_unit  : [],
  el_fs_length: [],

  level: 0,
  
  store: function() {
    fsc.org_main_fs  = fsc.main_fs = parseFloat($('#content').css('fontSize'));
    fsc.main_fs_unit = $('#content').css('fontSize').slice(-2);

    $('#content *').each(function(i, el) {
      if(el.style.fontSize) {
        fsc.org_el_fs[i]  = fsc.el_fs[i] = parseFloat($(el).css('fontSize'));
        fsc.el_fs_unit[i] = $(el).css('fontSize').slice(-2);
      }
      else {
        fsc.org_el_fs[i]  = fsc.el_fs[i] = 0;
      }
    });

    fsc.el_fs_length = fsc.el_fs.length;
  },

  modify: function(mod_value) {
    mod_value == 0
      ? fsc.main_fs = fsc.org_main_fs
      : fsc.main_fs += mod_value;

    $('#content').css('fontSize', fsc.main_fs + fsc.main_fs_unit);

    for(i=0; i<fsc.el_fs_length; i++) {
      if (fsc.org_el_fs[i] != 0 ) {
        mod_value == 0
          ? fsc.el_fs[i] = fsc.org_el_fs[i]
          : fsc.el_fs[i] += mod_value;
        
        $(fsc.el_fs[i]).css('fontSize', fsc.el_fs[i] + fsc.el_fs_unit[i]);
      }
    }
  },

  increase: function() {
    if (fsc.level < 3) {
      fsc.level += 1;
      fsc.modify(1);
    }
  },

  decrease: function() {
    if (fsc.level > -3) {
      fsc.level -= 1;
      fsc.modify(-1);
    }
  },

  reset: function() {
    if (fsc.level != 0) {
      fsc.level = 0;
      fsc.modify(0);
    }
  }
}


$(document).ready(function() {
  twitterShare.init();
  iwiwShare.init();
  emailShare.newSendAjax();

  share.boxAnim();
  links.clicks();

  if ($('#content').length > 0) { fsc.store(); }
  if ($('#dialog').length > 0) { dialog.create(); }
});