
function start(){var valid=false;var contentUrl='';var url=document.URL;var i=url.indexOf('?');if(i>0){var parameters=url.substring(i+1).split('&');for(i=0;i<parameters.length;i++){if(parameters[i].charAt(parameters[i].length-1)=='#'){parameters[i].substring(0,parameters[i].length-1);}
var parts=parameters[i].split('=',2);if(parts[0]=='url'){contentUrl=unescape(parts[1]);break;}}
if(contentUrl.length>7&&(contentUrl.substring(0,7)=='http://'||contentUrl.substring(0,8)=='youtube:')){valid=true;}}
if(valid){document.getElementById('url').value=contentUrl;checkUrl();}
try{document.getElementById('url').focus();}catch(e){}}
var source='';function checkUrl(){var url=document.getElementById('url').value;if(url.substring(0,8)!='youtube:'&&!(/^http:\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url))){showError('Invalid URL');}else{if(url.substring(0,23).toLowerCase()=='http://www.youtube.com/'||url.substring(0,19).toLowerCase()=='http://youtube.com/'){var parts=url.split('?',2);var yid='';if(parts.length==2){var parameters=parts[1].split('&');for(var i=0;i<parameters.length;i++){if(parameters[i].substring(0,2)=='v='){yid=parameters[i].substr(2);break;}}}
if(yid.length>0){source='youtube:'+yid;}}else{source=url;}
tmpDestination=readCookie('destination');tmpDestinationText=readCookie('destinationText');tmpTransferType=readCookie('transferType');if(tmpDestination!=null&&tmpDestination.length>0&&tmpDestinationText!=null&&tmpDestinationText.length>0&&tmpTransferType!=null&&tmpTransferType.length>0){destination=tmpDestination;destinationText=tmpDestinationText;transferType=tmpTransferType;if(transferType=='PC'){$('#storedConfiguration').html('<img src="images/pc.png"/> '+destinationText);}else if(transferType=='BT'){$('#storedConfiguration').html('<img src="images/bluetooth.png"/> '+destinationText);}else if(transferType=='USB'){$('#storedConfiguration').html('<img src="images/usb.png"/> '+destinationText);}else if(transferType=='MC'){$('#storedConfiguration').html('<img src="images/memorycard.png"/> '+destinationText);}
showTransferBlock('storedConfig');}else{showTransferBlock('selectMethod');}}}
function changeConfig(){eraseCookie('destination');eraseCookie('destinationText');eraseCookie('transferType');showTransferBlock('selectMethod');}
var applet;var transferType;var fromStored;function preloadApplet(type){transferType=type;fromStored=false;showTransferBlock('loading');if(type=='BT'){loadApplet(true);}else{loadApplet(false);}}
function preloadAppletStored(){fromStored=true;showTransferBlock('loading');if(transferType=='BT'){loadApplet(true);}else{loadApplet(false);}}
function preloadAppletNext(){if(fromStored){downloadToMobile();}else{if(transferType=='BT'){checkBluetooth();}else{showTransferBlock(transferType+'Method');initBrowser();}}}
function loadApplet(bt){var app;if(bt){app=document.getElementById('bluedingBluetooth');if(app!=null){applet=app;preloadAppletNext();}else{$('#bluedingBase').remove();$('body').append('<applet height="1" width="1" id="bluedingBluetooth" code="com.mobiscanner.blueding.Bluetooth" archive="applet/SignedBluedingBluetooth.jar" mayscript="true"><param name="onLoad" value="applet=document.getElementById(\'bluedingBluetooth\');preloadAppletNext()"/></applet>');}}else{app=document.getElementById('bluedingBase');if(app!=null){applet=app;preloadAppletNext();}else{app=document.getElementById('bluedingBluetooth');if(app!=null){applet=app;preloadAppletNext();}else{$('#bluedingBluetooth').remove();$('body').append('<applet height="1" width="1" id="bluedingBase" code="com.mobiscanner.blueding.Base" archive="applet/SignedBluedingBase.jar" mayscript="true"><param name="onLoad" value="applet=document.getElementById(\'bluedingBase\');preloadAppletNext();"/></applet>');}}}}
function checkBluetooth(){showTransferBlock('BT2');applet.bluetoothEnable('responseBluetoothEnable');}
function responseBluetoothEnable(result){if(result.error!=null){showError("Error detecting bluetooth");showIntro();}else{if(result.bluetooth){showTransferBlock('BT3');}else{showError('Bluetooth not detected');}}}
var transferId;function downloadToMobile(){createCookie('destination',destination)
$('#fromTransfer').html(source);$('#toTransfer').html(destinationText);$('#progressBarM').css('width','0px');$('#progressBarTitle').html('');$('#actTransCancBut').show();$('#actTransContBut').hide();$('#actTransRetrBut').hide();showTransferBlock('activeTransfer');createCookie('destination',destination);createCookie('destinationText',destinationText);createCookie('transferType',transferType);var d=new Date();transferId=d.getTime()+'-'+Math.round(Math.random()*1000000);var transferTypeId=0;if(transferType=='PC'){transferTypeId=0;}else if(transferType=='BT'){transferTypeId=1;}else if(transferType=='USB'){transferTypeId=2;}else if(transferType=='MC'){transferTypeId=3;}
applet.transfer(transferId,source,destination,10,transferTypeId,'responseTransfer');}
function responseTransfer(result){if(transferId==result.id){if(result.error!=null){showError("Transfer error");$('#actTransCancBut').show();$('#actTransContBut').hide();$('#actTransRetrBut').show();document.title='error';}else{if(result.completed){transferId='';$('#actTransCancBut').hide();$('#actTransContBut').show();$('#actTransRetrBut').hide();$('#progressBarTitle').html('completed');document.title='completed';}else{$('#timeTransfer').html(formatTime(result.duration));$('#progressBarTitle').html(formatSize(result.downloaded)+'/'+formatSize(result.size));$('#progressBarM').css('width',(result.downloaded/result.size*300)+'px');document.title=formatTime(result.duration)+' - '+Math.round(result.downloaded/result.size*100)+'%';}}}}
function cancelTransfer(){if(transferId.length>0){applet.cancelTransfer(transferId);transferId='';}
showIntro();}
var destination;var destinationText;function initBrowser(){var fb=$('#directoryBrowser'+transferType);fb.html('');$('<div/>').attr('id','browserBar'+transferType).addClass('browserBar').appendTo(fb);$('<div/>').attr('id','browserNav'+transferType).addClass('browserNav').appendTo(fb);directoryBrowser(null);}
function directoryBrowser(path){$('#browserBar'+transferType).html('<img src="images/loading_small.gif"/> loading...');applet.list(path,'responseList');}
function fileBrowserClick(e){directoryBrowser($(this).attr('path'));}
function responseList(result){if(result.error!=null){showError(result.error)
return;}
destination=result.path;destinationText=result.path;$('#browserBar'+transferType).html('<img src="images/folder_open.png"/> '+result.path);var nav=$('#browserNav'+transferType);nav.html('');$(result.root).each(function(i,entry){nav.append($('<div/>').addClass('link').attr('path',entry.path).html('<img src="images/folder_important.png"/> '+entry.name).click(fileBrowserClick));});nav.append('<div class="browserTitle">directories</div>')
if(result.dir){$(result.dir).each(function(i,entry){nav.append($('<div/>').addClass('link').attr('path',entry.path).html('<img src="images/folder.png"/> '+entry.name).click(fileBrowserClick));});}
nav.append('<div class="browserTitle">files</div>')
if(result.file){$(result.file).each(function(i,entry){nav.append($('<div/>').html('<img src="images/binary.png"/> '+entry.name));});}}
var scanId;function initBTScanner(){$('#browCancBut').show();$('#browRetrBut').hide();$('#browserBT').html('');$('#browTitle').html('<img src="images/loading_small.gif"/> detecting bluetooth devices...');showTransferBlock('BTMethod');var d=new Date();scanId=d.getTime()+'-'+Math.round(Math.random()*1000000);applet.listDevices(scanId,'responseListDevices');}
function responseListDevices(result){if(result.id==scanId){if(result.error!=null){showError('Error scanning devices');$('#browCancBut').show();$('#browRetrBut').show();$('#browTitle').html('Scan completed');}else{$('#browserBT').html('');if(result.completed){$('#browTitle').html('Scan completed');$('#browCancBut').show();$('#browRetrBut').show();}
if(result.device!=null){$(result.device).each(function(i,entry){$('#browserBT').append($('<div/>').addClass('device').attr('name',entry.name).attr('mac',entry.mac).click(selectDevice).html('<img src="images/mobile_small.png"/> '+entry.name));});}}}}
function selectDevice(){showTransferBlock('BTObex');$('#btDeviceName').html($(this).attr('name'));applet.getObexPushService($(this).attr('mac'),'responseGetObexPushService');}
function responseGetObexPushService(result){if(result.error!=null){showError('Error detecting Obex service');}else{if(result.obexUrl!=null){destinationText=$('#btDeviceName').html();destination=result.obexUrl;downloadToMobile();}else{showTransferBlock('BTMethod');showError('Can\'t find Obex service');}}}
function showTransferBlock(id){document.title='blueding, download to mobile';if(!$('#intro').is(':hidden')){$('#intro').hide();}
$('#transfer .transferBlock').hide();$('#'+id).show();if($('#transfer').is(':hidden')){$('#transfer').show();}}
function showIntro(){document.title='blueding, download to mobile';$('#intro').show();$('#transfer').hide();}
function showError(message){showDialog('Error',message,'error');}
function formatSize(size){if(size<0){return'unknown';}
if(size<4*1024){return size+'B';}
size=Math.round(size/1024);if(size<4*1024){return size+'KB';}
size=Math.round(size/1024*100)/100;return size+'MB';}
function formatTime(duration){var sec=duration%60;var min=Math.floor(duration/60);min=min%60;var hour=Math.floor(min/60);var result="";if(hour>0){result=hour+"h "+min+"m "+sec+"s";}else if(min>0){result=min+"m "+sec+"s";}else{result=sec+"s";}
return result;}
function createCookie(name,value,days){var expires="";if(days==null||!days||days.length==0){days=30;}
var date=new Date();date.setTime(date.getTime()+(days*24*60*60*1000));expires="; expires="+date.toGMTString();document.cookie=name+"="+escape(value)+expires+"; path=/";}
function readCookie(name){var nameEQ=name+"=";var ca=document.cookie.split(';');for(var i=0;i<ca.length;i++){var c=ca[i];while(c.charAt(0)==' ')c=c.substring(1,c.length);if(c.indexOf(nameEQ)==0)return unescape(c.substring(nameEQ.length,c.length));}
return null;}
function eraseCookie(name){createCookie(name,"",-1);}
var TIMER=5;var SPEED=10;var WRAPPER='htmlBody';function pageWidth(){return window.innerWidth!=null?window.innerWidth:document.documentElement&&document.documentElement.clientWidth?document.documentElement.clientWidth:document.body!=null?document.body.clientWidth:null;}
function pageHeight(){return window.innerHeight!=null?window.innerHeight:document.documentElement&&document.documentElement.clientHeight?document.documentElement.clientHeight:document.body!=null?document.body.clientHeight:null;}
function topPosition(){return typeof window.pageYOffset!='undefined'?window.pageYOffset:document.documentElement&&document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop?document.body.scrollTop:0;}
function leftPosition(){return typeof window.pageXOffset!='undefined'?window.pageXOffset:document.documentElement&&document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft?document.body.scrollLeft:0;}
function showDialog(title,message,type,autohide){if(!type){type='error';}
var dialog;var dialogheader;var dialogclose;var dialogtitle;var dialogcontent;var dialogmask;if(!document.getElementById('dialog')){dialog=document.createElement('div');dialog.id='dialog';dialogheader=document.createElement('div');dialogheader.id='dialog-header';dialogtitle=document.createElement('div');dialogtitle.id='dialog-title';dialogclose=document.createElement('div');dialogclose.id='dialog-close'
dialogcontent=document.createElement('div');dialogcontent.id='dialog-content';dialogmask=document.createElement('div');dialogmask.id='dialog-mask';document.body.appendChild(dialogmask);document.body.appendChild(dialog);dialog.appendChild(dialogheader);dialogheader.appendChild(dialogtitle);dialogheader.appendChild(dialogclose);dialog.appendChild(dialogcontent);;dialogclose.setAttribute('onclick','hideDialog()');dialogclose.onclick=hideDialog;}else{dialog=document.getElementById('dialog');dialogheader=document.getElementById('dialog-header');dialogtitle=document.getElementById('dialog-title');dialogclose=document.getElementById('dialog-close');dialogcontent=document.getElementById('dialog-content');dialogmask=document.getElementById('dialog-mask');dialogmask.style.visibility="visible";dialog.style.visibility="visible";}
dialog.style.opacity=.00;dialog.style.filter='alpha(opacity=0)';dialog.alpha=0;var width=pageWidth();var height=pageHeight();var left=leftPosition();var top=topPosition();var dialogwidth=dialog.offsetWidth;var dialogheight=dialog.offsetHeight;var topposition=top+(height/3)-(dialogheight/2);var leftposition=left+(width/2)-(dialogwidth/2);dialog.style.top=topposition+"px";dialog.style.left=leftposition+"px";dialogheader.className=type+"header";dialogtitle.innerHTML=title;dialogcontent.className=type;dialogcontent.innerHTML='<br/>'+message+'<br/><br/><a href="#" onclick="hideDialog()">continue</a>';var content=document.getElementById(WRAPPER);dialogmask.style.height=content.offsetHeight+'px';dialog.timer=setInterval("fadeDialog(1)",TIMER);if(autohide){dialogclose.style.visibility="hidden";window.setTimeout("hideDialog()",(autohide*1000));}else{dialogclose.style.visibility="visible";}}
function hideDialog(){var dialog=document.getElementById('dialog');clearInterval(dialog.timer);dialog.timer=setInterval("fadeDialog(0)",TIMER);}
function fadeDialog(flag){if(flag==null){flag=1;}
var dialog=document.getElementById('dialog');var value;if(flag==1){value=dialog.alpha+SPEED;}else{value=dialog.alpha-SPEED;}
dialog.alpha=value;dialog.style.opacity=(value/100);dialog.style.filter='alpha(opacity='+value+')';if(value>=99){clearInterval(dialog.timer);dialog.timer=null;}else if(value<=1){dialog.style.visibility="hidden";document.getElementById('dialog-mask').style.visibility="hidden";clearInterval(dialog.timer);}}