// JavaScript Document
var cur_sort = 0;
var cur_page = 1;
var all_page = 1;
var is_favorite = false;
$(function(){
	var ScWidth = document.body.clientWidth;
	var ScHeight = document.body.clientHeight;
	$('#SkinList li').mouseover(function(){$(this).css('background','#ccc');})
					.mouseout(function(){$(this).css('background','#fff');})
	$('.search_btn').mouseover(function(){$(this).css('background','url(templates/default/images/search_btn_on.png)');})
					.mouseout(function(){$(this).css('background','url(templates/default/images/search_btn.png)');})
					.click(function(){$('#search_form').submit()});
	$('#item_list').css('left',(ScWidth-590)/2+1);
	$('.sites_mask').css('left',(ScWidth-590)/2).animate({opacity:0.2},0);
	$('#add_fav_box').css('left',(ScWidth-400)/2);
	$('#msg_box').css('left',(ScWidth-200)/2);
	$('#SkinList').css('left',(ScWidth-110)/2+480);
	$('.add_fav_btn').css('left',(ScWidth-590)/2+500);
	$('.mask').animate({opacity:0.7},0);
	$('.footer_mask').animate({opacity:0.4},0);
	$('.search_item:eq(0)').click(function(){
		$('#item_list').css('visibility', $('#item_list').css('visibility')=='visible' ? 'hidden' : 'visible' );
	});
	$('#item_list .list_item').each(function(){
		$(this).mouseover(function(){$(this).css('background','#E7E7E7');})
			   .mouseout(function(){$(this).css('background','#FAFAFA');})
			   .click(function(){
				   $('.search_item:eq(0) .item_image:eq(0) img:eq(0)').attr('src',$(this).children('.item_image:eq(0)').children('img:eq(0)').attr('src'));
				   $('.search_item:eq(0) .item_title:eq(0)').html($(this).children('.item_title').html());
				   $('#item_list').css('visibility', 'hidden');
				   ChangeSearchItem($(this).children('.item_image:eq(0)').children('img:eq(0)').attr('alt'));
			   });
	});
	$('.menu li[class!=on]').each(function(){
		$(this).mouseover(function(){$(this).css('height','23px').css('border-bottom','2px solid #333333');})
			   .mouseout(function(){$(this).css('height','25px').css('border-bottom','0')});
	});
	$('.page_prev').click(function(){
		if(cur_page>1)
		{
			if(is_favorite)
			{
				LoadFavorite(cur_page-1);
			}
			else
			{
				LoadSites(cur_sort,cur_page-1,'null');
			}
			$('.cur_page').html(cur_page-1);
			cur_page = cur_page-1;
		}
	});
	$('.page_next').click(function(){
		if(cur_page<all_page)
		{
			if(is_favorite)
			{
				LoadFavorite(cur_page+1);
			}
			else
			{
				LoadSites(cur_sort,cur_page-1,'null');
			}
			$('.cur_page').html(cur_page+1);
			cur_page = cur_page+1;
		}
	});
	LoadSites(0,0,'null');
	setInterval(function(){$('#time').html(CurentTime())},1000);
	if(ReadCookie('background')!='')
		ChangeBg(ReadCookie('background'));
});

function ChangeSkin()
{
	if($("#SkinList").css('visibility')=='hidden')
		$("#SkinList").css('visibility','visible');
	else
		$("#SkinList").css('visibility','hidden');
}

function ChangeBg(name)
{
	if(name!='')
	{
		$('body').css('background','url(../images/background/'+name+') center center');
		WriteCookie('background',name);
			$("#SkinList").css('visibility','hidden');
	}
}

function ShowAddFav()
{
	$("#add_fav_box").fadeIn(500);
}

function HideAddFav()
{
	$("#add_fav_box").fadeOut(500);
}

function SaveAddFav()
{
	name = $("#fav_name").val();
	_link = $("#fav_link").val();
	images = '../uploads/logo/default.jpg';
	$.ajax({
	  type: 'GET',
	  url: 'ajax.php',
	  data: 'action=add_favorite&name='+name+'&link='+_link+'&images='+images,
	  success: function(data){
		  if(data=='1')
		  {
		  	MessageBox("添加收藏成功！");
			HideAddFav();
			LoadFavorite(cur_page);			
		  }
		  else
		  {
		  	MessageBox("网络错误，添加收藏失败！");
		  }
	  }
	});
}

