$("#dayGallery_jquery_descriptor").ready(ini_dayGallery);

function ini_dayGallery(){
	
	$("#dayGallery_jquery").html(
		 "<div class='content_1'><div id='content_img_big' class='content_img_big'></div>" +
		 "</div><div class='content_2'><div id='move_left' class='move_left' value='1'>&nbsp;</div><div id='content_thumbnails' class='content_thumbnails'>&nbsp;</div><div id='move_right' class='move_right' value='6' >&nbsp;" +
		 "</div></div>"	+
		 "<div id='content_downloads' class='content_downloads'></div>"
	);
	$("#dayGallery_jquery_descriptor").css('display', 'none');
	show_thumb(1,6);
	show_img(1);
	show_download(1);
	$('#move_left').click(move_left);
	$('#move_right').click(move_right);
}

function move_left(){
	if ( $('#move_left').attr("value") > 1){
		$('#move_left').attr("value",parseInt($('#move_left').attr("value")) - 1 );
		$('#move_right').attr("value",parseInt($('#move_right').attr("value")) - 1 );
		show_thumb($('#move_left').attr("value"), $('#move_right').attr("value"));
	}
}

function move_right(){
	if( $("#dayGallery_jquery_thumb span").size() > $('#move_right').attr("value") ){
		$('#move_left').attr("value",parseInt($('#move_left').attr("value")) + 1 );
		$('#move_right').attr("value",parseInt($('#move_right').attr("value")) + 1 );
		show_thumb($('#move_left').attr("value"), $('#move_right').attr("value"));
	}
}

function show_thumb(limit_inf, limit_sup){
	var select = 1;
	$(".item_thumbnails_select").each(function(i , item){
		select = $(this).attr('value');
	});
	
	$('#content_thumbnails').html("");
	$("#dayGallery_jquery_thumb span").each(function(i , item){
		if((i + 1) >= limit_inf && (i + 1) <= limit_sup){
			var class_tumb = "item_thumbnails";
			if((i + 1) == select){
				class_tumb = "item_thumbnails_select";
			}
			var contenido = $('#content_thumbnails').html();
			$('#content_thumbnails').html(
					contenido + "<div id='item_thumbnails' class='"+ class_tumb +"' value='" + (i + 1) + "'>" +
							"<img src='" + $(this).attr('value') + "' />" +
							"</div>"
			);
		}
	});
	if( $('#move_right').attr("value") < $("#dayGallery_jquery_thumb span").size() ){
		$('#move_right').html("<img src='./img/der_btn.png' />");
		$('#move_right').css("cursor", "pointer");
	}else{
		$('#move_right').html(" ");
		$('#move_right').css("cursor", "default");
	}
	if($('#move_left').attr("value") > 1){
		$('#move_left').html("<img src='./img/izq_btn.png' />");
		$('#move_left').css("cursor", "pointer");
		
	}else{
		$('#move_left').html(" ");
		$('#move_left').css("cursor", "default");
	}
	
	$('#content_thumbnails div').click(click_thumb);
}

function show_img(valor){
	$("#dayGallery_jquery_img span").each(function(i , item){
		if(valor == (i + 1) ){
			$("#content_img_big").attr('value', valor);
			$("#content_img_big").css('display', 'none');
			$("#content_img_big").html("<img src='"+ $(this).attr('value') +"' />");
			$("#content_img_big").delay(300).fadeIn(500);
			return false;
		}
	});
}

function show_download(valor){
	$("#content_downloads").html("");
	$("#dayGallery_jquery_download span").each(function(i , item){
		if(valor == $(this).attr('value')){
			var html_nuevo = "<span class='download_link' value='" + $(this).attr('param') + "'>" + $(this).attr('title') + "</span>";
			var html_actual = $("#content_downloads").html();
			$("#content_downloads").html(html_actual + html_nuevo);
		}
	});
	$("#content_downloads span").click(click_download);
}

function click_download(){
	self.location.href = $(this).attr('value');
}

function click_thumb(){
	var item_click = $(this).attr("value");
	$(".item_thumbnails_select").each(function(i , item){
		$(this).removeClass("item_thumbnails_select");
		$(this).addClass("item_thumbnails");
	});
	$(this).addClass("item_thumbnails_select");
	show_img(item_click);
	show_download(item_click);
}

