// JavaScript Document
$(function(){
  if(typeof(yepnope) != 'function') $.getScript('/Public/js/yepnope.js');//加载插件文件
  yepnope({
	  load:[
        '/Public/css/web_search.css'//加载搜索框的css
      ],
	  complete:function(){
		  $('#web_search').show();
	  }	  
  });
     $('<div id="web_search"><div id="web_search_list0">站内搜索<span> <span style="font-size:16px; margin:0 10px 0 11px;">找</span>一找</span></div><div id="web_search_list1"><div class="close">关闭</div><input type="text" maxlength="30" value="输入要找到的内容"/><input type="button" class="sub" value="找找"/><div id="error"></div></div></div>').appendTo('body').hide();  
  //获取页面列表  
  var $window = $(window);
  var _flag = 0;//表示缩小状态
  //初始化高度和宽度
  var _width = 158;
  var _height = 51;
  var base = function(){return {left:$window.width() + $window.scrollLeft() -_width,top:$window.height() + $window.scrollTop()-_height}};
  var offset = {width:_width,height:_height,left:base().left,top:base().top};
  $this = $('#web_search');
  $this.css(offset);
  $window.resize(function(){
	 $this.css(base());
  }).scroll(function(){
	 $this.css(base());
  });
  //打开之后的高度和宽度	  
  $this.find('span').click(function(){
	  if(_flag) return;
	  _width = 200;
	  _height = 105;
	  $('#web_search_list0').hide('fast');
	  $('#web_search_list1').show('fast');	      
	  $this.animate({width:_width,height:_height,left:base().left,top:base().top},{complete:function(){_flag = 1;},duration:1500})
  });
  $('#web_search input:first').focus(function(){
	  var obj = $(this);
	  if(obj.val() == '输入要找到的内容') obj.val('');
	  obj.css('color','#000000');
  });
  $('#web_search input:first').blur(function(){
	  var obj = $(this);
	  if(obj.val() == '') obj.val('输入要找到的内容');
	  obj.css('color','#999999');
  });
  $('#web_search input:first').bind('keypress',function(event){
	  var _event = event.which?event.which:event.keyCode;
	  if(_event == 13){
		  $('#web_search input:last').click();
	  }
  });
  $('#web_search_list1 .close').click(function(){
	  if(!_flag) return;
	  _width = 158;
	  _height = 51;
	  $('#web_search_list1').hide('fast');
	  $('#web_search_list0').show('fast');
	  $this.animate({width:_width,height:_height,left:base().left,top:base().top},{complete:function(){_flag = 0;},duration:1500});
  });
  $('#web_search input:last').click(function(){
	  $input = $('#web_search input:first');
	  if($input.val() == '' || $input.val() == '输入要找到的内容'){
		  $('#error').text('请输入你要查询的内容');
		  window.setTimeout(function(){$('#error').text('')},1000);
		  return;
	  }
	  window.open('/Search/Index/searchWeb/web/' + $input.val() );
   });
});
