﻿//获取窗口宽度
function getTotalWidth() {
	if ($.browser.msie) {
		return document.compatMode == "CSS1Compat" ? document.documentElement.clientWidth : document.body.clientWidth;
	} else {
		return self.innerWidth;
	}
}

//获取窗口高度
function getTotalHeight() {
	if ($.browser.msie) {
		return document.compatMode == "CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;
	} else {
		return self.innerHeight;
	}
}

// 滑动tab
var msn = msn || {};
msn.hp = msn.hp || {};
msn.hp.tab = {
	t: null,
	delayTime: 150,
	fx: true,
	tab: function(b) {
		$(b).siblings().removeClass("curTitle");
		$(b).addClass("curTitle");
		var c = $(b).parents(".tab").find("div.tabContent");
		var a = c.eq($(b).index());
		c.addClass("hide");
		a.removeClass("hide");
		if (this.fx) {
			if ($(b).parent().hasClass("nofx")) {
				return
			}
			$(b).parent().siblings(".tabAnimate").width($(b).outerWidth() - 2).animate({
				left: $(b).position().left
			}, "slow")
		}
	},
	delayTab: function(b, a) {
		clearTimeout(b.t);
		this.t = setTimeout(function() {
			b.tab(a)
		}, this.delayTime)
	},
	init: function() {
		var a = this;
		a.animate();
		if (window.Touch) {
			$(".tab .tabTitle>ul>li[class!='curTitle']>a").click(function() {
				return false
			})
		}
		$(".tab .tabTitle>ul>li, .tab>ul.hotread_menu>li").hover(function() {
			a.delayTab(a, this)
		},
		function() {
			clearTimeout(a.t)
		})
	},
	animate: function() {
		if (!this.fx) {
			return
		}
		$(".tab .tabTitle>ul").each(function() {
			if (!$(this).hasClass("nofx")) {
				$(this).addClass("fx")
			}
		});
		$(".tab .tabTitle").each(function(a, b) {
			if ($(this).find("ul").hasClass("nofx")) {
				return
			}
			$(b).append("<div class='tabAnimate' ></div>");
			$(b).find(".tabAnimate").width($(b).find("ul>li.curTitle").outerWidth() - 2).css("left", $(b).find("ul>li.curTitle").position().left)
		})
	}
};

