google.setOnLoadCallback(function() {
  $("#barbtn11").click(function(event){
    if( $(this).attr("class").indexOf("focus") < 0 )
      window.location=www_host+"/settype.php?t=1&b="+window.location.href;return false;
  });
  $("#barbtn12").click(function(event){
    if( $(this).attr("class").indexOf("focus") < 0 )
      window.location=www_host+"/settype.php?t=2&b="+window.location.href;return false;
  });
  $("#barbtn10").click(function(event){
    if( $(this).attr("class").indexOf("focus") < 0 )
      window.location=www_host+"/settype.php?t=0&b="+window.location.href;return false;
  });
  
  $("#barbtn22").click(function(event){
    if( $(this).attr("class").indexOf("focus") < 0 )
      window.location=www_host+"/list.php?tag=new";return false;
  });
  $("#barbtn23").click(function(event){
    if( $(this).attr("class").indexOf("focus") < 0 )
      window.location=www_host+"/list.php?tag=top";return false;
  });

  $("#barbtn21").click(function(event){
    if( $(this).attr("class").indexOf("focus") < 0 )
      window.location=www_host+"/image.php";return false;
  });

  $("#addtagbtn").click(function(event){
	$("#atfeedback").text(input_tags_hint);
	$("#addtagfrm").resetForm();
    $("#addtagbox").slideToggle("slow");return false;
  });

  $("#addtagclose").click(function(event){
    $("#addtagbox").slideUp("slow");return false;
  });

  $(".barbtn").mouseover(function(event){
    if( $(this).attr("class").indexOf("focus") < 0 )
      $(this).css("color", "#ffff00").css("text-decoration","underline");
  }).mouseout(function(){
    if( $(this).attr("class").indexOf("focus") < 0 )
      $(this).css("color", "").css("text-decoration","");
  });

  $("#fitags").click(function(){
    if( $(this).val() == input_tags_hint ) $(this).val("");
  }).blur(function(){
    if( $(this).val() == "") $(this).val(input_tags_hint);
  });

  $("#ficode").click(function(){
    if( $(this).val() == input_code_hint ) $(this).val("");
  }).blur(function(){
    if( $(this).val() == "") $(this).val(input_code_hint);
  });

  $('a.email').each(function(){
    e = this.rel.replace('#','@');
    this.href = 'mailto:' + e;
  });

  var options = { 
    target:        '#aufeedback',   // target element(s) to be updated with server response 
    beforeSubmit:  validate,  // pre-submit callback 
    success:       showResponse  // post-submit callback 
 
    // other available options: 
    //url:       url         // override for form's 'action' attribute 
    //type:      type        // 'get' or 'post', override for form's 'method' attribute 
    //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
    //clearForm: true        // clear all form fields after successful submit 
    //resetForm: true        // reset the form after successful submit 
 
    // $.ajax options can be used here too, for example: 
    //timeout:   3000 
  }; 
 
  // bind to the form's submit event 
  $("#footsubmit").click(function() { 
    // inside event callbacks 'this' is the DOM element so we first 
    // wrap it in a jQuery object and then invoke ajaxSubmit 
    $("#addurl").ajaxSubmit(options); 
 
    // !!! Important !!! 
    // always return false to prevent standard browser submit and page navigation 
    return false; 
  }); 


  var options1 = { 
    target:        '#atfeedback',   // target element(s) to be updated with server response 
    beforeSubmit:  validate1,  // pre-submit callback 
    success:       showResponse1,  // post-submit callback 
	resetForm:     true        // reset the form after successful submit 
  };
  $("#addtagsave").click(function() { 
    $("#addtagfrm").ajaxSubmit(options1); 
    return false; 
  }); 
});


function validate1(formData, jqForm, options) { 
  var form = jqForm[0]; 
  if (!form.tags.value) { 
    if(www_lang == 1) alert('You must input valid tags!'); 
	if(www_lang == 2) alert('请输入合法的标签!'); 
    return false; 
  }
  else {
    var len = form.tags.value.length;
    if (len < 2) {
      if(www_lang == 1) alert('You must input valid tags!');
	  if(www_lang == 2) alert('请输入合法的标签!'); 
      return false;
    }
  }
  return true; 
}

function showResponse1(responseText, statusText)  { 
  //if( responseText.indexOf("Thank you") < 0 ) {
  alert(responseText); 
  //}
}

// pre-submit callback 
function validate(formData, jqForm, options) { 
  var form = jqForm[0]; 
  if (!form.url.value) { 
    if(www_lang == 1) alert('You must input a valid URL!'); 
	if(www_lang == 2) alert('请输入合法的URL!'); 
    return false; 
  }
  else {
    var len = form.url.value.length;
    if (len < 12) {
      if(www_lang == 1) alert('You must input a valid URL!');
	  if(www_lang == 2) alert('请输入合法的URL!');
      return false;
    }
    if (form.url.value.substr(0, 7) != 'http://') {
      if(www_lang == 1) alert('A valid URL starts with [http]');
	  if(www_lang == 2) alert('URL格式不对，需要[http]开头!');
      return false;
    }
    var tmp = form.url.value.substr(len-4, 4).toLowerCase();
    if ( (tmp != '.jpg') && (tmp != '.gif') && (tmp != '.png') && (tmp != 'jpeg') && (tmp != '.bmp') && (tmp != '.tif') ){
      if(www_lang == 1) alert('It is not a valid image URL!');
	  if(www_lang == 2) alert('请输入合法的图片URL!');
      return false;
    }
  }
  if (!form.code.value) { 
    if(www_lang == 1) alert('Do not forget to type the code!'); 
	if(www_lang == 2) alert('请输入验证码!');
    return false; 
  }
  else
  {
    if (form.code.value.length != 8) {
      if(www_lang == 1) alert('Please double check your code!');
	  if(www_lang == 2) alert('请再次检查你的验证码');
      return false;
    }	
  }
  return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
  // for normal html responses, the first argument to the success callback 
  // is the XMLHttpRequest object's responseText property 

  // if the ajaxForm method was passed an Options Object with the dataType 
  // property set to 'xml' then the first argument to the success callback 
  // is the XMLHttpRequest object's responseXML property 
 
  // if the ajaxForm method was passed an Options Object with the dataType 
  // property set to 'json' then the first argument to the success callback 
  // is the json data object returned by the server 
  if( responseText.indexOf("Thank you") < 0 ) {
    alert(responseText); 
  }
  else {
    $('#addurl').resetForm();
  }
} 