var default_format='gallery';



function clean_up(){
	$('#img-container .picture').each(function(e){
		if($(this).find('a').size()>0){
			$(this).find('a').each(function(){
					$(this).after($(this).find('img')).remove();
			});
		}
	});
}
function load_lazyload(){
	/*$("#img-container img").lazyload({
            placeholder : "http://betdiez.com/grey.gif",
			cache		 : true,
            effect      : "fadeIn",
			threshold 	 : 300
  });*/
}

function change_styles(format){
	//alert(format);
	if(format=='horizontal'){
		var w=0;
		$('#img-container').removeClass();
		$('#img-container').addClass(format);
		$('#img-container img').each(function(e){
			//alert($(this).width());
			//width());
			w+=$(this).parent().outerWidth();
		});
		if($('#img-container #text').size()>0) w+=$('#img-container #text').outerWidth();
		$('#img-container').width(w);
		
		//load_lazyload();
	}else if(format=='vertical'){
		
		$('#img-container').removeClass();
		$('#img-container').addClass(format);
		$('#img-container').width('auto');
		//load_lazyload();
	}else{
		$('#img-container').removeClass();
		$('#img-container').addClass(format);
		$('#img-container').width('auto');
		
	}
}
function load_images(format){

		$('#img-container .picture').each(function(e){
		
			$(this).addClass('picture-loading');
			var oimg=$(this).find('img');
			var imgsrc=oimg.attr('alt');
			var barspot=imgsrc.lastIndexOf('/');
			var path=imgsrc.substring(0,barspot);
			var name=imgsrc.substring(barspot+1,imgsrc.length);
			//name=(name.indexOf('sys-')==0)?name.replace('sys-',''):name;
			
			var dotspot=name.lastIndexOf('.');
			var cleanname=name.substring(0,dotspot);
			var extension=name.substring(dotspot+1,name.length);
			var thname='sys-'+cleanname+"."+extension;
			
			var loadpath=imgsrc;//path+"/"+name;
			if(format=='gallery' || format=='thumbnails')
				loadpath=path+"/"+thname;
			
			//alert(loadpath);
			//$.get(loadpath,function(){
				//alert(oimg.attr('src'));
							
				//alert(format);
			
				
				if(format=='thumbnails'){
					//oimg.attr('src',path+"/"+thname);
					//$('#picture-container h3').remove();
					var tit=(oimg.parent().parent().find('.captioning h2').size()>0)?oimg.parent().parent().find('.captioning h2').html():"";
					oimg.wrap(function() {
					  //'<a rel="project-1" class="colorbox" href="'+ path+"/"+name +'/>'
					  return '<a rel="project-1" class="colorbox" title="'+tit+'" href="' + path+"/"+name + '" />';
					});
					$('.colorbox').colorbox({
						scrolling:false,
						maxHeight:'600px',
						maxWidth:'960px',
						current	:"{current} de {total}"
						
					});
					
					
					
				}else if(format=='gallery'){
					
					oimg.attr('src',path+"/"+thname);
				
					/*oimg.wrap(function() {
					  //'<a rel="project-1" class="colorbox" href="'+ path+"/"+name +'/>'
					  return '<a rel="project-1" class="gallery" href="' + path+"/"+name + '" />';
					});*/
					oimg.click(function(e){
						e.preventDefault();
						e.stopPropagation();
						load_gallery_picture($(this));
						
						
					});
					
					
				}
				change_styles(format);
				oimg.attr('src',loadpath);
				oimg.parent().removeClass('picture-loading');
				
				
				//$('#format-switcher').removeClass('loading');
				$('#img-container').fadeTo('fast',1);
				
			//});
		});
		
		if(format=='gallery'){
			load_gallery_picture($('#img-container .picture:first img'));
		}
	
}


function load_gallery_picture(img){
	//alert($(img)[0].outerHTML);
	//alert($(img).attr('alt'));
	$('img.selected').removeClass();
	img.addClass('selected');
	var tit=img.parent().find('.captioning h2').html();
	$('#picture-container h2').fadeTo('fast',0);
	
	$('#picture-container img').fadeTo('fast',0,function(){
		$('#picture-container').addClass("loading");
		
		var imgobj = $("<img />").attr('src', $(img).attr('alt'));
		imgobj.load(function(){
			 if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
				 alert("ĦOoops! No se ha podido cargar la imagen :-( ");
			 } else {
				$('#picture-container img').attr('src',$(img).attr('alt'));
				$('#picture-container').removeClass("loading");
				$('#picture-container h2').html(tit);
				$('#picture-container img').fadeTo('fast',1);
				$('#picture-container h2').fadeTo('fast',1);
				
				$('#picture-container').width($('#picture-container img').outerWidth());
				$('#img-container').width($('#picture-container img').outerWidth()+$('#img-innercontainer').outerWidth());
				
			 }
		  });
		
		/*$.get($(img).attr('alt'),function(){
			
			
			
		});*/
	
	});
}
function change_format(format){
		//alert(format);
		var i=2;
		$('#format-switcher select option').each(function(){
			
			if($(this).val()==format) i=$(this).index()+1;
		});
		
		//alert(i);
		$('#format-switcher ul li.active').removeClass('active');
		$('#format-switcher ul li a.active').removeClass('active');
		$('#format-switcher ul li:nth-child('+i+')').addClass('active');
		$('#format-switcher ul li:nth-child('+i+') a').addClass('active');
		$('#format-switcher select').val(format);
		
		
		$.cookie("current_format", format);
		//clean_up();
		$('#format-switcher').addClass('loading');
				
		$('#img-container').fadeTo('fast',0,function(){
			load_images(format);
			$('#format-switcher').removeClass('loading');
				
		});
}

$(document).ready(function() { 

	var curr_format=default_format;
	if($.cookie('current_format')==null) $.cookie("current_format", default_format);
	else curr_format=$.cookie('current_format');
	
	$('#img-container').addClass($('#format-switcher select').val());
	$('#format-switcher ul li a').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		//var i=$(this).parent().index();
		var text=$(this).attr('href').substring(1);
		change_format(text);
	});
	
	$('#format-switcher select').change(function(e){
		var format=$(this).val();
		change_format(format);
	});
	if($('#format-switcher').size()>0) change_format(curr_format);
});
