/**
* Written by Mark Geiger
* 12-11-2008
*/

function PhotoManager(num_set,h,w,selector,img_selector)
{
	this.first_img = 0;
	this.num_set = num_set;
	this.last_img = num_set;
	this.active_image_array = [];
	this.height = h;
	this.width = w;
	this.selector = selector;
	this.image_array = [];
	this.image_selector = img_selector;
	this.interval = 5500;
	this.random_display = 0;
	this.image_num = 0;

	this.init = function()
	{
		this.activate = this.set_images(this.selector);
		if(this.activate !== false)
		{
			this.total_num = this.activate.length;
			this.get_active_images();
			this.open_image();
			this.hide_images();
			this.hide_sectors();
			this.switch_image();
		}
		else
		{
			this.hide_container();
			return;
		}
	}
}

PhotoManager.prototype.set_images = function(selector)
{
	if(document.getElementById(selector)==null){return false;}
	var container=document.getElementById(selector);

	for (var i=0;i<container.getElementsByTagName('img').length;i++)
	{
		this.image_array[i] = container.getElementsByTagName('img')[i];
		this.image_array[i].height = this.height;
		this.image_array[i].width = this.width;
		this.image_array[i].style.cursor = 'hand';
	}
	
	if(this.image_array.length>1)
	{
		return this.image_array;
	}
	else
	{
		return false;
	}
}

PhotoManager.prototype.set_open_image = function()
{
	document.getElementById('slideImage').src = this.src;
	clearTimeout(image_set.timerID);
}

PhotoManager.prototype.open_image = function(selector)
{
	for(var i = 0; i < this.image_array.length; i++)
	{
		this.image_array[i].onclick = this.set_open_image;
	}
}

PhotoManager.prototype.hide_images = function()
{
	for(var i=0;i<this.total_num;i++)
	{
		if(i<this.first_img || i>this.last_img-1)
		{
			this.image_array[i].style.display="none";
		}
		else
		{
			this.image_array[i].style.display="inline";
		}
	}
}

PhotoManager.prototype.get_next_image_display = function()
{
	if(this.last_img<this.total_num)
	{
		this.first_img = this.first_img+this.num_set;
		this.last_img += this.first_img;
	}
	else
	{
		this.first_img = 0;
		this.last_img = this.num_set;
	}

	this.hide_images();
	this.get_active_images();

	if(typeof document.getElementById(this.image_selector) == "object")
	{
		document.getElementById(this.image_selector).src = this.active_image_array[this.first_img].src;
	}
	
	var myElementsEffects = new Fx.Elements($(this.selector));

	myElementsEffects.start({
		'0': { /*	let's change the first element's opacity */
		'opacity': [0,1] 
		}
	});
}

PhotoManager.prototype.count_sets = function()
{
	if(!document.getElementById(this.selector))
	{
		return false;
	}
	else
	{
		/*
		if(this.total_num<=this.num_set)
		{
			document.getElementById('count_sets').style.visibility = 'hidden';
			return false;
		}
		*/

		var boxes = document.getElementsByTagName('div');
		for (var i=0;i<boxes.length;i++)
		{
			if(boxes[i].getAttribute('id')=='count_sets')
			{
				var container = boxes[i];
			}
		}
		if(container)
		{
			var span = container.getElementsByTagName('span');
			for(i=0;i<span.length;i++)
			{
				id = span[i].getAttribute('id');
				switch(id)
				{
					case 'start_counter':
					start = this.first_img + 1;
					span[i].innerHTML = start;
					break;
                    case 'end_counter':
					span[i].innerHTML = (this.last_img<=this.total_num) ? this.last_img : this.total_num;
					break;
					case 'total_counter':
					span[i].innerHTML = this.total_num;
					break;
				}
			}
		}
	}
}

PhotoManager.prototype.get_previous_image_display = function()
{
	if(this.first_img > 0)
	{
		this.first_img = this.first_img - this.num_set;
		this.last_img = this.first_img + this.num_set;
	}
	else if(this.first_img == 0)
	{
		var c = (this.total_num / this.num_set);
		c = Math.floor(c);
		this.first_img=c*this.num_set;
		this.last_img=this.first_img+this.num_set;
	} 

	this.hide_images();
	this.get_active_images();

	if(typeof document.getElementById(this.image_selector) == "object")
	{
		document.getElementById(this.image_selector).src = this.active_image_array[this.first_img].src;
	}

	var myElementsEffects = new Fx.Elements($(this.selector));

	myElementsEffects.start({
		'0': { /*	let's change the first element's opacity */
		'opacity': [0,1] 
		}
	});
}

PhotoManager.prototype.get_active_images = function()
{
	if(this.image_array.length>1)
	{
		this.image_num = this.first_img;
		this.active_image_array.length = 0;
		for(var i=this.first_img;i<this.last_img;i++)
		{
			this.active_image_array[i] = this.image_array[i] || false;
		}

		return this.active_image_array;
	}
	else
	{
		container=document.getElementById(this.selector).innerHTML='';
		return;
	}
}

