﻿/*
	@ bayway.net.cn ;
	Creat by mypenn@gmail.com ;
*/

/*Page Loader*/
$(function(){
    var h;
    if(screen.height<=768)
    {
        h=396;
    }
    if(screen.height==800)
    {
        h=396;
    }
    if(screen.height==854)
    {
        h=412;
    }
    if(screen.height==864)
    {
        h=422;
    }
    if(screen.height>=900)
    {
        h=458;
    }
	$("body").height(h*3 + ___getPageSize()[3]);
	Pagesize();
	$("#Menu li:first a").addClass("active");
	$("#ContainerWrapper").css({marginTop: 0});
	
	showScroll();
	ImgRun();
	//newsShow();
	
	$("#ScrollWrapper ul").width(($("#ScrollWrapper ul li").outerWidth() + 1) * $("#ScrollWrapper ul li").length);
	xScroll("#TopScroll","#ScrollWrapper");
	$("#ScrollWrapper ul li").each(function(){
		var thisobj = this;
		$("img,h5,p",this).click(function(){
			showBigPicture(thisobj)
		});
	})
	/*.hover(function(){
		$("img",this).stop().animate({
			width:298,
			height:225,
			margin:10
		});
	},function(){
		$("img",this).stop().animate({
			width:318,
			height:245,
			margin:0
		});
	});
	*/
	
	//
	//$("#Scroll2 ul").width(($("#Scroll2 ul li").outerWidth() + 1) * $("#Scroll2 ul li").length);
	//xScroll("#BotScroll","#Scroll2");
	
	if(isIE6()){
		$("#ScrollWrapper ul li").hover(function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		});
	}
	
	//$(".main-scroll-wrap").each(function(){
		$(".main-scroll-wrap").hover(function(){
			var obj = $(this).prev();
			var btn = $("span",this);
			$(this).mousedown(function(event){
				event = window.event || event;
				var x = event.clientY;
				var btnTop = x - $(this).offset().top - 24;
				if(btnTop>310){
					btnTop = 310;
				}else if(btnTop<0){
					btnTop = 0;
				}
	
				btn.css("margin-top",btnTop);
				obj.stop().animate({
					marginTop: -(obj.outerHeight() - 358) * btnTop / 310
				})
				
				var a = parseInt(btn.css("margin-top"));
				
				document.onmousemove = function(event){
					event = window.event || event;
					var btnTop = a-x+event.pageY;
					if(event.clientY){
						btnTop = a-x+event.clientY;
					}
					if(btnTop>310){
						btnTop = 310;
					}else if(btnTop<0){
						btnTop = 0;
					}
					btn.css({"margin-top":btnTop});
					
					obj.stop().css({
						marginTop: -(obj.outerHeight() - 358) * btnTop / 310
					})
					
					return false;
				}
			}).mouseup(function(){
				document.onmouseup = function(){
					document.onmousemove = function(event){
						null;
					}
				}
			});
			
		},function(){
			document.onmouseup = function(){
				document.onmousemove = function(event){
					null;
				}
			}
		});
	//});
	
	//$(".main-scroll").each(function(){
		
		$(".main-scroll").hover(function(){
			var obj2 = this;
			WinScT = $(window).scrollTop();
			if($.browser.msie){
				this.attachEvent('onmousewheel', function(event){
					var btn = $(".main-scroll-wrap span",obj2);
					var obj = $(".main-cont",obj2);
					
					var btnTop = parseInt(btn.css("margin-top"));
					if(btn.css("margin-top") =="auto"){
						btnTop = 0;
					}
					if(event.wheelDelta > 0){
						btnTop >= 18 ? btnTop -= 18 : btnTop = 0;
					}else{
						btnTop <= 292 ? btnTop += 18 : btnTop = 310;
					}
					
					btn.css("margin-top",btnTop);
					obj.stop().css({
						marginTop: -(obj.outerHeight() - 358) * btnTop / 310
					})
					
					$(window).scrollTop(WinScT);
				});
			}else{
				addScrollListener(this, wheelHandle);
			}
		});
	//});
	
	//
	addScrollListener(document, __wheelType);
	
	//
	$(".news-list").each(function(){
		var i =  Math.ceil($("ul li",this).length / 4);
		$(".page",this).empty();
		for(var n=1;n<=i;n++){
			$(".page",this).append("<a href='javascript:void(0)' onclick='getPage("+n+",this)'>"+n+"</a>")
		}
		
		$(".page",this).children("a").eq(0).click();
	});
})
var WinScT = 0;

