/* Author: Iván Cruces

*/
var $utils = {
	/**
	 * Get url parameters
	 */
	getURLParameter: function(name) {
	    return decodeURI(
	        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
	    );
	}
}

var $carrusel = {
	config: {
		urlJSON: 'data/carrusel.json',
		imgPath: 'img/carrusel/'
	},
	
	init: function(config){
		$.extend(this.config, config);		
		this.loadJSON();
	},
	
	loadJSON: function(){
		$.getJSON(this.config.urlJSON, 
			function(data){	
				$carrusel.loadCarrusel(data);			
			});
	},
	
	loadCarrusel: function(data){
		var carrusel = $('#content ul');
		for (var i=0; i<data.length; i++){
    		carrusel.append('<li><p><a href="galerias.html?id=' + data[i].galeria + '"><img src="' + $carrusel.config.imgPath + data[i].fichero + '.jpg"></a></p><p><a class="transparent" href="galerias.html?id=' + data[i].galeria + '">' + data[i].galeria  + '</a></p>')
    	}
	}
	
}

var $gallery = {
	config: {
		//Default configuration
		width: 620,
		height: 520,
		transition: 'none',
		maxScaleRatio: 1,
        minScaleRatio: 1,
        clicknext: true,
        showImagenav: false,      
        urlJSON: 'data/galerias.json',
        galleryDOMId: 'gallery',
        imgPath: 'img/' ,
        galleryId: null     
	},
	
	init: function(config){
		$.extend(this.config, config);		
		this.loadJSON();		
	},
	
	loadJSON: function(){
		$.getJSON(this.config.urlJSON, 
			function(data){	
				if ($gallery.loadGallery(data) == -1)
					$carrusel.init();	
				$gallery.loadNav(data);								
			});
	},
	
	loadGallery: function(data){	
		var galleryId = $utils.getURLParameter('id');
		$gallery.config.galleryId = galleryId;
		
		if (galleryId == 'null') return -1;		
		
		Galleria.loadTheme('js/themes/classic/galleria.classic.min.js');	
		this.keyControls();	
		var gallery;				
		
    	//Search gallery
    	for (var i=0; i<data.length; i++){
    		if (data[i].nombre == galleryId){
    			gallery = data[i];
    			break;
    		}
    	}		    	
    	//Load Gallery in DOM		    	
    	for (var i=0; i<gallery.fotos.length; i++){       			
    		$('#' + $gallery.config.galleryDOMId).append('<img src="' + $gallery.config.imgPath + gallery.nombre + '/' + gallery.fotos[i].fichero + '.jpg" >');	
    	}
    	//Init Gallery plugin
    	$('#' + $gallery.config.galleryDOMId).galleria({
	        width: $gallery.config.width,
	        height: $gallery.config.height,
	        transition: $gallery.config.transition,      
	        maxScaleRatio: $gallery.config.maxScaleRatio,
	        minScaleRatio: $gallery.config.minScaleRatio,
	        clicknext: $gallery.config.clicknext,
	        showImagenav: $gallery.config.showImagenav,
	        thumbnails: true,
	        showCounter:false		        			                   
		}); 
		//Load gallery title
		$('#photo-info #photo-nav span#nav-title').html(gallery.nombre);	
		
		//Load gallery text
		$('#content #info-gallery').html(gallery.texto);
				
		//Load events		
		Galleria.get(0).bind("loadstart", function(e) {
			//Load title of the image loaded
			$('#photo-info #photo-title').html(gallery.fotos[e.index].texto);
			//Update counter
			$('#photo-info #photo-counter').html('(' + (e.index+1) + '/' + gallery.fotos.length + ')');
			//Show gallery controls
			$('#photo-info #img-next').show();
			$('#photo-info #img-prev').show();
		});			
		//Gallery navigation controls
		$('#photo-info #photo-nav span#img-prev').click(function(){
			Galleria.get(0).prev();
		});
		$('#photo-info #photo-nav span#img-next').click(function(){
			Galleria.get(0).next();
		});	
		
	},
	
	loadNav: function(data){
		$('#header-menu').append("<ul>");
		var nav	= $('#header-menu ul');	
		var classSelected = '';
		for (var i=0; i<data.length; i++){
			if (!data[i].padre){
				if (data[i].nombre == $gallery.config.galleryId)
					classSelected = 'class="gallery-selected"';		
				else
					classSelected = '';		
				if (data[i].fotos)
					nav.append('<li id="' + data[i].nombre +'"><a ' + classSelected  + ' href="galerias.html?id=' + data[i].nombre + '">' + data[i].nombre + '</a><ul>');
				else
					nav.append('<li id="' + data[i].nombre +'" class="parent-gallery">' + data[i].nombre + '<ul>');
			}else{			
				if (data[i].nombre == $gallery.config.galleryId){						
					classSelected = 'class="gallery-selected"';	
					//select parent too				
					$('#header-menu ul li#' + data[i].padre).addClass('gallery-selected');						
				}else
					classSelected = '';					
				$('#header-menu ul li#' + data[i].padre + ' ul').append('<li><a ' + classSelected  + ' href="galerias.html?id=' + data[i].nombre + '">' + data[i].nombre);
			}				
		}	
	},	
	//Keyboard events
	keyControls: function(){	
		var idle = false;	
		var t;	
		//timeout to fix no image when next/prev is executed too fast
		timeoutevent = function(){
				idle = false;							
		}
		$("html").keydown(function(event){				
			if (!idle){
				if (event.keyCode == 37){
					Galleria.get(0).prev();
					idle = true;
					t = setTimeout("timeoutevent()",100);
				}else if (event.keyCode == 39){
					Galleria.get(0).next();
					idle = true;
					t = setTimeout("timeoutevent()",100);
				}
			}							
		});
	}
	
}

/**
 * Main JQuery function
 */
$(document).ready(function(){
    
    $gallery.init(); 
   
});







