/*
 * 全站公共脚本,基于jquery-1.9.1脚本库
 */
 var isSettingOn = 0;
$(function() {
//阻止默认行为
	document.body.addEventListener(
		"touchmove",
		function(e) {
		  e.preventDefault();
		},
		{ passive: false }
	  );

//微信分辨率纠错
	function is_weixn()
	{
	    var ua = navigator.userAgent.toLowerCase();  
	    if(ua.match(/MicroMessenger/i)=="micromessenger") {  
	        return true;  
	    } else {  
	        return false;  
	    }  
	}  
	function isIos()  
	{  
       var userAgentInfo = navigator.userAgent;  
       var Agents = new Array("iPhone");  
       var flag = false;  
       for (var v = 0; v < Agents.length; v++) {  
           if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = true; break; }  
       }  
       return flag;  
	}
	var fullHeight = document.documentElement.clientHeight;
	if(fullHeight < 960){
		$(".jr").css({width:'540px', left:'50px', top:'30px'});
		$(".jr4").css({width:'540px', left:'50px', bottom:'10px'});
		$(".p6-2").css({top:'30px'});
		$(".p6-3").css({top:'90px'});
		$(".p6-4").css({top:'730px'});
		$(".p6-5").css({top:'730px'});

		$(".p7-3").css({top:'830px'});
		$(".p7-4").css({top:'830px'});
		$(".p7-5").css({top:'830px'});
		$(".p7-81").css({width:'440px', left:'100px', top:'50px'});
	}
	if(fullHeight > 1150){
		$(".jr").css({top:'100px'});
		$(".p6-3").css({top:'180px'});
		$(".p6-4").css({top:'880px'});
		$(".p6-5").css({top:'880px'});
		$(".p7-81").css({width:'540px', left:'50px', top:'50px'});
		$(".p7-82").css({bottom:'100px'});
	}

		var page = 0;
		var swiperH = new Swiper('.swiper-container-h', {
			pagination: '.swiper-pagination-h',
			paginationClickable: true,
			direction: 'vertical',
			pagination : 'null',
			spaceBetween: 50,
			onSlideChangeEnd: function(swiper){
				var np = parseInt(swiper.activeIndex);
				//$(".mc").html(swiper.activeIndex);
				page = np;
				np++;
			  animates(np);
			}
		});
    
		var html = new Array();
	
		function remove(slide)
		{
			slide.html(html[page]);
		}
	
		function animates(slide)
		{
			
			var type = parseInt(slide);
			$(".txt").fadeOut(100);
            console.log("swip : " + type);
			switch ( type )
			{
				case 1:
					page1(slide);
					break;
				case 2:
					page2(slide);
					break;
				case 3:
					page3(slide);
					break;
				case 4:
					page4(slide);
					break;
				case 5:
					page5(slide);
					break;
				case 6:
					page6(slide);
					break;
				case 7:
					page7(slide);
					break;
				case 8:
					page8(slide);
					break;
				case 9:
					page9(slide);
					break;
			}
		}

		function page1(slide){
			$(".p1-3").fadeIn(1000);
			$(".p1-4").delay(1000).fadeIn(1000);
			$(".p1-5").delay(2000).fadeIn(1000);
			$(".p1-6").delay(3000).fadeIn(1000);
			$(".p1-7").delay(4000).fadeIn(1000);
			$(".p1-8").delay(5000).fadeIn(1000);
			$(".p1-9").delay(6000).fadeIn(1000);
			$(".p1-10").delay(7000).fadeIn(1000);
			$(".p1-11").delay(8000).fadeIn(1000);
			
			setTimeout(function(){
				gun = true;
				
				$(".t1-1").fadeIn(1000);
				$(".t1-2").delay(1000).fadeIn(1000);
				$(".t1-3").delay(2000).fadeIn(1000);
				$(".t1-4").delay(3000).fadeIn(1000);
				$(".t1-5").delay(4000).fadeIn(1000);
				$(".t1-6").delay(5000).fadeIn(1000);
				$(".t1-7").delay(6000).fadeIn(1000);
				$(".t1-8").delay(7000).fadeIn(1000);
			},10000);
			$(".shang").fadeIn();
			var nm = 0;
			var tm = setInterval(function(){
				if(!gun){
					return false;
				}
				nm += 1;
				$(".p1-14").scrollTop(nm);
				if(nm > 500){
					clearInterval(tm);
				}
			},1000/40);
		}

		function page2(slide){
			$(".shang").delay(1000).fadeIn();
		}

		function page3(slide){
			$(".shang").delay(1000).fadeIn();
		}

		function page4(slide){
			$(".shang").delay(1000).fadeIn();
		}

		function page5(slide){
			$(".shang").delay(1000).fadeIn();
		}

		function page6(slide){
			$(".shang").delay(1000).fadeIn();
		}

		function page7(slide){
			$(".shang").delay(1000).fadeIn();
		}

		function page8(slide){
			$(".shang").delay(1000).fadeIn();
		}
		$(document).on("tap",".p6-3, .p6-4", function(){
			chooseimg();
		});

		$(document).on("tap",".p6-5", function(){
			if(pimg != ""){
				wid = parseInt($("#pic").width());
				hei = parseInt($("#pic").height());
				le = parseInt($("#pic").css("left"));
				to = parseInt($("#pic").css("top"));

				// $.post("upload2.php",{'url':pimg, 'wid':wid, 'hei':hei, 'le':le, 'to':to}, function(data){
				// 	if(data.msg == "ok"){
						var nimg = new Image();
						nimg.src = pimg;
						nimg.onload = function(){
							// pimg = data.url;
							$(".temp2").fadeIn();
							$(".p6-3 img").attr("src", pimg);
							$(".bgimg img").attr("src", pimg);
						}
				// 	}else{
				// 		alert(data.tishi);
				// 	}
				// },"json");

			}else{
				chooseimg();
			}
		});
        
        $(document).on("tap",".p7-83", function(){
            $(".p7-83").fadeOut();
        });
        
        $(document).on("tap",".p7-3", function(){
            $(".p7-3").css({'background':'url(http://mallapp.guoguodz.com/h5_pic/appwww/Assets/Admin/images/btn_bg2.png)'});
            
            if(mo == 1){
                /*
                $(this).css({'background':'url(images/btn_bg2.png)'});
                setTimeout(function(){
                    $(".p7-3").css({'background':'url(images/btn_bg1.png)'});
                },150);
                */
                mtxt += 1;
                mtxt = mtxt > 9 ? 2 : mtxt;
                $(".p7-6 img").attr("src","http://mallapp.guoguodz.com/h5_pic/appwww/Assets/Admin/images/m" + mtxt + '.png');
            }else{
                mo = 1;
                $(".p7-4").css({'background':'url(http://mallapp.guoguodz.com/h5_pic/appwww/Assets/Admin/images/btn_bg1.png)'});
                $(".p7-2").fadeOut(100);
                $(".p7-6").fadeIn(200);
            }
        });
        
        $(document).on("tap",".p7-4", function(){
            $(".p7-4").css({'background':'url(http://mallapp.guoguodz.com/h5_pic/appwww/Assets/Admin/images/btn_bg2.png)'});
            
            if(mo == 2){
                /*
                $(this).css({'background':'url(images/btn_bg2.png)'});
                setTimeout(function(){
                    $(".p7-4").css({'background':'url(images/btn_bg1.png)'});
                },150);
                */
            }else{
                mo = 2;
                $(".p7-3").css({'background':'url(http://mallapp.guoguodz.com/h5_pic/appwww/Assets/Admin/images/btn_bg1.png)'});
                $(".p7-6").fadeOut(100);
                $(".p7-2").fadeIn(200);
            }
        });

        
        $(document).on("tap",".p7-5", function(){
            if(mo == 1){
            }else{
                var txt1 = $(".txt1").val();
                var txt2 = $(".txt2").val();
                var txt3 = $(".txt3").val();
                var txt4 = $(".txt4").val();
                if(txt1 == "" && txt2 == "" && txt3 == "" && txt4 == ""){
                    tishi("请填写文字内容！");
                    return false;
                }
            }

            $(".p7-9").fadeIn();

            $.post("http://mallapp.guoguodz.com/h5_pic/appwww/index.php/Index/upload",{"sign_pic":sign_pic,"mo":mo, "mtxt":mtxt, 'txt1':txt1, 'txt2':txt2, 'txt3':txt3, 'txt4':txt4, 'user':user, 'pimg':pimg}, function(data){
                if(data.msg == "ok"){
                    var nimg = new Image();
                    nimg.src = data.url;
                    nimg.onload = function(){
                        $(".p7-9").fadeOut();
                        $(".p7-81 img").attr("src", data.url);
                        $(".p7-8").fadeIn(1000);
                    }
                }else{
                    alert("网络堵塞了，请稍后再试！");
                }
            },"json");
        });
            
		function page9(slide){
			
		}

		document.getElementById("pic").addEventListener('touchstart',touchstart);
		document.getElementById("pic").addEventListener('touchmove',touchmove);
		document.getElementById("pic").addEventListener('touchend',function(){
			dragable = false;

			le = parseInt($("#pic").css("left"));
			to = parseInt($("#pic").css("top"));
			wid = parseInt($("#pic").width());
			hei = parseInt($("#pic").height());

			var zl = le, zt=to;

			if(le > 0){
				zl = 0;
			}

			if(le < (wid - 349) * -1){
				zl = (wid - 349) * -1;
			}

			if(to > 0){
				zt = 0;
			}

			if(to < (hei - 620) * -1){
				zt = (hei - 620) * -1;
			}

			if(wid < 349){
				wid = 349;
				zl = 0;
			}
			if(hei < 620){
				hei = 620;
				zt = 0;
			}

			//$("#pic").css({left: zl + "px",top:zt + 'px'});
			// $("#pic").css({width:wid + 'px', height: hei + 'px', left: zl + "px",top:zt + 'px'});

			
			return false;
		});

		var x1,x2,y1,y2,le,to,dian1,dian2,wid,hei,ratio,dragable;
		function touchstart(e){
            e.stopImmediatePropagation();
			dragable = true;

			le = parseInt($("#pic").css("left"));
			to = parseInt($("#pic").css("top"));
			wid = parseInt($("#pic").width());
			hei = parseInt($("#pic").height());
			
			x1 = e.touches[0].clientX;
			y1 = e.touches[0].clientY;
			if(typeof(e.touches[1]) != "undefined"){
				x2 = e.touches[1].clientX;
				y2 = e.touches[1].clientY;
				var xdiff = e.touches[1].clientX - e.touches[0].clientX;            // 计算两个点的横坐标之差
				var ydiff = e.touches[1].clientY - e.touches[0].clientY;            // 计算两个点的纵坐标之差
				dian1 = Math.pow((xdiff * xdiff + ydiff * ydiff), 0.5); 
			}
		}
		function touchmove(e){
			e.stopImmediatePropagation();
                    
			if(typeof(e.touches[1]) != "undefined"){
				x1 = e.touches[0].clientX;
				y1 = e.touches[0].clientY;
				x2 = e.touches[1].clientX;
				y2 = e.touches[1].clientY;
				
				var xdiff = e.touches[1].clientX - e.touches[0].clientX;            // 计算两个点的横坐标之差
				var ydiff = e.touches[1].clientY - e.touches[0].clientY;            // 计算两个点的纵坐标之差
				dian2 = Math.pow((xdiff * xdiff + ydiff * ydiff), 0.5); 

				var zhi = parseInt(dian2 - dian1);
				if(zhi > 5){
					ratio = 1.01;
				}else if(zhi < 5){
					ratio = 0.99;
				}

				wid = parseInt($("#pic").width());
				hei = parseInt($("#pic").height());
				le = parseInt($("#pic").css("left"));
				to = parseInt($("#pic").css("top"));

				var vw = wid * ratio;
				var vh = hei * ratio;

				var vl = (vw - wid) / 2 * -1 + le;
				var vt = (vh - hei) / 2 * -1 + to;
	
				// $("#pic").css({width:vw + 'px', height: vh + 'px', left: vl + "px",top:vt + 'px'});

			}else{
				if (dragable)
				{

					var x = (e.touches[0].clientX - x1);
					var y = (e.touches[0].clientY - y1);

					wid = parseInt($("#pic").width());
					hei = parseInt($("#pic").height());
					
					var lex = le + x;
					var toy = to + y;
					var ml = (wid - 349) * -1;
					var mt = (hei - 620) * -1
					if(lex > 0){
						lex = 0;
					}

					if(le < ml){
						lex = ml;
					}

					if(toy > 0){
						toy = 0;
					}

					if(to < mt){
						toy = mt
					}

					// $("#pic").css({left: lex + "px",top:toy + 'px'});

					//document.title = le +'='+ to +'='+ lex +'='+ toy;
					
				}
			}
		}
		
	//加载成功
		window.onload = function()
		{
			loadImage();
			var k = 0, kuan=0;
			var timer = setInterval(function(){
				k += 1;
				kuan += 4.55;
				$(".load2").css({width: kuan + 'px'});
				if(k == 100){
					clearInterval(timer);
					loadend();
				}
			},1000/20);
		}

			var downImg = 0;//已下载数量
			var percent = 0;//百分比
			var length = 0;
		function loadImage() {
			var img = $("body").find("img[loadsrc]");//图片数组
			length = img.length;//图片数量
			for ( var i=0;i<length;i++ )
			{
				var imgs = new Image();
				var imgDiv = img.eq(i);
				var imgsrc = imgDiv.attr("loadsrc");
				imgs.src = imgsrc;
				if(imgs.complete)
				{
					imgDiv.attr("src",imgsrc).removeAttr("loadsrc");//有缓存
					imgDown();
				}
				else
				{
					imgDiv.attr("src",imgsrc).load(function(){
						$(this).removeAttr("loadsrc");//无缓存
						imgDown();
					})
				}
			}
			
		}

		function imgDown()
		{
			downImg ++;
			percent = parseInt(100*downImg/length);
			//$(".progress").html(percent + '%');
			if ( percent == 100 )
			{ 
				
			}
		}
		function loadend(){
			$(".loading-wrapper").remove();
			$("#mySwiper").css({"visibility":"visible",y:fullHeight});
			animates(1);
			//swiperH.slideTo(5, 600,false);
		}

		


		//背景音乐开关
		$(".mc").on("tap",function(){
			if ( $(this).hasClass("play") )
			{
				$(this).removeClass("play");
				document.getElementById("bgmusic").pause();
				$(".yinyue").attr('src',off);
			}
			else
			{
				$(this).addClass("play");
				document.getElementById("bgmusic").play();
				$(".yinyue").attr('src',on);
			}
		})
		

		function getPar(par){
			//获取当前URL
			var local_url = document.location.href; 
			//获取要取得的get参数位置
			var get = local_url.indexOf(par +"=");
			if(get == -1){
				return false;   
			}   
			//截取字符串
			var get_par = local_url.slice(par.length + get + 1);    
			//判断截取后的字符串是否还有其他get参数
			var nextPar = get_par.indexOf("&");
			if(nextPar != -1){
				get_par = get_par.slice(0, nextPar);
			}
			return get_par;
		}
})