window.addEvent('domready', function() {
    var aval = parseInt($('wrap').getStyle('width'));
    var s = ((aval-(aval*0.22))*0.37);
    $('content').style.width = s + 'px';
    
	if(document.body.id != 'media_page') {
		window.currenti = 0;
		if(typeof($('currentimg').getElements('img')[0]) == 'undefined') {
			new Element('img', {
				src: $('imgthumbs').getElements('img')[window.currenti].src.replace(/small_img/, 'half_img')
			}).injectInside($('currentimg'));
			window.currenti++;
		}
		setTimeout(switch_img, 5000);
	}
});

window.addEvent('resize', function() {
    var aval = parseInt($('wrap').getStyle('width'));
    var s = ((aval-(aval*0.22))*0.37);
    $('content').style.width = s + 'px';
});

function switch_img() {
	var img = $('imgthumbs').getElements('img');
	var current = $('currentimg');
	var scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0; var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
    // alert(scrollTop);
	var new_img = img[window.currenti];

	var old_img = current.getElements('img')[0];
	setTimeout(function() { old_img.destroy(); }, 4000);
	old_img.style.position = 'absolute';
	
	var el = new Element('img', {
		styles: {
			'width': '309px',
			'height': old_img.getCoordinates().height + 'px',
			'position': 'relative',
			'z-index': '1000'
		},
		src: new_img.src.replace(/small_img/, 'half_img')
	});
	
	var new_tween = new Fx.Tween(el, {property: 'opacity', duration: 1500}).set(0);
	new_tween.start(1);
	
	el.injectInside(current);
	
	if(window.currenti == img.length-1) {
		window.currenti = 0;
	} else {
		window.currenti++;
	}
	window.scrollBy(0,scrollTop);
	setTimeout(switch_img, 5000);
}
