// JavaScript Document

(function($) {
 
   $.fn.bdSlide = function(settings) {
	 var config = {	'speed' : '200',
					'opacity' : '0.5',	
					'animHeight' : 0	};
 
     if (settings)
	 {	if(settings.constructor == Object)
	 	{	$.extend(config, settings);
		}
		else
		{	alert('This jQuery extension expects the paramater to be an Object, but you have used a ' + settings.constructor.name + ' so the the default values have been used instead');
		}
	 } 
     this.each(function() {
       
		
	   $(this).find(".text").stop().animate({ top: config.animHeight }, 200);
		if ($(this).find("a").attr("href")==undefined) {
			$(this).css("cursor","default");
		}
     });
 
     return this;
 
   };
   
  
   $.fn.bdSlideImage = function(settings) {
    var config = {	'speed' : 200,
	 				'delay'	: 1,
					'direction' : 'left'};
     if (settings)
	 {	if(settings.constructor == Object)
	 	{	$.extend(config, settings);
		}
		else
		{	alert('This jQuery extension expects the paramater to be an Object, but you have used a ' + settings.constructor.name + ' so the the default values have been used instead');
		}
	 }	
     this.each(function() {	
			var delayAll = 300;
       		var totalDelay = config.delay * config.speed + delayAll;
			var currentWidth = $(this).width();
			var currentHeight = $(this).height();
			// jQuery css() takes an object as a paramater
			
			var css = 	
				{	left 	:	{	'left' 		: 	'-' + currentWidth + 'px'		},
					right	:	{	'right' 	: 	'-' + currentWidth + 'px'		},
					top		: 	{	'top' 		: 	'-' + currentHeight + 'px'		},
					bottom	:	{	'bottom' 	: 	'-' + currentHeight + 'px'		},
					opacity :	{	'opacity' 	: 	config.startOpacity		}
				}
			// jQuery animate() takes an object as a paramater	
			var anim = 
				{	left 	:	{	'left' 		: 	'+=' + currentWidth			},
					right	:	{	'right' 	: 	'+=' + currentWidth			},
					top		: 	{	'top' 		: 	'+=' + currentHeight 		},
					bottom	:	{	'bottom' 	: 	'+=' + currentHeight		},
					opacity	:	{	'opacity' 	: 	'+=' + config.finishOpacity		}
				}
			
			if(config.startOpacity || config.startOpacity == 0)
			{
				$(this).css(
						$.extend( //merges 2 objects
							css[config.direction],
							css[config.startOpacity]
						)
					)
					.delay(totalDelay)
					.animate(
						$.extend(
							anim[config.direction], 
							anim[config.finishOpacity]
						),
						config.speed
					);
			}
			else
			{
				$(this).css(css[config.direction])
					.delay(totalDelay)
					.animate(anim[config.direction],config.speed);
			}		
     }); 
     return this;
   };
   
$.fn.paginate = function(settings) {
    var config = {
        book: $(this),
        pages: $(this).children().length - 1,
        prevNext: true,
        startPage: 0
    };

    if (settings)
    {
        if (settings.constructor == Object)
        {
            $.extend(config, settings);
        }
        else
        {
            alert('This jQuery extension expects the paramater to be an Object, but you have used a ' + settings.constructor.name + ' so the the default values have been used instead');
        }
    }
    function prevNextModifier()
    {
        return config.prevNext ? 2: 0;
    }
    function getLength(p) {
        return p + prevNextModifier();
    }
    function getFirstPage() {
        return 0 + (prevNextModifier() / 2);
    }
    function getLastPage(p) {
        return p + (prevNextModifier() / 2);
    }

    function getPageNumber(i) {
        return i - (prevNextModifier() / 2) + 1;
    }
    function getSelected(s) {
        return s + (prevNextModifier() / 2);
    }
    function type(i) {
        if (!config.prevNext)
        {
            return 'page';
        }
        else if (i == 0)
        {
            return 'previous'
        }
        else if (i == state1.length)
        {
            return 'next';
        }
        else
        {
            return 'page';
        }
    }
    var state = function(initialState) {
        this.pages = initialState.pages;
        this.selected = getSelected(initialState.startPage);
        this.length = getLength(this.pages);
        this.firstPage = getFirstPage();
        this.lastPage = getLastPage(this.pages);
    }
    var state1 = new state(config);


    this.each(function() {
        $(this).after('<div id="pagination"></div>');
        buildPagination();
        function paginationEvents() {
            $('#pagination li.page').each(
            function(index) {
                var i = index;
                $(this).find('a').click(
                function()
                {
                    state1.selected = getPageNumber(i) + 1;
                    $('#pagination').empty();
                    buildPagination();
                    config.book.find('li').hide();
                    config.book.find('li').eq(state1.selected - 1).fadeIn('fast');
                    return false;
                }
                )
            }
            )
            $('#pagination li.next').each(
            function(index) {
                var i = index;
                $(this).find('a').click(
                function()
                {
                    state1.selected = state1.selected + 1;
                    $('#pagination').empty();
                    buildPagination();
                    config.book.find('li').hide();
                    config.book.find('li').eq(state1.selected - 1).fadeIn('fast');
                    return false;
                }
                )
            }
            )

            $('#pagination li.previous').each(
            function(index) {
                var i = index;
                $(this).find('a').click(
                function()
                {
                    state1.selected = state1.selected - 1;
                    $('#pagination').empty();
                    buildPagination();
                    config.book.find('li').hide();
                    config.book.find('li').eq(state1.selected - 1).fadeIn('fast');
                    return false;
                }
                )
            }
            )
        }
        function buildPagination() {
            $('#pagination').each(
            function() {
                $(this)
                .elems('ol')
                .elems('li', state1.length + 1)
                .each(
                function(index) {
                    if (type(index) == 'previous')
                    {
                        $(this).addClass('relative previous');
                        if (state1.selected != state1.firstPage)
                        {
                            $(this).elems('a').elems('span').text('Previous');
                        }
                        else
                        {
                            $(this).elems('span').text('Next');
                        }
                    }
                    else if (type(index) == 'next')
                    {
                        $(this).addClass('relative next');
                        if (state1.selected != state1.lastPage)
                        {
                            $(this).elems('a').elems('span').text('Next');
                        }
                        else
                        {
                            $(this).elems('span').text('Next');
                        }
                    }
                    else if (type(index) == 'page')
                    {
                        $(this).addClass('page i' + +getPageNumber(index));
                        if (index != state1.selected)
                        {
                            $(this).elems('a').elems('span').text('Page' + getPageNumber(index));
                        }
                        else
                        {
                            $(this).elems('span').text('Page' + getPageNumber(index));
                        }
                    }
                }
                );
            }
            );
            paginationEvents();
        }
    });
    return this;

};

$.fn.elems = function(el, n) {
    //alert(' o length = ' + $(this).length);
    var elementString = el;
    var quantity = n || 1;
    this.each(function() {
        var allElementsString = '';
        for (var i = 0; i < quantity; i++)
        {
            allElementsString += '<' + el + '>' + '</' + el + '>'
        }
        //alert('quantity = ' + quantity)
        $(this).append(allElementsString);
    });
    return $(this).find(elementString);
}

   
})(jQuery);