/* Copyright 2008 by Connecty. All rights reserved */
/* www.connecty.com */
function gal_get_cookie(name)
{
	var a = document.cookie.split(';');
	for(var i in a) {
		var v = a[i].split('=');
		if(v.length < 2) continue;

		while(v[0].charAt(0) == ' ') {
			v[0] = v[0].substring(1,v[0].length);
		}

		if(v[0] == name) {
			return v[1];
		}
	}
	return '';
}

function gal_set_cookie(name,val)
{
	var cur_date = new Date();
	var exp = new Date( cur_date.getTime() + 5*31536000000 );
	document.cookie = ' '+name+'='+val+'; expires='+exp.toGMTString()+'; path=/';
}

/* arrow */
function show_arrow()
{
	this.img.style.visibility = 'visible';

	if(this.img.style.opacity == undefined) {
		/* IE6 */
		return;
	}
	
	if(this.anim) this.anim.stop();

	this.anim = new YAHOO.util.Anim(this.img, { 
			opacity: { to: 1 }
	}, 0.3, YAHOO.util.Easing.easeOut);
	this.anim.animate();
}

function hide_arrow()
{
	if(this.img.style.opacity == undefined) {
		/* IE6 */
		this.img.style.visibility = 'hidden';
		return;
	}

	if(this.anim) this.anim.stop();

	this.anim = new YAHOO.util.Anim(this.img, { 
			opacity: { to: 0 }
	}, 1, YAHOO.util.Easing.easeOut);
	this.anim.onComplete.subscribe(function() {
		this.img.style.visibility = 'hidden';
		this.img.style.opacity = '';
	}); 
	this.anim.animate();
}

function real_hide_arrow()
{
	this.style.display = 'none';
	if(this.anim) this.anim.stop();
}

function real_unhide_arrow()
{
	this.style.display = '';
}

function make_arrow(a, click)
{
	var i = a.getElementsByTagName('img')[0];
	
	a.img = i;
	a.show = show_arrow;
	a.hide = hide_arrow;
	a.real_hide = real_hide_arrow;
	a.real_unhide = real_unhide_arrow;

	a.hide();

	a.onmouseover = function() {
		a.show();
	}
	a.onmouseout = function() {
		a.hide();
	}

	i.onclick = click;
}

/* scroller */
function add_image(img, title, desc, link)
{
	if(this.images.length == 0) {
		var a = this.getElementsByTagName('img');
		a[0].src = img;
		var b = this.getElementsByTagName('a');
		b[0].href = link;
	}
	this.images.push(img);
	this.titles.push(title);
	this.links.push(link);
	this.descriptions.push(desc);
	this.refresh_desc();

	/* preload image */
	if(!this.preloader) {
		this.preloader = new Image();
	}
	this.preloader.src = img;
}

function move_left()
{
	var old = this.images[this.cur];
	var old_link = this.links[this.cur];
	this.cur--;
	if(this.cur < 0) this.cur = this.images.length-1;
	var n = this.images[this.cur];
	var link = this.links[this.cur];

	scroll_left(this, old, n, old_link, link);
}

function move_right()
{
	var old = this.images[this.cur];
	var old_link = this.links[this.cur];
	this.cur++;
	if(this.cur >= this.images.length) this.cur = 0;
	var n = this.images[this.cur];
	var link = this.links[this.cur];

	scroll_right(this, old, n, old_link, link);
}

function refresh_desc()
{
	this.desc_node.innerHTML = '<strong>'+this.titles[this.cur]+'</strong><br />'+this.descriptions[this.cur];
}

function slide_random()
{
	var old = this.images[this.cur];
	var old_link = this.links[this.cur];

	var ncur = Math.floor(Math.random()*this.images.length);
	if(ncur == this.cur) { /* no slide to the same image */
		ncur--;
		if(ncur < 0) ncur = this.images.length-1;
	}

	var n = this.images[ncur];
	this.cur = ncur;
	var link = this.links[this.cur];

	scroll_left(this, old, n, old_link, link);
}

