﻿(function($){

// Creating the sweetPages jQuery plugin:
$.fn.sweetPages = function(opts){
	
	// If no options were passed, create an empty opts object
	if(!opts) opts = {};
	
	var resultsPerPage = opts.perPage || 3;
	var lightboxOpen = false;
	
	// The plugin works best for unordered lists, althugh ols would do just as well:
	var ul = this;
	var li = ul.find('li');
	
	li.each(function(){
		// Calculating the height of each li element, and storing it with the data method:
		var el = $(this);
		el.data('height',el.outerHeight(true));
	});
	
	// Calculating the total number of pages:
	var pagesNumber = Math.ceil(li.length/resultsPerPage);
	
	// If the pages are less than two, do nothing:
	if(pagesNumber<2) return this;

	// Creating the controls div:
	var swControls = $('<div class="swControls">');
	
	for(var i=0;i<pagesNumber;i++)
	{
		// Slice a portion of the lis, and wrap it in a swPage div:
		li.slice(i*resultsPerPage,(i+1)*resultsPerPage).wrapAll('<div class="swPage" />');
		
		// Adding a link to the swControls div:
		swControls.append('<a href="" class="swShowPage swShowPageNumber" rel="'+(i+1)+'">'+(i+1)+'</a>');
	}
	
	swControls.prepend('<a href="" class="swShowPage" id="prevPage">&laquo; Prev</a><span class="pageCounter">Page <span class="currentPage">1</span> of ' + pagesNumber + '</span><a href="" class="swShowPage" id="nextPage">Next &raquo;</a>');
	
	ul.append(swControls);
	
	var maxHeight = 0;
	var totalWidth = 0;
	
	var swPage = ul.find('.swPage');
	swPage.each(function(){
		
		// Looping through all the newly created pages:
		
		var elem = $(this);

		var tmpHeight = 0;
		elem.find('li').each(function(){ tmpHeight+=($(this).data('height')); });
        
        // alert(tmpHeight);

		if(tmpHeight>maxHeight)
			maxHeight = tmpHeight;

		totalWidth+=elem.outerWidth();
		 
		elem.css('float','left').width(ul.width());
	});
	
	swPage.wrapAll('<div class="swSlider" />');
	
	// Setting the height of the ul to the height of the tallest page:
	ul.height(maxHeight);
	
	var swSlider = ul.find('.swSlider');
	swSlider.append('<div class="clear" />').width(totalWidth);

	var hyperLinks = ul.find('a.swShowPageNumber');
	
	hyperLinks.click(function(e){
		
		// If one of the control links is clicked, slide the swSlider div 
		// (which contains all the pages) and mark it as active:

		$(this).addClass('active').siblings().removeClass('active');
		
		swSlider.stop().animate({'margin-left':-(parseInt($(this).text())-1)*ul.width()},'slow');
		
		checkForDisabled(parseInt($(this).text()));
		
		e.preventDefault();
	});
	
	$('#prevPage').click(function(e){
			
		// If the prev page link is clicked, slide the swSlider div to the prev page
		// (which contains all the pages) and mark it as active:

		var c = parseInt($('.swControls .active').attr('rel'));
		
		if (c <= 1) {
			c = $('.swShowPageNumber').length + 1;
		}
		
		swSlider.stop().animate({'margin-left':-(c-2)*ul.width()},'slow');
		$('.swControls a[rel="'+ (c-1) +'"]').addClass('active').siblings().removeClass('active');
		updatePageCount(c-1);
		
		e.preventDefault();
	});
	
	$('#nextPage').click(function(e){
				
		// If the prev page link is clicked, slide the swSlider div to the prev page
		// (which contains all the pages) and mark it as active:

		var c = parseInt($('.swControls .active').attr('rel'));
		
		if (c >= ($('.swShowPageNumber').length)) {
			c = 0;
		}
		
		swSlider.stop().animate({'margin-left':-(c)*ul.width()},'slow');
		$('.swControls a[rel="'+ (c+1) +'"]').addClass('active').siblings().removeClass('active');
		updatePageCount(c+1);
		
		e.preventDefault();
	});
	
	// start animation
	startProductAnimation();
	
	// stop animation on click
    $('.product_images li a').click( function () { lightboxOpen = true; });
    
    // start animation when lightbox closed button is clicked
    $('#cboxClose').click( function () { lightboxOpen = false; startProductAnimation(); });
    $('#cboxOverlay').click( function () { lightboxOpen = false; startProductAnimation(); });
    
    // pause animation on hover over image, next or prev links - restart animating on hover off
    $('.product_images li').hover( function () { pauseAnimation(); }, function () { if (! lightboxOpen) { startProductAnimation(); } });
    $('.swControls #nextPage').hover( function () { pauseAnimation(); }, function () { if (! lightboxOpen) { startProductAnimation(); } });
    $('.swControls #prevPage').hover( function () { pauseAnimation(); }, function () { if (! lightboxOpen) { startProductAnimation(); } });
	
	// update the page count (page x of x)
	function updatePageCount(c) { jQuery('.pageCounter .currentPage').text(c); }
	
	// start animating the images every x seconds
	function startProductAnimation() {
        $(".product_images").everyTime("3s", function() { $('#nextPage').click(); });
	}
	
	// pause the animation
	function pauseAnimation() { $(".product_images").stop(true).stopTime(); }
	
	// Mark the first link as active the first time this code runs:
	hyperLinks.eq(0).addClass('active');
	
	return this;
	
}})(jQuery);