$(function () {
	if($('#lang-value').length > 0) {
		$('#lang-value').change(function() {
			var lang = $('#lang-value').val();
			window.location = 'http://' + (lang == 'en' ? 'www' : lang) + '.photofunia.com' + location.pathname;
		});
	}
	if($('#home-ico').length > 0) {
		$('#logo-effects, #logo-lab').mouseover(function() {
			$('#home-ico').show();
		});
		$('#logo-effects, #logo-lab').mouseout(function() {
			$('#home-ico').hide();
		});
	}
	if($('#process').length > 0) {
		window.onbeforeunload = function(){
			if(!$('#stepUpload').is(':visible')) {
				$('#stepUpload').hide();
				$('#stepPhoto').show();	
			}
		}
		$('#process').get(0).reset();
   		if($('#processButton').length > 0) {
			$('.prompt.required').each(function() {
				$(this).bind('keyup change', function() {
					validateProcessButton(); 
				});                       
			});
			$('#processButton').click(function() {
				if(validatePrompts()) {
					startUpload();
				}
			});
			if(!window.uploadButtons || !window.uploadButtons.length) {
				validateProcessButton();
			}
   		}
	}
	$('#sortEffects li').click(function() {
		$(this).parent().find('li[class=active]').removeClass('active');
		$(this).addClass('active');
		var sortType = $(this).attr('id').substr(5);
		if(sortType == 'rand') {
			$('#script-icons .script').shuffle(); 
		} else {
			$('#script-icons .script').tsort('', {
				order : 'desc',
				attr  : 'data-' + sortType
			});
		}
	});
});

function validateProcessButton() {
	if($('#processButton').length > 0) {
		validatePrompts() ? $('#processButton').show() : $('#processButton').hide();
	}
}

function photoSelected() {
	if($('#image').val() != '') {
		startUpload();
	}
}

function startUpload(){
	$('#stepPhoto').hide();
	$('#stepUpload').show();
	$('#process').submit();
	setTimeout("updateProgressIndicator()", 200);
}

function updateProgressIndicator() {
	$('#progressIndicatorImage').attr('src', $('#progressIndicatorImage').attr('src'));
}

function validatePrompts() {
	var valid = true;
	$('.prompt.required').each(function() {
		if ($(this).val() == '') { 
			valid = false;
	      		return false;
		}
	});
	return valid;
}

function postOutput(method,key){
	$.ajax({
		type: 'GET',
		url: '/' + method + '/' + key + '/',
		beforeSend: function(){
			if($('#postOutputCont').is(':visible')) {
				$('#postOutputCont').slideUp('normal').html('');
			}
			$('#postOutputWait').show();
		},
		success: function(data) {
			$('#postOutputWait').hide();
			$('#postOutputCont').html(data).slideDown('slow');
		},
		error: function() {
			$('#postOutputWait').hide();
			$('#postOutputCont').html('Error while sending the request').show();
		}
	});
}

$.fn.shuffle = function(){
	var allElems = this.get(),
	getRandom = function(max){
		return Math.floor(Math.random() * max);
	},
	shuffled = $.map(allElems,function(){
		var random = getRandom(allElems.length),
		randEl = $(allElems[random]).clone(true)[0];
                allElems.splice(random, 1);
                return randEl;
           });
        this.each(function(i){
		$(this).replaceWith($(shuffled[i]));
	});
    return $(shuffled);
};



