var safe_ad_pages_array	= [];
var current_safe_image_index = 0;
var current_thumb_index = 0;
var allow_automatic = true;
var slider_bar_width = 0;
var restriction_date = new Date();
var splashthumbelements;
var visible_splash_thumbs = 6;

var splashAdThumbs;

function swap_set_thumb_sizes()
{
	splashthumbelements = $$('#splashAreaThumb li');
	
	var thumbelements = $$(".thumb");
	
	//load all of the values from the rel
	thumbelements.each(
		function(el) {
			
			 slider_bar_width += 92;
		
		}
	);
	
	$('splashAreaThumbUL').style.width = slider_bar_width+'px';
//		alert(slider_bar_width);

}

function swap_move_thumbnails_up()
{
	
	current_thumb_index += visible_splash_thumbs;
	
	if(safe_ad_pages_array.length <= current_thumb_index)
	{
		//current_thumb_index = 0; //reset to the begining
		current_thumb_index = safe_ad_pages_array.length-1;
	}
	
	
	splashAdThumbs.set(splashthumbelements[current_thumb_index]);
	
	swap_set_arrows();
}

function swap_move_thumbnails_down()
{
	
	current_thumb_index -= visible_splash_thumbs;
	
	if(current_thumb_index < 0)
	{
		//current_thumb_index = safe_ad_pages_array.length-1;//use this if you want it to recycle
		current_thumb_index = 0;
	}
	else if(current_thumb_index <= visible_splash_thumbs)
	{
		current_thumb_index = 0;
	}
	splashAdThumbs.set(splashthumbelements[current_thumb_index]);
	
	swap_set_arrows();
}

function swap_set_arrows()
{	
	if(safe_ad_pages_array.length <= visible_splash_thumbs)
	{
		$('index_splash_arrow_left').style.cursor='';
		$('index_splash_arrow_left').src = 'images/spacer.gif';
		
		$('index_splash_arrow_right').style.cursor='';
		$('index_splash_arrow_right').src = 'images/spacer.gif';
	}
	else
	{
		if(current_thumb_index==0)
		{
			$('index_splash_arrow_left').style.cursor='';
			$('index_splash_arrow_left').src = 'images/index_images/index_splash_arrow_left_inactive.gif';
		}
		else
		{
			$('index_splash_arrow_left').style.cursor='pointer';
			$('index_splash_arrow_left').src = 'images/index_images/index_splash_arrow_left.gif';
		}
		
		if(current_thumb_index+visible_splash_thumbs >= safe_ad_pages_array.length || safe_ad_pages_array.length <= visible_splash_thumbs)
		{
			$('index_splash_arrow_right').style.cursor='';
			$('index_splash_arrow_right').src = 'images/index_images/index_splash_arrow_right_inactive.gif';
		}
		else
		{
			$('index_splash_arrow_right').style.cursor='pointer';
			$('index_splash_arrow_right').src = 'images/index_images/index_splash_arrow_right.gif';
		}
	}
	
}


function swap_clear_selected_thumb()
{
	var thumbelements = $$(".thumbSelected");

	thumbelements.each(
		function(el) {
			el.className = 'thumb';
		}
	);		
}

function swap_safe_image(current_index)
{
	swap_clear_selected_thumb();
	
	var thumbelements = $$(".thumb");
	var current_obj = thumbelements[current_index];
	
	spash_obj = document.getElementById('splashArea');
	
	test = new Fx.Tween(spash_obj,{
				duration: 0,
				onComplete: function() {
					if(safe_ad_pages_array[current_index].usescript=='true')
						spash_obj.innerHTML = splash_from_script(safe_ad_pages_array[current_index].splash);
					else
						spash_obj.innerHTML = document.getElementById(safe_ad_pages_array[current_index].splash).innerHTML;
					document.getElementById('splashArea').fade(1);
				}
			}).start('opacity',0);
	
	current_safe_image_index = current_index;
	current_obj.className = 'thumbSelected';
	
	if(current_index%(visible_splash_thumbs) == 0)
	{
		current_thumb_index = current_index;
		splashAdThumbs.set(splashthumbelements[current_index]);
		swap_set_arrows();
	}
	
	if(current_index == 0)
	{
		current_thumb_index = current_index;
		splashAdThumbs.set(0);
		swap_set_arrows();
	}
}

