var speedTool = function(){
	var st;

	function init() {
		st = $('#speedTool');
		st.find('.st-button a').click(start);
		$('input[name="st-filetype"]').change(prepare);
	};

	function prepare() {
		stop();
		var size = $(this).attr('data-size')*1000; // size in bytes
		var hf = bytesToSize(size, 2);
		var stButton = st.find('.st-button');
		stButton.find('span').text(hf);
		stButton.find('strong').text($(this).parent().find('span').text());
		stButton.find('a').attr('data-size',size);
	};

	function bytesToSize(bytes, precision) {
		var kilobyte = 1000;
		var megabyte = kilobyte * 1000;
		var gigabyte = megabyte * 1000;
		var terabyte = gigabyte * 1000;

		if ((bytes >= 0) && (bytes < kilobyte)) {
			return bytes + ' B';

		} else if ((bytes >= kilobyte) && (bytes < megabyte)) {
			if ( (bytes / kilobyte).toFixed(precision) == (bytes / kilobyte) ) {
				return (bytes / kilobyte) + ' Kb';
			} else {
				return (bytes / kilobyte).toFixed(precision) + ' Kb';
			};
		} else if ((bytes >= megabyte) && (bytes < gigabyte)) {
			if ( (bytes / megabyte).toFixed(precision) == (bytes / megabyte) ) {
				return (bytes / megabyte) + ' Mb';
			} else {
				return (bytes / megabyte).toFixed(precision) + ' Mb';
			};
		} else if ((bytes >= gigabyte) && (bytes < terabyte)) {
			if ( (bytes / gigabyte).toFixed(precision) == (bytes / gigabyte) ) {
				return (bytes / gigabyte) + ' Gb';
			} else {
				return (bytes / gigabyte).toFixed(precision) + ' Gb';
			};
		} else if (bytes >= terabyte) {
			if ( (bytes / terabyte).toFixed(precision) == (bytes / terabyte) ) {
				return (bytes / terabyte) + ' Tb';
			} else {
				return (bytes / terabyte).toFixed(precision) + ' Tb';
			};
		} else {
			return bytes + ' b';
		};
	};

	function start() {
		var size = $(this).attr('data-size')*1; // get file size in b
		$(this).unbind('click').click(stop).text('Стоп');
		st.find('dd').each(function(){
			var dd = $(this);
			dd.find('em').remove(); // reset bars
			dd.find('span span').width(0);

			var dl = dd.attr('data-bandwidth-dl')*1; // get download speed in b
			var up = dd.attr('data-bandwidth-up')*1; // get upload speed in b
			var timeDL = Math.round(size/dl); // download time
			timeDL = (timeDL < 1) ? 1 : timeDL; // 1 sec minimum
			var timeUP = Math.round(size/up); // upload time
			timeUP = (timeUP < 1) ? 1 : timeUP;

			dd.find('.scale-dl span').animate({width:'100%'},(timeDL*1000),'linear',function(){
				$(this).parent().append('<em>'+secToTime(timeDL)+'</em>'); // show time label
			});
			dd.find('.scale-up span').animate({width:'100%'},(timeUP*1000),'linear',function(){
				$(this).parent().append('<em>'+secToTime(timeUP)+'</em>');
			});
		});
		return false;
	};

	function secToTime(sec) {
		var time = '';
		if (sec > 3600) {
			time = (parseInt(sec/3600) + ' ч. ' + Math.round((sec - parseInt(sec/3600)*3600) / 60) + ' м.');
		} else if (sec > 60) {
			time = (parseInt(sec/60) + ' м. ' + (sec - parseInt(sec/60)*60) + ' с.');
		} else {
			time = sec + ' сек.';
		};
		return time;
	};

	function stop() {
		st.find('dd span span').stop(); // stop bars
		st.find('.st-button a').unbind('click').click(start).text('Старт');
		return false;
	};

	return {
		start:start,
		stop:stop,
		init:init
	};
}();

$(function(){
	speedTool.init();
});
