function initializeGalleria(){
	$('#galleria').galleria({
    	showInfo: false,
    	showCounter: false,
    	width:768,
    	height:512,
    	autoplay: 5000,
    	lightboxFadeSpeed: 200,
    	idleTime: 3000,
    	debug: true,
    	transition: 'fade',
    	transitionSpeed: 800
    });
}

function validateEmail(elementValue){  
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
   return emailPattern.test(elementValue);  
}

function addToNewsletter(){
	if(validateEmail($('#newsletter-email').val())){
		$.ajax({
			url:'/newsletteremails/ajax_add/'+$('#newsletter-email').val(),
			success: function(msg){
				$('#newsletterEmailForm').hide();
		        $('#respons').show();
		        $('#errorLater').hide();
		        $('#errorMail').hide();
		    },
		    error: function(){
		    	$('#errorLater').show();
		    }
		})				
		
	}
	else {
		$('#errorMail').show();
	}
}

function changeProjects(content){
	$("#projectsLoader").hide();
	
	var carousel = jQuery('#galleryScroller').data('jcarousel');	
	
	var items = content.split("</li>");
	var size = 0;
	if(items.length-1 <= 7)
		size = 7;
	else
		size = items.length-1;
	carousel.reset();
	carousel.size(size);
	carousel.setup();
	
	for (var i=0;i<items.length-1;i++ ){
		carousel.add(i+1, items[i]+'</li>')
	}
	
	var titles1 = items[0].split("title=\"");
	var titles2 = titles1[1].split("\"");
	$('#galleryProjectTitle').html(titles2[0]);
	
	$('.galleryProject').click(function() {
			$('#galleryProjectTitle').html($(this).attr('title'));
	});
	
	//determine type
	var type = 'animation';
	if(content.indexOf('changeAnimations') == -1){
		type = 'gallery';
	}
	
	if(type == 'gallery'){
		$('#player').hide();
		$('#animation_container').hide();
		//build gallery, show first project
		var items2 = items[0].split("changeGallery(");
		var items3 = items2[1].split(");");
		var ids = items3[0].split(",");
		
		changeGallery(ids[0],ids[1]);
	}
	else {
		$('#gallery_container').hide();
		//build animations, show first animation
		$('#animation_container').show();
		$('#player').show();
		
		var items2 = items[0].split("changeAnimations(");
		var items3 = items2[1].split(");");
		var items4 = items3[0].split('\'');		
		
		var flashvars = { file:'/files/animations/'+items4[1],autostart:'false',skin:'/js/jwplayer/modieus.zip' }; 
		var params = { allowfullscreen:'true', allowscriptaccess:'always' }; 
		var attributes = { id:'player', name:'player' };
		
		$('#galleryProjectTitle').html(params[1]);
		swfobject.embedSWF('/js/jwplayer/player.swf','animation_embed','768','462','9.0.115','false', flashvars, params, attributes);
	}	
}

function changeGallery(projectId, categoryId){
	var urlString='';
	if(categoryId == 0)
		urlString = '/projects/get_project_by_id/'+projectId;
	else
		urlString = '/projects/get_project_by_id_and_category/'+projectId+'/'+categoryId;
	
	$.ajax({
		url:urlString,
		success: function(msg){
			$('#gallery_container').hide();
				$('#gallery_container').html('');
				$('#gallery_container').html(msg);
			
			initializeGalleria();
			$('#gallery_container').show();
			
	    },
	    error: function(){
	    	
	    }
	})
}

function changeAnimations(file, title){
	$('#galleryProjectTitle').html(title);
	$('#gallery_container').hide();
	$('#animation_container').html('<div id="animation_embed"></div>');
	$('#animation_container').show();
	
	//build animations, show first animation
	var flashvars = { file:'/files/animations/'+file,autostart:'false',skin:'/js/jwplayer/modieus.zip' }; 
	var params = { allowfullscreen:'true', allowscriptaccess:'always' }; 
	var attributes = { id:'player', name:'player' };
	
	//swfobject.removeSWF('player');
	swfobject.embedSWF('/js/jwplayer/player.swf','animation_embed','768','462','9.0.115','false', flashvars, params, attributes);
}
