var copySearch = "";

function overSearchBtn(){
	var over = document.getElementById("searchOverImg");		
	over.style.display = "block";		
	
}
function outSearchBtn(){
	var over = document.getElementById("searchOverImg");	
	over.style.display = "none";		
}

var lastSelectIndex = -1;
var selectedIndex = -1;
var navCategories = [];
var navCategoriesOver = [];

function liItemClick(total, index){
	
	liItemOut();		
	
	var ele = document.getElementById("li_item_" + index);	
	
	if (ele) {
		lastSelectIndex = index;
		ele.style.color = "#ffffff";
		ele.style.background = "#000000";
		ele.getElementsByTagName("img")[0].src = navCategoriesOver[index];
	}
}

function liItemOut(){
	
	var ele2 = document.getElementById("li_item_" + lastSelectIndex);	
	
	if(ele2 && lastSelectIndex != -1 && lastSelectIndex != selectedIndex){
		ele2.style.color = "#000000";
		ele2.style.background = "#E8E8E8";
		ele2.getElementsByTagName("img")[0].src = navCategories[lastSelectIndex];
	}
	
}

function liItemLink(url, cat){
	
	track("/click/blog_category_"+cat+"_click");
	document.location = url;
	
}

var sideLastSelectIndex = -1;
function sideLiItemClick(total, index){
	var ele = document.getElementById("side_li_item_" + index);	
	if(sideLastSelectIndex != -1){
		var ele2 = document.getElementById("side_li_item_" + sideLastSelectIndex);	
		ele2.style.color = "#686667";
		ele2.style.background = "none";            
	}
	
	 sideLastSelectIndex = index;
	 ele.style.color = "#ffffff";
	 ele.style.background = "#676767";        
}
function overSideTagItem(obj){
	 obj.style.color = "#ffffff";
	 obj.style.background = "#676767";        
}
function outSideTagItem(obj){
	 obj.style.color = "#686667";
	 obj.style.background = "none";     
}

function overStoryTitle(obj){
	 obj.style.color = "#ffffff";
	 obj.style.background = "#333333";   
}
function outStoryTitle(obj){
	 obj.style.color = "#333333";
	 obj.style.background = "#ffffff";   
}

function overPostTag(obj){
	obj.style.background = "#787878";
}
function outPostTag(obj){
	obj.style.background = "#999999";
}

function seeMore(url){
	
	track("/click/blog_post_alsocheckout_click");
	document.location = url;
	
}



var isTagOpen = false;
var initialContentLeft = 0;

function tagBtnClick(){
	
	track("/click/blog_tags_click");
	
	var closeBtn = document.getElementById("tagCloseBtn");	
	var openBtn = document.getElementById("tagOpenBtn");
	var sideTags = document.getElementById("sideTags");
	
	if(isTagOpen){
		openBtn.style.display = "none";
		closeBtn.style.display = "block";
		sideTags.style.display = "none";
		isTagOpen = false;
	}else{        
		openBtn.style.display = "block";
		closeBtn.style.display = "none";
		sideTags.style.display = "block";
		isTagOpen = true;  
	}
	
	arrange();
	// to fix IE6
	arrange();
   
}
function overSideTagBtn(imgId){
	var btnImg = document.getElementById(imgId);
	btnImg.style.display = "block";
}
function outSideTagBtn(imgId){
	 var btnImg = document.getElementById(imgId);
	btnImg.style.display = "none";
}

function clickSideTagItem(url, tag) {
	track("/click/blog_tags_"+tag+"_click");
	document.location=url;
}



function mouseOverBtn(imgId){
	 var btnImg = document.getElementById(imgId);
	 btnImg.style.display = "block";
}
function mouseOutBtn(imgId){
	var btnImg = document.getElementById(imgId);
	btnImg.style.display = "none";
}
function mouseOverBtn2(imgId){
	 var btnImg_o = document.getElementById(imgId + "_o");
	 var btnImg_n = document.getElementById(imgId + "_n");
	 btnImg_o.style.display = "block";
	 btnImg_n.style.display = "none";
}
function mouseOutBtn2(imgId){
	 var btnImg_o = document.getElementById(imgId + "_o");
	 var btnImg_n = document.getElementById(imgId + "_n");
	 btnImg_o.style.display = "none";
	 btnImg_n.style.display = "block";
}

function focusSearchBox(obj){
	
	if (copySearch == "") {
		copySearch = obj.value;
	}
	obj.value = "";

}
function blurSearchBox(obj){
	if(obj.value == "")
		obj.value = copySearch;
}

function submitForm(){

	var form = document.getElementById("commentform");
	var msg = document.getElementById("leftColumnMsg");
	
	if (vaildateForm()) {
		form.submit();
	}
	else {
		msg.style.visibility = "visible";
	}

}

function vaildateForm() {
	
	var author=document.getElementById("author").value;
	var email=document.getElementById("email").value;
	var comment=document.getElementById("comment").value;
	
	if ( author.length==0 || email.length==0 || comment.length==0 )
	{
		return false;
	}
	else {
		
		var regm = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
		if( email.match(regm) ) {
			return true;
		}
		else {
			return false;
		}
	}
	
}

