﻿
(function($) {
    $.galleryUtility = {};
    $.galleryUtility.centerCell = {};
    $.galleryUtility.rightCell = {};
    $.galleryUtility.leftCell = {};
    $.galleryUtility.rightCellStorage = {};
    $.galleryUtility.leftCellStorage = {};
    $.galleryUtility.gallery = {};
    
    $.galleryUtility.Options = {
        height: 300,
        width: 400,
        centerCellHeight: 280,
        centerCellWidth: 280,
        sideCellHeight: 130,
        sideCellWidth: 150,
		zoomFactor: 0.50,
        startClass: 'start',
        slideSpeed: 'slow',
        zoomSpeed: 'fast',
        gutterWidth: 30,
        autoStart: true
    };

    $.fn.slidingGallery = function(options) 
    {
     
       	$.galleryUtility.Options.height = $(this).parent().height();
       	$.galleryUtility.Options.width = $(this).parent().width();

        $.extend($.galleryUtility.Options, options);
        $.galleryUtility.gallery = $(this).css('cursor', 'pointer');
        $.galleryUtility.definePositions();

        var lastIndex = 0;
        var gallerySize = $.galleryUtility.gallery.each(function(i) {
        
        	if (i == 2) {
        		$(this).addClass($.galleryUtility.Options.startClass);
        		$(this).addClass('big');
        	} else {
        		$(this).addClass('small');
        	}
        	
            $(this).attr({
                'index': i,
                'prev': (i - 1),
                'next': (i + 1)
            }).css('position', 'absolute');
            
            lastIndex = i;
        }).hide().size();

        //fill in gallery with duplicates until there are at least 7
        var currIndex = 0;
        while (gallerySize < 7) {
            var $clone = $.galleryUtility.gallery.filter('[index=' + currIndex + ']').clone().attr({
                'index': lastIndex + 1,
                'prev': lastIndex,
                'next': lastIndex + 2
            }).removeClass($.galleryUtility.Options.startClass);
            $.galleryUtility.gallery.filter('[index=' + (lastIndex) + ']').after($clone);
            $.galleryUtility.gallery = $.galleryUtility.gallery.add('[index=' + (lastIndex + 1) + ']');
            lastIndex++;
            currIndex++;
            gallerySize++;
        }
        $.galleryUtility.gallery.filter('[index=' + lastIndex + ']').attr('next', 0);
        $.galleryUtility.gallery.filter('[index=0]').attr('prev', lastIndex);

        $.galleryUtility.setCenter($.galleryUtility.gallery.filter('.' + $.galleryUtility.Options.startClass).show());
        $.galleryUtility.setLeft($.galleryUtility.gallery.filter('[index=' + $.galleryUtility.centerCell.cell.attr('prev') + ']').show());
        $.galleryUtility.setRight($.galleryUtility.gallery.filter('[index=' + $.galleryUtility.centerCell.cell.attr('next') + ']').show());
        $.galleryUtility.setLeftStorage($.galleryUtility.gallery.filter('[index=' + $.galleryUtility.leftCell.cell.attr('prev') + ']'));
        $.galleryUtility.setRightStorage($.galleryUtility.gallery.filter('[index=' + $.galleryUtility.rightCell.cell.attr('next') + ']'));

        $.galleryUtility.leftCell.cell.one('click', $.galleryUtility.slideRight);
        $.galleryUtility.rightCell.cell.one('click', $.galleryUtility.slideLeft);
        $.galleryUtility.centerCell.cell.one('click', $.galleryUtility.zoomIn);
        
        $(window).bind('resize', function() {
				$.galleryUtility.definePositions();
				$.galleryUtility.setCenter($.galleryUtility.centerCell.cell);
				$.galleryUtility.setLeft($.galleryUtility.leftCell.cell);
				$.galleryUtility.setRight($.galleryUtility.rightCell.cell);
				$.galleryUtility.setLeftStorage($.galleryUtility.leftCellStorage.cell);
				$.galleryUtility.setRightStorage($.galleryUtility.rightCellStorage.cell);
        });

		if ($.galleryUtility.Options.autoStart) {
			$(document).everyTime(6000, function(i) {
				$.galleryUtility.slideRight();
			});
		}
        return $(this);
    };

    $.galleryUtility.slideRight = function()
    {
		
		$.galleryUtility.rightCell.cell.unbind().animate({
			'top': $.galleryUtility.rightCellStorage.top,
			'left': $.galleryUtility.rightCellStorage.left,
			'height': $.galleryUtility.rightCellStorage.height,
			'width': $.galleryUtility.rightCellStorage.width,
			'opacity': 0
		},
		$.galleryUtility.Options.slideSpeed, 'linear');
		
		$.galleryUtility.centerCell.cell.unbind().removeClass('small').addClass('big').animate({
			'top': $.galleryUtility.rightCell.top,
			'left': $.galleryUtility.rightCell.left,
			'height': $.galleryUtility.rightCell.height,
			'width': $.galleryUtility.rightCell.width
		},
		$.galleryUtility.Options.slideSpeed, 'linear', function() {
			$(this).one('click', $.galleryUtility.slideLeft);
		});


		$.galleryUtility.leftCell.cell.unbind().animate({
			'top': $.galleryUtility.centerCell.top,
			'left': $.galleryUtility.centerCell.left,
			'height': $.galleryUtility.centerCell.height,
			'width': $.galleryUtility.centerCell.width
		},
		$.galleryUtility.Options.slideSpeed, 'linear', function() {
			$(this).one('click', $.galleryUtility.zoomIn);
		});
		
		
		$.galleryUtility.leftCellStorage.cell.animate({
			'top': $.galleryUtility.leftCell.top,
			'left': $.galleryUtility.leftCell.left,
			'height': $.galleryUtility.leftCell.height,
			'width': $.galleryUtility.leftCell.width,
			'opacity': 1
		},
		$.galleryUtility.Options.slideSpeed, 'linear', function() {
			$(this).one('click', $.galleryUtility.slideRight);
		});
        		

		
		
		
	
		
        
        $.galleryUtility.rightCellStorage.cell =  $.galleryUtility.rightCell.cell;
        $.galleryUtility.rightCell.cell = $.galleryUtility.centerCell.cell;
        $.galleryUtility.centerCell.cell = $.galleryUtility.leftCell.cell;
        $.galleryUtility.leftCell.cell = $.galleryUtility.leftCellStorage.cell;
        
        $.galleryUtility.rightCell.cell.removeClass('big');
		$.galleryUtility.rightCell.cell.addClass('small');
		
		$.galleryUtility.leftCell.cell.removeClass('big');
		$.galleryUtility.leftCell.cell.addClass('small');
		
		$.galleryUtility.centerCell.cell.removeClass('small');
		$.galleryUtility.centerCell.cell.addClass('big');
       
   		$.galleryUtility.setLeftStorage($.galleryUtility.gallery.filter('[index=' + $.galleryUtility.leftCellStorage.cell.attr('prev') + ']'));
    };

    $.galleryUtility.slideLeft = function()
    {
           
		$.galleryUtility.rightCellStorage.cell.animate({
			'top': $.galleryUtility.rightCell.top,
			'left': $.galleryUtility.rightCell.left,
			'height': $.galleryUtility.rightCell.height,
			'width': $.galleryUtility.rightCell.width,
			'opacity': 1
		},
		$.galleryUtility.Options.slideSpeed, 'linear', function() {
			$(this).one('click', $.galleryUtility.slideLeft);
		});

		$.galleryUtility.rightCell.cell.unbind().animate({
			'top': $.galleryUtility.centerCell.top,
			'left': $.galleryUtility.centerCell.left,
			'height': $.galleryUtility.centerCell.height,
			'width': $.galleryUtility.centerCell.width
		},
		$.galleryUtility.Options.slideSpeed, 'linear', function() {
			$(this).one('click', $.galleryUtility.zoomIn);
		});

		$.galleryUtility.centerCell.cell.unbind().animate({
			'top': $.galleryUtility.leftCell.top,
			'left': $.galleryUtility.leftCell.left,
			'height': $.galleryUtility.leftCell.height,
			'width': $.galleryUtility.leftCell.width
		},
		$.galleryUtility.Options.slideSpeed, 'linear', function() {
			$(this).one('click', $.galleryUtility.slideRight);
		});
              
		$.galleryUtility.leftCell.cell.unbind().animate({
			'top': $.galleryUtility.leftCellStorage.top,
			'left': $.galleryUtility.leftCellStorage.left,
			'height': $.galleryUtility.leftCellStorage.height,
			'width': $.galleryUtility.leftCellStorage.width, 
			'opacity': 0
		},
		$.galleryUtility.Options.slideSpeed, 'linear');
        
        $.galleryUtility.leftCellStorage.cell = $.galleryUtility.leftCell.cell;
        $.galleryUtility.leftCell.cell = $.galleryUtility.centerCell.cell;
        $.galleryUtility.centerCell.cell = $.galleryUtility.rightCell.cell;
        $.galleryUtility.rightCell.cell = $.galleryUtility.rightCellStorage.cell;

		$.galleryUtility.rightCell.cell.removeClass('big');
		$.galleryUtility.rightCell.cell.addClass('small');
		
		$.galleryUtility.leftCell.cell.removeClass('big');
		$.galleryUtility.leftCell.cell.addClass('small');
		
		$.galleryUtility.centerCell.cell.removeClass('small');
		$.galleryUtility.centerCell.cell.addClass('big');

        $.galleryUtility.setRightStorage($.galleryUtility.gallery.filter('[index=' + $.galleryUtility.rightCellStorage.cell.attr('next') + ']'));
    };

    $.galleryUtility.zoomIn = function() 
   	{
    
    	//alert("ZoomIn Function");
    	
    };

    $.galleryUtility.setRightStorage = function( cell )
    {
		$.galleryUtility.rightCellStorage.cell = cell;       
		$.galleryUtility.rightCellStorage.cell.hide().css({
			'top': $.galleryUtility.rightCellStorage.top,
			'height': $.galleryUtility.rightCellStorage.height,
			'width': $.galleryUtility.rightCellStorage.width,
			'left': $.galleryUtility.rightCellStorage.left
		});
    };

    $.galleryUtility.setLeftStorage = function( cell )
    {
        $.galleryUtility.leftCellStorage.cell = cell;
		$.galleryUtility.leftCellStorage.cell.hide().css({
			'top': $.galleryUtility.leftCellStorage.top,
			'height': $.galleryUtility.leftCellStorage.height,
			'width': $.galleryUtility.leftCellStorage.width,
			'left': $.galleryUtility.leftCellStorage.left
		});
    };

    $.galleryUtility.setCenter = function( cell )
    {
        $.galleryUtility.centerCell.cell = cell;
     	$.galleryUtility.centerCell.cell.css({
			'top': $.galleryUtility.centerCell.top,
			'left': $.galleryUtility.centerCell.left,
			'height': $.galleryUtility.centerCell.height,
			'width': $.galleryUtility.centerCell.width
		});
    };

    $.galleryUtility.setRight = function( cell )
    {
   		$.galleryUtility.rightCell.cell = cell;
   		$.galleryUtility.rightCell.cell.css({
			'top': $.galleryUtility.rightCell.top,
			'height': $.galleryUtility.rightCell.height,
			'width': $.galleryUtility.rightCell.width,
			'left' : $.galleryUtility.rightCell.left
      	});
    };

    $.galleryUtility.setLeft = function( cell )
    {
        $.galleryUtility.leftCell.cell = cell;
        $.galleryUtility.leftCell.cell.css({
			'top': $.galleryUtility.leftCell.top,
			'height': $.galleryUtility.leftCell.height,
			'width': $.galleryUtility.leftCell.width,
			'left' : $.galleryUtility.leftCell.left
		});
    };

    $.galleryUtility.definePositions = function() {
    
        $.galleryUtility.centerCell.height = $.galleryUtility.Options.centerCellHeight;
        $.galleryUtility.centerCell.width = $.galleryUtility.Options.centerCellWidth;
        $.galleryUtility.centerCell.top = ($.galleryUtility.Options.height - $.galleryUtility.Options.centerCellHeight) / 2;
    	$.galleryUtility.centerCell.left = ($.galleryUtility.Options.width - $.galleryUtility.Options.centerCellWidth ) / 2;
        
        $.galleryUtility.leftCell.height = $.galleryUtility.Options.sideCellHeight;
        $.galleryUtility.leftCell.width = $.galleryUtility.Options.sideCellWidth;
        $.galleryUtility.leftCell.top = ($.galleryUtility.Options.height - $.galleryUtility.leftCell.height ) / 2;
        $.galleryUtility.leftCell.left = $.galleryUtility.centerCell.left - $.galleryUtility.leftCell.width - $.galleryUtility.Options.gutterWidth;

        $.galleryUtility.rightCell.height = $.galleryUtility.Options.sideCellHeight;
        $.galleryUtility.rightCell.width = $.galleryUtility.Options.sideCellWidth;
        $.galleryUtility.rightCell.top = ($.galleryUtility.Options.height - $.galleryUtility.rightCell.height ) / 2;
        $.galleryUtility.rightCell.left = $.galleryUtility.centerCell.left + $.galleryUtility.centerCell.width + 5;

        $.galleryUtility.leftCellStorage.height = Math.round($.galleryUtility.leftCell.height * $.galleryUtility.Options.zoomFactor);
        $.galleryUtility.leftCellStorage.width = Math.round($.galleryUtility.leftCell.width * $.galleryUtility.Options.zoomFactor);
       	$.galleryUtility.leftCellStorage.top = Math.round($.galleryUtility.Options.height / 2);
       	// $.galleryUtility.leftCellStorage.top = Math.round($.galleryUtility.leftCell.top + (($.galleryUtility.leftCell.height - $.galleryUtility.leftCellStorage.height) / 2));
        //$.galleryUtility.leftCellStorage.left =  Math.round($.galleryUtility.leftCell.left - ($.galleryUtility.leftCellStorage.width + $.galleryUtility.Options.gutterWidth));
        $.galleryUtility.leftCellStorage.left =  Math.round($.galleryUtility.Options.width /2);
          
        $.galleryUtility.rightCellStorage.height = Math.round($.galleryUtility.rightCell.height * $.galleryUtility.Options.zoomFactor);
        $.galleryUtility.rightCellStorage.width = Math.round($.galleryUtility.rightCell.width * $.galleryUtility.Options.zoomFactor);
        
        //$.galleryUtility.rightCellStorage.top = Math.round($.galleryUtility.rightCell.top + (($.galleryUtility.rightCell.height - $.galleryUtility.rightCellStorage.height) / 2));
        //$.galleryUtility.rightCellStorage.left = Math.round($.galleryUtility.rightCell.left + ($.galleryUtility.rightCell.width + $.galleryUtility.Options.gutterWidth));
        
        $.galleryUtility.rightCellStorage.top = Math.round($.galleryUtility.Options.height / 2);
        $.galleryUtility.rightCellStorage.left = Math.round($.galleryUtility.Options.width /2);
       
    };
})(jQuery);


