function fav_job(url,id,type)
{
  $.ajax({
    type : 'GET',
    url : url,
    cache: false,
    dataType : 'json',
    success : function(data){
      if (data.error == 200) {
        if (type == 'detail') {
          $("#fav-btn").html("<span><span>已收藏</span></span>");
          $( "#fav-list" ).prepend( "<li><a href='?m=detail&a=job&compid="+data.jobid+"'>"+data.jobtitle+"</li>");
        } else {
          $("#fav-"+id).html('已收藏');
          $( "#fav-list" ).prepend( "<li><a href='?m=detail&a=job&compid="+data.jobid+"'>"+data.jobtitle+"</li>");

        }

      } else if (data.error == 201) {
        alert('已收藏过了');
      } else {
				$( "#login-form" ).dialog( "open" );
      }
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
      //alert('ajax invoke error');
    }
  });
}
function dialog(data, headtitle, id, h ,w)
{
  if (data) {
  } else {
    data = $("#"+id).html();
  }
  h?h:300;
  w?w:350;
  var $dialog = $('<div></div>') .html(data) .dialog({
        autoOpen: false,
        title: headtitle,
        height: h,
        width: w,
        modal:true
      });
      $dialog.dialog('open');
}

$(function() {
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		var email = $( "#email" ),
			password = $( "#password" ),content = $( "#content" ),jobid = $( "#jobid" ),
			allFields = $( [] ).add( name ).add( email ).add( password ).add( content ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( n + "长度在" + min + " - " + max + "字符之内." );
				return false;
			} else {
				return true;
			}
		}

		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
		
		$( "#login-form" ).dialog({
			autoOpen: false,
			height: 240,
			width: 350,
			modal: true,
			buttons: {
				"登录": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					//bValid = bValid && checkLength( name, "username", 3, 16 );
					//bValid = bValid && checkLength( email, "email", 6, 80 );

					//bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp( email,
            /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,
            "邮箱格式错误." );
					//bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
					bValid = bValid && checkLength( password, "密码", 5, 16 );

					if ( bValid ) {
            checkLogin(email.val(),password.val());
					}
				},
				'取消': function() {
					$( this ).dialog( "close" );
				},

				'注册新用户': function() {
          location.href = "?m=reg";
				}

			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});

		$( "#create-user" )
			.click(function() {
				$( "#dialog-form" ).dialog( "open" );
			});

    //apply-form

	$( "#apply-form" ).dialog({
			autoOpen: false,
			height: 300,
			width: 450,
			modal: true,
			buttons: {
				"发送申请": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );
					bValid = bValid && checkLength( content, "求职内容", 10, 300 );
					//bValid = bValid && checkRegexp( content, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );

					if ( bValid ) {
            sendApply(content.val(),jobid.val());
					}
				},

				'取消': function() {
					$( this ).dialog( "close" );
				}

			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
    //end apply-form

  //set show more script
  $('.show-more').click(function(){
      $(this).siblings('li:hidden').show();
      $(this).hide();
  });

  $('#fav-del').click(function(){
      //$(this).siblings('li:hidden').show();
  });
});


function checkLogin(u,p)
{
  $.ajax({
    type : 'POST',
    url : '?m=ajax&a=login',
    data: 'email='+u+'&passwd='+p,
    cache: false,
    dataType : 'json',
    success : function(data){
      if (data.error == 200) {
          $( "#login-form" ).dialog( "close" );
          $( "#mymenu" ).html('<a href="./">首页</a> | <a href="?m=home&a=profile">帐户</a> | <a href="?m=home&a=loginout">注销</a> | <a href="#">关于我们</a>');
      } else {
          $( ".validateTips" ).text(data.message).addClass( "ui-state-highlight" );
      }
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
      //alert('ajax invoke error');
    }
  });
}


function apply_job(url,id)
{
  $.ajax({
    type : 'GET',
    url : url,
    cache: false,
    dataType : 'json',
    success : function(data){
      if (data.error == 200) {
          $("#fav-btn").html("<span><span>已收藏</span></span>");
          $( "#fav-list" ).prepend( "<li><a href='?m=detail&a=job&compid="+data.jobid+"'>"+data.jobtitle+"</li>");

      } else if (data.error == 201) {
        alert('已申请过了');
      } else if (data.error == 202) {
				$( "#apply-form" ).dialog( "open" );
      } else if (data.error == 203) {
        location.href = "?m=resume&a=edit&do=profile&error=203";
      } else {
				$( "#login-form" ).dialog( "open" );
      }
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
      //alert('ajax invoke error');
    }
  });
}

function sendApply(content, jobid)
{
  $.ajax({
    type : 'POST',
    url : '?m=ajax&a=apply&jobid='+jobid+'&do=apply',
    data: 'content='+content,
    cache: false,
    dataType : 'json',
    success : function(data){
    $( "#apply-form" ).dialog( "close" );
      if (data.error == 200) {
        $("#apply-btn").html("<span><span>已投递</span></span>");
      } else {
          $( ".validateTips" ).text(data.message).addClass( "ui-state-highlight" );
      }
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
      //alert('ajax invoke error');
    }
  });
}


function del_fav(jobid)
{
  $.ajax({
    type : 'POST',
    url : '?m=ajax&a=fav&jobid='+jobid+'&op=del',
    cache: false,
    dataType : 'json',
    success : function(data){
      if (data.error == 200) {
          $("#fav-del-"+jobid).parent().remove();
      }
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
      //alert('ajax invoke error');
    }
  });
}


function showinfo(thisObj, contentId, isFirstItem) {
  $("#article-pop-menu li").each(function(i) {
      if (i < 4)
      $(this).attr("className", "");
  })
  $(thisObj).attr("className", "over");
  $(".article-more-link a").attr("href", $(thisObj).find("a").attr("href"));
  $("#article-content").find(".article-s").attr("style", "display:none");
  contentId = "#" + contentId;
  $(contentId).show();
} 



