var saveWidth = 0;

$(document).ready(function(){

	$('div#menu ul li').hover(
		function(e){
			$('#'+this.id+'_sub').show();
		}, 
		function(e){
			$('#'+this.id+'_sub').hide();
		}
	);

	$(':input').bind('mouseout', function() {
		$('#menu ul li ul').hide();
	});


	$('#button_upload_ready').bind('click' , function() {
		location.href = $(this).attr('href');
	});

	$('#button_picture_change').bind('click' , function() {
		$('#container_picture_change').slideToggle('fast');	
	});

	$('#button_upload').bind('click' , function() {

		var input_title = $('input#gallery_title').attr('value');
		var input_type = $('#PictureType').val();
		var input_size = $('#PictureThumbsize').val();

		if (input_title.length < 3) {
			$("div#errorgallery_title").show();
		} else {
			$("div#errorgallery_title").hide();
			$("div#container_upload_files").show();
			
			$.ajax({
				type: "POST",
				url: "/galleries/create",
				data: "title=" + input_title + '&type=' + input_type + '&size' + input_size,
				dataType: 'json',
				success: function(msg){
					$('<h1></h1>')
						.html(msg.galleryname)
						.prependTo('div#container_upload_files');
					$('#container_upload_form').hide();
					$('#Files0Uploadedfile').fileUploadSettings('scriptData','&gallery_id=' + msg.gallery_id + '&thumbsize=' + input_size + '&type=' + input_type);
					$('#button_upload_ready').attr('href','/gallery/'+msg.gallery_id+'/'+msg.gallerykey+'#codes');				
				}
			});		
			
		}
	});
	
	$('#upload_form').change(function() {		
		var total = 0;
		var filled = 0; 

		$('#form_inputs :file').each(function() {
			total++;
			if ($(this).attr('value').length > 3) {
				filled++
			}
		});
		
		var math = total - filled;
		if (math <= 1)
		{
			var number = total+1;
			var str = '<div class="input file"><label for="Files'+number+'Uploadedfile">Select picture '+number+':</label><input name="data[Files]['+number+'][uploadedfile]" value="" id="Files'+number+'Uploadedfile" type="file"></div>';
			$('#form_inputs').append(str);
			filled++;
			total++;
		}
	});

});

function MonitorProgress(uploadId)
{
	$(function() {
		$("#progressbar").progressbar({
			value: 0
		});
	});
	
	$.ajax({
	 type: "GET",
	 url: "/progress.php",
	 data: "uploadId=" + uploadId,
	 success: function(xml){
		Progress(xml);
	 }
	});
}

function Progress(xmlResult) 
{
	var root = xmlResult.getElementsByTagName('upload')[0];
	
	uploadProgress = root.getElementsByTagName("uploadprogress")[0].firstChild.nodeValue;
	uploadSpeed = root.getElementsByTagName("uploadspeed")[0].firstChild.nodeValue;
	uploadTime = root.getElementsByTagName("uploadtime")[0].firstChild.nodeValue;
	uploadTimeRemaining = root.getElementsByTagName("uploadtimeremaining")[0].firstChild.nodeValue;
	uploadFullSize = root.getElementsByTagName("uploadfullsize")[0].firstChild.nodeValue;
	uploadCurrentSize = root.getElementsByTagName("uploadcurrentsize")[0].firstChild.nodeValue;
	
	SetProgress(uploadProgress, uploadSpeed, uploadFullSize, uploadCurrentSize, uploadTimeRemaining);
	
	if (uploadProgress == 100) {
		clearInterval(monitorInterval);
	}
}

function SetProgress(percentage, uploadSpeed, uploadFullSize, uploadCurrentSize, uploadTimeRemaining)
{
	$('#progressbar').progressbar('option', 'value', percentage);

	if (uploadTimeRemaining < 60) {
		timeStr =  'less than ' + uploadTimeRemaining + ' seconds remaining'
	} else {
		minutesRemaining = Math.floor(uploadTimeRemaining / 60);
		secondsRemaining = uploadTimeRemaining - (minutesRemaining * 60);
		timeStr =  minutesRemaining + ' minutes ' + secondsRemaining + ' seconds remaining';
	}

	$('#progressInfo').html(uploadCurrentSize + ' KB of ' + uploadFullSize + ' KB at ' + uploadSpeed + ' KB/s; <br /> ' + timeStr);
}

function StartProgress(uploadId) {
	// start the script :)
	$('#progress_screen').show();
	$('#upload_input').hide();

	monitorInterval = setInterval("MonitorProgress('" + uploadId + "')", 1000);
}

function StartMovieUpload(uploadId)
{
	var errors = 0
	var filled = 0;
	var str = '';

	if ($("input#PictureTitle").attr('value').length < 3) {
		$("div#errorPictureTitle").css('display', 'block');
		errors++;
	} else {
		$("div#errorPictureTitle").css('display', 'none');
	}

	$('#upload_form :file').each(function() {
		if ($(this).attr('value').length > 3) {
			filled++
			str += '<li>'+ $(this).attr('value') +'</li>';
		}
	});

	if (filled == 0) {
		$("div#errorPictureFile").css('display', 'block');
		errors++;
	} else {
		$("div#errorPictureFile").css('display', 'none');
	}

	if (errors == 0) {	
		StartProgress(uploadId);
		$('#files').append(str);

		return(true);
	} else {
		return(false);
	}
}

function EmailAgainstSpamBots(alias) 
{
	var username = alias;
	var site = "chickupload";
	var ext = "com"
	document.write('<a href=\"mailto:' + username + '\@' + site + '.' + ext + '\">');
	document.write(username + '\@' + site + '.' + ext + '</a>');
}

function scaleImg(item) 
{
	what = document.getElementById(item);

	if (what.width>(930) || saveWidth>(930)) {
		$('#picture_message').show();
		if (what.width==(930)) {
			what.width=saveWidth;
		} else {
			saveWidth = what.width;
			what.style.cursor = "pointer";
			what.width=(930);
		}
	}

}