function make_scroller(s, desc_node)
{
	s.desc_node = desc_node;
	s.images = new Array();
	s.titles = new Array();
	s.descriptions = new Array();
	s.links = new Array();
	s.add_image = add_image;
	s.left = move_left;
	s.right = move_right;
	s.refresh_desc = refresh_desc;
	s.slide_random = slide_random;
	s.cur = 0;
}

/* base */
function show_desc()
{
	this.desc_node.style.display = '';
}

function hide_desc()
{
	this.desc_node.style.display = 'none';
}

function gal_show()
{
	this.is_hidden = false;
	this.arrow_left.real_unhide();
	this.arrow_right.real_unhide();
	this.show_desc();
	this.style.display = '';
	this.button.className = '';
	gal_set_cookie('gal_hidden', 'false');
}

function gal_hide()
{
	this.is_hidden = true;
	this.arrow_left.real_hide();
	this.arrow_right.real_hide();
	this.hide_desc();
	this.style.display = 'none';
	this.button.className = 'off';
	gal_set_cookie('gal_hidden', 'true');
}

function make_base(base, desc_node, arrow_left, arrow_right, button, scroller)
{
	make_scroller(scroller, desc_node);
	base.scroller = scroller;
	base.arrow_left = arrow_left;
	base.arrow_right = arrow_right;
	base.desc_node = desc_node;
	base.button = button;

	/* methods */
	base.show_desc = show_desc;
	base.hide_desc = hide_desc;
	base.show = gal_show;
	base.hide = gal_hide;

	base.pause_slide = function() {
		if(this.interval) clearInterval(this.interval);
		this.interval = null;
	}

	base.resume_slide = function() {
		if(!this.interval) this.interval = setInterval("document.getElementById('"+this.id+"').scroller.slide_random();", this.rand_time);
	}

	base.suspend_slide = function() {
		this.pause_slide();
		if(this.timeout) clearTimeout(this.timeout);
		this.timeout = setTimeout("document.getElementById('"+this.id+"').resume_slide();", 5000);
	}

	base.onmouseover = function() {
		base.pause_slide();
	}

	base.onmouseout = function() {
		base.resume_slide();
	}

	base.is_hidden = false;

	make_arrow(arrow_left, function() {scroller.left();});
	make_arrow(arrow_right, function() {scroller.right();});

	var hidden = gal_get_cookie('gal_hidden');
	if(hidden == 'true') {
		base.hide();
	} else {
		base.show();
	}


	button.base = base;
	button.onclick = function() {
		if(base.is_hidden) base.show();
		else base.hide();
	}

}

/* others */
function scroll_left(s, from, to, oldl, l)
{
	s.style.left = '-939px';

	var a = s.getElementsByTagName('img');
	var b = s.getElementsByTagName('a');
	a[0].src = to;
	a[1].src = from;
	b[0].href = l;
	b[1].href = oldl;

	a[1].style.marginLeft = '-2px'; /* fix errors resulting in transparent line between images when animating */

	var anim = new YAHOO.util.Anim(s, { 
			left: { to: 0 }  
	}, 1, YAHOO.util.Easing.easeOut);
	anim.onComplete.subscribe(function() {
		s.refresh_desc();
		a[1].style.marginLeft = '0';
	}); 
	anim.animate();
}

function scroll_right(s, from, to, oldl, l)
{
	s.style.left = '0';

	var a = s.getElementsByTagName('img');
	var b = s.getElementsByTagName('a');
	a[0].src = from;
	a[1].src = to;
	b[0].href = oldl;
	b[1].href = l;

	a[1].style.marginLeft = '-2px'; /* fix errors resulting in transparent line between images when animating */

	var anim = new YAHOO.util.Anim(s, { 
			left: { to: -939 }  
	}, 1, YAHOO.util.Easing.easeOut);
	anim.onComplete.subscribe(function() {
		s.refresh_desc();
		a[1].style.marginLeft = '0';
		s.style.left = '-941px';
	}); 
	anim.animate();
}

function init_gal(base, title, left, right, button, scroller)
{
	make_base(document.getElementById(base), document.getElementById(title), document.getElementById(left), document.getElementById(right), document.getElementById(button), document.getElementById(scroller));
	return document.getElementById(base);
}

function setup_random_slide(base_id, time)
{
	var base = document.getElementById(base_id);
	base.rand_time = time;
	base.resume_slide();
}
