﻿var selected_service_id = 1;
var processing = false;
var jCar;
var autoplay_interval = null;

var loaded_thumbs = 0;
var curr_images_hash = new Object;

//TODO: implement slideshow functionality

slideshow_start = function(e) {
    /*
    * for each time a service link is clicked, remove the current gallery components from the DOM,
    * then do an ajax call to get the new UL for the current link clicked.  also, disable all other links
    * while the loading process is occuring.
    */
    $('ul.service_links a').click(function() {
        if (processing) return; // don't do anything until the current gallery load is finished
        processing = true;
        stop_autoplay();
        loaded_thumbs = 0;
        var thisobj = this;

        // remove old gallery and show load gif
        $('div.scrollable').remove();
        $('div.img_container').removeClass('galleria_container').empty();
        $('<img />').attr('src', ajax_loader_path).attr('alt', 'Loading...').css({ marginTop: '200px', border: '0' }).appendTo('div.img_container'); // ajax_loader_path is set on the usercontrol.

        // ajax call
        $.get($('form').attr('action'), { serviceid: $(this).attr('data-id') }, function(data, status) {
            if (status.indexOf('success') > -1) {
                $('div.img_container').empty();
                $('div.img_controller').after($('<div></div>').addClass('scrollable').append($(data)));
                init_gallery();
                // replace <br> tags with space's
                $('h3.gallery_head').text($(thisobj).html().replace("<br>", " ").replace("<BR>", " "));
                processing = false;
            }
            else {
                // some error has occured. o_O
                if (typeof log != 'undefined') {
                    log.info("status: " + status);
                    //log.info(data); // uncomment this line to see what the page has returned
                }
            }
        },
        'html');

    });

    // now that the links know what to do, click one to init the gallery for the first time.
    $('a[data-id=' + e + ']').trigger('click');

    $('div.img_controller span.prev').click(function() {
        $.galleria.prev();
    });

    $('div.img_controller span.next').click(function() {
        $.galleria.next();
    });

    $('div.img_controller span.auto').toggle(function() {
        start_autoplay();
    },
    function() {
        stop_autoplay();
    });

    $('img.timer_gif').hide();
}

function init_gallery()
{    
    // reset the hash for this instance of galleria
    curr_images_hash = new Object();
    $('#gallery_carousel li img').each(function(i)
    {
        // track the index of each image by title so I know when to advance the carousel
        curr_images_hash[this.title] = i;
    });
    
    // create the gallery from the UL
    $('#gallery_carousel').galleria({
        insert: 'div.img_container',
        onThumb: function(thumb) {
            var _li = thumb.parents('li');
            var _fadeTo = _li.is('.active') ? '1' : '0.6';
            thumb.css({ display: 'none', opacity: _fadeTo }).fadeIn(500);

            init_first(thumb);
            thumb.hover(
				function() { thumb.fadeTo('fast', 1); },
				function() { _li.not('.active').children('img').fadeTo('fast', 0.6); }
			);
            loaded_thumbs++;
            thumb.rightClick(function() {
                alert('Copyright © 2009 COT-Puritech.  Unauthorized use is prohobited');
            });
        },
        onImage: function(image, caption, thumb) {
            image.css('display', 'none').fadeIn(200);
            caption.attr("data-index", caption.text()); 
            caption.text(
                    caption.text().substr(
                         caption.text().indexOf(":")+1
                    )
                ); 
            var _li = thumb.parents('li');
            _li.siblings().children('img.selected').fadeTo(100, 0.6);
            thumb.fadeTo('fast', 1).addClass('selected');
            $('.img_container').trigger("img_change");
            image.rightClick(function() {
                alert('Copyright © 2009 COT-Puritech.  Unauthorized use is prohobited');
            });
        },
        clickNext: false,
        history: false
    });
        
    // now that the gallery has been created, do the Carousel
    jCar = $('#gallery_carousel').jcarousel({
        scroll: 5,
        visible: 5,
        initCallback: carousel_init
    });
                
}

function carousel_init(jcar)
{   
    // if thumb is clicked, make sure the scroller advances when needed
    jQuery('.img_container').bind('img_change', function()
    {
        jcar.scroll(parseInt(curr_images_hash[$('span.caption').attr("data-index")]) - 1);
    }); 
             
}

/*
 * This fires after a service link is clicked for each thumbnail when it finishes loading.
 */
function init_first(thumb)
{
    if ($('#gallery_carousel li:first img')[0] == thumb[0])
        thumb.trigger('click');
}

function start_autoplay()
{
    if ($('#gallery_carousel li img').length < loaded_thumbs) 
        return false;
        
    $('div.img_controller span.auto').addClass('auto_on');

    var imgs = $('#gallery_carousel li img');
    var current_index = 0;
    
    $(imgs[0]).trigger('click');
    
    var img = $('img.timer_gif');
    img.replaceWith($("<img />").attr('src', img.attr('src')).attr('alt', img.attr('alt')).addClass('timer_gif'));  
    
    autoplay_interval = setInterval(function()
    {
        var img = $('img.timer_gif');
        img.replaceWith($("<img />").attr('src', img.attr('src')).attr('alt', img.attr('alt')).addClass('timer_gif').show());  
        $(imgs[++current_index]).trigger('click');
        if (current_index == imgs.length - 1)
            current_index = -1;
    }, 4000);
}

function stop_autoplay()
{
    clearInterval(autoplay_interval);
    $('div.img_controller span.auto').removeClass('auto_on');
    $('img.timer_gif').hide();
}