PhotoManager.prototype.next_image = function () 
{
	if ((this.image_num >= this.first_img) && 
		(this.image_num < this.last_img-1) && 
		(this.image_num != this.total_num-1))
	{
		this.image_num++;
	}
	else
	{
		this.image_num = this.first_img;
	}
	if(typeof document.getElementById(this.image_selector) == "object")
	{
		document.getElementById(this.image_selector).src = this.active_image_array[this.image_num].src;
	}
	
	var myElementsEffects = new Fx.Elements($(this.image_selector));

	myElementsEffects.start({
		'0': { /*	let's change the first element's opacity */
		'opacity': [0,1] 
		}
	});
}

PhotoManager.prototype.prev_image = function() 
{
	if((this.image_num == this.first_img))
	{
		if((this.total_num - this.first_img)-1 < this.num_set)
		{
			this.image_num = (this.total_num - 1);
		}
		else
		{
			this.image_num = this.last_img-1;
		}
	}
	else 
	{
		this.image_num--;
	}
	
	if(typeof document.getElementById(this.image_selector) == "object")
	{
		document.getElementById(this.image_selector).src = this.active_image_array[this.image_num].src;
	}

	var myElementsEffects = new Fx.Elements($(this.image_selector));

	myElementsEffects.start({
		'0': { /*	let's change the first element's opacity */
		'opacity': [0,1] 
		}
	});
}

PhotoManager.prototype.switch_image = function()
{

	document.getElementById(this.image_selector).src = this.active_image_array[this.image_num].src;

	var num = (this.last_img <= this.image_array.length) ? this.last_img - 1 : (this.image_array.length - this.first_img) + (this.first_img-1);
	if((this.image_num >= num))
	{					
		this.image_num = this.first_img;
	}
	else
	{
		this.image_num++;
	}

	this.count_sets();

	var recur_call = 'image_set.switch_image()';
	this.timerID = setTimeout(recur_call, this.interval);

	var myElementsEffects = new Fx.Elements($(this.image_selector));

	myElementsEffects.start({
		'0': { /*	let's change the first element's opacity */
		'opacity': [0,1] 
		}
	});
}

PhotoManager.prototype.hide_container = function()
{
	document.getElementById(this.selector).style.visibility = 'hidden';
}

PhotoManager.prototype.hide_sectors = function()
{
	if(this.image_array)
	{
		_length_a = this.image_array.length;
	}

	if(_length_a <= this.num_set)
	{
		document.getElementById("next_set").style.display = 'none';
		document.getElementById("previous_set").style.display = 'none';
	}
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document.images[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		Tucker_Web_Final_with_background_3__09_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-11.jpg");
		Tucker_Web_Final_with_background_3__12_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-15.jpg");
		Tucker_Web_Final_with_background_3__14_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-18.jpg");
		Tucker_Web_Final_with_background_3__16_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-21.jpg");
		Tucker_Web_Final_with_background_3__18_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-24.jpg");
		Tucker_Web_Final_with_background_3__20_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-27.jpg");
		Tucker_Web_Final_with_background_3__22_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-30.jpg");
		Tucker_Web_Final_with_background_3__24_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-33.jpg");
		Tucker_Web_Final_with_background_3__33_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-43.jpg");
		Tucker_Web_Final_with_background_3__42_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-53.jpg");
		Tucker_Web_Final_with_background_3__44_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-56.jpg");
		Tucker_Web_Final_with_background_3__46_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-59.jpg");
		Tucker_Web_Final_with_background_3__48_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-62.jpg");
		Tucker_Web_Final_with_background_3__50_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-65.jpg");
		Tucker_Web_Final_with_background_3__52_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-68.jpg");
		Tucker_Web_Final_with_background_3__54_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-71.jpg");
		Tucker_Web_Final_with_background_3__56_over = newImage("http://tucker-group.com/newsite/images/Tucker_Web_Final_with_ba-74.jpg");
		preloadFlag = true;
	}
	var container_main = document.getElementsByTagName("div");
	for (i=0;i<container_main.length;i++)
	{
		if(container_main[i].id=="main_image")
		{
			var image_main = container_main[i].getElementsByTagName("img")[0];
			var retrieve_image = document.getElementById("thumb_container").getElementsByTagName("img")[0];
			image_main.src=retrieve_image.src;
		}
	}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

var opener = function(open_window)
{
	var width = 700;
	var height = 600;
	
	var window_opener = window.open(open_window.src, 'PopUp', 'height=' + height + ', width=' + width + ', status=yes, resizable=yes, scrollbars=yes');
	if(window.focus)
	{
		window_opener.focus();
	}

	return false;
}

window.addEvent('domready', function() {

	if(document.getElementById('slideImage')!= null)
	{
		image_set = new PhotoManager(5,66,66,'thumb_container','slideImage');
		image_set.init();
	}

	if(document.getElementById('tucker_properties_viewer')!= null)
	{
		var get_tag = document.getElementById('tucker_properties_viewer');
		var get_tag_siblings = get_tag.getElementsByTagName('div');
		var alternate = 0;
		for(var i=0;i<get_tag_siblings.length;i++)
		{
			if(get_tag_siblings[i].className = 'get_background_color')
			{
				get_tag_siblings[i].style.backgroundColor = (alternate++ % 2 == 0) ? '#ECE6D9' : false;
			}
		}
	}

	preloadImages();

});	