﻿/* ************************************************************************************ */
/* Change Log                                                                           */
/*                                                                                      */
/* 11-01-2011  - Richard Chapman - Deltasoft - Add                                      */
/* 15-05-2011  - Richard Chapman - Deltasoft - Add Fade option                          */
/* ************************************************************************************ */
var run = 0;

function init_sliders(speed,admin)
{
// Loop through each slider on the page and set the width accordingly
jQuery('[id^="slider-body-"]').each(function(index){sliderinit(this,index,speed,admin);});

// Assign a timer, so it will run periodically
if (admin == 0)
    run = setInterval('scrollslider(0)', speed);	
}

function sliderinit(s,i,speed,admin)
{
var sid = jQuery(s).attr('slid');
var swidth = jQuery(s).width();

jQuery('#slider-gallery-'+sid+', #slider-gallery-list-'+sid+' li').width(swidth);
jQuery('#slider-gallery-list-'+sid).width(swidth * jQuery('#slider-gallery-list-'+sid+' li').length);
jQuery('#slider-gallery-'+sid+', #slider-gallery-list-'+sid+' li, #slider-gallerytitle-'+sid+', #slider-gallerytitle-list-'+sid+' li').height(jQuery(s).height());
jQuery('#slider-gallery-list-'+sid+' li:first, #slider-gallerytitle-list-'+sid+' li:first').addClass('selected');

// Mouse over, pause it, on mouse out, resume the slider show
if (admin == 0) {
    if (jQuery(s).hasClass('slidermanual')) {
        jQuery(s).hover(
            function() {
	        jQuery('#slider-navprev-'+jQuery(this).attr('slid')).show();
	        jQuery('#slider-navnext-'+jQuery(this).attr('slid')).show();
            }, 
            function() {	
            jQuery('#slider-navprev-'+jQuery(this).attr('slid')).hide();
            jQuery('#slider-navnext-'+jQuery(this).attr('slid')).hide();
            }
        );        
        jQuery('#slider-navprev-'+jQuery(s).attr('slid')).click(function(){slidertick(this.parentNode,-1,1);});
        jQuery('#slider-navnext-'+jQuery(s).attr('slid')).click(function(){slidertick(this.parentNode,-1,0);});
        
        if (jQuery('#slider-gallery-list-'+sid+' li').hasClass('slidertop')) {
            jQuery('#slider-navprev-'+jQuery(s).attr('slid')).removeClass('sliderleftarrow').addClass('slideruparrow');
            jQuery('#slider-navnext-'+jQuery(s).attr('slid')).removeClass('sliderrightarrow').addClass('sliderdownarrow');
            }
        }           
    else
    jQuery(s).hover(
	    function() {
		    jQuery(this).addClass('sliderpause');
	        }, 
	    function() {	
		    jQuery(this).removeClass('sliderpause');
	        }
        );
    }
}
	
function scrollslider(prev) {
// Loop through each slider on the page and set the width accordingly
jQuery('[id^="slider-body-"]').each(function(index){slidertick(this,index,prev);});
}

function slidertick(s,i,prev)
{
if (i >= 0) {
    if (jQuery(s).hasClass('sliderpause') || jQuery(s).hasClass('slidermanual'))
        return;
        }

var sid = jQuery(s).attr('slid');
var galleryid = '#slider-gallery-list-'+sid;
var titleid = '#slider-gallerytitle-list-'+sid;

// Get the current selected item (with selected class), if none was found, get the first item
var current_gallery = jQuery(galleryid+' li.selected').length ? jQuery(galleryid+' li.selected') : jQuery(galleryid+' li:first');
var current_title = jQuery(titleid+' li.selected').length ? jQuery(titleid+' li.selected') : jQuery(titleid+' li:first');

// if prev is set to 1 (previous item)
if (prev) {		
	// Get previous sibling
	var next_gallery = (current_gallery.prev().length) ? current_gallery.prev() : jQuery(galleryid+' li:last');
	var next_title = (current_title.prev().length) ? current_title.prev() : jQuery(titleid+' li:last');

// if prev is set to 0 (next item)
} else {		
	// Get next sibling
	var next_gallery = (current_gallery.next().length) ? current_gallery.next() : jQuery(galleryid+' li:first');
	var next_title = (current_title.next().length) ? current_title.next() : jQuery(titleid+' li:first');
}

// clear the selected class
current_gallery.removeClass('selected');
current_title.removeClass('selected');

// reassign the selected class to current items
next_gallery.addClass('selected');
next_title.addClass('selected');

// Scroll the items
if (next_gallery.hasClass('sliderleft'))
    jQuery('#slider-gallery-'+sid).scrollTo(next_gallery, 800,{axis:'x'});		
else if (next_gallery.hasClass('slidertop'))
    jQuery('#slider-gallery-'+sid).scrollTo(next_gallery, 800,{axis:'y'});		
else
    jQuery('#slider-gallery-'+sid).fadeTo(800,0,function() {
           jQuery('#slider-gallery-'+sid).scrollTo(next_gallery, 0,{axis:'x',onAfter:function(){jQuery('#slider-gallery-'+sid).fadeTo(800,1);}});});
               
jQuery('#slider-gallerytitle-'+sid).scrollTo(next_title, 800);
}
