/*insert by lichun*/
var commonFn = {};

commonFn.listShowOneDetails = function(o){
	var btn = $(o + " button");
	$(o + " .list-channel-content").hide();
	$(o + " button:first").addClass("current-channel-header");
	$(o + " .list-channel-content:first").show();
	$.each(btn, function(i, n){
	    $(n).click(function(){
	            $(o + " button").removeClass("current-channel-header");
	            $(o + " .list-channel-content").hide();
	            $(this).next().show();
	            $(this).addClass("current-channel-header");;
	     });
	});
};

(function($){
     $(document).ready(function(){
		 $(".main-sub-nav-c1").mouseover(function(){
			  $(this).css("display","block");
		  });
		 $(".main-sub-nav-c1").mouseout(function(){
			  $(this).css("display","none");
		  });
                 $("#main-nav li a").mouseover(function(){
			  $(this).next().css("display","block");
		  });
		 $("#main-nav li a").mouseout(function(){
			  $(this).next().css("display","none");
		  });

		  /*Main right channel list new*/
		  commonFn.listShowOneDetails("#list-channel-items");

		  $("#rec-jCarouse1Lite").jCarouselLite({
							btnNext: "#rec-slide-next",
							btnPrev: "#rec-slide-prev",
							speed:800,
							visible:5});

		  $("#fashion-jCarouse1Lite").jCarouselLite({
							btnNext: "#fashion-slide-next",
							btnPrev: "#fashion-slide-prev",
							speed:800,
							visible:5});

		  $("#slide-image-list").jCarouselLite({
							btnNext:"#home-slide-next",
							btnPrev:"#home-slide-prev",
							speed:800,
							visible:3});	
		  
		  $("#slide-image-list-1").jCarouselLite({
							btnNext: "#home-slide-next-1",
							btnPrev: "#home-slide-prev-1",
							speed:800,
							visible:3});

		  $("#mg-rec-jCarouselLite").jCarouselLite({
							btnNext: "#mg-rec-slide-next",
							btnPrev: "#mg-rec-slide-prev",
							speed:800,
							visible:7});

		  showGalleryNextImg($("#gallery-img-nav"));
                  showGalleryNextImg($("#gallery-img-nav-t"));
		  showGalleryLargeImg($("#rec-jCarouse1Lite-l li a"));
		  showFriendBox($("#final-to-f"));
		  showCommentBox($("#final-post"));
		  showCommentBox($("#c-user-cmt-post"));
		  showCommentBox($("#c-list-post"));
		  $(".btn-reply").each(function(i){showCommentBox($(this));});
		  doZoom();
		  //login box
		  $("#user-login-btn").click(function(){
			var w=document.documentElement.clientWidth;
			var h=document.documentElement.clientHeight;	
			$.blockUI({
				message:$("#login-box"),
				css:{border:"0",cursor:"default",top:(h-390)/2+'px',left:(w-550)/2+'px',width:'550px',"z-index":"10000",display:"block"}
			});	

			showPopupBg($("#popupBg"));

			$(".security-box-close").click(function(){
				$.unblockUI();
				$("#popupBg").css("display","none");
			});
		});

		  //new urser register
		  $("#btn-login-register").click(function(){
			window.location.href="/action/register";
		  });
		  //password recovery box
		  $("#btn-login-recovery").click(function(){
		   showForgotPassword();
		  });

		  $("#btn-forgetpassword").click(function(){
		   showForgotPassword();
		  });

		//edit password
		 $("#chg-pwd").click(function(){
			        var w=document.documentElement.clientWidth;
		                var h=document.documentElement.clientHeight;	
		                $('#txt-email').val("").focus(); 	
		                $.blockUI({
				    message:$("#pwd-change"),
				    css:{border:"0",cursor:"default",top:(h-390)/2+'px',left:(w-550)/2+'px',width:'550px',"z-index":"1000000",display:"block"}
		                });	
		                showPopupBg($("#popupBg"));
		                $(".security-box-close").click(function(){
				        $.unblockUI();
				        $("#popupBg").css("display","none");
		                });
		 });
		 //subscribe newsletter
		  /*$("#subscribe-confirm").click(function(){
			var count=0;			  	
			$("#form-subscribe p :checkbox").each(function(i,o){
				if($(o).attr("checked"))
				{
					count++;
				}
			});
			if(count >= 1)
			{
				$("#subscribe-step1").css("display","none");
				$("#subscribe-step2").css("display","block");	
			}
			else
			{
				//alert("请选择您订阅的Newsletter！");
				$("#form-subscribe p .error").text("请选择您订阅的Newsletter");
			}
		  });	*/
     });

function showPopupBg(o){
		 var sizes=getPageSize();
		 var widthTotal=sizes[0];
		 var heightTotal=sizes[1];
		 $(o).css({"display":"block",
			      "width":widthTotal,
			      "height":heightTotal});	
}

function showCommentBox(o){
	 var textarea=$("#light-box-comment textarea");
         var defaultVal="请在此发表评论，限1000字内。";
	 var cookielogin = getCookie("user_logged");
	 if( $("#checkLogged").length > 0 )
	 {
    	 	$("#checkLogged").empty();
    	 	if(cookielogin == "oui"){
	 		loginname = decodeURIComponent(escape(getCookie('login')));
	 		$("#checkLogged").append(loginname+' 用户，您好！');
         	}else{
	 		$("#checkLogged").append('<label>用户名：<input type="text" name="UserLogin" id="login" /></label><label>密码：<input type="password" name="UserPassword" id="password" /></label>');
		}
    	}
    	o.click(function(){
		var w=document.documentElement.clientWidth;
		var h=document.documentElement.clientHeight;
		textarea.val(defaultVal);
		if(cookielogin == "oui"){
	    		$("#ckbAnonymity").attr("checked",false);
		}
		else{
	    		$("#ckbAnonymity").attr("checked",true);
		}
		//$("#commentID").attr("value",o.val());
		$("#commentID").attr("value",o.attr("name"));
		$.blockUI({
			message:$("#light-box-comment"),
				css:{border:"0",cursor:"default",top:(h-390)/2+'px',left:(w-550)/2+'px',width:'550px',"z-index":"10000"}
		});
		showPopupBg($("#popupBg"));
    	});
    	textarea.click(function(){
		if($(this).val()==defaultVal)textarea.val("");
    	});
    	$("#box-close").click(function(){
		$.unblockUI();
		$("#popupBg").css("display","none");
	});
 }

function showGalleryNextImg(o){
    o.click(function(e){
	var nextImgListId=parseInt($("#gallery-img-key").text());
	var galleryListCount=parseInt($("#gallery-list-count").text());
	var nextGallery=parseInt($("#other-gallery").text());
	var offset=parseInt($("#gallery-img-offset").text());	
	var totalpage=parseInt($("#gallery-img-totalpage").text());	

	if( nextImgListId < galleryListCount ){
	    e.preventDefault();
	    var nav=$("#gallery-img-nav");
	    nav.empty();
	    $("#gallery-img-nav-name").empty();
	    nav.addClass("gallery-img-nav-bg");
	}

	if( nextImgListId == galleryListCount && nextGallery == 0 && offset == (totalpage-1)){
	    e.preventDefault();
	}
			    
	var nextImgHref=$("#small_img"+nextImgListId).attr("href");
	var pageOffset=offset+1;
	var url=$("#gallery-img-url").text();
	var pageUrl="/"+url+"/(offset)/"+pageOffset;
	var img=new Image();img.alt=$(this).attr("title");

	img.onload=function(){
	    setTimeout(function(){
	    nav.empty();
	    nav.append(img);
	    nav.removeClass("gallery-img-nav-bg");},500);
	};

	img.src=$("#small_img"+nextImgListId).attr("href");
	var caption=$("#small_img"+nextImgListId).attr("title");
	$("#gallery-img-nav-name").text(caption);

	if(nextImgListId<(galleryListCount-1) && offset<(totalpage-1)){
	    $("#gallery-img-nav").attr("href",nextImgHref);
	    $("#gallery-img-nav-t").attr("href",nextImgHref);
	}
	else if(offset==(totalpage-1)){
	    if(nextImgListId<(galleryListCount-1)){
		$("#gallery-img-nav").attr("href",nextImgHref);
		$("#gallery-img-nav-t").attr("href",nextImgHref);
	    }
	    else if(nextImgListId==(galleryListCount-1)) {
		if(nextGallery==0){
		    $("#gallery-img-nav").attr("href","javascript:void(0);");
		    $("#gallery-img-nav-t").attr("href","javascirpt:void(0);");
		}
		else {
		    $("#gallery-img-nav").attr("href","/"+url);
		    $("#gallery-img-nav-t").attr("href","/"+url);
		}
	    }
	}
	else{
	    $("#gallery-img-nav").attr("href",pageUrl);
	    $("#gallery-img-nav-t").attr("href",pageUrl);
	}
	if( nextImgListId < galleryListCount ){
	    $("#gallery-img-key").text(nextImgListId+1);
	}

	var currentGalleryImgKey = $("#gallery-img-key").text() - 1;
	if( currentGalleryImgKey < 0 )
	{
	    currentGalleryImgKey = 0;
	}
	else if ( currentGalleryImgKey >= galleryListCount )
	{
	    currentGalleryImgKey = galleryListCount;
	}
	var lightboximage = "#lightbox-image-list" + currentGalleryImgKey;
	$("#lightbox-zoom a").attr("href", $(lightboximage).text());
    });
}
		
function showGalleryLargeImg(o){
    $.each(o,function(i,n){
	$(n).click(function(e){
	    e.preventDefault();
	    var nav=$("#gallery-img-nav");
	    nav.empty();
	    nav.addClass("gallery-img-nav-bg");
	    var img=new Image();
	    var imgListId=parseInt($(this).attr("id"));
	    img.alt=$(this).attr("title");
	    img.onload=function(){
		setTimeout(function(){
		nav.empty();
		nav.append(img);
		nav.removeClass("gallery-img-nav-bg");
		},500);};
	    img.src=$(this).attr("href");
	    var caption=$(this).attr("title");
	    $("#gallery-caption").empty();
	    $("#gallery-caption").append(caption);
	    $("#gallery-img-key").text(imgListId+1);
	    var lightboximage = "#lightbox-image-list"+imgListId;
	    $("#lightbox-zoom a").attr("href", $(lightboximage).text());
	});
    });
}

function showFriendBox(o){
    o.click(function(){
	var w=document.documentElement.clientWidth;
	var h=document.documentElement.clientHeight;
	$.blockUI({
		message:$("#light-box-friend"),css:{cursor:"default",top:(h-390)/2+'px',left:(w-550)/2+'px',width:'550px',border:'0','z-index':'10000'}
	});
	showPopupBg($("#popupBg"));
    });
}

function showForgotPassword(){
	 var w=document.documentElement.clientWidth;
	 var h=document.documentElement.clientHeight;	
	 $('#txt-email').val("").focus(); 	
	 $.blockUI({
			message:$("#pwd-recovery"),
			css:{border:"0",cursor:"default",top:(h-390)/2+'px',left:(w-550)/2+'px',width:'550px',"z-index":"1000000",display:"block"}
	 });	
	 showPopupBg($("#popupBg"));
	 $(".security-box-close").click(function(){
			$.unblockUI();
			$("#popupBg").css("display","none");
	});
	         	
}

function doZoom() {
	$('.font-choose  a').each(function(i){
	    if(i<3)
	    {
		var fontID = 'font_'+i;	
		$('.font-choose a').each(function(i){
			if($('.final-p p').css('font-size') == '14px')
				$('#font_1').css('font-weight','bold');
		});
		$('#'+fontID).click(function(){
			$('.font-choose a').each(function(i){
				var fID = 'font_'+i;	
				$('#'+fID).css('font-weight','normal');
			});

			var fontSize = $(this).attr("title");
			$('.final-p').css('font-size',fontSize+'px');

			if($('.final-p').css('font-size') == fontSize+'px')
				$('#'+fontID).css('font-weight','bold');

			$('.final-p p').each(function(i){
				$(this).css('font-size',fontSize+'px');
			});

			$('.final-p p a').each(function(i){
				$(this).css('font-size',fontSize+'px');
			});

			$('.final-p p strong').each(function(i){
				$(this).css('font-size',fontSize+'px');
			});

			$('.final-p p b').each(function(i){
				$(this).css('font-size',fontSize+'px');
			});

			$('.final-p p a strong').each(function(i){
				$(this).css('font-size',fontSize+'px');
			});	

			$('.final-p p a b').each(function(i){
				$(this).css('font-size',fontSize+'px');
			});	
		});
	    }
	});
}

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

})(jQuery);

function setHomepage(url)
{
    if (document.all)
    {
        document.body.style.behavior='url(#default#homepage)';
        document.body.setHomePage(url);
    }
    else if (window.sidebar)
    {
        if(window.netscape)
        {
            try
            {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch(e)
            {
                alert("该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true");
            }
         }
         var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
         prefs.setCharPref('browser.startup.homepage',url);
     }
}