//
function showScroll(){
	$(".main-scroll").each(function(){
		if($(".main-cont",this).height() > 358){
			$(".main-scroll-wrap",this).show();
		}else{
			$(".main-scroll-wrap",this).hide();
		}
	});
}

//
function xScroll(btnObj,scrollObj){
	var btn = $("span",btnObj);
	var scr = $("ul",scrollObj);
	$(btnObj).hover(function(){
		$(this).mousedown(function(event){
			event = window.event || event;
			var x = event.pageX || event.clientX;
			var btnLeft = x - $(this).offset().left - btn.width() / 2;
			var btnScrWax = $(btnObj).width() - btn.width();
			var scrWrapWidth = $(scrollObj).width();
			var scrWidth = scr.width();
	
			if(btnLeft>btnScrWax){
				btnLeft = btnScrWax
			}else if(btnLeft<0){
				btnLeft= 0;
			}
			
			btn.css("margin-left",btnLeft);
			scr.stop().animate({
				marginLeft: -(scrWidth - scrWrapWidth) * btnLeft / btnScrWax
			})
			
			var a = parseInt(btn.css("margin-left"));
			
			document.onmousemove = function(event){
				event = window.event || event;
				var btnLeft = a-x+event.pageX;
				if(event.clientX){
					btnLeft = a-x+event.clientX;
				}
				if(btnLeft>btnScrWax){
					btnLeft = btnScrWax;
				}else if(btnLeft<0){
					btnLeft= 0;
				}
				btn.css({"margin-left":btnLeft});
				
				scr.stop().css({
					marginLeft: -(scrWidth - scrWrapWidth) * btnLeft / btnScrWax
				})
				
				return false;
			}
		}).mouseup(function(){
			document.onmouseup = function(){
				document.onmousemove = function(event){
					null;
				}
			}
		});
	},function(){
		document.onmouseup = function(){
			document.onmousemove = function(event){
				null;
			}
		}
	})
}

//
function showBigPicture(obj){
	var script = $(obj).children("script");
	if(script.length == 1){
		var data = $(obj).children("script");
		data = eval("("+data.html()+")");
	}
	
	var size = ___getPageSize();
	var srollsize = ___getPageScroll(); 
	
	if(data && data.list.length>0){
		$(obj).addClass("hover");
		var len = data.list.length,thisi = 1;
		ImgRunHTML();
		$("#imgCont ul").width(700 * len).empty();
		$("#imgNum span:eq(0)").html(1);
		$("#imgNum span:eq(1)").html(len);
		
		$("#bgCover").css({
			height:size[1],
			"opacity":0.5
		}).fadeIn();
		
		$("#imgRunner").css({
			top:srollsize[1] + (size[3] - $("#imgRunner").height()) / 2,
			left:srollsize[0] + (size[2] - $("#imgRunner").width()) / 2
		}).fadeIn();
		
		$.each(data.list,function(){
			$("#imgCont ul").append("<li><img src=\""+this[0]+"\" alt=\"\" /><h5>"+this[1]+"</h5><p>"+this[2]+"</p></li>");
		});
		
		$("#BtnNext").click(function(){
			thisi < len ? thisi++ : thisi = 1;
			$("#imgNum span:eq(0)").html(thisi);
			$("#imgCont ul").stop().animate({marginLeft:-thisi*700 + 700})
		});
		
		$("#BtnPrev").click(function(){
			thisi > 1 ? thisi-- : thisi = len;
			$("#imgNum span:eq(0)").html(thisi);
			$("#imgCont ul").stop().animate({marginLeft:-thisi*700 + 700})
		});
		
		$("#CloseThis").click(function(){
			$(obj).removeClass("hover");
			$("#imgRunner").hide();
			$("#bgCover").fadeOut();
		});
		
		$(window).scroll(function(){
			var size = ___getPageSize();
			var srollsize = ___getPageScroll(); 
			$("#imgRunner").css({
				top:(srollsize[1] + (size[3] - $("#imgRunner").height()) / 2) > 0 ? srollsize[1] + (size[3] - $("#imgRunner").height()) / 2 : 0,
				left:(srollsize[0] + (size[2] - $("#imgRunner").width()) / 2) > 0 ?srollsize[0] + (size[2] - $("#imgRunner").width()) / 2 :0
			});
		});
		
		
		$(window).resize(function(){
			var size = ___getPageSize();
			var srollsize = ___getPageScroll(); 
			$("#imgRunner").css({
				top:(srollsize[1] + (size[3] - $("#imgRunner").height()) / 2) > 0 ? srollsize[1] + (size[3] - $("#imgRunner").height()) / 2 : 0,
				left:(srollsize[0] + (size[2] - $("#imgRunner").width()) / 2) > 0 ?srollsize[0] + (size[2] - $("#imgRunner").width()) / 2 :0
			});
		})
	}	
}

