/* CONTENTELEMENT TOP */

var currentitem = null;
	
function initmouseovertop() {
	
	$$('#topcontent #topscrollcontent .contentelement .contentimg').each(function(item, index){
		
		// mouseover
		item.addEvent('mouseover', function(event) {
			
			// show legend
			var parent = item.getParent();
			var contenttext = parent.getFirst();
			contenttext.setStyle('visibility', 'visible');
		});
		
		// mouseout
		item.addEvent('mouseout', function(event) {
			
			// hide legend
			if (item != currentitem) {
				var parent = item.getParent();
				var contenttext = parent.getFirst();
				contenttext.setStyle('visibility', 'hidden');
			}
		});
		
		// click
		item.addEvent('click', function(event) {
		
			//alert(0);
			currentitem = item;
		});
		
	});
	
}



function initmouseclickbottom() {
	
	var currentitem = null;
	
	$$('#bottomcontent #bottomscrollcontent .contentelement .contentimg').each(function(item, index){
		
		// click
		item.addEvent('click', function(event) {
			changeimg(item);
		});
	});
	
	$$('#bottomcontent #bottomscrollcontent .contentelement .contentbigimg').each(function(item, index){
		
		// click
		item.addEvent('click', function(event) {
			changeimg(item);
		});
	});
}



// use small img and resize it
function cahngeimgSmallSmall(item, img) {

	item.toggleClass('contentbigimg');
	item.toggleClass('contentimg');
	img.removeProperty('width');
	img.setProperty('height', IMG_HEIGHT);
	
	if (mySlideB==null) {
		var mySlideBStep = 0;
	} else {
		var mySlideBStep = mySlideB.step;
	}
	loadbottomscrollers(mySlideBStep);
}

// set bigger img
function cahngeimgSmallBig(item, img, src) {
	//alert(src);
	img.setProperty('src', src);
	img.removeProperty('width');
	img.removeProperty('height');
	item.toggleClass('contentbigimg');
	item.toggleClass('contentimg');	
	img.setProperty('height', IMG_HEIGHT);
	
	if (mySlideB==null) {
		var mySlideBStep = 0;
	} else {
		var mySlideBStep = mySlideB.step;
	}
	loadbottomscrollers(mySlideBStep);
	
	// scroll if necessary
	var itemright = item.getCoordinates().right;
	if ((itemright - mySlideB.step) > bodywidth) {
		var diff = itemright - bodywidth + parseInt(item.getStyle('marginRight'));
		scroll2.scrollTo(diff, 0);
	}
	var itemleft = item.getCoordinates().left;
	if (itemleft < mySlideB.step) {
		scroll2.toElement(item.getParent().id);
	}
}

function changeimg(item) {
		
	var img = item.getFirst();
	var srcold = img.getProperty('src');
	
	// small to big
	if (item.hasClass('contentimg')) {
		
		var srcnew = srcold.replace(IMG_THUMB_DIR, IMG_POPUP_DIR);
		cahngeimgSmallBig(item, img, srcnew);
		
		/*
		// check if file exists
		new Ajax(srcnew, {
			method: 'get',
			evalScripts: false,
			onSuccess: function(){ cahngeimgSmallBig(item, img, srcnew); },
			onFailure: function(){ cahngeimgSmallBig(item, img, srcnew); }
			//onFailure: function(){ cahngeimgSmallSmall(item, img); }
		}).request();
		*/
	}
	
	// big to small
	else if (item.hasClass('contentbigimg')) {
		
		var srcnew = srcold.replace(IMG_POPUP_DIR, IMG_THUMB_DIR);
		img.setProperty('src', srcnew);
		img.removeProperty('width');
		img.setProperty('height', IMG_THUMB_HEIGHT);
		item.toggleClass('contentimg');
		item.toggleClass('contentbigimg');
		
		if (mySlideB==null) {
			var mySlideBStep = 0;
		} else {
			var mySlideBStep = mySlideB.step;
		}
		loadbottomscrollers(mySlideBStep);
		//alert(mySlideB.step);
	}
}



/* EVENTS */

window.addEvent('domready', initmouseovertop); 
window.addEvent('domready', initmouseclickbottom); 

