var header = {
  sites: function(){
    $('.cig_sites_wrap').hover(
      function(){
        $('.cig_sites_body').css('height','121px');
        $('.cig_sites_body ul')
          .css('display','block')
          .animate({
            top: 0
          }, 100);
      },
      function(){
        $('.cig_sites_body ul').animate({
          top: -101
        }, 100, function(){
          $('.cig_sites_body').css('height','0px');
          $('.cig_sites_body ul').css('display','none');
        });
      }
    );
  }
}

var login = {
  validator: function(){
    $('#cp_login_name').keyup(function(e){
      var usercode = $('#cp_login_name').val();
      if (!/^\d{0,9}$/.test(usercode)){
        $('.cp_login_box .error').removeClass('invisible');
      }
      else {
        $('.cp_login_box .error').addClass('invisible');
      }
    });
  }
}

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;
    });
  },
  hoverColor: function(){
    $('.box_distributor .boxinner').hover(
      function(){
        $(this).addClass('distributor_hover')
      },
      function(){
        $(this).removeClass('distributor_hover')
      }
    );
    $('.box_distributor_double .boxinner').hover(
      function(){
        $(this).addClass('distributor_hover')
      },
      function(){
        $(this).removeClass('distributor_hover')
      }
    );
  }
}


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');
  }
}

var label = {
  features: function(){
    $('.withlabeltext').each(function(){
      if ($(this).val() != ''){
        $(this).addClass('with_bg');
      }

      $(this).focus(function (){
        $(this).addClass('with_bg');
      });

      $(this).click(function (){
        $(this).addClass('with_bg');
      });

      $(this).keypress(function (){
        $(this).addClass('with_bg');
      });

      $(this).blur(function (){
        if ($(this).val() == ''){
          $(this).removeClass('with_bg');
        }
      });
    });
  }
}


/* 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 email = {
  selectAll: function(){
    $('#select_all').bind('click', function(){
      var sa_input = this;
      var itembody = $(this).parents('.itembody');

      if ($(sa_input).hasClass('selected')){
        $(sa_input).toggleClass('selected').attr('checked', '');
        $(itembody).find('input').each(function(){
          $(this).attr('checked', '');
        });
      }
      else {
        $(sa_input).toggleClass('selected').attr('checked', 'checked');
        $(itembody).find('input').each(function(){
          $(this).attr('checked', 'checked');
        });
      }
    });
  },
  unsubscribe: function(){
    if ($('#subscriber_unsubscribe_1').attr('checked')){
      $('#subscribe_block').addClass('invisible');
      $('.email_inner').addClass('unsubscribe');
    }
    $('#subscriber_unsubscribe_0').click(function(){
      $('#subscribe_block').removeClass('invisible');
      $('.email_inner').removeClass('unsubscribe');
    });
    $('#subscriber_unsubscribe_1').click(function(){
      $('#subscribe_block').addClass('invisible');
      $('.email_inner').addClass('unsubscribe');
    });
  }
}


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);
    }
  }
}


var promo = {
  clickableArea: function(){
    $('#promobox_content li.clickable_area').each(function(){
      $(this).bind('click', function(){
        location.href = $(this).find('.more')[0].href
      })
    })
  }
}


$(document).ready(function() {
  header.sites();
  /*if ($('#cp_login_name').length) login.validator();*/

  twitterShare.init();
  iwiwShare.init();
  emailShare.newSendAjax();

  label.features();

  share.boxAnim();

  links.clicks();
  links.hoverColor();

  if ($('#content').length) { fsc.store(); }

  if ($('.email_inner').length) {
    email.selectAll();
    email.unsubscribe();
  }

  if ($('#dialog').length && $('#form_fund_information').length == 0) {
    dialog.create();
  }
  
  if ($('#promobox').length) { promo.clickableArea(); }
});