function manual_swap_images(obj)
{
	
	allow_automatic = false;
	swap_clear_selected_thumb();
	
	var current_index=0;
	var counter = 0;
	var thumbelements = $$(".thumb");
	thumbelements.each(
		function(el) {
			if(el==obj)
			{
				current_index = counter;
			}
			else
				counter++
		}
	);
	
	if(current_safe_image_index != current_index)
		swap_safe_image(current_index);

	obj.className = 'thumbSelected';
}

function stop_cycle()
{
	allow_automatic = false;
}

function start_cycle()
{
	restriction_date = new Date();
	allow_automatic = true;
}

var cycle_images_count = 0;
function cycle_images()
{
	
	time_delay = 4000;

	if(safe_ad_pages_array[current_safe_image_index].delay!=null)
			time_delay = safe_ad_pages_array[current_safe_image_index].delay;
	
	cycle_images_count++;
	
	curDate = new Date();
			
	if(allow_automatic && curDate - restriction_date > time_delay)
	{
		current_safe_image_index++;
		
		if(safe_ad_pages_array.length <= current_safe_image_index)
		{
			current_safe_image_index = 0; //reset to the begining
		}
		
		swap_safe_image(current_safe_image_index);
		
		if(safe_ad_pages_array[current_safe_image_index].delay!=null)
			time_delay = safe_ad_pages_array[current_safe_image_index].delay;
		
	}
	
	setTimeout("cycle_images()", time_delay);
}

function load_cycle()
{
	var thumbelements = $$(".thumb");
	var obj = thumbelements[0];
	obj.className = 'thumbSelected';
	var value = '';
	var value_array = [];
	
	safe_ad_pages_array;
	
	//load all of the values from the rel
	thumbelements.each(
		function(el) {
			value = el.rel;
			value_array = value.split(',');
			safe_ad_pages_array.push({'splash':value_array[0],'delay':value_array[1],'usescript':value_array[2]});
		
			el.addEvent('mouseenter',function() {
				manual_swap_images(el);
			});
			
			el.addEvent('mouseleave',function() {
				start_cycle();
			});
		
		}
	);
	
	spash_obj = $('splashArea');
	
	spash_obj.addEvent('mouseenter',function() {
		stop_cycle();
	});
	
	spash_obj.addEvent('mouseleave',function() {
		start_cycle();
	});
	
	cycle_images();
	
	swap_set_arrows();
}

var splash12_global = 'true';
function splash_from_script(splash)
{
	//this was put in because IE has bugs with param and innerHTML
	
	if(splash=='splash12')
	{
		var splash12div = '<div style="width:647px;height:420px;background-image:url(images/index_images/torturetests.jpg);"><a href="torture_tests.php"><img src="images/spacer.gif" alt="Torture Tests" width="647" height="75" border="0" /></a><div style="text-align:center;"><object width="451" height="256" bgcolor="#000000" data="http://media.libertysafe.com/includes/js/flowplayer/flowplayer.commercial-3.2.5.swf" type="application/x-shockwave-flash"><param name="movie" value="http://media.libertysafe.com/includes/js/flowplayer/flowplayer.commercial-3.2.5.swf" /><param name="allowfullscreen" value="true" /><param name="wmode" value="opaque" /><param name="flashvars"  value=\'config={"key":"#$36f0af8352d244e642d","playlist":["http://media.libertysafe.com/media/images/Liberty_Safe_17.jpg",{"url":"http://media.libertysafe.com/media/Liberty_Safe_17.mp4","autoPlay":'+splash12_global+'}]}\' /></object></div></div>';
		splash12_global = 'false';
		return splash12div;
	}
	
}

function rollover_banner_image(class_name)
{
	
	$$('img.'+class_name).each(function(img) {
		
		img.addEvent('mouseenter', function() { 
			img.setStyle('border-color', '#FF0000');
		});
		
		img.addEvent('mouseleave', function() {
			img.setStyle('border-color', 'transparent');
		});
	  
	});
	
}