function ChangeTab(type,obj)
{
	$(".top_bar .wrap ul li").each(function(){$(this).attr('class','')});
	$(obj).parent().attr('class','on');
	
	switch(type)
	{
		case 'index':
			cur_page = 1;
			is_favorite = false;
			LoadSites(0,0,'null');
			$('.add_fav_btn').hide();
		break;
		
		case 'favorite':
			cur_page = 1;
			is_favorite = true;
			LoadFavorite(1);
			$('.add_fav_btn').show();
		break;
		
		case 'about':
			$.ajax({
			  type: 'GET',
			  url: 'ajax.php',
			  data: 'action=about',
			  success: function(data){
				is_favorite = false;
				$('#sites').html(data);
				$('.page_box').hide();
			  }
			});
			$('.add_fav_btn').hide();
		break;
		
		case 'feedback':
			$.ajax({
			  type: 'GET',
			  url: 'ajax.php',
			  data: 'action=feedback',
			  success: function(data){
				is_favorite = false;
				$('#sites').html(data);
				$('.page_box').hide();
			  }
			});
			$('.add_fav_btn').hide();
		break;
		
		case 'linkin':
			$.ajax({
			  type: 'GET',
			  url: 'ajax.php',
			  data: 'action=linkin',
			  success: function(data){
				is_favorite = false;
				$('#sites').html(data);
				$('.page_box').hide();
			  }
			});
			$('.add_fav_btn').hide();
		break;
		
		case 'links':
			cur_page = 1;
			LoadSites(-2,0,'null');
			$('.add_fav_btn').hide();
		break;
	}
}

