﻿//圖片預先載入
function loadImage(url, callback) {
    var img = new Image(); //创建一个Image对象，实现图片的预下载
    img.src = url;
   
    if (img.complete) { // 如果图片已经存在于浏览器缓存，直接调用回调函数
        callback.call(img);
        return; // 直接返回，不用再处理onload事件
    }

    img.onload = function () { //图片下载完毕时异步调用callback函数。
        callback.call(img);//将回调函数的this替换为Image对象
    };
};
var $jq = jQuery.noConflict();  
//全局设置 变量
function Request(name)  //获取当前页面参数
 {
   new RegExp("(^|&)"+name+"=([^&]*)").exec(window.location.search.substr(1));
   return RegExp.$jq2
 }
var AnimateStyle;    // NULL为标准动画 如果为1为无动画版
var AnimateSpeed;    // NULL为默认运动速度 
var Volume;          // NULL为默认音量
var MagJson;         // 杂志信息
$jq(function(){
document.oncontextmenu=new Function("event.returnValue=false;"); //禁止右键功能,单击右键将无反应
MagJson = json; 
document.title=json.MagIfo.MagName;  //修改该页的title
var MagPage= json.MagPage;
var starPage;
   if(Request("page"))          //有Page参数分配当前页数
    {
	    var reqPage = Request("page");
	    if(isEven(reqPage ))
	    	starPage=reqPage-2;   //偶数Page定义出现在右边
	    else
	    	starPage=reqPage-1;   //奇数Page定义出现在左边
    }  
   else
    starPage=0;   //定义第当然页数默认为第一页
$jq(".MainBook").JMag({
    MagJson:MagJson,
    MagMain:$jq("#MagMain"),
    LeftPage:$jq("#LeftMag"),
    RightPage:$jq("#RightMag"),
    ViewPage:$jq("#ViewMag"),
    PageHeight:140,
    PageWidth:115,
    StarPageNum:starPage,
    DefaultPageSpeed:200,
    BeforeLoad:function(e){
	    $jq.fn.JTooltips({
	    isCenter:true,
	    content:'<p>下載中，請稍後...</p>',
	    newClass:'perGirl',
	    initWidth:110,
	    showTime:30000
	    }); 
	},
    AfterLoad:function(e){
	    $jq(".MainBook").AutoPlay(4000); 		
 		$jq.fn.JTooltipsRmove();		
    },
    PageCount:json.MagIfo.PageCount,
    PageError:function(e){
	    $jq(".MainBook").StopAuto();
	    $jq.fn.JTooltips({
	    isCenter:true,
	    content:'<p>'+e.errMessage+'</p>',
	    newClass:'perGirl',
	    initWidth:80,
	    showTime:1500
	    });
    }
}); 	  
	$jq("#Copy_bttn").click(function(){
		window.clipboardData.setData("text",$jq(".MainBook").data("MagTitle")+"\n"+window.location+"?page="+$jq(".MainBook").getPageNum())
		alert("已经成功复制到你剪切板中");
	})
	
	$jq("#R_bttn").add("#RightMag").click(function(){
		 $jq(".MainBook").PageGo();
	})
	
	$jq("#L_bttn").add("#LeftMag").click(function(){	
		 $jq(".MainBook").PageBack();
	})

	$jq("#StopAuto_bttn").click(function(){
		 $jq(".MainBook").StopAuto();
	})

	$jq("#Music_bttn").click(function(){
		$jqthis = $jq(this)
		if(!$jq("#player_container").is(":visible"))
		{
			$jq("#player_container").css({"zIndex":99,"left":$jqthis.offset().left-430/2,"top":$jqthis.offset().top-140}).show();
		}
		else
		{
			$jq("#player_container").hide();
		}
	})

	$jq("#Auto_bttn").click(function(){
		$jq.fn.JTooltips({
			buttonText:["確定","取消"],
			buttonEvent:['確定',function(e) { 
				$jq(".MainBook").AutoPlay(4000);
				$jq.fn.JTooltipsRmove();
			},"取消",function(e){
				$jq.fn.JTooltipsRmove();
			}],
			newClass:'Myspace',
			isCenter:true,
			initWidth:300,
			content:"確定開啟自動翻頁嗎？",
			postion:[parseInt($jq(".CenterBook").width())/2+$jq(".CenterBook").offset().left,parseInt($jq(".MagMain").height())/2+$jq(".MagMain").offset().top]
		});
	})

	$jq("#LeftMag").bind("mousedown",function(e){
		if(e.which == "3")
		{	
			$jq(".MainBook").StopAuto();
			$jq(".MainBook").Zoom(0,2);
		}
	});
	$jq("#RightMag").bind("mousedown",function(e){
		if(e.which == "3")
		{
			$jq(".MainBook").StopAuto();
			$jq(".MainBook").Zoom(1,2);
		}
	});
	$jq("#ViewMag").bind("mousedown",function(e){
		if(e.which == "3")
			$jq(".MainBook").StopZoom();
	});
		var oY;
		var mY; 
	$jq(".viewContanier").draggable({axis:'y',containment:[0,-512,0,0]}); 
	
	$jq("#ViewMag").mousewheel(function(objEvent, intDelta){
		var bigpic =$jq("#ViewMag .viewContanier")
		if(parseInt(bigpic.css('top'),10)+intDelta*120/3 >-530)
		{
			if(parseInt(bigpic.css('top'),10)+intDelta*120/5 < 0)
			{
				if(parseInt(bigpic.css('top'),10)+intDelta*120/5 > -intDelta*120/5)
				{
					bigpic.css({'top':0})
				}
				else
				{
		  		bigpic.css({'top': parseInt(bigpic.css('top'),10)+intDelta*120/5});
		  		}
	  		}
	  		else
	  		{
	  			bigpic.css({'top':0})
	  		}
	    }
	    else
	    {
	    bigpic.css({'top':-530})
	    }
	});

});
function isEven(num)  //检查奇偶数 (偶数 True 奇数 False)
 {
  if(num%2==0)    
    return true;   
  else      
    return false; 
 }

 
 
 
 
 
 
 
 
 
 
 
 
 
 
