/*
imageViwer jQuery plugin. Written for www.n1ki.com (c)2011 by j.
*/
(function ($) {
    $.fn.n1ImageView = function (options) {
        // support mutltiple elements
        if (this.length > 1) {
            this.each(function () {$(this).n1ImageView(options)});
            return this;
        }

        // private variables
        var interval;
        var currentPage = 0;
        var thisID;
        var isPaused = false;
        var animationRunning = false;
        var thisWidth = 0;
        var nrofitems = 0;
        var settings = {
            pageSpeed: 1000,
            interval: 6000,
            initPaused: false,
            displayBar: true,
            items: []
        }

        // Private methods
        function switchImage(iswho) {
            var page = (parseInt(currentPage, 10) + 1);
            if (page == settings.items.length) {
                page = 0;
            }
            setImage(page, iswho);
        }
        function setImage(page, iswho) {
            if (page != currentPage && !animationRunning) {
                animationRunning = true;
                
                var previousPage = currentPage;
                currentPage = page;
                
                var item =settings.items[(currentPage)]
                var html = '<a href="' + item.href + '" title="' + item.title + '"><img class="n1_iv_item_img" src="' + item.src + '" alt="' + item.title + '"/></a>';
                
                if(previousPage < currentPage || previousPage == (settings.items.length - 1) && currentPage == 0 && iswho == true){
                    // Move forward
                    $('#' + thisID + '_pg_3').empty().append(html);
                    $('#' + thisID + ' .n1_iv_container').animate({'margin-left' : '-' + (thisWidth * 2) + 'px'}, settings.pageSpeed, function(){
                        $('#' + thisID + '_pg_2').empty().append(html);
                        $('#' + thisID + ' .n1_iv_container').css("marginLeft","-"+thisWidth+"px");
                        animationRunning = false;
                    });
                }else{
                    // Move backwards
                    $('#' + thisID + '_pg_1').empty().append(html);
                    $('#' + thisID + ' .n1_iv_container').animate({'margin-left' : '0'}, settings.pageSpeed, function(){
                        $('#' + thisID + '_pg_2').empty().append(html);
                        $('#' + thisID + ' .n1_iv_container').css("marginLeft","-"+thisWidth+"px");
                        animationRunning = false;
                    });
                }
                $('#' + thisID + '_' + previousPage).css({backgroundPosition: 'left center'});
                $('#' + thisID + '_' + currentPage).css({backgroundPosition: 'right center'});
            }
        }


        // Public methods
        this.init = function () {
            this.empty();
            // If there is options, append them with our settings
            if (options) {
                $.extend(settings, options);
            }
            isPaused = settings.initPaused;
            thisWidth = this.width();
            nrofitems = 3;
            
            // Set the id of this button
            thisID = $(this).attr('id');
            var that = this;
            // Print the images
            var printer = '<div class="n1_iv_container" style="margin-left:-'+thisWidth+'px; width:'+(thisWidth * (nrofitems))+'px;">';
            
            var captionItemsWidth = (43 * 4) + 10;
            var captionPadding = (thisWidth / 2) - (captionItemsWidth/2);
            var captionWidth = thisWidth - captionPadding;
            
            
            var caption = '<div class="n1_iv_caption" style="width:'+captionWidth+'px; padding-left:'+captionPadding+'px;">';
            for (var n = 0; n < settings.items.length; n++) {
                var item = settings.items[n];
                var setClassCaption = '';
                if (n == 0) {
                    setClassCaption = ' n1_iv_item_caption_selected';
                    printer += '<div id="'+thisID+'_pg_1" class="n1_iv_item" style="width:'+thisWidth+'px" >&nbsp;</div>';
                    printer += '<div id="'+thisID+'_pg_2" class="n1_iv_item" style="width:'+thisWidth+'px"><a href="' + item.href + '" title="' + item.title + '"><img class="n1_iv_item_img" src="' + item.src + '" alt="' + item.title + '"/></a></div>';
                    printer += '<div id="'+thisID+'_pg_3" class="n1_iv_item" style="width:'+thisWidth+'px">&nbsp;</div>';
                }
                caption += '<div class="n1_iv_item_caption' + setClassCaption + '" id="' + thisID + '_' + n + '"></div>';
            }
            printer+= '</div>';
            var pausedClass = '';
            if (isPaused) {
                pausedClass = ' n1_iv_item_caption_r_paused';
                isPaused = true;
            }
            caption += '<div class="n1_iv_item_caption_r' + pausedClass + '"></div><div style="clear:both"></div></div>';
            if(settings.items.length > 1){
                this.append(printer + caption);
                    //.n1_iv_caption
                $('#' + thisID + ' .n1_iv_item_caption').hover(function () {
                    if ($(this).attr("id") != thisID + '_' + currentPage) {
                        $(this).css({backgroundPosition: '-33px center'});
                    }
                }, function () {
                    if ($(this).attr("id") != thisID + '_' + currentPage) {
                        $(this).css({backgroundPosition: 'left center'});
                    }
                }).mousedown(function (e) {
                    e.preventDefault();
                    if ($(this).attr("id") != thisID + '_' + currentPage) {
                        $(this).css({backgroundPosition: '-66px center'});
                    }
                }).mouseup(function (e) {
                    e.preventDefault();
                    if ($(this).attr("id") != thisID + '_' + currentPage) {
                        $(this).css({backgroundPosition: 'right center'});
                    }
                }).click(function () {
                    that.paus();
                    var page = $(this).attr('id').replace(thisID + '_', '');
                    setImage(page, false);
                });

                $('#' + thisID + ' .n1_iv_item_caption_r').hover(function () {
                    if (!isPaused) {
                        $(this).css({backgroundPosition: '-33px top'});
                    } else {
                        $(this).css({backgroundPosition: '-33px -32px'});
                    }
                }, function () {
                    if (!isPaused) {
                        $(this).css({backgroundPosition: 'left top'});
                    } else {
                        $(this).css({backgroundPosition: 'left -32px'});
                    }
                }).mousedown(function (e) {
                    e.preventDefault();
                    if (!isPaused) {
                        $(this).css({backgroundPosition: '-66px top'});
                    } else {
                        $(this).css({backgroundPosition: '-66px -32px'});
                    }
                }).mouseup(function (e) {
                    e.preventDefault();
                    if (!isPaused) {
                        that.paus();
                        $(this).css({backgroundPosition: '-33px -32px'});
                    } else {
                        that.play();
                        $(this).css({backgroundPosition: '-33px top'});
                    }
                });
                if (!isPaused) {
                    this.play();
                }
            }else{
                this.append(printer);
            }
            return this;
        };

        // public methods 
        this.play = function () {
            $('#' + thisID + ' .n1_iv_item_caption_r').css({backgroundPosition: 'left top'});
            if (isPaused) {
                isPaused = false;
                switchImage(true);
            }
            interval = setInterval(function () {switchImage(true);}, settings.interval);
            return this;
        };
        this.paus = function () {
            $('#' + thisID + ' .n1_iv_item_caption_r').css({backgroundPosition: 'left -32px'});
            clearInterval(interval);
            isPaused = true;
            return this;
        }
        // return 
        return this.init();
    }
})(jQuery);
