$(document).ready(function(){
	$("#downsubmit").click(function(){
       downsubmit();
    });
	$("#downfilesubmit").click(function(){
       downfilesubmit();
    });
	showverify();
	enter();
	//首先将#back-to-top隐藏
	$("#back-to-top").hide();
	//当滚动条的位置处于距顶部100像素以下时，跳转链接出现，否则消失
	$(function () {
		$(window).scroll(function(){
			if ($(window).scrollTop()>100){
				$("#back-to-top").fadeIn(1500);
			}
			else
			{
				$("#back-to-top").fadeOut(1500);
			}
			});
				//当点击跳转链接后，回到页面顶部位置
				$("#back-to-top").click(function(){
				$('body,html').animate({scrollTop:0},1000);
				return false;
		});
	});
});


/* 软件下载 */

function downsubmit(){
	var softkey = $("#softkey").val();
	var verify = $("#verify").val();
	$.ajax({
			type: "POST",
			url: "/app/downloadsoft1",
			timeout: 15000,
			data: "verify=" + verify + "&softkey=" + softkey ,
			beforeSend: function(){
				$("#tip").html("<img src='/public/image/loading.gif' height='15' width='15'> 提交中，请稍候...");
			},
			dataType: "json",
			success: function(msg){
				var info = msg.info;
				switch (info){
				   case '0':
				     $("#tip").text(msg.txt);
				     break
				   case '1': 
				     //$("#tip").text('等一下，下载中');
					 if(msg.down_server == 0){// 下载服务器1
						  getdownfile(msg.down_key);
					 }else{//下载服务器2
					 	  getdownfile(msg.down_key);
					 	//alert('下载服务器2,暂时无法使用');
					 }
				     break
				   case '2': 
				    $("#tip").text('正式下载');
				     break
				   default:
				     $("#tip").text('未知错误，请重新刷新页面，然后下载！');
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$("#verifyimg").attr("src","/app/verify/"+Math.random());
				$("#tip").html('系统繁忙，请重新提交，谢谢！');
			},
			complete: function(XMLHttpRequest, textStatus) {
				this;
			}
	});
}

function getdownfile(down_key){
	var softkey = $("#softkey").val();
	var down_key = down_key;
	$.ajax({
			type: "POST",
			url: "/app/downloadsoft2",
			timeout: 15000,
			data: "down_key=" + down_key + "&softkey=" + softkey ,
			beforeSend: function(){
				$("#tip").html("<img src='/public/image/loading.gif' height='15' width='15'>请稍候...");
			},
			dataType: "json",
			success: function(msg){
				var info = msg.info;
				switch (info){
				   case '0':
				     $("#tip").text(msg.txt);
				     break
				   case '1':
				   	 //$("#tip").html('5 秒之后下载，请稍后...');
					 if ($("#downlink").length > 0) {
						  $('#downlink').val(msg.txt);
					 }else{
						  $("body").append("<input type='hidden' id='downlink' value="+msg.txt+" />");
					 }
					 getdown();
					 //setTimeout("jumppage()",5000);
					 setTimeout("showlink()",8000);
				     break
				   case '2':
				    $("#tip").text('正式下载咯');
				     break
				   default:
				     $("#tip").text('未知错误，请重新刷新页面，然后下载！');
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$("#verifyimg").attr("src","/app/verify/"+Math.random());
				$("#tip").html('系统繁忙，请重新提交，谢谢！');
			},
			complete: function(XMLHttpRequest, textStatus) {
				this;
			}
	});
}

function getdown(){
	var value = 5;
	if ($("#init_value").length > 0) {
		$('#init_value').val(value);
		time = setInterval('auto_jump()',1000);	
	}else{
		$("body").append("<input type='hidden' id='init_value' value="+value+" />");
		time = setInterval('auto_jump()',1000);	
	}
}

function auto_jump() {
	secs = $("#init_value").val() - 1;
	$("#init_value").val(secs);
        if(secs < 0){return false;}
	if(secs == 0){
		clearInterval(time);
		$("#tip").html("<img src='/public/image/loading.gif' height='15' width='15'>正在载入...");
		//window.location.href='';
		jumppage();
	}else{
		$("#tip").html("<img src='/public/image/loading.gif' height='15' width='15'>"+ secs +" 秒之后下载，请稍后...");
	}
}

function jumppage()
{
	var loc = $("#downlink").val();
	//window.location = loc;
	window.open(loc);
}

function showlink()
{
	var loc = $("#downlink").val();
	$("#tip").html('如果还没有开始下载，请<a href="'+ loc +'" target="_blank" onclick="clickurl()">点击下载</a>');
}

function clickurl(){
	$("#verifyimg").attr("src","/app/verify/"+Math.random());
	$("#tip").html('下载地址已失效，请重新获取地址！验证码已更新，请输入！');
}




/* 文件下载 */
function downfilesubmit(){
	var fileid	= $("#fileid").val();
	var softkey = $("#softkey").val();
	var verify = $("#verify").val();
	$.ajax({
			type: "POST",
			url: "/app/downloadfile1?type=1",
			timeout: 15000,
			data: "verify=" + verify + "&softkey=" + softkey + "&fileid=" + fileid,
			beforeSend: function(){
				$("#tip").html("<img src='/public/image/loading.gif' height='15' width='15'> 提交中，请稍候...");
			},
			dataType: "json",
			success: function(msg){
				var info = msg.info;
				switch (info){
				   case '0':
				     $("#tip").text(msg.txt);
				     break
				   case '1': 
				   	 
					 //$("#tip").html('5 秒之后下载，请稍后...');
					 if ($("#downlink").length > 0) {
						  $('#downlink').val(msg.txt);
					 }else{
						  $("body").append("<input type='hidden' id='downlink' value="+msg.txt+" />");
					 }
					 getdown();
					 //setTimeout("jumppage()",5000);
					 setTimeout("showlink()",8000);
					 /*
				     //$("#tip").text('等一下，下载中');
					 if(msg.down_server == 0){// 下载服务器1
						  getdownfile(msg.down_key);
					 }else{//下载服务器2
					 	  getdownfile(msg.down_key);
					 	//alert('下载服务器2,暂时无法使用');
					 }
					 */
				     break
				   case '2': 
				    $("#tip").text('正式下载');
				     break
				   default:
				     $("#tip").text('未知错误，请重新刷新页面，然后下载！');
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$("#verifyimg").attr("src","/app/verify/"+Math.random());
				$("#tip").html('系统繁忙，请重新提交，谢谢！');
			},
			complete: function(XMLHttpRequest, textStatus) {
				this;
			}
	});
}

/* 验证码显示 */
function show(obj){
	obj.src="/app/verify/"+Math.random();
}

function showverify(){
	if ($("#verify").length > 0) {
		$("#verify").after('<img id="verifyimg" src="/app/verify" onClick="show(this)">');
	}
}

function enter(){//enter事件
	$("#verify").die().live("keydown",function(event){  
        if(event.keyCode==13){  
           if($("#downfilesubmit").length > 0) {
			   downfilesubmit();
		   }
		   
		   if($("#downsubmit").length > 0) {
			   downsubmit();
		   }
         }  
    });
}


function scrollx(p) {
 var d = document, dd = d.documentElement, db = d.body, w = window, o = d.getElementById(p.id), ie6 = /msie 6/i.test(navigator.userAgent), style, timer;
 if (o) {
  cssPub = ";position:"+(p.f&&!ie6?'fixed':'absolute')+";"+(p.t!=undefined?'top:'+p.t+'px;':'bottom:0;');
  if (p.r != undefined && p.l == undefined) {
   o.style.cssText += cssPub + ('right:'+p.r+'px;');
  } else {
   o.style.cssText += cssPub + ('margin-left:'+p.l+'px;');
  }
  if(p.f&&ie6){
   cssTop = ';top:expression(documentElement.scrollTop +'+(p.t==undefined?dd.clientHeight-o.offsetHeight:p.t)+'+ "px" );';
   cssRight = ';right:expression(documentElement.scrollright + '+(p.r==undefined?dd.clientWidth-o.offsetWidth:p.r)+' + "px")';
   if (p.r != undefined && p.l == undefined) {
    o.style.cssText += cssRight + cssTop;
   } else {
    o.style.cssText += cssTop;
   }
   dd.style.cssText +=';background-image: url(about:blank);background-attachment:fixed;';
  }else{
   if(!p.f){
    w.onresize = w.onscroll = function(){
     clearInterval(timer);
     timer = setInterval(function(){
      //双选择为了修复chrome 下xhtml解析时dd.scrollTop为 0
      var st = (dd.scrollTop||db.scrollTop),c;
      c = st - o.offsetTop + (p.t!=undefined?p.t:(w.innerHeight||dd.clientHeight)-o.offsetHeight);
      if(c!=0){
       o.style.top = o.offsetTop + Math.ceil(Math.abs(c)/10)*(c<0?-1:1) + 'px';
      }else{
       clearInterval(timer);
      }
     },10)
    }
   }
  }
 }
}
