/*
	Gallery functions 
*/
var current_image = 1;
var image_timeout = 5000;
var image_count = 0;
var timer, temp_img, image_array;

var dummy = false;

var use_transitions = true;

var IS_IE = (navigator.userAgent.indexOf('MSIE') > -1) ?  true : false;

if (IS_IE) {
	var IE_VER = navigator.appVersion;
	if (IE_VER.indexOf('MSIE 5') > -1) use_transitions = false;
}


function start_slideshow(my_img_array) {

	if (typeof(image_array) == 'undefined' && typeof(my_img_array) != 'undefined') image_array = my_img_array;
	
	if (typeof(image_array) != 'undefined') {
		image_count = image_array.length;
		timer = setInterval("change_pic()", image_timeout);
	}
}

function stop_slideshow() {
	clearInterval(timer);
}


function change_pic() {
	
	current_image++;
	
	if (current_image > (image_count-1)) current_image = 0;
	
	indicator('show');

	temp_img = document.createElement('IMG');
	temp_img.style.display = 'none';
	
	ph = document.getElementById('image_ph');
	ph.appendChild(temp_img);
	
	temp_img.src = image_array[current_image];
	
	if (temp_img.complete){
		temp_img.img_loaded = img_loaded;
		temp_img.img_loaded();
	} else {
		temp_img.onload = img_loaded;
	}
	temp_img.onerror = change_pic;
	
}

function step_pic(mode) {

	stop_slideshow();

	if (mode == 'prev') {
		
		if (current_image > 1) {
			current_image -=2;
		} else {
			current_image = image_count-2;
		}
		
	} 

	change_pic();
	
	start_slideshow();
	return false;
}

function img_loaded() {

	$('top_image').style.zIndex = '90';
	
	if (use_transitions) {
		var fadeout = $('top_image').effect('opacity', {
			onComplete: function() {
				frame = document.getElementById('top_image');
				frame.parentNode.removeChild(frame);
			}
		});
		fadeout.custom(1,0);
	} else {
		frame = document.getElementById('top_image');
		frame.parentNode.removeChild(frame);
	}
	
	this.id = 'top_image';
	this.style.display = 'block';
	this.style.zIndex = '80';

	indicator();
}


function indicator(action) {
	ind = document.getElementById('indicator');
	ind.style.display = (typeof(action) != 'undefined' && action == 'show') ? 'block' : 'none';
}

/*
	WEB CAM
*/
var counter = 30;

function refresh_webcam() {
	if (counter == 0) {

		var dt = new Date();
	
		indicator('show');
		
		temp_img = document.createElement('IMG');
		temp_img.style.display = 'none';
		/*temp_img.style.width = '909px';
		temp_img.style.height = '620px';*/
		
		ph = document.getElementById('webcam');
		ph.appendChild(temp_img);
		
		temp_img.src = "http://www.earthcam.net/cams2/yacht/yacht2.jpg?" + dt.getTime();
		
		if (temp_img.complete){
			temp_img.img_loaded = img_loaded;
			temp_img.img_loaded();
		} else {
			temp_img.onload = img_loaded;
		}
		temp_img.onerror = change_pic;
		
		counter = 30;
	} else {
		counter--;
		counter_str = (counter < 10) ? '0' + counter : counter;
		$('countdown').innerHTML = 'refreshing in '+counter_str+' sec';
	}

}

window.onload = indicator;

/*
	Ajax functions
*/
var use_ajax = false;
var current_content = '';

function change_content(content_path) {
	if (use_ajax == true) {
		if (content_path != current_content) {
			
			stop_slideshow();
			
			ids = content_path.split(',');
			
			if (ids.length == 1) {
				update_main_menu(ids[0]);
			}
			
			indicator('show');
			var myAjax = new Ajax('ajax.response.php?sv_path='+content_path+'&mode=sub_menu', {
				method: 'get', 
				update: $('sub_menu'),
				onComplete: indicator
				
			}).request();
		
			indicator('show');
			var myAjax = new Ajax('ajax.response.php?sv_path='+content_path+'&mode=picture', {
				method: 'get', 
				update: $('image_ph'),
				evalScripts: true,
				onComplete: indicator
			}).request();
			
			indicator('show');
			var myAjax = new Ajax('ajax.response.php?sv_path='+content_path+'&mode=content', {
				method: 'get', 
				update: $('content_inner'),
				evalScripts: true,
				onComplete: indicator
			}).request();
		
			current_content = content_path;
		}
		return false;
	} else {
		return true;
	}
}

function update_main_menu(id) {
	items1 = document.getElementById('main_menu').getElementsByTagName('A');
	
	for (i=0; i<items1.length; i++) {
		items1[i].className = (items1[i].id == 'main' + id) ? 'selected' : '';
	}
	
	items2 = document.getElementById('toolbox').getElementsByTagName('A');
	
	for (i=0; i<items2.length; i++) {
		items2[i].className = (items2[i].id == 'main' + id) ? 'selected' : '';
	}
}

/*
	Text toggle
*/

function toggle_mouseover(obj, file_ext) {
	var source = obj.src
	if (source.indexOf('_over.'+file_ext) > -1) {
		obj.src = source.replace('_over.'+file_ext, '.'+file_ext);
	} else {
		obj.src = source.replace('.'+file_ext, '_over.'+file_ext);
	}
}

function toggle_text() {
	obj = $('content');
	
	if (typeof(obj.originalH) == 'undefined' || !obj.originalH) { 
		obj.originalH = obj.scrollHeight;
		obj.opened = true;
	}
	
	
	
	if (typeof(obj) != 'undefined' && obj.style.display != 'none') {
		
		$('content_inner').scrollTop = 0;
		
		if (obj.opened == true) {
			obj.style.height = "30px";
			$('content_inner').style.overflow = 'hidden';
			$('btn').src = 'images/show.gif';
			obj.opened = false;

		} else {
			obj.style.height = obj.originalH + 'px';
			$('content_inner').style.overflow = 'auto';
			$('btn').src = 'images/hide.gif';
			obj.opened = true;
		}
	}
	
	return false;
}