//
function isIE6(){
	var b=false;
	if($.browser.msie && parseInt($.browser.version,10)<7){
		b=true;
	}else{
		b=false;
	}
	return b;
}

function ImgRun(){
	$(".imgrun").each(function(){
		$(this).append("<p></p>");
		var u = $("ul",this);
		var p = $("p",this);
		var h= $(this).height();
		
		$("li",u).each(function(i){
			i++;
			p.append("<a href='javascript:void(0)'>"+i+"</a>");
		})
		
		var n=0;
		$("a",p).hover(function(){
			n = $("a",p).index($(this));
			$(this).addClass("active").siblings().removeClass("active");
			u.stop().animate({marginTop:-h*n}); 
		})
		
		$("a:first",p).mouseover();
	})
}


//
function ToggleType(t,o){
	var obj = $(t).parent("h2");
	$(obj).hide().siblings().show();
	
	$(o).show().siblings().hide();
	
	showScroll();
}

//

function ImgRunHTML(){
	var thisHTML = "<div id=\"imgRunner\">";
		thisHTML += "<a id=\"CloseThis\" href=\"javascript:void(0)\"></a>";
		thisHTML += "<span id=\"imgNum\"><span></span>/<span></span></span>";
		thisHTML += "<a id=\"BtnPrev\" href=\"javascript:void(0)\"></a>";
		thisHTML += "<a id=\"BtnNext\" href=\"javascript:void(0)\"></a>";
		thisHTML += "<div id=\"imgCont\">";
		thisHTML += "<ul></ul>";
		thisHTML += "</div></div>";

	if($("#bgCover").length == 0){
		$("body").append("<div id=\"bgCover\"></div>");
	}
	
	if($("#imgRunner").length == 0){
		$("body").append(thisHTML);
	}


}

//
function closeShow(obj){
	$(obj).parent().hide();
}

function newsShow(){
	$(".news-list").each(function(i){
		var showDiv = $(this).children("div.news-show");
		var iframe = showDiv.children("iframe");
		$(".news-top a,ul li a",this).click(function(){
			var url = $(this).attr("href");
			iframe.attr("src",url);
			showDiv.show();
			return false;
		})
		
	});
}

function wheelHandle(event){
	event = window.event || event;
	var wheelType;
	if(event.wheelDelta) {
        wheelType = event.wheelDelta > 0 ? 1 : -1;
    } else {
        wheelType = event.detail < 0 ? 1 : -1;
    }

	var btn = $(".main-scroll-wrap span",this);
	var obj = $(".main-cont",this);
	
	var btnTop = parseInt(btn.css("margin-top"));
	if(wheelType == 1){
		btnTop >= 36 ? btnTop -= 36 : btnTop = 0;
	}else{
		btnTop <= 274 ? btnTop += 36 : btnTop = 310;
	}
	
	btn.css("margin-top",btnTop);
	obj.stop().css({
		marginTop: -(obj.outerHeight() - 358) * btnTop / 310
	})
	
	$(window).scrollTop(WinScT);
}

/*Window Loader*/
$(window).resize(function(){
    var h;
    if(screen.height<=768)
    {
        h=396;
    }
    if(screen.height==800)
    {
        h=396;
    }
    if(screen.height==854)
    {
        h=412;
    }
    if(screen.height==864)
    {
        h=422;
    }
    if(screen.height>=900)
    {
        h=458;
    }
	Pagesize();
	$("body").height(h*3 + ___getPageSize()[2]);
});

