/**
 * smartSlider JQuery Plugin
 * version 0.1 beta
 *
 * Copyright (c) 2009 Yasin DAĞLI
 *
 * @author Yasin DAĞLI <yasindagli@gmail.com>
 * 14-06-2009
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */
jQuery.fn.smartslider = function(options){

    function Pager(){
        this.bound = 10;
        this.linkNumber = 5;
        this.firstLink = 'First';
        this.lastLink = 'Last';
        this.prevLink = '&lt;';  
        this.nextLink = '&gt;';
        
        this.setBound = function(bound){
            this.bound = bound;
        }
        this.setLinkNumber = function(linkNumber){
            this.linkNumber = linkNumber;
        }    
        this.setFirstLink = function(firstLink){
            this.firstLink = firstLink;
        }     
        this.setLastLink = function(lastLink){
            this.lastLink = lastLink;
        }     
        this.setPrevLink = function(prevLink){
            this.prevLink = prevLink;
        }     
        this.setNextLink = function(nextLink){
            this.nextLink = nextLink;
        }
        
        this.getLinks = function(curPage){
            if (typeof curPage == 'undefined' || curPage <= 0) {
		curPage = 1;
	    }
      
            var links = '';
            
            //get first page's and previous page's links
            if (curPage > 1) {
                links += '<a href="javascript:void(0);" class="pager_link" title="' + 1 + '"> ' + this.firstLink + ' </a> ';
                
                links += ' <a href="javascript:void(0);" class="pager_link" title="' + (curPage - 1) + '"> ' + this.prevLink + ' </a> ';
            }
            
            //get previous pages' links
            for (var i = curPage - this.linkNumber; i < curPage; i++){
                if (i <= 0) {
					continue;
				}
                links += ' <a href="javascript:void(0);" class="pager_link" title="' + i + '"> ' + i + '</a> ';
            }
            
            //get current page's link
            links += '<span class="current_page">' + curPage + '</span> ';
            
            //get next pages' links
            for (var j = curPage + 1; j < curPage + (this.linkNumber + 1) && j <= this.bound; j++) {
                links += ' <a href="javascript:void(0);" class="pager_link" title="' + j + '"> ' + j + '</a> ';
            }
            
            //get next page's and last page's links
            if (curPage < this.bound) {
                links += ' <a href="javascript:void(0);" class="pager_link" title="' + (curPage + 1) + '"> ' + this.nextLink + ' </a> ';
                
                links += ' <a href="javascript:void(0);" class="pager_link" title="' + this.bound + '"> ' + this.lastLink + ' </a> ';
            }
            
            return links;
        }
    }
    //end of Pager Function
    
    options = jQuery.extend({
        "width": 600,
        "height": 320,
	"autoSlide": true,
        "slide_speed": 2000,
        "opening_image_index": 1,
        "image_tooltip": true,
        "pager_link_number": 4,
        "pager_next_link": '&gt;',
        "pager_prev_link": '&lt;',
        "pager_first_link": 'First',
        "pager_last_link": 'Last'
    }, options);
    
    return this.each(function(){
    
        var _smartSliderDiv = $(this);
        var _imgTitleArray = new Array();
        var _pager = new Pager();
        var curPage = 1;
        var images = '';
        
        _smartSliderDiv.css({
            "position": "relative",
            "width": options.width,
            "height": options.height,
            "float": "left",
            "overflow": "hidden",
	    "font-family": "sans-serif, Verdana"
        });
        
        $('ul#images', _smartSliderDiv).css({
            "display": "none"
        });
        
        //_smartSliderDiv.append('<div id="slider_loading" style="width:' + options.width + 'px; height:' + options.height + 'px; background-color:black; color:white; font-family:sans-serif, Verdana;">Loading Images</div>');
        
        $('li img', _smartSliderDiv).each(function(i, val){    
            _imgTitleArray.push($(this).attr('title'));
            var img = '<img src="' + $(this).attr('src') + '" style="left:' + (i * options.width) + 'px;" />';
            images += img;
        });
        
        _smartSliderDiv.append(images);
        
        $('img', _smartSliderDiv).css({
            "position": "absolute",
            "width": options.width,
            "height": options.height,
	    "text-align": "center"
        }); //positioning new images
        
	var imgLinks = '';
	 $('li img', _smartSliderDiv).each(function(i,v){
	    imgLinks += '<span class="pager_link" page="' + i + '">' + $(this).attr("title") + '</span>';
	 });
	
        _smartSliderDiv.append('<div class="ui-corner-all" id="pager">' + imgLinks + '</div>');
        
        $('div#pager').css({
            "position": "absolute",
            "width": options.width + "px",
			"margin": "0 auto",
            "background-color": "#2E6E9E",
			"color": "#fff",
            "bottom": "0px",
            "left": "0px",
            "text-align": "center",
            "padding": "3px",
			"opacity": 0.6,
			"border": "1px solid #1d446d",
			"font-weight": "normal"
        });
	
	$('.pager_link').css({
	    "font-size": "12px",
	    "line-height": "14px",
	    "font-weight": "bold",
        "padding": "0 10px",
	    "cursor": "pointer"	   
        }).not(":last-child").css({
	     "border-right": "0px dashed #fff"
	});
        
        /*
        if (options.image_tooltip == true) {
			
            _smartSliderDiv.append('<div id="image_tooltip"></div>');
            
            $('div#image_tooltip').css({
                "position": "absolute",
                "width": options.width,
                "height": "auto",
                "background-color": "black",
                "opacity": "0.4",
                "top": "0px",
                "left": "0px",
                "color": "white",
                "font-size": "13px",
                "font-weight": "bold",
                "font-family": "sans-serif, Verdana",
                "padding": "10px",
                "display": "none",
                "visibility": "hidden"
            });
            
			if(options.autoSlide !== true){
				_smartSliderDiv.hover(function(){
	                $('div#image_tooltip').slideDown(options.slide_speed);
	            }, function(){
	                $('div#image_tooltip').slideUp(options.slide_speed);
	            });	
			}
        }
        */
	
        /**
         * page slider function
         * @param int
         */
        function slidePage(page){
            if (page > curPage) {
                $('img', _smartSliderDiv).animate({
                    'left': '-=' + Math.abs(page - curPage) * options.width
                }, options.slide_speed, "easeInOutBack");
            } else {
                $('img', _smartSliderDiv).animate({
                    'left': '+=' + Math.abs(page - curPage) * options.width
                }, options.slide_speed);
            }        
            curPage = page;			
        }
	
        $('.pager_link').live('click', function(){ 
            var page = Number($(this).attr('page'));
            slidePage(page + 1);
        });	

    });
};