/*
 * 
 * Product: Glow Gallery    
 * Product Version: 1.2
 * Author: Terence Jefferies
 * Company: Glow Creative (www.makemeglow.com)
 * 
 * 09/06/2011 15:29 +0000
 * 
 */
/***************CONFIG VARS**************/
var config_leftimg              = "images/left.png";
var config_rightimg             = "images/right.png";
var config_cycle                = true;
var config_cycle_timeout        = 3000;
var config_debug                = false;
var config_transit_time         = 1500;
/****************************************/
var cur_left = 0;
var cur_pos = 0;
var fading = false;
jQuery.fn.gallery = function(width,height) {

    var count = 0;
    var imgs = [];
    $('li',this).each(function() { imgs[count] = $('img',this).attr('src'); count ++; });
    $(this).css('width',width + 'px');
    $(this).css('max-height',height + 'px');
    $(this).css('position','relative');
    $(this).html('<div class="gal-display" style="position:relative;"></div>');
    $(this).append('<div class="gal-menu"></div>');
    $('.gal-display').css('width',width);
    $('.gal-display').css('height',(height - 70));
    $('.gal-menu').css('width','100%');
    $('.gal-menu').css('max-height','57px');
    $('.gal-menu').css('position','relative');
    $('.gal-menu').css('overflow','hidden');
    $('.gal-menu').html('<div class="gal-menu-inner" style="width:10000px; max-height:60px; overflow:hidden; margin-top:5px; position:relative; left:0px;"></div>');
    $('.gal-menu-inner').html('<ul style="listy-style:none; padding:0; margin:0; width:10000px;"></ul>');
    for(var i = 0; i < imgs.length; i ++)
    {
        var padd = (i > 0) ? 'padding:3px;' : null;
        $('.gal-menu-inner ul').append('<li style="display:inline; width:60px; height:40px; ' + padd + '"><img src="' + imgs[i] + '" style="display:none; width:60px; height:40px; cursor:pointer; cursor:hand;" /></li>');

    }
    $('.gal-menu-inner img').load(function(a,b,c) { $(this).fadeIn('slow'); });
    $('.gal-display').html('<img src="' + imgs[0] + '" style="width:' + width + 'px; height:' + (height-70) + 'px; display:none; position:absolute; top:0; left:0;" />');
    $('.gal-display img').load(function() { $(this).fadeIn('slow'); });
    $(this).append('<img src="' + config_leftimg + '" class="gal-left" />');
    $('.gal-left').css('position','absolute');
    $('.gal-left').css('bottom','0px');
    $('.gal-left').css('left','-40px');
    $('.gal-left').css('cursor','pointer'); $('.gal-left').css('cursor','hand');
    $(this).append('<img src="' + config_rightimg + '" class="gal-right" />');
    $('.gal-right').css('position','absolute');
    $('.gal-right').css('bottom','0px');
    $('.gal-right').css('right','-40px');
    $('.gal-right').css('cursor','pointer'); $('.gal-right').css('cursor','hand');
    $('.gal-menu-inner ul li img').click(function() {
        
        if(!fading)
        {
           fading = true;
           var c = 0; var active = 0; var obj = this; 
           $('.gal-menu-inner img').each(function() {

              if(this == obj) { active = c; return false; }
              c ++;

           }); 
           $('.gal-display').append('<img src="' + imgs[active] + '" style="width:' + width + 'px; height:' + (height - 70) + 'px; position:absolute; top:0; left:0; display:none;" />');
           $('.gal-display img:first').fadeOut(config_transit_time,function() { $(this).remove(); fading = false; });
           $('.gal-display img:last').fadeIn(config_transit_time);
           cur_pos = active;
        }
        
    });
    $('.gal-right').click(function() {
       
       var movement_width = (imgs.length * 65);
       if((cur_left - 65) > -(movement_width - width)) { cur_left -= 65; }
       else { cur_left = -(movement_width - width); }
       $('.gal-menu-inner').animate({ left:cur_left },'fast');
        
    });
    $('.gal-left').click(function() {
        
        if((cur_left + 65) < 0) { cur_left += 65; }
        else{ cur_left = 0; }
        $('.gal-menu-inner').animate({ left:cur_left },'fast');
    });
    if(config_cycle) { setTimeout("cycle()",(config_transit_time + config_cycle_timeout)); }
    if(config_debug) { $('body').append('<div style="width:400px; min-height:200px; padding:10px; border:1px solid black; text-align:center;" class="glow-debug-console"></div>'); debug(); };
    
};

function cycle() { $('.gal-menu-inner ul li img:eq(' + (cur_pos + 1) + ')').click(); setTimeout("cycle()",(config_transit_time + config_cycle_timeout)); }
function debug()
{
   
    setTimeout("debug()",250);
    $('.glow-debug-console').html('GLOW DEBUG CONSOLE:<br><table><tr><td>CUR_POS:</td><td>' + cur_pos + '</td></tr><tr><td>CUR_LEFT:</td><td>' + cur_left + '</td></tr><tr><td>FADING:</td><td>' + fading + '</td></tr></table>');
    
}
