// START IMAGE ROLLOVER
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



var fx;
var fxOut;
var fxOutDone;
fxOutDone = false;
var oldProduct;
var aTimeout;
var navDone;navDone = false;

function checklink(link,pid) {

	for (var i=0; i < prodArray.length; i++) {
		if(prodArray[i][3] == pid) {
			if(currentProduct == i) {
				// okay, got the one whos active
				//window.location.href = '/index.html?p='+pid;
				return false;
			}
		}
	}
	
	

	
	window.location.href = link+pid;
}
function changeBGPic(src, secSrc, overSource, pid,first, link) {

	if(first == false) {
		for (var i=0; i < prodArray.length; i++) {
			if(prodArray[i][3] == pid) {
				if(currentProduct == i) {
					// okay, got the one whos active
					//window.location.href = '/detail.html?p='+pid;
					return false;
				}
			}
		}
	}

	$('loading').setStyle('visibility', 'visible');
	$('pictureinner').setStyle('opacity', '0');

	//$('imagetextlink').href = '/detail.html?p='+pid;
	if(pid == 100) {
		/* TODO */
		if(link == "/detail_de.html?p=") {
			$('pictureimage').href = '/allproducts_de.html';
		}
		else {
			$('pictureimage').href = '/allproducts.html';
		}
	}
	else {
		$('pictureimage').href = link+pid;
	}
	
	if(!fx) {
		fx = new Fx.Style($('pictureinner'), 'opacity', {wait:false,duration:1200,
		onComplete: function() {
			fxOutDone = 0;
		},
		onCancel: function() {
			
		}

		});
		
	}

	
	
	var images = [src];
	var loadedImages = [0];
	new Asset.images(images, {
				onProgress: function(i) {
					loadedImages[i] = this;
				},
				onComplete: function(i) {
					$('loading').setStyle('visibility', 'hidden');
					$$('#pictureimage img').each(function(e){ e.remove(); });
					loadedImages[0].inject($('pictureimage'));;
					fx.set(0);
					fx.start(0, 1);
				}
		}
	);
	
	var images = [secSrc];
	//alert(secSrc);
	var loadedImages = [0];
	new Asset.images(images, {
				onProgress: function(i) {
					loadedImages[i] = this;
				},
				onComplete: function(i) {

					//loadedImages[0].id = "prodtext";
					/*loadedImages[0].addEvents({
						'mouseover': function() {
							this.src = overSource;
						},
						'mouseout': function() {
							this.src = secSrc;
						},
					});*/
					$$('#productimagetext img').each(function(e){ e.remove(); });	
					loadedImages[0].inject($('productimagetext'));
				}
		}
	);
	
	var o2 = {};
	var o = {};
	o2[oldProduct] = {width: [75, 1]};
	
	for (var i=0; i < prodArray.length; i++) {
		if(prodArray[i][3] == pid) {
			currentProduct = i;
		}
	};
	

	if(pid == 100) {
		currentProduct = 8;
	}
	o[currentProduct] = {width: [1, 75]};
	accfx.start.delay(200,accfx, o);

	if (typeof(oldProduct)!='undefined'){
		accfx2.start.delay(2500,accfx2, o2);
	}
	
	$$('.prodlinks').each(function(e){e.fireEvent('mouseout');});
	updateDirections();
	window.clearTimeout(aTimeout);
	aTimeout = window.setTimeout("nextProduct()", 15000);


	oldProduct = currentProduct;
}

function addRollover(element, cnt) {

	element.addEvent('mouseover', function(){
		if(cnt-1 == currentProduct) {
			return true;
		}
		this.src = rolloverArray[cnt-1][1];
	});
	element.addEvent('mouseout', function(){
		
		if(cnt-1 == currentProduct) {
			this.src = rolloverArray[cnt-1][1];
		}
		else {
			if(cnt > 0) {
				this.src = rolloverArray[cnt-1][0];
			}
		}
	
	});
}

function nextProduct(link) {
	oldProduct = currentProduct;
	currentProduct++;
	if(currentProduct >= prodArray.length) {
		currentProduct = 0;
	}
	changeBGPic(prodArray[currentProduct][0],prodArray[currentProduct][1],prodArray[currentProduct][2],prodArray[currentProduct][3], true, link);
	updateDirections();
}
function prevProduct(link) {
	oldProduct = currentProduct;
	currentProduct--;
	if(currentProduct < 0) {
		currentProduct = prodArray.length-1;
	}
	changeBGPic(prodArray[currentProduct][0],prodArray[currentProduct][1],prodArray[currentProduct][2],prodArray[currentProduct][3], true, link);
	updateDirections();
}

function updateDirections() {
	//$('leftclick').setStyle("visibility", "visible");
	//$('rightclick').setStyle("visibility", "visible");
}