﻿jQuery(document).ready(function () {
	
	//png底圖修復
	if ($.browser.msie && $.browser.version.substring(0,1) === '6') {
	$('#logo, #IndexBannerBG, #IndexCourse, #IndexMusicTheory, #leftAD, #IndexMusicTheory, #IndexQA, .Courses, .News, .AboutUs, .VideoAlbum, .Careers, .Board, .Contactus').supersleight();
	}
	
	//圓角
	if ( $("#NewCourseBox, #LinkBox").length > 0 ) { //如果當前元素存在...
		$("#NewCourseBox, #LinkBox").corner(); //AlbumList頁面下方，相關文章區塊
	}

	

//	if ( $("embed").length > 0 ) { //如果當前元素存在..
		$("object embed").attr("wmode","transparent");
//	}
	
	
	//在要顯示信箱的地方加上一個class="getmail"的標籤，
	//如：信箱：<span class="getmail">載入中，請稍後... </span>
    var mailhead = "service";
    var mailbody = "@dvhousing.com.tw";
	if ( $(".getmail").length > 0 ) { //如果當前元素存在...
		setTimeout(function(){
			$('.getmail').html(mailhead + mailbody);
		}, 6000);
	}
	
	
    //首頁Tab頁籤
	if($("#News").length > 0 ) { //如果當前元素存在...
		//啟動首頁Tab頁籤功能
		$("#flowtabs").tabs(".panes", { history: true, effect:'fade'});
		//alert (window.location.pathname);
	}
	if($("#room").length > 0 ) { //如果當前元素存在...
		//啟動首頁Tab頁籤功能
		$("#roomTabs").tabs("div.roompPanes > div", {initialIndex:2, effect:'fade'});
		//alert (window.location.pathname);
	}
	
	//告知瀏覽器版本太舊
	if ($.browser.msie && $.browser.version.substring(0,1) === '6' && $.cookie('ie6_cookie') == null) {
		alert("您所使用的瀏覽器已過於老舊，無法使用本站許多的新功能，請更換瀏覽器再瀏覽本站，謝謝。");
		$.cookie('ie6_cookie', 'yes', { expires: 1, path: '/'}); //寫入cookie值
	}
	
	
	
	
	//上方超級選單
	//On Hover Over
	if ( $(this).find("#topnav").length > 0 ) {
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo(0, 0.9).show(); //Find sub and fade it in(
			
		//用函數來計算ul的總寬度
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//計算 row
				$(this).find("ul").each(function() { //for each ul...
					rowWidth += $(this).width(); //將所有ul的寬度加總
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //如果 .row 存在..
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() { //for each row...
				$(this).calcSubWidth(); //Call function to calculate width of all ul's
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow}); //Set width
			$(this).find(".row:last").css({'margin':'0'}); //Kill last row's margin
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth(); //Call function to calculate width of all ul's
			$(this).find(".sub").css({'width' : rowWidth+20}); //Set Width
			
		}
	}
	//On Hover Out
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo(1, 0, function() { //Fade to 0 opactiy
		  $(this).hide(); //after fading, hide it
	  });
	}
 
    //客製化設定檔參數
	var config = {    
		 sensitivity: 2, // 數值 = 靈敏度設置 (必須1或更高，原設值2)    
		 interval: 100, // 數值 = 滑鼠移到選單後幾秒才會展開(原設值100)   
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 100, // 數值 = 滑鼠移出後延遲多久才消失(原設值500)
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
 
	//$("ul#topnav li .sub").css({'opacity':'0'}).corner("bottom"); //Fade sub nav to 0 opacity on default，corner("bottom")增加子選單下方圓角;
	$("ul#topnav li").hoverIntent(config); //觸發超級選單客製化設定檔
	
	$('div.row').last().remove();
	
	}
    //超級選單結束
	
	
	
	
}); //$(document).ready end






	//在頁面引入此js，
	//然後在要替換的<img>標籤內，加上 onerror="errorimg(this)"
	var rimg = "../images/Sample.jpg"; //當無圖片時，顯示的替代圖
	function errorimg(x, y) {
		if (y == null) {
			x.src = rimg;
		}
		else {
			x.src = y;
		}
	}
	
	
