// Gallery control
GalleryControl.prototype.createGalleryControl = _createGalleryControl;
GalleryControl.prototype.setActionButtons = _setActionButtons;
GalleryControl.prototype.setBestFit = _setBestFit;
GalleryControl.prototype.setActualSize = _setActualSize;
GalleryControl.prototype.setNextPicture = _setNextPicture;
GalleryControl.prototype.setSpeed = _setSpeed;

var galleryControl = [];
function GalleryControl(type, gallery)
{
	this.type = type;
	this.gallery = gallery;
	this.idx = galleryControl.length;
	galleryControl[this.idx] = this;
}

function _createGalleryControl(parent)
{
	var control_table = '';
	var control_tbody = '';
	var control_tr = '';
	
	var separator_num = this.type.length - 1;
	
	control_frame = document.createElement('div');
	control_frame.id = 'gallery_control_frame';
	
	control_table = document.createElement('table');
	control_tbody = document.createElement('tbody');
	control_tr = document.createElement('tr');
	control_tr.id = 'img_actions';
	
	for(j=0; j<this.type.length; j++)
	{
		if (this.type[j] == 'one-by-one')
		{
			td_first = document.createElement('td');
			div_first = document.createElement('div');
			div_first.className = 'one-by-one';
			div_first.id = 'first_img';
			img_first = new Image();
			img_first.src = one_by_one.first_img;
			img_first.alt = 'First Image';
			div_first.appendChild(img_first);
			td_first.appendChild(div_first);
			
			td_previous = document.createElement('td');
			div_previous = document.createElement('div');
			div_previous.className = 'one-by-one';
			div_previous.id = 'previous';
			img_previous = new Image();
			img_previous.src = one_by_one.previous_img;
			img_previous.alt = 'Previous Image';
			div_previous.appendChild(img_previous);
			td_previous.appendChild(div_previous);
			
			td_next = document.createElement('td');
			div_next = document.createElement('div');
			div_next.className = 'one-by-one';
			div_next.id = 'next';
			img_next = new Image();
			img_next.src = one_by_one.next_img;
			img_next.alt = 'Next Image';
			div_next.appendChild(img_next);
			td_next.appendChild(div_next);
			
			td_last = document.createElement('td');
			div_last = document.createElement('div');
			div_last.className = 'one-by-one';
			div_last.id = 'last_img';
			img_last = new Image();
			img_last.src = one_by_one.last_img;
			img_last.alt = 'Last Image';
			div_last.appendChild(img_last);
			td_last.appendChild(div_last);
			
			control_tr.appendChild(td_first);
			control_tr.appendChild(td_previous);
			control_tr.appendChild(td_next);
			control_tr.appendChild(td_last);
			
			if(separator_num > 0)
			{
				td_separator = document.createElement('td');
				div_separator = document.createElement('div');
				div_separator.className = 'separator';
				td_separator.appendChild(div_separator);
				control_tr.appendChild(td_separator);
				separator_num--;
			}
		}
		if (this.type[j] == 'picture-size')
		{
			td_best_fit = document.createElement('td');
			div_best_fit = document.createElement('div');
			div_best_fit.id = 'best_fit';
			div_best_fit.className = 'picture_size_btn';
			img_best_fit = new Image();
			img_best_fit.src = picture_size.best_fit_img;
			img_best_fit.alt = 'Best Fit';
			div_best_fit.appendChild(img_best_fit);
			td_best_fit.appendChild(div_best_fit);
			
			td_actual_size = document.createElement('td');
			div_actual_size = document.createElement('div');
			div_actual_size.id = 'actual_size';
			div_actual_size.className = 'picture_size_btn';
			img_actual_size = new Image();
			img_actual_size.src = picture_size.actual_size_img;
			img_actual_size.alt = 'Actual Size';
			div_actual_size.appendChild(img_actual_size);
			td_actual_size.appendChild(div_actual_size);
			
			control_tr.appendChild(td_best_fit);
			control_tr.appendChild(td_actual_size);
			/*
			if(separator_num > 0)
			{
				td_separator = document.createElement('td');
				div_separator = document.createElement('div');
				div_separator.className = 'separator';
				td_separator.appendChild(div_separator);
				control_tr.appendChild(td_separator);
				separator_num--;
			}*/
		}
		if (this.type[j] == 'slide-show')
		{
			td_speed_action = document.createElement('td');
			div_speed_action= document.createElement('div');
			div_speed_action.className = 'speed_act';
			div_speed_action.id = 'slide_show_action';
			img_speed_action = new Image();
			img_speed_action.src = slide_show.start_button;
			img_speed_action.id = 'slide_show_act'
			img_speed_action.alt = 'Play';
			div_speed_action.appendChild(img_speed_action);
			td_speed_action.appendChild(div_speed_action);
			
			td_speed_min = document.createElement('td');
			div_speed_min = document.createElement('div');
			div_speed_min.className = 'speed_btn';
			div_speed_min.id = 'min_btn';
			img_speed_min = new Image();
			img_speed_min.src = slide_show.min_button;
			img_speed_min.alt = 'Min';
			div_speed_min.appendChild(img_speed_min);
			td_speed_min.appendChild(div_speed_min);
			
			td_speed = document.createElement('td');
			div_speed = document.createElement('div');
			div_speed.id = 'speed';
			div_speed.className = '';
			div_speed.style.width = '0px';
			div_speed_back = document.createElement('div');
			div_speed_back.id = 'speed_back';
			div_speed_back.className = '';
			div_speed_back.appendChild(div_speed);
			td_speed.appendChild(div_speed_back);
			
			td_speed_max = document.createElement('td');
			div_speed_max = document.createElement('div');
			div_speed_max.className = 'speed_btn';
			div_speed_max.id = 'max_btn';
			img_speed_max = new Image();
			img_speed_max.src = slide_show.max_button;
			img_speed_max.alt = 'Max';
			div_speed_max.appendChild(img_speed_max);
			td_speed_max.appendChild(div_speed_max);
			
			control_tr.appendChild(td_speed_action);
			control_tr.appendChild(td_speed_min);
			control_tr.appendChild(td_speed);
			control_tr.appendChild(td_speed_max);
			
			if(separator_num > 0)
			{
				td_separator = document.createElement('td');
				div_separator = document.createElement('div');
				div_separator.className = 'separator';
				td_separator.appendChild(div_separator);
				control_tr.appendChild(td_separator);
				separator_num--;
			}
		}
	}
	control_tbody.appendChild(control_tr);
	control_table.appendChild(control_tbody);
	$(control_frame).appendChild(control_table);
	$(parent).appendChild(control_frame);
	
	return control_table;
}

