var brightness = 0;
var slideObj = null;
var slidePar = null;
var slideImg = null;
var bgImage = 'typo3conf/ext/iwshop/static/img/slider-bg.jpg';
var slImage = 'typo3conf/ext/iwshop/static/img/slider.gif';

var mousestate = 'up';

function shadeImage(id, alpha) {

	shader = document.getElementById(id+'_shade');
	image = document.getElementById(id);
	if(shader == null) {
		shader = document.createElement('div');
		shader.id = id+'_shade';
		shader.innerHTML = '&nbsp;';
		
		img = getObjectData(image);
		
		shader.style.border = 'none';
		shader.style.margin = '0px';
		shader.style.padding = '0px';
		shader.style.position = 'absolute';
		shader.style.top = img.y+'px';
		shader.style.left = img.x+'px';
		shader.style.width = img.w+'px';
		shader.style.height = img.h+'px';
		shader.style.zoom = 1;
		shader.style.zIndex = 99;
		//image.parentNode.appendChild(shader);
		document.body.appendChild(shader);
	}
	
	if(alpha == 0) shader.style.display = 'none';
	else shader.style.display = 'block';
	
	if(alpha > 0 && alpha <= 100) {
		shader.style.backgroundColor = 'white';
            shader.style.filter = 'alpha(opacity='+alpha+')';
		shader.style.opacity = alpha/100.0;
	}
	
	if(alpha < 0 && alpha >= -100) {
	      alpha *= -1;
	      shader.style.backgroundColor = 'black';
            shader.style.filter = 'alpha(opacity='+alpha+')';
		shader.style.opacity = alpha/100.0;
	}
}

function repaintShader(id) {
	shader = document.getElementById(id+'_shade');
	image = document.getElementById(id);
	if(shader == null) shadeImage(id, 0);

	img = getObjectData(image);
		
	shader.style.position = 'absolute';
	shader.style.top = img.y+'px';
	shader.style.left = img.x+'px';
	shader.style.width = img.w+'px';
	shader.style.height = img.h+'px';
}

function startSlide(obj, imgID) {
	mousestate = 'down';
	slideImg = imgID;
      slideObj = obj;
      slideObj.style.position = 'absolute';
//      slideObj.style.position = 'fixed';
	//document.body.onmouseup = function(e) { console.log('body.mouseup'); stopSlide(e); }
	document.body.onmouseup = stopSlide;
	document.body.onmousemove = doSliding;
//	document.body.onselect = function(e) { if(console) console.log('body.select', e); return false; }
//	document.body.ondragstart = function(e) { if(console) console.log('body.dragstart', e); return false; }
}

function doSliding(e) {
	if(mousestate == 'up') return stopSlide({msg:'caught something'});

	var sliderScale = 50;
//	if(console) console.log(e);

	if(!e) e = window.event;
	if(slideObj == null) {
		document.body.onmousemove = null;
	      return;
	}
	obj = getObjectData(slidePar);
	slider = getObjectData(slideObj);
	fromX = obj.x-slider.w/2.0-2;
	toX = obj.x+obj.w-slider.w/2.0-3;
	coords = mouseCoords(e);
	coords.x -= (slider.w)/2.0+1;
	if(coords.x > toX) coords.x = toX;
	if(coords.x < fromX) coords.x = fromX;
	slideObj.style.left = coords.x+'px';
	brightness = 2*sliderScale*(coords.x-obj.x)/(obj.w-slider.w) - sliderScale;
	shadeImage(slideImg, brightness);
}

function stopSlide(e) {
	mousestate = 'up';
//	if(console) console.log('stopSlide(', e);
	var blankfunc = function() {};
	slideObj = null;
      document.body.onmousemove = blankfunc;
      document.body.onmouseup = blankfunc;
//      document.body.onselect = document.body.ondragstart = blankfunc;
}

function initSlider(obj) {
	slidePar = obj.parentNode;
	obj.parentNode.removeChild(obj);
	document.body.appendChild(obj);
	
	sliderParent = getObjectData(slidePar);
	slider = getObjectData(obj);
	resetSlider(obj);
	
	i = document.createElement('img');
	i.id = 'sliderImgBg';
	i.src = bgImage;
	i.alt = 'background';
	i.style.position = 'absolute';
	i.style.top = sliderParent.y+1+'px';
	i.style.left = sliderParent.x+1+'px';
	i.style.width = sliderParent.w-2+'px';
	i.style.height = sliderParent.h-2+'px';
	i.style.zIndex = 50;
	//obj.parentNode.appendChild(i);
	document.body.appendChild(i);
	
	//obj.style.backgroundImage = 'url("'+slImage+'")';
	//obj.style.backgroundRepeat = 'no-repeat';
	obj.style.zIndex = 75;
	obj.style.border = 'none';
	obj.style.top = sliderParent.y-8+'px';
	
}

function setSliderPos(obj, val) {
	brightness = val;
	sliderParent = getObjectData(slidePar);
	slider = getObjectData(obj);
	fromX = sliderParent.x;
	toX = sliderParent.x + sliderParent.w - slider.w;
	posX = (brightness/200.0+0.5)*(sliderParent.w-slider.w) + fromX;
	obj.style.position = 'absolute';
	//obj.style.position = 'fixed';
	obj.style.left = posX+'px';
	shadeImage(slideImg, brightness);
}

function resetSlider(obj) {
	setSliderPos(obj, 0);
}

function initFakeImage(img) {
	imgData = getObjectData(img);
      img.style.height = imgData.h/3+'px';
      img.style.width = imgData.w+'px';
	newData = getObjectData(img);
	newData.h = imgData.h;
	imgData = newData;

	fake = document.createElement('div');
	fake.id = img.id+'_fake';
	fake.style.border = 'none';
	fake.style.margin = '0px';
	fake.style.padding = '0px';
	fake.style.position = 'absolute';
	fake.style.top = imgData.y+'px';
	fake.style.left = imgData.x+'px';
	fake.style.width = imgData.w+'px';
	fake.style.height = imgData.h/3+'px';
	fake.style.zoom = 1;
	fake.style.zIndex = 98;
	fake.style.display = 'block';

	fake.style.backgroundImage = "url('"+img.src+"')";
	fake.style.backgroundRepeat = 'no-repeat';
	//img.parentNode.appendChild(fake);
	document.body.appendChild(fake);
	
	return fake;
}

function showColorImage(id) {
	img = document.getElementById(id);
	fake = document.getElementById(id+'_fake');
	
	if(fake == null) fake = initFakeImage(img);
	
	fake.style.backgroundPosition = '0px 0px';
}

function showGreyscaleImage(id) {
	img = document.getElementById(id);
	fake = document.getElementById(id+'_fake');

	if(fake == null) fake = initFakeImage(img);

	fake.style.backgroundPosition = '0px -'+(imgData.h/3)+'px';
}

function showSepiaImage(id) {
	img = document.getElementById(id);
	fake = document.getElementById(id+'_fake');

	if(fake == null) fake = initFakeImage(img);

	fake.style.backgroundPosition = '0px -'+((imgData.h/3)*2)+'px';
}

