var web = {
  init: function(){
    $('.input').focus(function(){ $(this).addClass('onfocus'); }).blur(function(){ $(this).removeClass('onfocus'); });
    $('input:first').focus();
  },
  stamp: function(){ return new Date().getTime(); }
};

var winner = {
  init: function(){
    $('.header a:first').css({'color':'#ff0'});
    $('#tab1').click(function(){
      $('.header').children('a').css({'color':'#fff'});
      $('#team1').show();
      $('#team2').hide();
      $(this).css({'color':'#ff0'});
      return false;
    });
    $('#tab2').click(function(){
      $('.header').children('a').css({'color':'#fff'});
      $('#team1').hide();
      $('#team2').show();
      $(this).css({'color':'#ff0'});
      return false;
    });
    
    $('input[type=radio][name^=team]').click(function(){
      var n = $(this).attr('name');
      var obj = $(this).parent().parent();
      var css = {'checked':{'border':'1px solid #ff0'}, 'unchecked':{'border':'1px solid #666'}};
      $('#' + n).find('.item').css(css.unchecked);
      if($(this).attr('checked')){
        obj.css(css.checked);
      }else{
        obj.css(css.unchecked);
      }
    });
    $('#btn').click(function(){
      var team1 = $('input[type=radio][name=team1]:checked');
      var team2 = $('input[type=radio][name=team2]:checked');
      var msg = '';
      if(team1.length == 0){ msg += '您忘了票選高中組團體\n'; }
      if(team2.length == 0){ msg += '您忘了票選大專院校組團體\n'; }
      if(msg == ''){
        tb_show('填寫個人資料', 'vote.php?id1=' + team1.val() + '&id2=' + team2.val() + '&KeepThis=true&TB_iframe=true&height=300&width=600');
      }else{
        alert(msg);
        return false;
      }
    });
  }
};

var vote = {
  path: 'update.php',
  param: {},
  init: function(){
    $('body').ajaxStart(function(){ $('.loading').show(); }).ajaxStop(function(){ $('.loading').fadeOut(); });
    $(':submit, :button').removeAttr('disabled');
    $('.close_tb').click(function(){ self.parent.tb_remove(); });
    var captcha_img = $('#captcha-img');
    var captcha_btn = $('#captcha-btn');
    if(captcha_img.length > 0){ captcha_img.html('<img src="captcha/captcha.php?s=' + new Date().getTime() + '" alt="" title="" class="captcha" />'); }
    captcha_btn.click(function(){
      if(captcha_img.length > 0){
        captcha_img.empty().html('<img src="captcha/captcha.php?s=' + new Date().getTime() + '" alt="" title="" class="captcha" />');
      }
    });

    $('.submit').click(function(){
      var msg = '';
      vote.param = {
        'mf'      : 'vote',
        'id1'      : $('input[type=hidden][name=id1]').val(),
        'id2'      : $('input[type=hidden][name=id2]').val(),
        'name'    : $('#user-name').val(),
        'tel'     : $('#user-tel').val(),
        'email'   : $('#user-email').val(),
        'captcha' : $('#captcha').val(),
        's'     : web.stamp()
      }
      if(vote.param.name.length == 0){ msg += '您的姓名必須填寫\n'; }
      if(vote.param.tel.length == 0){ msg += '聯絡電話必須填寫\n'; }
      if(!vote.regexp(vote.param.email)){ msg += '電子郵件格式錯誤\n'; }
      if(!msg == ''){
        $(':submit, :button').removeAttr('disabled');
        alert(msg);
        return false;
      }else{
        $(':submit, :button').attr('disabled', true);
        $.post(vote.path, vote.param, function(result){
          if(result){
            alert(result.msg);
            if(result.status == '0'){
              location.href = '?name=' + vote.param.name + '&tel=' + vote.param.tel + '&email=' + vote.param.email + '&id1=' + vote.param.id1 + '&id2=' + vote.param.id2;
            }else{
              self.parent.location.reload();
            }
          }
        }, 'json');
      }
    });
  },
  regexp: function(str, dotype){
    dotype = dotype || 'email';
    switch(dotype){
      default: var re = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/; break;
    }
    return re.test(str);
  }
};

$(function(){
  web.init();
  winner.init();
  vote.init();
});