//allMenu效果	
$(function(){
	
	$('.picButton').mousedown(function(){
		autoRun = false;
		nextpic($('.picButton').index(this));
	});
	$('.picBackButton').mousedown(function(){
		autoRun = false;
		var backIndex = (moveIndex-1) < 0 ? $('.pic').length-1 : moveIndex-1;
		nextpic(backIndex);
	});
	$('.picNextButton').mousedown(function(){
		autoRun = false;
		var nextIndex = (moveIndex+1) >= $('.pic').length ? 0: moveIndex+1;
		nextpic(nextIndex);
	});
	
		function nextpic(index){
			if(moveIndex!=index && !moveing){
				$('.picButton').removeClass('nowpic').eq(index).addClass('nowpic');
				var picLength = $('.pic').length;
				var postion = -(index*1000) + 'px';
				moveing = true;
				moveIndex = index;
				if ($.browser.msie && $.browser.version.substring(0,1) == '8') {
				$('.carouselMove').animate({'margin-left':postion},1000,function(){
					moveing=false;
				});
				}else{	
				$('.carouselMove').animate({opacity:.0},300,function(){
					$(this).css('margin-left',postion);
					$(this).animate({opacity:1},700,function(){moveing=false;});
				});	
				}
			}
		}
	
//		function nextpic(index){
//			if(moveIndex!=index && !moveing){
//				$('.picButton').removeClass('nowpic').eq(index).addClass('nowpic');
//				var picLength = $('.pic').length;
//				var postion = -(index*1000) + 'px';
//				moveing = true;
//				moveIndex = index;
//				$('.carouselMove').animate({opacity:.0},300,function(){
//					$(this).css('margin-left',postion);
//					$(this).animate({opacity:1},700,function(){moveing=false;});
//				});	
//			}
//		}
	
	//自動撥放及速度
	var autoRun = true;
	var autoSpeed = 5000;
	
	//移動距離
	var moveWidth = 990;
	//移動最大範圍
	var moveMaxX = 150;
	var moveMinX = -(($('.pic').length-1) * moveWidth) - 150;
	var moveing = false;
	
	//建立索引位置
	var moveArray = new Array();	
	for (var i=0;i<$('.pic').length;i++){
		moveArray[i] = -(i * moveWidth);
	}
	var moveIndex = 0;
	var movestratX = 0;
	var movetempX = 0;
	
	var movestratY = 0;
	var movetempY = 0;
	
	function touchStart(e) {
		e.preventDefault();
		autoRun = false;
		var touches = e.changedTouches,
			i = 0, l = touches.length, touch;
		for (; i < l; i++) {
			touch = touches[i];
			movestratX = touch.pageX;
			movetempX = movestratX;
			
			movestratY = touch.pageY;
			movetempY = movestratY;
		}
		return false;
	}
	
	function touchMove(e) {
		e.preventDefault();
		var touches = e.targetTouches,
			i = 0, l = touches.length, touch;
		for (; i < l; i++) {
			if(!moveing){
				touch = touches[i];
				
				var nowML = parseInt($('.carouselMove').css('margin-left'));
				var moveP = nowML + (touch.pageX-movetempX);
				var moveWin = parseInt($(document).scrollTop()) - (touch.pageY-movetempY);
				
				//超出範圍時，移動位置等於最大位置
				if(moveP>moveMaxX){	
					moveP = moveMaxX;
					return;
				}else if(moveP<moveMinX){
					moveP = moveMinX;
					return;
				}
				
				//移動開始
				$('.carouselMove').css('margin-left',moveP);
				$(document).scrollTop(moveWin);

				//備存移動後位置
				movetempX = touch.pageX;

			}
		}
		return false;
	}
	
	function touchEnd(e) {
		var touches = e.changedTouches,
		i = 0, l = touches.length, touch;
		for (;i < l; i++) {
			if(!moveing){
				moveing = true;
				touch = touches[i];
				var nowML = parseInt($('.carouselMove').css('margin-left'));
				var moveP = nowML + (touch.pageX-movetempX);
				if(moveP>moveArray[0]){
					moveP = moveArray[0];
				}else if(moveP<moveArray[moveArray.length-1]){
					moveP = moveArray[moveArray.length-1];
				}else if((touch.pageX < movestratX - 100 )){
					moveIndex++;
					moveIndex = (moveIndex>=moveArray.length) ? moveArray.length : moveIndex;
					moveP = moveArray[moveIndex];
				}else if((touch.pageX > movestratX + 100 )){
					moveIndex--;
					moveIndex = (moveIndex<=0) ? 0 : moveIndex;
					moveP = moveArray[moveIndex];
				}else if( (touch.pageX < movestratX )|| (touch.pageX > movestratX ) ){
					moveP = moveArray[moveIndex];
				}
				$('.picButton').removeClass('nowpic').eq(moveIndex).addClass('nowpic');
				$('.carouselMove').animate({marginLeft:moveP},200,function(){moveing=false;});
			}
		}
	}
	if(!$.browser.msie){
		$(".carouselMove").get(0).addEventListener("touchstart", touchStart, false);
		$(".carouselMove").get(0).addEventListener("touchmove", touchMove, false);
		$(".carouselMove").get(0).addEventListener("touchend", touchEnd, false);
//		$(window).addEventListener("touchstart", touchStart, false);
//		$(window).addEventListener("touchmove", touchMove, false);
//		$(window).addEventListener("touchend", touchEnd, false);
	}
	setTimeout(function(){
		if(autoRun){
			var clickIndex = moveIndex+1;
			var picLength = $('.pic').length;
			clickIndex = clickIndex >= picLength ? 0 : clickIndex;
			nextpic(clickIndex);
			setTimeout(arguments.callee, autoSpeed);
		}
	},autoSpeed);
	
});
