//  TG Slider (The Tom Gay Slider) Configurations
var tg_slider_manual = false;
var tg_first = true;
var default_attrs = ['3000','tg_slider_arrow.gif'];
var my_attrs = new Array();
var totalRotate = 2;
var rotated = 0;

jQuery(document).ready(function() {

	//  Are there any sliders?
	if( !( jQuery('div.tg_slider') && jQuery('div.tg_slider').length > 0 ) ) {
		return;
	}

	jQuery('div.tg_slider').children('div.tg_slider_main').children( 'p' ).children( 'img' ).each(function(){
		jQuery(this).attr( 'relsrc', jQuery(this).attr( 'src' ) );
		jQuery(this).attr( 'rel', '' );
	});	
	

	parent_count = 0;

	jQuery('div.tg_slider').each(function(){

		my_attrs[parent_count] = ['3000','tg_slider_arrow.gif'];

		if( jQuery(this).attr( 'rel' ) != '' && jQuery(this).attr( 'rel' ).split( ',' ).length == default_attrs.length ) {
			my_attrs[parent_count] = jQuery(this).attr( 'rel' ).split( ',' );
		}

		//  Manual click default.
		my_attrs[parent_count].push(0);

		//  Slider counter.
		my_attrs[parent_count].push(1);

		//  Set the click events.
		tgSetSlideClicks( this, parent_count );

		//  Start the slide show.
		//tgSlider( this, parent_count );
		jQuery(this).attr( 'sliderNum', parent_count )
		jQuery(this).attr( 'sliderOn', 1 );
		jQuery(this).attr( 'curSlide', 0);
		tgSlider(this);

		parent_count++;
	});
});

function tgSetSlideClicks( my_parent, my_attrs_count ) {
	
	jQuery(my_parent).children('ul.tg_slider_nav').children( 'li.tg_slider_nav_img' ).click(function() {
		var src = new String( jQuery(this).children( 'img' ).attr( 'src' ) );
		var id = src.replace( /.*?_(\d+)_.*/, "$1" );
		jQuery(my_parent).attr( 'sliderOn', 0 );
		jQuery(my_parent).attr( 'curSlide', parseInt(id)-1 );
		tgShowSlide( my_parent, my_attrs_count );
	});
}

function tgSlider(theSlider) {
	if( jQuery(theSlider).attr( 'sliderOn') == 1) {
		tgShowSlide( theSlider, jQuery(this).attr( 'sliderNum') );
		
		//  Set the slide counter.
		if( jQuery(theSlider).attr( 'curSlide' ) == jQuery(theSlider).children('ul.tg_slider_nav').children( 'li.tg_slider_nav_img' ).length - 1 ) {
			jQuery(theSlider).attr( 'curSlide', 0 );
			rotated++;
		} else {
			jQuery(theSlider).attr( 'curSlide', parseInt(jQuery(theSlider).attr( 'curSlide'))+1 );
		}
		
		//  Setup the return function.
		var call_me_again = function() {
			tgSlider( theSlider);
		}
		
		t=setTimeout( call_me_again, jQuery(theSlider).attr( 'rel' ) );
	} else {
		clearTimeout(t);
	}
}

function tgShowSlide( my_parent, my_attrs_count ) {
	if(rotated == totalRotate) {
		jQuery(my_parent).attr( 'sliderOn', 0 );
	}
	//  Transition the nav.
	jQuery(my_parent).children('ul.tg_slider_nav').children( 'li.tg_slider_nav_img' ).each(function(i, v) {
			var nav_src = new String( jQuery(this).children('img').attr( 'src' ) );
			if(i == jQuery(my_parent).attr( 'curSlide')) {
				jQuery(this).children('img').attr( 'src', nav_src.replace( /_(on|off)/, '_on' ) );
			} else {
				jQuery(this).children('img').attr( 'src', nav_src.replace( /_(on|off)/, '_off' ) );
			}
	});
	
	//transition the main image
	if(tg_first) {
		tg_first = false;
	} else {
		jQuery(my_parent).children('div.tg_slider_main').children( 'p' ).fadeOut(750);
		jQuery(my_parent).children('div.tg_slider_main').children( 'p' ).each(function(i, v){
				if(i == jQuery(my_parent).attr( 'curSlide')) {
					jQuery(this).delay(750).fadeIn(750);
				}
		});
	}
}