$(document).ready(function() {	
    //时间
    var now = new Date();
    var year, month, date;

    year = now.getFullYear();
    month = now.getMonth() + 1;
    date = now.getDate();

    month = ((month < 10) ? "0" : "") + month;
    date = ((date < 10) ? "0" : "") + date;

    var dateNow = year + "年" + month + "月" + date + "日";
    var week;

    week = now.getDay();

    $.each(["天", "一", "二", "三", "四", "五", "六"], function(i, n) {
        if (i == week) { week = n; return; }
    });

    var weekNow = "星期" + week;

    $(".dateweek").html(dateNow + "   " + weekNow);

    //滑动tab
	var a = msn.hp;
	a.tab.init();

    //在适合宽度下隐藏水平滚动条
    if (getTotalWidth() >= 950) {
        $("html").css("overflow-x", "hidden");
    }

    //链接虚框
    $("a").focus(function() {
        $(this).blur();
    });

    //顶部下拉菜单	
    $(".topHover").hover(function() {
        $(this).find(".topList").slideDown("fast");
    }, function() {
        $(this).find(".topList").slideUp("fast");
    });

    //顶部菜单随屏幕滚动
    $topFadeTo = function() {
        var st = $(document).scrollTop(), winh = $(window).height();
        (st > 0) ? $(".top .topBg").fadeTo(100, 0.8) : $(".top .topBg").fadeTo(100, 1.0);
    }
    $(window).bind("scroll", function() {
        $topFadeTo();
        //IE6下的定位
        if (!window.XMLHttpRequest) {
            //adCouplet 为对联广告 loginWindow 为登录窗口
            $(".top").add(".adCouplet").add(".loginWindow").css("top", $(document).scrollTop());
        }
    });
    $(".top").hover(function() {
        $(this).find(".topBg").fadeTo(100, 1.0);
    }, function() {
        $topFadeTo();
    });

    //登录窗口
    $(".btnLogin").attr("href", "javascript:void(0)").click(function() {
        $("html").css("overflow-y", "hidden");
        $(".loginWindow").add(".loginBg").width(getTotalWidth()).height(getTotalHeight());
        $(".loginWindow").show();
    });
    $(window).resize(function() {
        $(".loginBg").width(getTotalWidth()).height(getTotalHeight());
    });

    //返回顶部
    var $backToTopTxt = "返回顶部", $backToTopEle = $('<div class="backToTop"></div>').appendTo($("body"))
    //.text($backToTopTxt)
		.attr("title", $backToTopTxt).click(function() {
		    $("html, body").animate({ scrollTop: 0 }, 120);
		}), $backToTopFun = function() {
		    var st = $(document).scrollTop(), winh = $(window).height();
		    (st > 0) ? $backToTopEle.fadeIn() : $backToTopEle.fadeOut();
		    //IE6下的定位
		    if (!window.XMLHttpRequest) {
		        $backToTopEle.css("top", st + winh - 60);
		    }
		};
    $(window).bind("scroll", $backToTopFun);
    $(function() { $backToTopFun(); });

    //表单焦点
    $.fn.inputFocus = function(focusText) {
        var thisColor = $(this).css("color");
        var thisBg = $(this).css("background-color");
        var thisBorder = $(this).css("border-color");
        $(this).focus(function() {
            if ($(this).val() == "" || $(this).val() == focusText) {
                $(this).val("");
            }
            $(this).css({ "color": "#444", "background-color": "#fff", "border-color": "#cc6" });
        }).blur(function() {
            if ($(this).val() == "" || $(this).val() == focusText) {
                $(this).val(focusText);
                $(this).css({ "color": thisColor, "background-color": thisBg, "border-color": thisBorder });
            } else {
                $(this).css({ "color": "#444", "background-color": "#f7f7f7", "border-color": "#ddd" });
            }
        });

    }
    $(".inputText:not('.shareCopy .inputText')").each(function() {
        $(this).inputFocus($(this).attr("value"));
    });
    $("textarea").each(function() {
        $(this).inputFocus($(this).html());
    });

    //选项卡
    var autoIndex = 0;
    var autoTime = 3000;
    var tabStar
    function tabHover(tabTarget) {
        autoIndex = tabTarget.index();
        tabTarget.addClass("curTitle").siblings("h3").removeClass("curTitle");
        tabTarget.closest(".channelTab").find(".tabMain").eq(autoIndex).show().siblings(".tabMain").hide();
    }
    function tabAuto() {
        $(".autoTab .tabName h3").eq(autoIndex).each(function() {
            tabHover($(this));
        });
        if (autoIndex < ($(".autoTab .tabName h3").length) - 1) {
            autoIndex++;
        } else {
            autoIndex = 0;
        }
    }
    $(".channelTab .tabName h3").each(function() {
        if ($(this).hasClass("curTitle")) {
            tabHover($(this));
        }
    }).hover(function() {
        clearInterval(tabStar);
        tabHover($(this));
    }, function() {
        tabStar = setInterval(tabAuto, autoTime);
    });
    $(".autoTab .tabMain").hover(function() {
        clearInterval(tabStar);
    }, function() {
        tabStar = setInterval(tabAuto, autoTime);
    });
    //自动播放
    tabStar = setInterval(tabAuto, autoTime);

    //排行版
    $(".rankStyle").each(function() {
        var rankNum = 0;
        $(this).find("li").each(function(rankNum) {
            rankNum++;
            $(this).css("background-image", "url(/images/rank/" + rankNum + ".png)");
        });
    });

    //顶一下 踩一下
    $(".diggGood").hover(function() {
        $(this).addClass("diggGoodHover");
    }, function() {
        $(this).removeClass("diggGoodHover");
    });
    $(".diggBad").hover(function() {
        $(this).addClass("diggBadHover");
    }, function() {
        $(this).removeClass("diggBadHover");
    });

    // 商品分享
    $(".articleShare .im").click(function() {
        if ($(this).hasClass("imOpen")) {
            $(this).removeClass("imOpen");
            $(this).parent().find(".shareCopy").slideUp("fast");
        } else {
            $(this).addClass("imOpen");
            $(this).parent().find(".shareCopy").slideDown("fast");
        }
    });

    //复制地址
    $(".shareCopy p .inputBtn").click(function() {
        var copyText = $(this).parent().find(".inputText").val();
        //判断ie
        if (document.all) {
            window.clipboardData.setData("text", copyText);
            alert($(this).parent().find(".inputText").attr("tip"));
        } else {
            alert("您的浏览器不支持此操作，请手动复制文本框中的内容。");
        }
    });

    //频道焦点图片
    var sWidth = $(".channelFocus").width(); //获取焦点图的宽度（显示面积）
    var len = $(".channelFocus ul li").length; //获取焦点图个数
    var index = 0;
    var picTimer;
    //以下代码添加数字按钮和按钮后的半透明长条
    var btn = "<div class='btnBg'></div><div class='btn'>";
    for (var i = 0; i < len; i++) {
        btn += "<span>" + (i + 1) + "</span>";
    }
    btn += "</div>"
    $(".channelFocus").append(btn);
    $(".channelFocus .btnBg").css("opacity", 0.5);
    //为数字按钮添加鼠标滑入事件，以显示相应的内容
    $(".channelFocus .btn span").mouseenter(function() {
        index = $(".channelFocus .btn span").index(this);
        showPics(index);
    }).eq(0).trigger("mouseenter");
    //本例为左右滚动，即所有li元素都是在同一排向左浮动，所以这里需要计算出外围ul元素的宽度
    $(".channelFocus ul").css("width", sWidth * len);
    //鼠标滑入某li中的某div里，调整其同辈div元素的透明度，由于li的背景为黑色，所以会有变暗的效果
    //$(".channelFocus ul li div").hover(function() {
    //	$(this).siblings().css("opacity",0.7);
    //},function() {
    //	$(".channelFocus ul li div").css("opacity",1);
    //});
    //鼠标滑上焦点图时停止自动播放，滑出时开始自动播放
    $(".channelFocus").hover(function() {
        clearInterval(picTimer);
    }, function() {
        picTimer = setInterval(function() {
            showPics(index);
            index++;
            if (index == len) { index = 0; }
        }, 4000); //此3000代表自动播放的间隔，单位：毫秒
    }).trigger("mouseleave");
    //显示图片函数，根据接收的index值显示相应的内容
    function showPics(index) {
        var nowLeft = -index * sWidth; //根据index值计算ul元素的position
        $(".channelFocus ul").stop(true, false).animate({ "left": nowLeft }, 500); //通过animate()调整ul元素滚动到计算出的position
        $(".channelFocus .btn span").removeClass("on").eq(index).addClass("on"); //为当前的按钮切换显示效果
    }
	
	//文章图片最大宽度
	$(".articleContent img").each(function() {
		var maxWidth = 570;
		var imgWidth  = $(this).width();
		var imgHeight  = $(this).height();
		//当图片宽度大于最大宽度时，高度按比例缩小
		if (imgWidth > maxWidth) {
			$(this).width(maxWidth);
			var newHeight = imgHeight * (maxWidth / imgWidth);
			$(this).height(newHeight);
		}
	});
});
