(function($){
  $(function(){

    if ( $('#feature_box').length ) {
      $('#feature_box').s3Slider({
        timeOut: 4000
      });
    }

    $(".colorbox").colorbox({
      iframe:true,
      innerWidth:835,
      innerHeight:500
    });
    
    // equal height
    $('.listcat').each(function(){
      var maxh;
      maxh = 0;
      $('.listcat > li').each(function(){
        var height;
        height = $(this).height();
        if ( height > maxh ) maxh = height;
      }).height(maxh);
    });
    
    // category
    $('.gotocat').click(function(){
      var $t,cid,scrolltop,$loader,winwidth,loaderleft;
      $t = $(this);
      cid = $t.attr('rel');
      $loader = $('#catloader');
      scrolltop = $(window).scrollTop() + 250;
      winwidth = $(window).width();
      loaderleft = (winwidth - 240)/2;
      if ($loader.length == 0)
      {
        $loader = $('<div id="catloader">Please wait while processing...</div>');
        $('body').append($loader);
      }
      $loader.css({
        'top': scrolltop+'px',
        'left': loaderleft+'px',
        'opacity': 0.8
      });
      $.get(AJAXURL,{
        action: 'checkurl',
        category_id: cid
      },function(rs){
        if ( rs.success ) {
          window.location.href = rs.url;
        } else {
          $loader.remove();
          $.colorbox({html:rs.html});
        }
      },'json');
      return false;
    });

    $('#getaccessfrm').live('submit',function(){
      var $t, $submit, $name, $code, $image;
      $t = $(this);
      $submit = $(':submit',$t);
      $name = $('[name=name]',$t);
      $email = $('[name=email]',$t);
      $code = $('[name=code]',$t);
      $image = $('.recaptchaimage',$t);
      if ( $name.val() == '' )
      {
        alert("Please enter your name.");
      }
      else if ( $email.val() == '' )
      {
        alert("Please enter your email address.");
      }
      else if ( $code.val() == '' )
      {
        alert("Please enter validation code.");
      }
      else
      {
        $t.ajaxSubmit({
          dataType: 'json',
          beforeSubmit: function() {
            $submit.val('Submitting...').attr('disabled',false);
          },
          success: function(rs) {
            if ( rs.success ) {
              window.location.href = rs.url;
            } else {
              alert(rs.error);
              $image.attr('src',rs.recaptchaimage)
            }
            $submit.val('Submit').attr('disabled',false);
          }
        });
      }
      return false;
    });
    
  });
})(jQuery);
