$(document).ready(

    function(){

    BarrGallery();

    });





function BarrGallery(){

    

    

    //fade in first image

    var theImage = $('.umb:first').attr('name');

    $('#gallerycontent').append('<img id="theImage" src="' + theImage + '">');

    $('#theImage').hide();

    $('#theImage').bind('load', function(){ $('#theImage').fadeIn(500); })

    

    var inDex = 0;

    

    //images array

    var theImages = new Array();

    

    //umb handler

    $('.umb').each(function(){

        

        theImages.push($(this).attr('name'));

       

    });

    

    $('#back').hide();

    $('#next').hide();

    

   

    $('#back').mouseenter(function(){ 

    	

    	this.show();

    	

    });

    

    var isMousein = false;

    

    $('#gallerycontent').hover(

    		

    		function(){

    			

    	    	if(inDex <= 0 ){ 

    	    		$('#next').fadeIn(); 

    	    	}else if(inDex >= theImages.length - 1){

    	    		$('#back').fadeIn();

    	    	}else{

    	    		$('#next').fadeIn();

    	    		$('#back').fadeIn();

    	    	}

    	    	

    	    	isMousein = true;

    	    	

    		},

    		function(){

    			

    			$('#next').fadeOut();

        		$('#back').fadeOut();

        		

        		isMousein = false;

        		

    		}

    

    );

    

    

    $('#back').bind("click", function(){

        inDex = theImages.indexOf(theImage) - 1;

        if(inDex>=0){

        	$('#next').show();

            $('#theImage').fadeOut(500,function(){

                $('#theImage').remove();

                $('#gallerycontent').append('<img id="theImage" src="' + theImages[inDex] + '" >');

                    $('#theImage').hide();

                    $('#theImage').bind('load', function(){

                        $('#theImage').fadeIn(500);

                        theImage = theImages[inDex];



                    });

                });

            //alert(inDex);

            //alert(theImages.indexOf(theImage) - 1)

            if(inDex == 0){ $('#back').fadeOut(); }

        }else{

        	$('#back').hide();

        }

    });

    

    $('#next').bind("click", function(){

        inDex = theImages.indexOf(theImage);

        inDex += 1;

        //alert(inDex);

        if(inDex < theImages.length ){

        	$('#back').show();

            $('#theImage').fadeOut(500,function(){

                $('#theImage').remove();

                $('#gallerycontent').append('<img id="theImage" src="' + theImages[inDex] + '" >');

                    $('#theImage').hide();

                    $('#theImage').bind('load', function(){

                        $('#theImage').fadeIn(500);



                        theImage = theImages[inDex];



                    });

                });

            //alert(inDex);

            //alert(theImages.length - 3)

            if(inDex == theImages.length - 1 ){ $('#next').fadeOut(); }

        }else{

        	$('#next').hide();

        	//alert (inDex);

        }

    });

    

    $(document).keydown(function(event){

    	//next key

        if (event.keyCode == '39'){

            inDex = theImages.indexOf(theImage);

            inDex += 1;

            if(inDex < theImages.length){

            	if(isMousein){$('#back').show();}

            	

                $('#theImage').fadeOut(500,function(){

                    $('#theImage').remove();

                    $('#gallerycontent').append('<img id="theImage" src="' + theImages[inDex] + '" >');

                        $('#theImage').hide();

                        $('#theImage').bind('load', function(){

                            $('#theImage').fadeIn(500);



                            theImage = theImages[inDex];



                        });

                    });

                if(inDex == theImages.length - 1 ){ $('#next').fadeOut(); }

            }

        //back key

        }else if(event.keyCode == '37'){

            inDex = theImages.indexOf(theImage) - 1;

            if(inDex>=0){

            	if(isMousein){$('#next').show();}

                $('#theImage').fadeOut(500,function(){

                    $('#theImage').remove();

                    $('#gallerycontent').append('<img id="theImage" src="' + theImages[inDex] + '" >');

                        $('#theImage').hide();

                        $('#theImage').bind('load', function(){

                            $('#theImage').fadeIn(500);



                            theImage = theImages[inDex];



                        });

                    });

                if(inDex == 0){ $('#back').fadeOut(); }

            }

        }

    });

        

}



Array.prototype.indexOf=function(o,i){for(var j=this.length,i=i<0?i+j<0?0:i+j:i||0;i<j&&this[i]!==o;i++);return j<=i?-1:i};





/*

<img src="/wordpress/images/left.png" class="left-button" rel="1"><img src="/wordpress/images/right.png" class="right-button" rel="1"><center>

<div class="simpleSlide-window" rel="1">

             <div class="simpleSlide-tray" rel="1"> 

                          <div class="simpleSlide-slide" rel="1"> 

                                       <img src="/wordpress/images/custom-slideshow/image1.jpg" alt="SEO" title="SEO"> 

                          </div> 

                          <div class="simpleSlide-slide" rel="1"> 

                                       <img src="/wordpress/images/custom-slideshow/image2.jpg" alt="SEO" title="SEO"> 

                          </div> 

                          <div class="simpleSlide-slide" rel="1"> 

                                       <img src="/wordpress/images/custom-slideshow/image3.jpg" alt="SEO" title="SEO"> 

                          </div> 

             </div> 

</div> 

</center>

*/
