var button_label0='获取地址';
var button_label1='正在获取';
var result_label0='等待提交...';
var result_label1='<img src="images/load.gif" alt="loading" /> 获取中(专辑需时稍长,不要关闭本页)...';
var input_label0='在这里粘贴视频页面的地址...';
var msg = "请用右键“目标另存为”或用快车下载\n【提示：大部分视频无法使用迅雷下载】";

String.prototype.trim= function(){  
    // 用正则表达式将前后空格  
    // 用空字符串替代。  
    return this.replace(/(^\s*)|(\s*$)/g, "");  
}

$(document).ready(function(){
	/*绑定事件 javascript
	function $(id){return window.document.getElementById(id);}
	$('videoPageURI').onfocus = function(){this.select();if(this.value==input_label0){this.value='';}};
	$(videoPageURI).onblur = function(){if(this.value==''){this.value=input_label0}};
	*/
	//绑定事件 use jquery
	$('#videoPageURI').focus(function(){this.select();if(this.value==input_label0){this.value='';}});
	$('#videoPageURI').blur(function(){if(this.value==''){this.value=input_label0}});
	//$('#fetchButton').click(function(){parseFlv();});	//写在html里更容易维护
	
	//根据url的地址，执行init
	var videoPageURI=location.toString().queryString('url');
	$('#fetchButton').val(button_label0);
	if(videoPageURI==null){
		$('#videoPageURI').val(input_label0);
		$('#resultbox').hide();
	} else {
		if(videoPageURI.indexOf('http')===0){
			$('#videoPageURI').val(decodeURIComponent(videoPageURI).replace('http:##','http://').replace('https:##','https://'));
			getFlv();
		} else {
			videoPageURI=decodeBase64(videoPageURI).replace('http:##','http://').replace('https:##','https://');
			if(videoPageURI.indexOf('http')!==0 && videoPageURI.indexOf(':')===0) videoPageURI='http'+videoPageURI;
			$('#videoPageURI').val(videoPageURI);
			getFlv();
		}
	}
	$('#examplebox').show();
});

function getFlv() {
  var uri = $('#videoPageURI').val();
  if ((uri == null) || (uri == ""))	return;
  $('#fetchButton').attr('disabled',"disabled");
  $('#fetchButton').val(button_label1);
  $('#result').css('display','block');
  $('#result').html(result_label1);
  $('#resultbox').show();
	$.getScript("http://www.flvxz.com/getFlv.php?url="+encodeBase64(uri.replace('http://','http:##').replace('https://','https:##')), function(){
		$('#fetchButton').attr('disabled','');
		$('#fetchButton').val(button_label0);
		});
	$.getScript("http://log.flvxz.com/?url="+encodeBase64(uri.replace('http://','http:##').replace('https://','https:##')));
}
function copyToClipboard(txt) 
{ 
 if(window.clipboardData)  
  { 
    window.clipboardData.clearData(); 
    window.clipboardData.setData("Text", txt); 
	return true;
  } 
  else
  {
	return false;
  }
}  
function down(flv,vname) {
	if(copyToClipboard(vname)){
		alert("\n已复制文件名："+vname);
	}
	var winobj=window.open(flv,"down");
	if(winobj==null) window.location=flv;
	return false;
}
function parseFlv(){
	var uri=$('#videoPageURI').val().trim();
	if(uri.indexOf('http')!==0){
		alert('无效的URI网址');
		//location.href='/';
		return;
	}
	uri = uri.replace(/(http:\/\/[^\/]*(?:youku|tudou|ku6|youtube))xia.com\//,'$1.com/');
	top.location='?url=' + encodeBase64(uri.replace('http://','http:##').replace('https://','https:##'));
}
String.prototype.queryString = function(key){
    var sValue=this.match(new RegExp("[\?\&]"+key+"=([^\&]*)(\&?)","i"));
    return sValue ? sValue[1]:sValue
}

String.prototype.setKeyValue = function(key,value){
    if(this.queryString(key) != null){
        var reg = new RegExp("([\?\&])(" + key + "=)([^\&]*)(\&?)","i");
        return this.replace(reg,"$1$2" + value + "$4");
    }else{
        var add = arguments[2];
        if(add === true){
            return this + (this.indexOf("?") > -1 ? "&" : "?") + key + 
"=" + value;
        }else return this;
    }
}

function in_array(stringToSearch, arrayToSearch) {
    for (s = 0; s <arrayToSearch.length; s++) {
        thisEntry = arrayToSearch[s].toString();
        if (thisEntry == stringToSearch) {
            return true;
        }
    }
    return false;
}
function switchbox(boxid){
	var boxes=Array('lastquerybox','supportsitebox','examplebox','lastupdatebox','advbox');
	if( in_array(boxid,boxes) ){
		for (s = 0; s <boxes.length; s++) {
			$('#'+boxes[s]).hide();
		}
		if(boxid=='lastquerybox') $.getScript("http://log.flvxz.com/lastquery.php");
		if(boxid=='lastupdatebox') $.getScript("http://www.flvxz.com/lastupdate.php");
		if(boxid=='supportsitebox') $.getScript("http://www.flvxz.com/supportsite.php");
		$('#'+boxid).show();
	}
}
function preview(sharecode){
	$('#previewbox').html(sharecode);
	$('#previewbox').slideDown('slow');
}
