/**
 * @author Patricia Tiedemann, StrgB <tiedemann@steuerungb.de>
 */

var intCurrLargeImg		= 0;
var intCurrShownText	= 0;

//Event.onReady(function() {
document.observe("dom:loaded", function() {
	if($("attribute272") != undefined) {
		//alert(getSelectedOption$("attribute272")());
		$("attribute272").observe('change', changeToAppropriateImg);
	}
	if($("prodview-descbox-text-1") != undefined) {
		new Effect.Fade("prodview-descbox-text-1",	{duration: 0, from: 1, to: 0});
	}
	if($("prodview-descbox-text-2") != undefined) {
		new Effect.Fade("prodview-descbox-text-2",	{duration: 0, from: 1, to: 0});
	}
});

function changeToAppropriateImg() {
	//alert('val: ' + $("attribute272").value + $("attribute272").getValue());
	
	var colorLabel = $("attribute272").options[$("attribute272").selectedIndex].text;
	var cleanColorLabel = colorLabel.replace(/\W/g, "-");
	//alert("label " + cleanColorLabel);
	loopThroughImages(cleanColorLabel.toLowerCase());
	
	return true;
	
}

var foundStringInImage = false;

function loopThroughImages(searchString) {

	$("all-large-imgs").select('li').each(function(eachListElement) {
		// eachListElement 	= HTMLLIElement
		// containingImg 	= HTMLImageElement
		//alert(eachListElement + ' / ' + eachListElement.id + ' / ' + eachListElement.readAttribute('class'));
		
		//containingImg = eachListElement.select('a').extend;
		//containingImg = eachListElement.select('a');
		//alert(containingImg + ' / ' + eachListElement.select('a').readAttribute('class'));
		/*extContainingImg = map(containingImg.extend);
		alert(eachListElement.id + ' /el ' + extContainingImg.readAttribute('src'));*/

		eachListElement.select('a').each(function(eachImage) {
			//alert(eachImage + '// ' + searchString);
			searchStringAsRegExp = new RegExp(searchString, 'gi');
			if (eachImage.match(searchStringAsRegExp)) {
				foundStringInImage = true;
			}			
		});
		
		//alert(eachListElement.id + ' / ' + foundStringInImage);
		if(foundStringInImage == true) {
			foundStringInImage = false;
			//alert("foo " + eachListElement.id.substr(eachListElement.id.length - 1, 1));
			showLargeImg(eachListElement.id.substr(eachListElement.id.length - 1, 1));
			
			//return eachListElement.id.substr(-1);
			throw $break;
		}
	});
}


function showLargeImg(intImgIndex) {
//	if($('prod-img-' +intImgIndex).visible() != true) {
		$('prod-img-' + intCurrLargeImg).show();
//	}
	if(intCurrLargeImg != intImgIndex) {
		// show the new img, fade out the current
		new Effect.Parallel([
				new Effect.Appear('prod-img-' +intImgIndex,		{sync: true, from: 0, to: 1 }),
		  		new Effect.Fade('prod-img-' + intCurrLargeImg,	{sync: true, from: 1, to: 0 })
		], { 
		  duration: 3,
		  delay: 0
		});
		

		// set new img as current img
		intCurrLargeImg = intImgIndex;		
	}
	
	window.setTimeout('checkCurrShown()', 3100);
	//else {
//		if($('prod-img-' +intImgIndex).visible() != true) {
//			$('prod-img-' +intImgIndex).show();
//		}
	//}
	return false;
}

function checkCurrShown() {
	if($('prod-img-' + intCurrLargeImg).visible() != true) {
//		alert('notshown!');
		$('prod-img-' + intCurrLargeImg).show();
	}
}

function displayDescText(intTextToDisplay) {

	if(intCurrShownText != intTextToDisplay) {
		// show the new text, fade out the current
		new Effect.Appear("prodview-descbox-text-" + intTextToDisplay,	{duration: 0});
		new Effect.Fade("prodview-descbox-text-" + intCurrShownText,	{duration: 0, from: 1, to: 0});
		$("prodview-descbox-headline-" + intTextToDisplay).toggleClassName('active');
		$("prodview-descbox-headline-" + intCurrShownText).toggleClassName('active');
		
		// set new text as current text
		intCurrShownText = intTextToDisplay;		
	}
	return false;	
}

function addBookmark(title, url) {
        if (window.sidebar) { 
		// firefox
              window.sidebar.addPanel(title, url,"");
        } else if( document.all ) { 
		//MSIE
                window.external.AddFavorite( url, title);
        } else {
               alert("Sorry, your browser doesn't support this");
        }
}

