

function setup_imgviewer() {
	thumbs = $('kaimg_preview').getElements('a');
	thumbs.each(function(item) {
		item.addEvent('click',function() {
			$clear(img_timer);
			if(!inaction)
				set_new_image(this);
			return false;
		});
	});
}

function set_new_image(atag) {
	inaction = true;
	atag.blur();
	thumbs = $$('#kaimg_preview img');
	thumbs.each(function(item) {
		if(item.hasClass('pic-current'))
			item.removeClass('pic-current');
	});
	img = atag.getElement('img');
	img.addClass('pic-current');
	// fade new image
	$('kaimg_big_ol').setStyle('display','block');
	newImage = new Asset.image(atag.get('href'), {title: atag.get('href'), onload: function() {
		ni = new Element('img', {
    		'src': this.get('href'),
			'alt': this.get('title'),
			'border': 0,
			'tween': { duration:1000, onComplete: function() {
				$('kaimg_big_ol').empty();
				$('kaimg_big_ol').setStyle('display','none');
				$('kaimg_big').getElement('img').set('src',this.get('href'));
				inaction = false;
				img_timer = fire_random_pic_click.delay(5000);
			}.bind(this)}
    	});
		ni.fade('hide');
		ni.inject($('kaimg_big_ol'));
		ni.fade('in');
	}.bind(atag)});

}

function fire_random_pic_click() {
	tags = $$('#kaimg_preview a');
	m = tags.length;
	if(m < 2)
		return false;
	rand = $random(1,m)-1;
	if(rand == current_img) {
		if(current_img > 0)
			rand = 0;
		else
			rand = 1;
	}
	if(!inaction)
		tags[rand].fireEvent('click', tags[rand]);
}

function htag_animation() {
	if($('headingcontainer')) {
		c = $('headingcontainer');
		c.setStyle('width','0px');
		c.set('tween', {duration: 'long'});
		c.tween('width', '270px');
	}
}

function image_animation() {
	blue = $$('#secondmenucontainer .box_blue_current');
	yellow = $$('#secondmenucontainer .box_yellow_current');
	if(blue.length)
		a = blue[0];
	else if(yellow.length)
		a = yellow[0];
	else
		return false;
	img = a.getElement('img');
	src = img.get('src');
	bigsrc = src.substr(0,src.lastIndexOf('.jpg'));
	bigsrc = bigsrc+"_big.jpg";
	myImage = new Asset.image(bigsrc, {onload: function() {
			img.set('height',this.height);
			img.set('width',this.width);
			img.set('src',bigsrc);
			imagescroll(img,100,151);
		}
	});
}

function imagescroll(img,width,height) {
	gox = (img.get('width') - width) / 3;
	goy = (img.get('height') - height) / 3;
	img.setStyle('top','0px');
	img.setStyle('left','0px');
	img.setStyle('position','absolute');
	img.set('morph', {duration: 2000, transition: Fx.Transitions.Circ.easeOut});
	img.morph({top: '-'+goy+'px',left: '-'+gox+'px', width: 300, height: 225});
}

// ONLOAD
var inaction = false;
var current_img = 0;
var img_timer = false;
window.addEvent('domready', function() {
	image_animation();
	if($('kaimg_preview') && $('kaimg_big')) {
		setup_imgviewer();
		img_timer = fire_random_pic_click.delay(5000);
		htag_animation();
	}
});