/*Scroll*/
var pageNum;
$(window).scroll(function(event){
    var h;
    if(screen.height<=768)
    {
        h=396;
    }
    if(screen.height==800)
    {
        h=396;
    }
    if(screen.height==854)
    {
        h=412;
    }
    if(screen.height==864)
    {
        h=422;
    }
    if(screen.height>=900)
    {
        h=458;
    }
	var scrollsize = $(window).scrollTop();
	var x = Math.round(scrollsize/h);
	$("#Menu li").eq(x).children("a").addClass("active").parent().siblings().children("a").removeClass("active");
	$("#ContainerWrapper").stop().animate({
		marginTop:-x * h
	},500);
	pageNum = x;
	
	addScrollListener(document, __wheelType);
});

function __wheelType(event){
	var pageScrollTop;
	event = window.event || event;
	if(event.wheelDelta) {
		if(event.wheelDelta>0){
			pageScrollTop = "true";
		}else{
			pageScrollTop = "false";
		}
	}else {
		if(event.detail>0){
			pageScrollTop = "true";
		}else{
			pageScrollTop = "false";
		}
	}
	
	/*
	if(pageScrollTop == "true"){
		$("div.banner").eq(pageNum-1).stop().css("margin-top",260).animate({
			marginTop:0
		})
	}else{
		$("div.banner").eq(pageNum-1).stop().animate({
			marginTop:260
		},400,function(){
			$("div.banner").css("margin-top",0)
		})
	}
	*/

}

$(window).load(function(){

});

/**/
function Pagesize(){
	var size = ___getPageSize();
	var srollsize = ___getPageScroll();
	if(size[2] < 981){
		$("#PageContainer").width(981);
	}
	else{
		$("#PageContainer").width(981);
	}
	$("#Container").width($("#PageContainer").width() - $("#Menu").width());
	
	$(".main").css({
		"margin-left":($("#Container").width()-922)/2 
	})
	
	$("body").width($("#PageContainer").width(981));
}
//
function getPage(i,obj){
	$(obj).addClass("active").siblings().removeClass("active");
	var obj = $(obj).parent().parent().find("ul");
	var h = $("li",obj).outerHeight() * 4;
	obj.stop().animate({
		marginTop: - h * (i-1)
	})
}

/**/

function isIE6(){
	if($.browser.msie && parseInt($.browser.version,10) < 7){
		return true;
	}else{
		return false;
	}
}

/**/
function pageScroll(i,obj){
    var h;
    if(screen.height<=768)
    {
        h=396;
    }
    if(screen.height==800)
    {
        h=396;
    }
    if(screen.height==854)
    {
        h=412;
    }
    if(screen.height==864)
    {
        h=422;
    }
    if(screen.height>=900)
    {
        h=458;
    }
	$(window).scrollTop(i*h);
	$(obj).blur();
	//$(obj).addClass("active").parent().siblings().children("a").removeClass("active");
}

/**/
//==================================================
/**
 / THIRD FUNCTION
 * getPageSize() by quirksmode.com
 */
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		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
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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 = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
/**
 / THIRD FUNCTION
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};


//
/**
 * 注册滚轮事件函数
 * @param element     : 注册的事件对象
 * @param wheelHandle : 注册事件函数
 */
function addScrollListener(element, wheelHandle) {
    if(typeof element != 'object') return;
    if(typeof wheelHandle != 'function') return;
    // 监测浏览器
    if(typeof arguments.callee.browser == 'undefined') {
        var user = navigator.userAgent;
        var b = {};
        b.opera = user.indexOf("Opera") > -1 && typeof window.opera == "object";
        b.khtml = (user.indexOf("KHTML") > -1 || user.indexOf("AppleWebKit") > -1 || user.indexOf("Konqueror") > -1) && !b.opera;
        b.ie = user.indexOf("MSIE") > -1 && !b.opera;
        b.gecko = user.indexOf("Gecko") > -1 && !b.khtml;
        arguments.callee.browser = b;
    }
    if(element == window){
        element = document;
	}
    if(arguments.callee.browser.ie){
        element.attachEvent('onmousewheel', wheelHandle);
	}else{
        element.addEventListener(arguments.callee.browser.gecko ? 'DOMMouseScroll' : 'mousewheel', wheelHandle, false);
	}
}
