///Photo Gallery
jQuery.fn.gallery = function(galid, options) {


    var defaults = { speed: 8000, scrollslide: 1, squares: 1, centre: 1, focuscol: '#154293', backcol: '#3FB152'};
    var settings = $.extend(defaults, options)
    var slidespeed = settings.speed;
    var auto = settings.scrollslide;
    var showsquare = settings.squares;
    var focuscolour = settings.focuscol;
    var backgroundcolour = settings.backcol;
    var centresquare = settings.centre;
    var squareclick = 0;
	

    if (showsquare == 1) {
    var show = 1;
    var sqa = 1;
        $('#gallery' + galid).find('.photogal').each(function(){$(this).wrap("<div class='photohold' id='image" + show + "'></div>"); show++;});
        $('#gallery' + galid).find('.photogal').each(function(){$('#gallery' + galid).append("<div class='square' id='square" + sqa + "'></div>"); sqa++;});
        $('#gallery' + galid).find('.square').wrapAll("<div class='squareinner'></div>");
        $('#gallery' + galid).find('.square').wrapAll("<div class='squarecon'></div>");
        if(centresquare !=0)
        {
        $('.squarecon').css("position", "absolute").css("left", "50%");
        $('.squareinner').css("position","relative").css("right", $('.squarecon').width() / 2 + 5 + "px");
        }
    } else { };

    function autoslide() {
        $(this).find('.photogal').fadeOut(1000);
        if (maximum == 2) {
            counter = 1;
        } else {
            $('#gallery' + galid).find('.photogal').fadeOut(1000);
            $('#gallery' + galid).find('.square').css('background', backgroundcolour);
            if (counter == 1) {
                $('#gallery' + galid).find('.square:first').css('background', focuscolour);
                $('#gallery' + galid).find('.photogal:first').fadeIn(1500);
            }
            else {
                $('#gallery' + galid).find('.square:gt(' + gtline + '):lt(1)').css('background', focuscolour);
                $('#gallery' + galid).find('.photogal:gt(' + gtline + '):lt(1)').fadeIn(1500);
                gtline++;
            }
            counter++;

            if (counter == maximum) {
                counter = 1;
                gtline = 0;
            }
        }
    }
	
	$('#gallery' + galid).find('.square').css('background', backgroundcolour);
    $('.photogal').hide();
    $('.gallerywrapper').each(function() {
        $(this).find('.photogal').first().show();
        $('#gallery' + galid).find('.square').first().css('background', focuscolour);
    });
    
    $(this).find('.square').click(function(event) {
        if ($(this).closest(".photogal").is(':visible')) { } else {
            $(this).parents('#gallery' + galid).find('.photogal').hide();
            $(this).parents('#gallery' + galid).find('.square').css('background', backgroundcolour);
             $('#gallery' + galid).find("#image" + $(this).attr("id").replace("square","")).find(".photogal").show();
            $(this).css('background', focuscolour);
            clearInterval(interm);
        }
           squareclick = 1;
        return false;
    });
    
    var counter = 2;
    var gtline = 0;
    var maximum = $(this).find('.photogal').length + 1;
    if (auto == 0) { } else {
        var interm = setInterval(autoslide, slidespeed);
    };
    
    if(squareclick == 0)
    {
 $(this).hover(function(){clearInterval(interm);},function(){interm = setInterval(autoslide, slidespeed);});
 }
	
}
