function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}






//******** Functions for homepage *****************************************
function swap_swap_image(current_h_index, current_i_index)
{
	total_index = swap_image_swap_array.length;

	for(i=0; i<total_index; i++)
	{
		//document.getElementById("thumb"+i).className = 'thumb';
	}

	//document.getElementById("thumb"+current_index).className = 'thumbSelected';
	
	
	
	//document.getElementById("swap_main").src = swap_image_swap_array[current_index];

	blendimage('blenddiv','blendimage',swap_image_swap_array[current_h_index][current_i_index],400);
	document.getElementById('mainImageLink').href=swap_image_html_array[current_h_index][current_i_index]; //swap links
	//document.getElementById("thumbText").innerHTML = swap_thumb_text_array[current_index]; //swap text
	
	
	current_swap_image_h_index = current_h_index;
	current_swap_image_i_index = current_i_index;
	
	
	//********************** Display Area ********************** 
		var changeElements = $$("li.buttonsSelected");
		changeElements.each(function(currentElement)
		{
			currentElement.className = 'buttonsNotSelected';
		});
		document.getElementById('indexAdHeading'+current_h_index).className = 'buttonsSelected';
		
		
		changeElements = $$("li.buttonsNavSelected");
		changeElements.each(function(currentElement)
		{
			currentElement.className = 'buttonsNavNotSelected';
		});
		document.getElementById('indexAdNavigation'+current_h_index+current_i_index).className = 'buttonsNavSelected';
		
		changeElements = $$("div.buttonsNavShow");
		changeElements.each(function(currentElement)
		{
			currentElement.className = 'buttonsNavHide';
		});
		document.getElementById('indexAdNavigationArea'+current_h_index).className = 'buttonsNavShow';
	//********************** END Display Area ********************** 
}

function manual_swap_images(current_h_index, current_i_index)
{
	allow_automatic = false;
	swap_swap_image(current_h_index, current_i_index);
}

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

function swap_lock_heading(current_h_index)
{
	lock_heading = true;
	current_swap_image_h_index = current_h_index;
	current_swap_image_i_index = 0;
	manual_swap_images(current_swap_image_h_index, current_swap_image_i_index);
	start_cycle();
}

function swap_unlock_heading()
{
	lock_heading = false;
	start_cycle();
}

function cycle_images()
{

	curDate = new Date();
	
	if(allow_automatic && curDate - restriction_date > 4000)
	{
		get_next_indexes();
		swap_swap_image(current_swap_image_h_index, current_swap_image_i_index);
	}
	
	setTimeout("cycle_images()", 4000);
}

function swap_next_image(current_h_index)
{
	lock_heading = true;
	current_swap_image_h_index = current_h_index;
	get_next_indexes();
	manual_swap_images(current_swap_image_h_index, current_swap_image_i_index);
}

function swap_previous_image(current_h_index)
{
	lock_heading = true;
	current_swap_image_h_index = current_h_index;
	current_swap_image_i_index--;
	if(current_swap_image_i_index < 0)
	{
		current_swap_image_i_index = swap_image_swap_array[current_swap_image_h_index].length;
		current_swap_image_i_index--;
	}
	
	manual_swap_images(current_swap_image_h_index, current_swap_image_i_index);
}

function get_next_indexes()
{
	loop_counter = 0;
	while(true)
	{
		
		if(lock_heading)
		{
			current_swap_image_i_index++;
			
			if(swap_image_swap_array[current_swap_image_h_index].length <= current_swap_image_i_index || swap_image_swap_array[current_swap_image_h_index][current_swap_image_i_index] == null)
			{	
				current_swap_image_i_index = 0; //reset to the begining
			}
			
			break;
		}
		else
		{
			if(swap_image_swap_array.length <= loop_counter)
			{
				//hit the end so reset
				current_swap_image_h_index = 0; //reset to the begining
				current_swap_image_i_index = 0; //reset to the begining
				loop_counter = 0;
			}
			else
			{
				current_swap_image_h_index++;
				loop_counter++;
			}
			
			if(swap_image_swap_array.length <= current_swap_image_h_index)
			{
				current_swap_image_h_index = 0; //reset to the begining
				current_swap_image_i_index++;
			}
			
			if(swap_image_swap_array[current_swap_image_h_index][current_swap_image_i_index] != null)
			{	
				break;
			}
		}
		
	}
}
//******** Functions for homepage *****************************************