function ChangeSearchItem(name)
{
	switch(name)
	{
		case 'baidu':
			$('#search_form').attr('action','http://www.baidu.com/baidu');
			$('#keyword').attr('name','word');
		break;
		
		case 'google':
			$('#search_form').attr('action','http://www.google.com.hk/search');
			$('#keyword').attr('name','q');
		break;
		
		case 'soku':
			$('#search_form').attr('action','http://music.baidu.com/search');
			$('#keyword').attr('name','key');
		break;
		
		case 'soso':
			$('#search_form').attr('action','http://video.baidu.com/v');
			$('#keyword').attr('name','word');
		break;
	}
}
function LoadIndex(obj)
{
	$('.menu li').each(function(){
		$(this).unbind('mouseover');
		$(this).unbind('mouseout');
		$(this).css('height','25px').css('border-bottom','0').attr('class','');
	});
	$(obj).css('height','23px').css('border-bottom','2px solid #CD0000').attr('class','on');
	LoadSites(0,0,'null');
}
function LoadSubSotrs(id,obj)
{
	if(obj!='null')
	{
	  $('.left_menu li').each(function(){
		  $(this).unbind('mouseover');
		  $(this).unbind('mouseout');
		  $(this).css('height','25px').css('border-bottom','0').attr('class','');
	  });
	  $(obj).css('height','23px').css('border-bottom','2px solid #CD0000').attr('class','on');
	}
	is_favorite = false;
	cur_sort = id;
	cur_page = 1;
	$.ajax({
	  type: 'GET',
	  url: 'ajax.php',
	  data: 'action=subsorts&id='+id,
	  success: function(data){
		$('#right_menu').html('');
		$(data).find('sort').each(function(){
			$('#right_menu').html($('#right_menu').html()+"\n<li onclick=\"LoadSites("+$(this).attr('id')+",0,this)\">"+$(this).attr('name')+"</li>");
		});
		$('.menu li[class!=on]').each(function(){
			$(this).mouseover(function(){$(this).css('height','23px').css('border-bottom','2px solid #333333');})
				   .mouseout(function(){$(this).css('height','25px').css('border-bottom','0')});
		});
	  },
	  dataType: 'xml'
	});
	LoadSites(id,0,'null');
}
function LoadSites(id,page,obj)
{
	if(id!=-2)
	{
		$(".top_bar .wrap ul li").each(function(){$(this).attr('class','')});
		$(".top_bar .wrap ul li:eq(0)").attr('class','on');
	}
	if(obj!='null')
	{
	  $('.right_menu li').each(function(){
		  $(this).unbind('mouseover');
		  $(this).unbind('mouseout');
		  $(this).css('height','25px').css('border-bottom','0').attr('class','');
	  });
	  $(obj).css('height','23px').css('border-bottom','2px solid #CD0000').attr('class','on');
	  $('.right_menu li[class!=on]').each(function(){
		  $(this).mouseover(function(){$(this).css('height','23px').css('border-bottom','2px solid #333333');})
				 .mouseout(function(){$(this).css('height','25px').css('border-bottom','0')});
	  });
	}
	cur_sort = id;
	$('.sites_mask').fadeIn(200);
	$.ajax({
	  type: 'GET',
	  url: 'ajax.php',
	  data: 'action=sites&id='+id+'&page='+cur_page,
	  success: function(data){
		$('#sites').html('');
		$(data).find('site').each(function(){
			$('#sites').html($('#sites').html()+"<div class=\"site_box\"><div class=\"add_favorite\"><a style=\"display:none\" href=\""+$(this).attr('link')+"\" target=\"_blank\">"+$(this).attr('name')+"</a></div><div class=\"site_logo\"><img width=\"80\" height=\"60\" src=\"../uploads/logo/"+$(this).attr('images')+".jpg\" /></div><div class=\"site_title\">"+$(this).attr('name')+"</div></div>");
		});
		$('.site_box').each(function(){
			$(this).mouseover(function(){$(this).children('.add_favorite').show()});
			$(this).mouseout(function(){$(this).children('.add_favorite').hide()});
			$(this).click(function(){
				obj = $(this).children('.add_favorite').children('a');
				if($.browser.msie || $.browser.mozilla)
					obj.get(0).click();  
				else
					window.open(obj.attr('href'), obj.attr('target'));
			});
			$(this).children('.add_favorite').click(function(e){MovetoFavorite(this);e.stopPropagation();});
		});
		//page
		all_page = $(data).find('page').text();
		if(all_page==1||all_page==0)
			$('.page_box').hide();
		else
			$('.page_box').show();
		
		$('#all_page').html(all_page);
		$('#cur_page').html(cur_page);
	  },
	  dataType: 'xml'
	});
	$('.sites_mask').fadeOut(200);
	$('.add_fav_btn').hide();
}
function LoadFavorite(page)
{
	is_favorite = true;
	$('.sites_mask').fadeIn(200);
	$.ajax({
	  type: 'GET',
	  url: 'ajax.php',
	  data: 'action=favorite&page='+page,
	  success: function(data){
		$('#sites').html('');
		$(data).find('site').each(function(){
			$('#sites').html($('#sites').html()+"<div class=\"site_box\"><div onclick=\"DeleteFavorite('"+$(this).attr('id')+"',event)\" class=\"del_favorite\"><a id=\"fav-"+$(this).attr('id')+"\" style=\"display:none\" href=\""+$(this).attr('link')+"\" target=\"_blank\">"+$(this).attr('name')+"</a></div><div class=\"site_logo\"><img width=\"80\" height=\"60\" src=\""+$(this).attr('images')+"\" /></div><div class=\"site_title\">"+$(this).attr('name')+"</div></div>");
		});
		$('.site_box').each(function(){
			$(this).mouseover(function(){$(this).children('.del_favorite').show()});
			$(this).mouseout(function(){$(this).children('.del_favorite').hide()});

			$(this).click(function(){
				obj = $(this).children('.del_favorite').children('a');
				if($.browser.msie || $.browser.mozilla)
					obj.get(0).click();  
				else
					window.open(obj.attr('href'), obj.attr('target'));
			});
		});
		//page
		all_page = $(data).find('page').text();
		if(cur_page>all_page) cur_page = all_page;
		if(all_page==1||all_page==0)
			$('.page_box').hide();
		else
			$('.page_box').show();
		
		$('#all_page').html(all_page);
		$('#cur_page').html(cur_page);
	  },
	  dataType: 'xml'
	});
	$('.sites_mask').fadeOut(200);
	$('.add_fav_btn').show();
}
function MovetoFavorite(obj)
{
	name = $(obj).children('a').html();
	images = $(obj).next().children('img').attr('src');
	_link = $(obj).children('a').attr('href');
	$.ajax({
	  type: 'GET',
	  url: 'ajax.php',
	  data: 'action=add_favorite&name='+name+'&link='+_link+'&images='+images,
	  success: function(data){
		  if(data=='1')
		  {
		  	MessageBox("添加收藏成功！");
		  }
		  else
		  {
		  	MessageBox("网络错误，添加收藏失败！");
		  }
	  }
	});
}
function DeleteFavorite(id,e)
{
	$.ajax({
	  type: 'GET',
	  url: 'ajax.php',
	  data: 'action=del_favorite&id='+id,
	  success: function(data){
		  if(data=='1')
		  {
		  	MessageBox("删除收藏成功！");
			LoadFavorite(cur_page);
		  }
		  else
		  {
		  	MessageBox("网络错误，删除收藏失败！");
		  }
	  }
	});
	e.stopPropagation();
}
function ReadCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}
function WriteCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}
function  CurentTime()
{  
	var now = new Date();  
	var hh = now.getHours();  
	var mm = now.getMinutes();  
	var ss = now.getTime() % 60000;  
	ss = (ss - (ss % 1000)) / 1000;  
	var clock  =  hh+':';  
	if  (mm < 10)  clock += '0';  
		clock  +=  mm+':';  
	if  (ss < 10)  clock += '0';  
		clock += ss;  
	return(clock); 
}
function MessageBox(str)
{
	$('#msg_box').html(str);
	$('#msg_box').fadeIn(300);
	setTimeout(function(){$('#msg_box').fadeOut(300);},1000);
}