function _setPreviousPicture()
{
	array_id = this.gallery.getArrayIdByPictureId(this.gallery.current_picture);
	if(array_id == 0)
		array_id = this.gallery.picture_number - 1;
	else
		array_id --;
	picture_id = this.gallery.getPictureIdByArrayId(array_id);
	this.gallery.setPicture(picture_id);
}

function _setNextPicture()
{
	array_id = this.gallery.getArrayIdByPictureId(this.gallery.current_picture);
	if(array_id == this.gallery.picture_number - 1)
		array_id = 0;
	else
		array_id ++;
	picture_id = this.gallery.getPictureIdByArrayId(array_id);
	this.gallery.setPicture(picture_id);
}

function _setFirstPicture()
{
	picture_id = this.gallery.getPictureIdByArrayId(0);
	this.gallery.setPicture(picture_id);
}

function _setLastPicture()
{
	last = this.gallery.picture_number - 1;
	picture_id = this.gallery.getPictureIdByArrayId(last);
	this.gallery.setPicture(picture_id);
}

function _setActionButtons()
{
	for(j=0; j<this.type.length; j++)
	{
		if (this.type[j] == 'one-by-one')
		{
			first = $('first_img');
			if(first) 
				first.onclick = _setFirstPicture.bindAsEventListener(this);
			
			previous_btn = $('previous');
			if(previous_btn)
				previous_btn.onclick = _setPreviousPicture.bindAsEventListener(this);
			
			next_btn = $('next');
			if(next_btn)
				next_btn.onclick = _setNextPicture.bindAsEventListener(this);
			
			last = $('last_img');
			if(last) 
				last.onclick = _setLastPicture.bindAsEventListener(this);
		}
		if (this.type[j] == 'slide-show')
		{
			slideShow_action = $('slide_show_action');
			if (slideShow_action)
				slideShow_action.onclick = _slideShow.bindAsEventListener(this);
				
			minButton = $('min_btn');
			if (minButton)
				minButton.onclick = _setMinSpeed.bindAsEventListener(this);
				
			maxButton = $('max_btn');
			if (maxButton)
				maxButton.onclick = _setMaxSpeed.bindAsEventListener(this);
		}
		if (this.type[j] == 'picture-size')
		{
			bestFit = $('best_fit');
			if(bestFit)
				bestFit.onclick = _setBestFitButton.bindAsEventListener(this);
			
			actualSize = $('actual_size');
			if(actualSize)
				actualSize.onclick = _setActualSizeButton.bindAsEventListener(this);
		}
	}
}

function _setMinSpeed()
{
	this.gallery.slideShow.show_speed = _setSpeed(-1, this.gallery.slideShow.show_speed);
}

function _setMaxSpeed()
{
	this.gallery.slideShow.show_speed = _setSpeed(1, this.gallery.slideShow.show_speed);
}

function _setSpeed(step, speed)
{
	s = Math.round((10000 - speed) / 1000);
	s += step;
	if (s >= 0 && s <= 10)
	{
		if($('speed'))
			$('speed').style.width = (10 * s) + 'px';
		speed = 10100 - (1000 * s);
	}
	return speed;
}

function _slideShow()
{
	if (this.gallery.slideShow.slide_show == false)
	{	
		this.gallery.slideShow.slide_show = true;
		this.gallery.slideShow.show_interval = window.setInterval("galleryControl["+this.idx+"].setNextPicture()", this.gallery.slideShow.show_speed);
		$('slide_show_act').src = slide_show.stop_button;
		$('slide_show_act').alt = 'Stop';
	}
	else if (this.gallery.slideShow.slide_show == true)
	{
		this.gallery.slideShow.slide_show = false;
		window.clearInterval(this.gallery.slideShow.show_interval);
		$('slide_show_act').src = slide_show.start_button;
		$('slide_show_act').alt = 'Play';
	}
}

function _setBestFitButton()
{
	this.gallery.setBestFitControl();
}

function _setActualSizeButton()
{
	this.gallery.setActualSizeControl();
}


function _setBestFit()
{
	this.gallery.pictureSize.best_fit = true;
	$('best_fit').className = 'picture_size_btn_click';
	$('actual_size').className = 'picture_size_btn';
}

function _setActualSize()
{
	this.gallery.pictureSize.best_fit = false;
	$('best_fit').className = 'picture_size_btn';
	$('actual_size').className = 'picture_size_btn_click';
}