jQuery.fn.extend({
	everyTime: function(interval, label, fn, times, belay) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times, belay);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});

jQuery.extend({
	timer: {
		global: [],
		guid: 1,
		dataKey: "jQuery.timer",
		regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value === undefined || value === null) {
				return null;
			}
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseFloat(result[1]);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times, belay) {
			var counter = 0;
			
			if (jQuery.isFunction(label)) {
				if (!times) {
					times = fn;
				}
				fn = label;
				label = interval;
			}
			
			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval <= 0) {
				return;
			}

			if (times && times.constructor != Number) {
				belay = !!times;
				times = 0;
			}
			
			times = times || 0;
			belay = belay || false;
			
			var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
			
			if (!timers[label])
				timers[label] = {};
			
			fn.timerID = fn.timerID || this.guid++;
			
			var handler = function() {
				if (belay && this.inProgress) 
					return;
				this.inProgress = true;
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
				this.inProgress = false;
			};
			
			handler.timerID = fn.timerID;
			
			if (!timers[label][fn.timerID])
				timers[label][fn.timerID] = window.setInterval(handler,interval);
			
			this.global.push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = jQuery.data(element, this.dataKey), ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.timerID ) {
							window.clearInterval(timers[label][fn.timerID]);
							delete timers[label][fn.timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					jQuery.removeData(element, this.dataKey);
			}
		}
	}
});

jQuery(window).bind("unload", function() {
	jQuery.each(jQuery.timer.global, function(index, item) {
		jQuery.timer.remove(item);
	});
});