$(document).ready(function() {
	
	    //Speed of the slideshow
    var speed = 6000;

    //You have to specify width and height in #slider CSS properties
    //After that, the following script will set the width and height accordingly
    $('#mask-gallery, #gallery li ,#mask-excerpt, #excerpt li').width($('#slidergal').width());    
    $('#gallery').width($('#slidergal').width() * $('#gallery li').length);
    $('#mask-gallery, #gallery li, #mask-excerpt, #excerpt li').height($('#slidergal').height());
    $('#excerpt').width($('#slidergal').width() * $('#excerpt li').length);

    

    if ($('page')) {
        
    var    page = $('#page').val();
        //Get previous sibling
    $('#gallery li:eq('+page+'), #excerpt li:eq('+page+')').addClass('selected');    
    $('#mask-gallery').scrollTo('li:eq('+page+')');        
    $('#mask-excerpt').scrollTo('li:eq('+page+')');        
    //if prev is set to 0 (next item)
    } 
    else 
    {
    $('#gallery li:first, #excerpt li:first').addClass('selected');    
    }
    
    //Next Slide by calling the function
    $('#btn-next').click(function () {
        newsscoller(0);    
        return false;
    });    

    //Previous slide by passing prev=1
    $('#btn-prev').click(function () {
        newsscoller(1);    
        return false;
    });    
    
    //Mouse over, pause it, on mouse out, resume the slider show
    $('#slidergal').hover(
    function () {
            return false;
    });
	//move the image in pixel
	var move = -15;
	
	//zoom percentage, 1.2 =120%
	var zoom = 1.2;

	//On mouse over those thumbnail
	$('.zitem').hover(function() {
		
		//Set the width and height according to the zoom percentage
		width = $('.zitem').width() * zoom;
		height = $('.zitem').height() * zoom;
		
		//Move and zoom the image
		$(this).find('img').stop(false,true).animate({'width':width, 'height':height, 'top':move, 'left':move}, {duration:200});
		
		//Display the caption
		$(this).find('div.caption').stop(false,true).fadeIn(200);
	},
	function() {
		//Reset the image
		$(this).find('img').stop(false,true).animate({'width':$('.zitem').width(), 'height':$('.zitem').height(), 'top':'0', 'left':'0'}, {duration:100});	

		//Hide the caption
		$(this).find('div.caption').stop(false,true).fadeOut(200);
	});

    $('.error_p').click(function() {

           $('.error_p').hide();
    });
    
});

function changeGal ($id)
{
  

	// Llamada ajax para obtener el estado de la galeria seleccionada
   $.ajax(
   {
   	  url: "get_protegido.php",
      data:({id_gal : $('#gal_id').attr('value')}),
      type: "POST",
      success: Protegido
   })
}   

function Protegido(respuesta)
 {

	if (respuesta == '1')
	{
	  if (confirm('This gallery contains explicit contents. If you are over 18 and want to go on, click on ACCEPT, else click CANCEL'))
	  {
	    $('#selection_gal').submit()
	  }
	  else
	  {
	   return false;
	  }
	 
	}
	else
	{
	  $('#selection_gal').submit() 
	}
 }
 
 function SelectBanner (id)
 {
	mariposaid = 'mariposa_'+id;
	$("#"+mariposaid).removeClass("mariposaUnselect")
	$("#"+mariposaid).addClass("mariposaSelect")
		 
		 
 }
 function UnselectBanner (id)
 {
	 mariposaid = 'mariposa_'+id;
	$("#"+mariposaid).removeClass("mariposaSelect")
	$("#"+mariposaid).addClass("mariposaUnselect")
		 
		 
 }
function newsscoller(prev) {

    //Get the current selected item (with selected class), if none was found, get the first item
    var current_image = $('#gallery li.selected').length ? $('#gallery li.selected') : $('#gallery li:first');
    var current_excerpt = $('#excerpt li.selected').length ? $('#excerpt li.selected') : $('#excerpt li:first');

    //if prev is set to 1 (previous item)
    if (prev) {
        
        //Get previous sibling
        var next_image = (current_image.prev().length) ? current_image.prev() : $('#gallery li:last');
        var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : $('#excerpt li:last');
    
    //if prev is set to 0 (next item)
    } else {
        
        //Get next sibling
        var next_image = (current_image.next().length) ? current_image.next() : $('#gallery li:first');
        var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : $('#excerpt li:first');
    }

    //clear the selected class
    $('#excerpt li, #gallery li').removeClass('selected');
    
    //reassign the selected class to current items
    next_image.addClass('selected');
    next_excerpt.addClass('selected');

    //Scroll the items
    $('#mask-gallery').scrollTo(next_image, 800);        
    $('#mask-excerpt').scrollTo(next_excerpt, 800);                    
    
}
function jumpto(page) {
    $('#mask-gallery').scrollTo('li:eq(page)', 800);        
    $('#mask-excerpt').scrollTo('li:eq(page)', 800);                
}

            $(function(){

                $('#cropbox').Jcrop({
                    aspectRatio: 1,
                    trueSize: [800,600], 
                    onChange: showPreview,
                    onSelect: updateCoords
                });

            });

            function showPreview(coords)
            {
                if (parseInt(coords.w) > 0)
                {
                    var rx = 100 / coords.w;
                    var ry = 100 / coords.h;

                    jQuery('#preview').css({
                        width: Math.round(rx * 800) + 'px',
                        height: Math.round(ry * 600) + 'px',
                        marginLeft: '-' + Math.round(rx * coords.x) + 'px',
                        marginTop: '-' + Math.round(ry * coords.y) + 'px'
                    });
                }
            }    
            function updateCoords(c)
            {
                $('#x').val(c.x);
                $('#y').val(c.y);
                $('#w').val(c.w);
                $('#h').val(c.h);
            };

            function checkCoords()
            {
                if (parseInt($('#w').val())) return true;
                alert('Please select a crop region then press submit.');
                return false;
            };
            
                        
            function mostrar_upload() 
            {
                 $("#div_uploading").show();
                };
