function initUI(){
	$('#lang-value').change(function(){
		var lang = $('#lang-value').val();
		window.location = 'http://' + (lang=='en' ? 'www' : lang) + '.photofunia.com' + location.pathname;
	});
	$('#logo-effects, #logo-lab').mouseover(function(){
		$('#home-ico').show();
	});
	$('#logo-effects, #logo-lab').mouseout(function(){
		$('#home-ico').hide();
	});
}

function initScriptSelectUI(){
	window.onbeforeunload=function(){
		if(!$('#stepUpload').is(':visible')){
			$('#stepUpload').hide();
			$('#stepPhoto').show();	
		}
	}
	document.forms['process'].reset();
   if($('#processButton').length>0)
   {
      $("#process").find('input,select,textarea').each(function(){
   	   $(this).change(function(){
   	      validatePrompts() ? $('#processButton').show() : $('#processButton').hide(); 
   	   });                           
      });
      $('#processButton').click(function(){
         if(validatePrompts()){
            startUpload();
         }
      });
   }
}

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

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

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

function validatePrompts(){
   var valid=true;
   $("#process").find('input[type!=hidden],select,textarea').each(function(){
	   if($(this).hasClass('required') && $(this).val()==""){ 
	      valid=false;
      }
   });
   return valid;
}

function postOutput(method,key){
	$.ajax({
		type: 'GET',
		url: '/'+method+'/'+key+'/',
		beforeSend: function(){
			if($('#postOutputCont').is(':visible')){
				$('#postOutputCont').slideUp('normal').html(null);
			}
			$('#postOutputWait').show();
		},
		success: function(str){
			$('#postOutputWait').hide();
			$('#postOutputCont').html(str).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);
};