function clickShare() {
	
	var share=document.getElementById("leftHeaderShare");
	if (share) {
		share.style.visibility = "visible";
	}
	
}

function shareFunction(val){

	var u = document.location;
	var link = encodeURIComponent(u);
	var title = document.title;
	
	var url = '';
	var tag = '';
	
	switch(val){
		
		case '1':
			var encodeString = encodeURIComponent(title);
			url = "http://share.renren.com/share/buttonshare.do?link="+link+"&title="+encodeString;
			tag = "/click/blog_post_share_renren_click";
			break;
			
		case '2':
			var encodeString = encodeURIComponent(title);
			url = "http://www.kaixin001.com/repaste/share.php?rurl="+link+"&rtitle="+encodeString;
			tag = "/click/blog_post_share_kaixin_click";
			break;
			
		case '3':
			var encodeString = title;
			url = "http://www.douban.com/recommend/?url="+link+"&title="+encodeString;
			tag = "/click/blog_post_share_douban_click";
			break
	}
	
	var share=document.getElementById("leftHeaderShare");
	if (share) {
		share.style.visibility = "hidden";
	}
	
	track(tag);
	window.open(url);
}
	

// track
function track(tag) {

	pageTracker._trackPageview(tag);
	
}

function download(url, tag) {
	
	window.open(url);
	var trackUrl = url.replace(new RegExp("//","g"),"/");
	pageTracker._trackPageview("/download/" + trackUrl);

}

function trackPlayMusic(url) {
	
	/*
	pageTracker._trackPageview(tag);
	var pageTracker = _gat._getTracker(acount);
	var trackUrl = url.replace(new RegExp("//","g"),"/");
	pageTracker._trackPageview("/blog/" + language + "/playMusic/" + trackUrl);
	*/

}

function getFlashObj(name){
	var flashObj;
	if(navigator.appName.indexOf ("Microsoft") !=-1){
		flashObj = window[name];
	
	}else{
		flashObj = document[name];
	}
	return flashObj
}

function adjustNavPos(leftX, rightX){

	if (leftX && rightX) {
			
		var navObj = getFlashObj("navigationSwf");
		var footerObj = getFlashObj("footerSwf");
		
		if(navObj && navObj.stageResize){
			try {
				navObj.stageResize(leftX,rightX);
			}
			catch (e) {
			}
		}
		
		if(footerObj && footerObj.footerResize){
			try {
				footerObj.footerResize(leftX+8,rightX-30);
			}
			catch (e) {
			}
		}
	}
	else {
		arrange();
	}

}


function openTerms() {
	var win = window.open("/swf/html/terms.html", "popWin",'width=650,height=600,scrollbars=yes');
	win.focus();
	
}

function openCopyright() {
	var win = window.open("/swf/html/terms.html", "popWin",'width=650,height=600,scrollbars=yes');
	win.focus();
}

function openRss() {
	var win = window.open("http://feeds.feedburner.com/converse/cn", "rss");
	win.focus();
}


function gotoTop(){
	scrollTo(0,0);
}

function setNavMaxHeight(){
	$("#navigationBar").css("height","340px");
}

function setNavMinHeight(){
	$("#navigationBar").css("height","42px");
}

function callMusicPlayer(type){

	window.open('../swf/musicPlayer/musicPlayer.html','newwindow','height=106,width=236');

}

var slideshowKvs = [];
var slideshowKvLinks = [];

function addSlideshow(kv, link) {
	slideshowKvs.push(kv);
	
	if (link) {
		slideshowKvLinks.push(link);
	}
	else {
		slideshowKvLinks.push("");
	}
	
}

function writeSlideshow(kvs) {
	
	var flashvars = {kvs:slideshowKvs.join(","), links:slideshowKvLinks.join(",")};
	var params = {scale:"noScale",allowFullScreen:"true",wmode:"transparent"};
	var attributes = {id: "leftColumnFlash"};
	swfobject.embedSWF(templateUrl + "/swf/frontpage_mini.swf", "leftColumnFlash", "465", "337", "9.0","",flashvars,params,attributes);
	
}

function writeKvSlideshow(kvs) {
	
	var flashvars = {kvs:slideshowKvs.join(","), links:slideshowKvLinks.join(",")};
	var params = {scale:"noScale",allowFullScreen:"true",wmode:"transparent"};
	var attributes = {id: "flashContent"};
	swfobject.embedSWF(templateUrl + "/swf/frontpage.swf", "flashContent", "710", "295", "9.0","", flashvars,params,attributes);
	
}

//////// menu hover ////////
$(document).ready(function() {
  
	function megaHoverOver(){
		$(this).find(".submenu").stop().fadeTo('fast', 1).show();
	}
	
	function megaHoverOut(){ 
	  $(this).find(".submenu").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}


	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("#navigation li .submenu").css({'opacity':'0'});
	$("#navigation li").hoverIntent(config);



});


