﻿function fadeOutPop() {
	jQuery('.popMsg').fadeOut("slow", function() {
		jQuery('.popMsg').remove();
	});

}
var timeout;

var clip = null;

function $(id) { return document.getElementById(id); }

function init() {
	// Enable Rich HTML support (Flash Player 10 Only)
	ZeroClipboard.setMoviePath( '/scripts/ZeroClipboard10.swf' );
	
	// Create our clipboard object as per usual
	clip = new ZeroClipboard.Client();
	clip.setHandCursor( true );
	
	clip.addEventListener('mouseOver', function (client) {
		// update the text on mouse over
		clip.setText( $('urlToFile').value );
	});
	
	clip.addEventListener('complete', function (client, text) {
            jQuery("body").append('<div class="popMsg"><div class="popMsgInside">Úspěšně zkopírováno do schránky</div></div>');
            
            jQuery('.popMsg').center();

            clearTimeout(timeout);
            timeout = setTimeout('fadeOutPop()', 2000);

            
            jQuery('.popMsg').click(function() {
            	fadeOutPop();
            });
	});

	
	clip.glue( 'd_clip_button' );
}


jQuery(document).ready(function() {
	init();
	
	//alert(jQuery("#browser").val());
	jQuery("#uploadify").uploadify({
		'uploader'       : 'scripts/uploadify.swf',
		'script'         : '/upload.php?browserip='+jQuery("#browser").val()+' / '+jQuery("#ip").val(),
		//'script'         : '/upload.php?browser=sfd&asfa=sdfs',
		'cancelImg'      : '/images/cancel.png',
		'folder'         : 'uploads',
		'queueID'        : 'fileQueue',
		'auto'           : true,
		'fileDesc'		 : 'Typ souboru',
		'fileExt'		 : '*.pdf;*.rar;*.zip;*.jpg;*.png;*.gif;*.bmp;*.exe;*.psd;*.jpeg;*.7z;*.doc;*.txt;*.xls;*.docx;*.xlsx;*.pptx;*.crd;',
		'multi'          : false,
		'buttonText'	 : 'Vybrat soubor',
		
		'onError'		 : function (a, b, c, d) {
								console.log(d);						
							},	
		
		'onComplete'	 : function (a, b, c, d) {
								//console.log(d);
								
								if (d != 0) {
									var json = eval('(' + d + ')');
									
									if (json.list) {
										jQuery("#files").prepend(json.list);
										jQuery("#files .clip.hidden").removeClass("hidden");
										jQuery("#urlToFile").val(json.url);
										
										jQuery('<span class="or">nebo zkopírujte odkaz ručně..</span>'+json.bigInput).insertAfter("#files");
										
										jQuery("#forKubajz").focus(function(){
											jQuery(this).select();
										});


										clip.reposition();
									
									} else {
										$('#uploadify').uploadifyClearQueue();
									}
								} else {
									$('#uploadify').uploadifyClearQueue();
									
								}
							},
							
		'onSelectOnce'	 : function() {
							jQuery("#files dt, #files dd").not(".clip").remove();
							jQuery("#files .clip").addClass("hidden");
							jQuery("#forKubajz, .or").remove();
						}

	});
});
