var current_photo = 1;
var total_photos;
jQuery.prototype.sort = Array.prototype.sort; 
if (typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} }; 
    var lock_count = 4;
	var thumb_space = 150;
        window.onload = function() {
            initGal();
			$('#blackout').fadeOut();
        }
		$(function(){ //document.ready
			$('#blackout').css({opacity:0.7, display: 'block'});
		});
		function stripId(img){
			return parseInt(img.id.split('_')[1],10);
		}
		
		function returnPhoto(arr,codition){ //returns the photo with the smaller or bigger id
			var replacedimg=arr[0];
			for(var i=1;i<arr.length;i++){
				if(codition == "lower"){
					if(stripId(replacedimg) > stripId(arr[i])) replacedimg = arr[i];
				}
				else{
					if(stripId(replacedimg) < stripId(arr[i])) replacedimg = arr[i];
				}
			}
			return replacedimg;
		}

        
        function slide_left(img){
            if(img === undefined || !img || img.length == 0) lock_count++;
            else{
                //.css({width: 400, height:400, left:280, top: 50})
                var old_img_height = $(img).attr('height');
                var old_img_width = $(img).attr('width');
				img_width = (old_img_width * 195)/(old_img_height);
                img_height = 195;
				var new_left = thumb_space - img_width;
                $(img).siblings('.color-full').animate({opacity:0,width: img_width, height:img_height, left:new_left, top: 148},1500,function(){
                    $(this).css({visibility:'hidden',width:old_img_width,height:old_img_height});
                });
                $(img).animate({width: img_width, height:img_height, left:new_left, top: 148},1500,'swing',function(){
                        $(this).removeClass().addClass('left-photo');
                        lock_count++;
                    }).css({'z-index':30});
            }
        }
        
        function slide_left_from_right(num){ //trae una imagen del no-show left
            //if(img === undefined || !img || img.length == 0) lock_count++;
            //else{
				//if($('.no-show-left').length < 2){
				//	$(returnPhoto($('.no-show'),'higher')).removeClass().addClass('no-show-left');
				//}
				var new_id = (num - 2 + total_photos ) % total_photos; //da la foto que hay que traer de la izquierda
				//console.log("Id a traer:" + new_id);
				var new_left = thumb_space - $('#photo_'+ new_id).attr('width');
				$('#photo_'+ new_id).removeClass().addClass('no-show-left').css({left:-269, top: 148}).animate({left:new_left, top: 148},1500,'swing',function(){
                        $(this).removeClass().addClass('left-photo');
                        lock_count++;
                    });
				current_photo = (current_photo - 1 + total_photos) % total_photos;
				//if (current_photo < 0) current_photo *= -1;
				//console.log("Current photo:" + current_photo);
            //}
        }
		
        function bring_from_right(num){ //trae una imagen del no-show right
            //if(img === undefined || !img || img.length == 0) lock_count++;
            //else{				
				//if($('.no-show').length < 2){
					//var noshow_arr = $('.no-show-left');
				//	$(returnPhoto($('.no-show-left'),'lower')).removeClass().addClass('no-show');
				//}
                //var img_left = 960 - $(img).attr('width');
				var new_id = (num + 2 + total_photos ) % total_photos; //da la foto que hay que traer de la derecha
				var img_left = 960 - thumb_space;
                $('#photo_'+ new_id).removeClass().addClass('no-show').css({left:960, top: 148}).animate({left:img_left},1500,function(){
                            $(this).removeClass().addClass('right-photo');
                            lock_count++;
                        });
				current_photo = (current_photo + 1 + total_photos) % total_photos;
            //}
        }
        function slide_right(img){
            if(img === undefined || !img || img.length == 0) lock_count++;
            else{
                var old_img_height = $(img).attr('height');
                var old_img_width = $(img).attr('width');                
                img_width = (old_img_width * 195)/(old_img_height);
                img_height = 195;  
                var img_left = 960 - thumb_space;
                //.css({width: 400, height:400, left:280, top: 50})
                $(img).siblings('.color-full').animate({opacity:0,width: img_width, height:img_height, left:img_left, top: 148},1500,function(){
                    $(this).css({visibility:'hidden',width:old_img_width,height:old_img_height});
                });
                $(img).animate({width: img_width, height:img_height, left:img_left, top: 148},1500,'swing',function(){
                        $(this).removeClass().addClass('right-photo');
                        lock_count++;
                    }).css({'z-index':30});
            }
        }
        
        function slide_center_from_left(img){
            if(img === undefined || !img || img.length == 0) lock_count++;
            else{
				var full_img = $(img).siblings('.color-full');
                var old_img_height = $(img).attr('height');
                var old_img_width = $(img).attr('width');
                var img_height = $(full_img).attr('height');
                var img_width = $(full_img).attr('width');
                var img_left = (960 - img_width) /2;
                var img_top = (500 - img_height) /2;
				var new_left = thumb_space - old_img_width;
                $(full_img).css({height:old_img_height,width:old_img_width,left:new_left,top:148,'z-index':60,opacity:0,visibility:'visible'}).animate({opacity:1,width: img_width, height:img_height, left:img_left, top: img_top},1500,function(){
                });
                
                $(img).css({left:new_left,top:148,'z-index':50}).animate({width: img_width, height:img_height, left:img_left, top: img_top},1500,function(){
                            $(this).removeClass().addClass('main-photo');
                            lock_count++;
                        });
            }
        }
        
        function slide_center_from_right(img){ //pone en ppal una foto que estaba a la derecha
            if(img === undefined || !img || img.length == 0) lock_count++;
            else{
				var full_img = $(img).siblings('.color-full');
                var old_img_height = $(img).attr('height');
                var old_img_width = $(img).attr('width');
                var css_img_left = 960 - thumb_space;
                var img_height = $(full_img).attr('height');//2.5 * old_img_height;
                var img_width = $(full_img).attr('width');//2.5 * old_img_width;
                var img_left = (960 - img_width) /2;
                var img_top = (500 - img_height) /2;
                $(full_img).css({height:old_img_height,width:old_img_width,left:css_img_left,top:148,'z-index':60,opacity:0,visibility:'visible'}).animate({opacity:1,width: img_width, height:img_height, left:img_left, top: img_top},1500,function(){
                });
                $(img).css({left:css_img_left,top:148,'z-index':50}).animate({width: img_width, height:img_height, left:img_left, top: img_top},1500,function(){
                            $(this).removeClass().addClass('main-photo');
                            lock_count++;
                        });
            }
        }
        function take_to_left(img){ //leva una foto de la izquierda al no show izquierda
            if(img == undefined || !img || img.length == 0) lock_count++;
            else{
                var img_left = -1 * $(img).attr('width');
                $(img).animate({left:img_left},1500,function(){
                            $(this).removeClass().addClass('no-show-left');
                            lock_count++;
                });
            }
        }
        
        function take_to_right(img){
            if(img === undefined || !img || img.length == 0) lock_count++;
            else{
                $(img).animate({left:960},1500,function(){
                            $(this).removeClass().addClass('no-show');
                            lock_count++;
                });
            }
        }
        
        function scrollRight(){
            if(lock_count == 4 && $('.left-photo').length > 0){
                lock_count = 0;
                slide_right($('.main-photo'));
                take_to_right($('.right-photo'));
                slide_center_from_left($('.left-photo'));
                var img_arr = $('.no-show-left');
                slide_left_from_right(current_photo); //ahora envio solo el numero del la actual ppal
            }
        }
        
        function scrollLeft(){
            if(lock_count == 4 && $('.right-photo').length > 0){
                lock_count = 0;
                slide_left($('.main-photo'));
                take_to_left($('.left-photo'));
                slide_center_from_right($('.right-photo'));
                bring_from_right(current_photo);
            }
        }
        
        function initGal(){
            var img = $('.center-photo');
			var orig_left = $('img').attr('left');
			var orig_top = $('img').attr('top');
			var full_img = $(img).siblings('.color-full');
            $(img).removeClass().addClass('main-photo');
            var old_img_height = $(img).attr('height');
            var old_img_width = $(img).attr('width');
            var img_height =  $(full_img).attr('height');
            var img_width= $(full_img).attr('width');
            var img_left = (960 - img_width) /2;
            var img_top = (500 - img_height) /2;
            $(img).css({'z-index': 40}).animate({width: img_width, height:img_height, left:img_left, top: img_top});
            $(full_img).css({width: old_img_width, height:old_img_height,visibility:'visible', 'z-index': 50, left:orig_left, top: orig_top}).animate({width: img_width, height:img_height, left:img_left, top: img_top});
			var lphoto = $('.left-photo');
			var lphoto_left = thumb_space - lphoto.attr('width');
			lphoto.css({left : lphoto_left});
			var nslphoto = $('.no-show-left');
			var nslphoto_left = -1 * nslphoto.attr('width');
			nslphoto.css({left : nslphoto_left});
			var rpleft = 960 - thumb_space;
			$('.right-photo').css({left:rpleft});
			total_photos = $('.left-photo').length + $('.center-photo').length + $('.right-photo').length + $('.no-show-left').length + $('.no-show').length + 1;
        }