/*!
 * jQuery lookingglass plugin: Simple inline gallery scroll
 * version .1 (15-JUN-2010)
 * Requires jQuery v1.3.2 or later
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Author: Kevin McClellan
 */

/**
 *  lookingglass() takes a hash:  $('#myDiv').lookingglass({
     *                                  previous: 'previousSelector',
     *                                  next: 'nextSelector',
     *                                  positionPrevious: function,
     *                                  positionNext: function,
     *                                  nav: 'navSelector',
     *                                  circular: 1|0,
     *                                  speed: 200,
     *                                  auto: 0})
 *  nav: generate pagination navigation in selected div
 *  circular: default 1
 *  speed: default 200
 *  auto: default 0 /slideshow speed
 *
 */
;(function($) {

jQuery.fn.reduce = function(block, init)
{
    value = 0;
    if(init){value = init;}
	$(this).each(function()
	{
		value = block(this,value);
	});
	return value;
}

function previous(){
    if(!$.fn.lookingglass.options.changePane())return;
    $.fn.lookingglass.options.auto = 0;
    pane = $('#'+$.fn.lookingglass.options.pane);
    index = $.fn.lookingglass.options.iterator;
    previousIndex = index - 1;
    if(previousIndex == 0){
        previousIndex = pane.children().size();
    }else if(previousIndex == 1 && !$.fn.lookingglass.options.circular){
        $($.fn.lookingglass.options.previous).hide();
    }

    $.fn.lookingglass.options.iterator = previousIndex;
    $($.fn.lookingglass.options.previous).unbind('click');
    pane.children(':nth-child('+index+')').hide("slide",{direction:'right'},$.fn.lookingglass.options.speed);
    pane.children(':nth-child('+previousIndex+')').show("slide",{direction:'left'},$.fn.lookingglass.options.speed,function(){$($.fn.lookingglass.options.previous).click(previous);if($.fn.lookingglass.options.resize){pane.lookingglass.resize();}});
    $($.fn.lookingglass.options.next).show();
    
}

function next(){
    if(!$.fn.lookingglass.options.changePane())return;
    $.fn.lookingglass.options.auto = 0;
    pane = $('#'+$.fn.lookingglass.options.pane);
    index = $.fn.lookingglass.options.iterator;
    nextIndex = index + 1;
    if(nextIndex == pane.children().size()+1){
        nextIndex = 1;
    }else if(nextIndex == pane.children().size() && !$.fn.lookingglass.options.circular){
        $($.fn.lookingglass.options.next).hide();
    }

    $.fn.lookingglass.options.iterator = nextIndex;
    $($.fn.lookingglass.options.next).unbind('click');
    pane.children(':nth-child('+index+')').hide("slide",{direction:'left'},$.fn.lookingglass.options.speed);
    pane.children(':nth-child('+nextIndex+')').show("slide",{direction:'right'},$.fn.lookingglass.options.speed,function(){$($.fn.lookingglass.options.next).click(next);if($.fn.lookingglass.options.resize){pane.lookingglass.resize();}});
    $($.fn.lookingglass.options.previous).show();
}

function to(){
    if(!$.fn.lookingglass.options.changePane())return;
    $.fn.lookingglass.options.auto = 0;
    pane = $('#'+$.fn.lookingglass.options.pane);
    index = $.fn.lookingglass.options.iterator;
    newIndex = $(this).attr('iterator');
    directionShow = 'right';
    directionHide = 'left';
    if(index > newIndex){
        directionShow = 'left';
        directionHide = 'right';
    }
    $.fn.lookingglass.options.iterator = newIndex;
    $($.fn.lookingglass.options.nav).children().unbind('click');
    pane.children(':nth-child('+index+')').hide("slide",{direction:directionHide},$.fn.lookingglass.options.speed);
    pane.children(':nth-child('+newIndex+')').show("slide",{direction:directionShow},$.fn.lookingglass.options.speed,function(){$($.fn.lookingglass.options.navArea).children().filter(function(){return $(this).attr('iterator') != newIndex;}).click(to);if($.fn.lookingglass.options.resize){pane.lookingglass.resize();}});
    $($.fn.lookingglass.options.navArea).children().attr('src',$.fn.lookingglass.options.icon);
    $($.fn.lookingglass.options.navArea).children().filter(function(){return $(this).attr('iterator') == newIndex;}).attr('src',$.fn.lookingglass.options.iconActive);


}

function auto(){
    if($.fn.lookingglass.options.auto){
        if(!$.fn.lookingglass.options.changePane())return;
        pane = $('#'+$.fn.lookingglass.options.pane);
        index = $.fn.lookingglass.options.iterator;
        nextIndex = index + 1;
        if(nextIndex == pane.children().size()+1){
            nextIndex = 1;
        }else if(nextIndex == pane.children().size() && !$.fn.lookingglass.options.circular){
            $($.fn.lookingglass.options.next).hide();
        }

        $.fn.lookingglass.options.iterator = nextIndex;
        $($.fn.lookingglass.options.next).unbind('click');
        pane.children(':nth-child('+index+')').hide("slide",{direction:'left'},$.fn.lookingglass.options.speed);
        pane.children(':nth-child('+nextIndex+')').show("slide",{direction:'right'},$.fn.lookingglass.options.speed,function(){$($.fn.lookingglass.options.next).click(next);if($.fn.lookingglass.options.resize){pane.lookingglass.resize();}});
        $($.fn.lookingglass.options.previous).show();
        setTimeout(auto,$.fn.lookingglass.options.auto);
    }
}

$.fn.lookingglass = function(options) {
    jQuery.extend(true,$.fn.lookingglass.options,options);
    $.fn.lookingglass.options.pane = $(this).attr('id');

    if($.fn.lookingglass.options.resize){
        height = $(this).children(':nth-child('+$.fn.lookingglass.options.iterator+')').outerHeight();
        width = $(this).children(':nth-child('+$.fn.lookingglass.options.iterator+')').outerWidth();
        
        $(this).children().slice(1).hide();
        $(this).css({'height':height,'width':width,'position':'relative'});
        $(this).children().each(function(){$(this).css({'position':'absolute','top':0,'left':0})});
    }else{
        maxHeight = $(this).children().reduce(function(key,value){if($(key).outerHeight() > value){ return $(key).outerHeight(); } return value;});
        maxWidth = $(this).children().reduce(function(key,value){if($(key).outerWidth() > value){ return $(key).outerWidth(); } return value;});
        $.fn.lookingglass.options.maxHeight = maxHeight;
        $(this).children().slice(1).hide();
        $(this).children().each(function(){$(this).css({'width':maxWidth})});
        $(this).css({'height':maxHeight,'width':maxWidth+20,'position':'relative'});
        $(this).children().each(function(){$(this).css({'position':'absolute','top':$.fn.lookingglass.options.top(this)+'px','left':0})});
    }

    if(options.navArea && !$(options.navArea).children().size()){
        for(i=1;i<=$(this).children().size();i++){
            icon = options.icon;
            if(i == $.fn.lookingglass.options.iterator)icon = options.iconActive;
            $(options.navArea).append('<img src="'+icon+'" iterator="'+i+'">');
        }
        $(options.navArea).children().click(to);
    }

    if(options.previous){
        $(options.previous)
	    .click(previous);
        if(options.positionPrevious){
            options.positionPrevious(options.previous);
        }
        if($.fn.lookingglass.options.iterator == 1 && !$.fn.lookingglass.options.circular){
            $(options.previous).hide();
        }
    }
    if(options.next){
        $(options.next)
	    .click(next);
        if(options.positionNext){
            options.positionNext(options.next);
        }
    }
    
    //auto
    
    if($.fn.lookingglass.auto != 0){
        setTimeout(auto,$.fn.lookingglass.options.auto);
    }
    
};
$.fn.lookingglass.resize = function(){
    pane = $('#'+$.fn.lookingglass.options.pane);
    newHeight = pane.children(':nth-child('+$.fn.lookingglass.options.iterator+')').outerHeight();
    newWidth = pane.children(':nth-child('+$.fn.lookingglass.options.iterator+')').outerWidth();
    pane.css({'height':newHeight,'width':newWidth});
}
$.fn.lookingglass.options = {
    circular: 1,
    iterator: 1,
    speed: 200,
    resize: 0,
    changePane: function(){return true},
    top: function(obj){
        if($('#'+$.fn.lookingglass.options.pane).attr('align') == 'top'){
            return 0;
        }else{
            return (($.fn.lookingglass.options.maxHeight - $(obj).height()) / 2);
        }
    },
    auto: 0
};

})(jQuery);

