﻿var isTouch = Modernizr.touchevents,
	isMobile = false,
	mobile = false,
	win_width = 0,
	win_height = 0,
	navItem = 0,
	atH = jQuery(".header").height(),
	$menuBtn = jQuery('.menu-handler'),
	$menuBox = jQuery(".siteMapBox"),
	pageNavNum=0,
	scrollNav=0;

var pageInit = {
	    init: function () {
	        win_width = $(window).width();
	        win_height = $(window).height();
	        atH=jQuery(".header").height();
	        if (win_width <= 1024) {
	            isMobile = true;
			    jQuery(".ilink-ico2 i").bind("click",function(){
			    	jQuery(this).parent().addClass("act");
			    });
	            jQuery(".ilink-ico2 .black-30").bind("click",function(){
			    	jQuery(this).parent().removeClass("act");
			    });
			    jQuery(".ilink-ico2 i").unbind("mouseenter mouseleave");
	        } else if (win_width > 1024) {
	            isMobile = false;
			    jQuery(".ilink-ico2 i").bind("mouseenter",function(){
			    	jQuery(this).parent().addClass("act");
			    });
			    jQuery(".ilink-ico2 i").bind("mouseleave",function(){
			    	jQuery(this).parent().removeClass("act");
			    });	
			    jQuery(".ilink-ico2 .black-30").unbind("click");
			    jQuery(".ilink-ico2 i").unbind("click");
	        };
	        if (win_width <= 640) {
	            mobile = true;
	            jQuery(".siteMap-list").addClass("mobile-menu");
	        } else if (win_width > 640) {
	            mobile = false;
	            jQuery(".siteMap-list").removeClass("mobile-menu");
	        };
	        
	    },
	    setImgMax: function (img, imgW, imgH, tW, tH) {
	        var tWidth = tW || win_width;
	        var tHeight = tH || win_height;
	        var coe = imgH / imgW;
	        var coe2 = tHeight / tWidth;
	        if (coe < coe2) {
	            var imgWidth = tHeight / coe;
	            img.css({ height: tHeight, width: imgWidth, left: -(imgWidth - tWidth) / 2, top: 0 });
	        } else {
	            var imgHeight = tWidth * coe;
	            img.css({ height: imgHeight, width: tWidth, left: 0, top: -(imgHeight - tHeight) / 2 });
	        };
	    },
	    setScroll: function (anchorCur) {
	    	if(jQuery(anchorCur).length>=1){
	        	jQuery("html,body").animate({ scrollTop: jQuery(anchorCur).offset().top-atH}, 0);
	    	}
	    },
       	openbox:function(htmlAddress){
			$.ajax({
				url: htmlAddress,
				dataType: "html",
				success: function (data) {
					if (data == "" || data == null) {
						return;
					}
					else {
						if(jQuery(".md-modal").length>=1){
							jQuery('html').removeClass('md-show');
							jQuery('.md-modal').remove();
						};
						$("body").after('<div class="md-modal"><div class="align-vertical"><div class="vertical-inner"></div></div></div>');
						$('.md-modal .vertical-inner').append(data);
						setTimeout(function(){$("html").addClass("md-show");},50);
						jQuery('.form-box-close').bind('click',function(e){
							jQuery('html').removeClass('md-show');
							setTimeout(function(){jQuery('.md-modal').remove();},400);
						});
		 				jQuery('.md-modal .vertical-inner').bind('click', function (e) { 
						 	if ($(e.target).hasClass('vertical-inner')) { 
						 		jQuery('html').removeClass('md-show');
								setTimeout(function(){jQuery('.md-modal').remove();},400);
						 	} 
		 				});
					}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					jQuery('html').removeClass('md-show');
					jQuery('.md-modal').remove();
				}
			});
		},
	    pbanner:function(){
	    	if(jQuery('.pbanner').length>=1){
		    	if (!isMobile) {
			        jQuery('.pbanner').css("height", jQuery(".pbanner .load-img").height());
			    } else {
			        jQuery('.pbanner').css("height", "auto");
			    }
	       }
	       jQuery(window).scroll(function () {
		        var sTop  = $(window).scrollTop();
				if (sTop < win_height && !isMobile) {
		            jQuery('.pbanner .load-img').css({ 'transform': "translate(0px," + (sTop) / 1.5 + "px)", '-webkit-transform': "translate(0px," + (sTop) / 1.5 + "px)" });
		        }
		    });
	    }
	},
	menu={
		init:function(){
		    $(document).on("click", ".menu-handler,.mapLink", function (e) {
		        if (navItem == 0) {
		            jQuery(this).addClass("active");
   	                jQuery("html").addClass("menuOpen");
		            navItem = 1;
		        }else{
		        	jQuery(this).removeClass("active");
   	                jQuery("html").removeClass("menuOpen");
		            navItem = 0;
		        }
		    });
		    $(document).on("click", ".siteMap-overlay", function (e) {
   	            if (navItem == 1 ) {
   	                menu.close();
   	            };
   	        });
   	        $(document).on("click", ".mobile-menu .sm-item", function (e) {
   	            var mnavcur = $(this);
   	            var mnavbox = $(this).parents("li");
   	            if (mnavbox.find(".siteMap-sub").size() > 0) {
   	                if (mnavcur.hasClass("cur")) {
   	                    mnavbox.find(".siteMap-sub").stop(false, false).slideUp();
   	                    mnavcur.removeClass("cur");
   	                } else {
   	                    jQuery(".mobile-menu a.sm-item").removeClass("cur");
   	                    jQuery(".siteMap-sub").stop(false, false).slideUp();
   	                    mnavbox.find(".siteMap-sub").stop(false, false).slideDown();
   	                    mnavcur.addClass("cur");
   	                    e.preventDefault();
   	                }
   	            }
   	        });
   	        $(document).on("click", ".siteMap-list a", function (e) {
   	        	var $this=jQuery(this);
				var hash = $this.attr("href").split("#")[1];
				if(hash && jQuery("#"+hash).length>=1){
					e.preventDefault();
	        		jQuery("html,body").animate({ scrollTop: jQuery("#"+hash).offset().top-atH},0);
					menu.close();
				}
   	        });
   	        jQuery('.link-select-box').click(function(){
				if($(this).hasClass('act')){
					$(this).removeClass('act');
					$(this).find('.mc').stop().slideUp(300);
				}else{
					$(this).addClass('act');
					$(this).find('.mc').stop().slideDown(300);
				}
			}).mouseleave(function(){
				$(this).removeClass('act');
				$(this).find('.mc').stop().slideUp(300);
			}); 
		},
		close:function(){
			$menuBtn.removeClass("active");
   	        jQuery(".mapLink").removeClass("active");
            jQuery("html").removeClass("menuOpen");
            navItem = 0;
		}
	},
	searchBox={
		openSearch:function(){
			$(".search-Ibox").stop().fadeIn();
			$("body,html").addClass('ovh');
		},
		closeSearch:function(){
			$(".search-Ibox").stop().fadeOut();
			$("body,html").removeClass('ovh');
		}
	},
	pageNav={
		init:function(){
			jQuery(".cpnavbox a").click(function(e){
				var $this=jQuery(this);
				var hash = $this.attr("href").split("#")[1];
				if(hash && jQuery("#"+hash).length>=1){
					e.preventDefault();
	        		jQuery("html,body").animate({ scrollTop: jQuery("#"+hash).offset().top-atH},800,'easeInOutExpo');
					if(isMobile){
						jQuery(this).parent().fadeOut();
					}
				}
			});
			jQuery(".cpnav").bind("click",function(){
				if(pageNavNum==0){
					jQuery(this).next(".cpnavbox").stop(false,false).fadeIn();
					pageNavNum=1;
				}else{
					jQuery(this).next(".cpnavbox").stop(false,false).fadeOut();
					pageNavNum=0;
				}
			});	
			if( jQuery(".pagenav .pinner").length>=1){
				if(jQuery(".pagenav .pinner a").length%2!=0){
				    jQuery(".cpnavbox").addClass("jcpanv");
				}
			}
			if( jQuery(".pagenav .inner").length>=1){
				if(jQuery(".pagenav .inner a").length%2==0){
				    jQuery(".cpnavbox").addClass("jcpanv");
				}
			}
		}
	},
	pbanner={
		init:function(){
			if(jQuery(".load-img").length>=1){
				_PreLoadImg([
					jQuery(".load-img").attr("src")
				],function(){
					setTimeout(function() {
					  	jQuery(".pbanner-C").addClass("inview");
					}, 50);
					pageInit.pbanner();
				});
				jQuery(window).resize(function () {
					pageInit.pbanner();
				});
			}
		}
	};
jQuery(window).resize(function () {
    pageInit.init();
});
$(document).ready(function () {
    pageInit.init();
    menu.init();
    pageNav.init();
    pbanner.init();
    jQuery(".blackTop").bind("click", function() {jQuery('html, body').stop().animate({scrollTop: 0}, 600,'easeInOutExpo');});
});
jQuery(".vwrap .close,.vwrap .videobtg").click(function () {
    jQuery(".vwrap").hide();
    $('#videobox').html("");
});
window.onload = function () {
    var hash = location.href.split("#")[1];
    if (hash) {
        pageInit.setScroll("#" + hash);
    }
}