/**

 */
var superInit=init;

init = function() {
	superInit();	
	
	$('#selectColor').click(function(){
		selectTab("color");
	});
	
	$('#selectProduct').click(function(){
		selectTab("product");
	});
	
	$('#selectFavourit').click(function(){
		selectTab("favourit");
	});
}
/**

 */
function initHoverAccordion() {
}

/**

 */
function initColorButtons() {
	$('#ColorSelector').addColorPicker({
		clickCallback: function(c) {
			onColorPicker(c);
		},
		titles: [
		         '50.02.01', '05.03.07', '05.02.07', '40.03.06', '50.03.05', '60.03.06',
		         '60.03.05', '50.03.06', '30.05.03', '50.01.05', '10.05.01', '30.05.02',
		         '40.03.01', '40.23.06', '50.03.04', '40.01.05', '30.05.03', '40.01.06',
		         '70.02.03', '30.02.02', '40.03.06', '50.03.03', '50.04.05', '50.06.06',
		         '50.065.06', '50.05.06', '50.04.06', '40.05.07', '40.065.05', '70.07.05',
		         '70.065.06', '70.07.06', '70.08.07', '80.08.07', '90.06.03', '70.065.06',
		         '70.065.02', '40.08.05', '40.08.03', '60.06.06', '40.07.05', '40.07.06',
		         '40.065.07', '40.08.07', '40.07.07', '40.11.05', '60.07.06', '40.08.05',
		         '70.08.07', '70.11.05', '70.09.06', '90.10.06', '60.10.07', '70.09.01',
		         '90.14.04', '90.14.05', '70.13.07', '90.14.05', '10.21.06', '70.14.04',
		         '90.14.07', '80.14.05', '80.14.06', '90.14.06', '60.15.05', '80.14.07',
		         '80.14.02', '60.15.06', '70.14.06', '60.17.05', '70.15.06', '70.17.04',
		         '90.12.05', '70.14.04', '60.15.03', '70.18.04', '70.20.04', '70.20.05',
		         '90.21.02', '90.17.05', '90.19.05', '90.03.07', '90.21.06', '90.04.07',
		         '90.19.06', '70.21.06', '70.21.01', '90.17.07', '70.22.01', '90.02.05',
		         '90.21.07', '70.18.05', '60.21.05', '40.20.07', '40.20.02', '90.21.05',
		         '70.20.01', '90.03.07', '80.21.04', '90.17.04', '40.16.03', '90.17.05',
		         '40.18.07', '70.17.03', '40.16.04', '30.16.03', '60.14.01', '30.05.03',
		         '20.02.04', '30.13.02', '10.17.05', '30.06.04', '80.03.02', '10.21.04',
		         '10.17.06', '10.17.06', '30.02.06', '10.15.06', '30.13.07', '20.01.06',
		         '10.15.05', '30.13.06', '30.16.07', '10.17.04', '30.16.05', '30.19.03',
		         '70.23.01', '05.14.03', '30.13.04', '10.15.03', '05.14.04', '10.15.04',
		         '30.16.06', '10.17.03', '80.03.02', '70.04.04', '80.06.03', '80.05.04',
		         '80.06.03', '90.04.03', '90.04.02', '90.05.05', '90.06.04', '90.04.05',
		         '90.06.05', '90.07.06', '90.08.06', '30.08.07', '20.03.07', '70.04.05',
		         '90.05.02', '90.06.01', '90.04.04', '00.21.01', '10.15.02', '00.19.01'		         
		         ],
		colors: [	
		         '#CCC189', '#CEB487', '#D0B172', '#F2AD00', '#E4A700', '#C79600', 
		         '#D99300', '#E69400', '#AF8552', '#D8BA2E', '#E5DFCC', '#DFCEA1', 
		         '#f8e8c0', '#ECEA41', '#F6B256', '#f8d840', '#A6937A', '#F2C000', 
		         '#B89650', '#a79d68', '#F7A11F', '#EBA557', '#D97604', '#BB4926', 
		         '#C13524', '#E8540D', '#F46F29', '#DB5316', '#D6654E', '#A02725', 
		         '#760d1e', '#6C1B2A', '#581E29', '#430a10', '#6D312B', '#901820', 
		         '#C68872', '#CF7178', '#E3A0AC', '#AB392D', '#CC515E', '#CA3F51', 
		         '#BF111B', '#B01D42', '#A7323E', '#865D86', '#903F51', '#CA5B91', 
		         '#69193B', '#7858a0', '#912D76', '#48243E', '#853D7C', '#9D8593', 
		         '#2F4A70', '#0E4665', '#162E7A', '#193058', '#1A1D2A', '#38618D',
		         '#2D3944', '#245878', '#00427E', '#1A2740', '#2782BB', '#202E53', 
		         '#657A9A', '#0070B5', '#004C91', '#138992', '#005688', '#00747C', 
		         '#28275A', '#486591', '#6391B0', '#327663', '#266D3B', '#276230', 
		         '#4E553D', '#004547', '#0E4438', '#3B3D33', '#2B3626', '#302F22', 
		         '#213529', '#426E38', '#68835F', '#293A37', '#76785B', '#443F31', 
		         '#383B34', '#00654F', '#4D8542', '#4B9B3E', '#b0d080', '#394937', 
		         '#87A180', '#3C372A', '#56713D', '#005C54', '#77BBBD', '#2E554B', 
		         '#00855A', '#3F8885', '#75ADB1', '#798790', '#8D969F', '#837C67', 
		         '#79796C', '#9A9A9F', '#6D7170', '#766A5D', '#756444', '#585E55', 
		         '#565957', '#575E62', '#585346', '#4C5057', '#2E3236', '#464644', 
		         '#7E8379', '#484B52', '#354044', '#919089', '#5B686F', '#7A8476', 
		         '#928E75', '#C4CACA', '#7D8083', '#B0B3AF', '#9AA0A7', '#92989A', 
		         '#505455', '#BAB9B0', '#8B7045', '#9C6935', '#774C3B', '#825333', 
		         '#904E3B', '#6B442A', '#725230', '#5B3927', '#64312A', '#49372A', 
		         '#5A2E2A', '#4F3128', '#45302B', '#3B3332', '#1E1A1A', '#A45C32', 
		         '#7A5741', '#887060', '#4F3B2B', '#EEE9DA', '#DADBD5', '#F8F9FB'
		         ]
	});
	
}
/**

 */
function initializeModules() {

	
	initializeCategory();
	initializeColorInfo();
	
	initializeGalleryScroller();
	initializeSlider();
	
	listPositionFeedback($("#ProductList"), showPositionInProductList);
	colorListPositionFeedback($("#ColorSelector"), showPositionInColor);
}

function initializeCategory() {
	showTransparent();
	$('#showPanel').click(function(){
		if($('#transparent').css('display')== 'none'){
			showTransparent();
		}else{
			hideTransparent();
		}
	});
	
	$('#OverlayGallery').mouseleave(function(){
		hideTransparent();
	});
	
	$('#Content').click(function(){
		hideTransparent();
	});
}

function showTransparent() {
	$('#transparent').slideDown('slow');
	$('#transparent_background').slideDown('slow');
	$('#showPanel').removeClass('showPanelUp').addClass('showPanelDown');
}

function hideTransparent() {
	$('#transparent').slideUp('slow');
    $('#transparent_background').slideUp('slow');
    $('#showPanel').removeClass('showPanelDown').addClass('showPanelUp');
}

function initializeColorInfo() {
	$('.txtColorInfo').unbind('click');
	$('.txtColorInfo').click(function(){
		if($('#OverlayColorInfo').css('display')== 'none'){
			showColorInfo();
		}else{
			hideColorInfo();
		}
	});
}

function showColorInfo() {
	$('#OverlayColorInfo').fadeIn(function(){
		$('#Content').unbind('click', hideColorInfo);
		$('#Content').click(hideColorInfo);
		
		$('.closeColorInfo').unbind('click', hideColorInfo);
		$('.closeColorInfo').click(hideColorInfo);
	});
}

function hideColorInfo() {
	$('#Content').unbind('click', hideColorInfo);
	$('#OverlayColorInfo').fadeOut();
}

function selectTab(tabName) {

	var tabs={
		"product": ["#ProductSelectionBox", "#selectProduct"],
		"color": ["#ColorBox", "#selectColor"],
		"favourit": ["#FavoritesBox", "#selectFavourit"]
	}
	
	if(tabs[tabName]==null) {
		alert("unknown tab '"+tabName+"'");
		return;
	}
	
	for(name in tabs) {
		if(name==tabName) {
			$(tabs[name][0]).show(); // content
			$(tabs[name][1]).parent('li').addClass('selected');
		} else {
			$(tabs[name][0]).hide();
			$(tabs[name][1]).parent('li').removeClass('selected');
		}
	}
}

/**

 */
function isCentered() {
    return false;
}

/**

 */
function shapeSelected(sceneObjectIndex) {
	selectSceneObject(sceneObjectIndex);
}

/**

 */
function createCarouselScroller(element, fwdElement, bwdElement, options) {
	var scroller=new Scroller(element, options);
	scroller.start();
	
	var needScroller=element[0].scrollHeight>element.height();
	if(needScroller)
	{
		fwdElement.show();
		bwdElement.show();
	
		var fwd=new Scroller(element, options);
		fwd.listener=fwdElement;
		fwd.start();
		
		var bwd=new Scroller(element, options);
		bwd.listener=bwdElement;
		bwd.start();
		

		scroller.super_stop=scroller.stop;
		scroller.stop = function() {
			this.super_stop();
			fwd.stop();
			bwd.stop();
		}
	}
	else {
		fwdElement.hide();
		bwdElement.hide();
	}
	
	return scroller;
}

var galleryScroller=null;
function initializeGalleryScroller() {
	if(galleryScroller!=null) galleryScroller.stop();
	
	var carousel=$("#carousel");
	galleryScroller=createCarouselScroller(carousel, $("#scrollGalleryFwd"), $("#scrollGalleryBack"), {
		area: 30,
		velocity: 0.2,
		attachEvent: "mouseenter",
		detachEvent: "mouseleave"
	});
	
	Scroller.setListSize(carousel);
	listPositionFeedback(carousel, showPositionInGallery);
	listHoverFadein(carousel);
}

function listPositionFeedback(element, callback) {
	var list=$('ul:last', element);
	var li=list.children('li');

	var self = this;
	if (li.size() > 0) {
		var i=0;
		var size=li.size();
		li.each(function() {
			i++;
			this.listPosition=i+"/"+size;
			$(this).mouseenter(callback);
		});

		callback.call(li.get(0));
	}
}

function colorListPositionFeedback(element, callback) {
	var list=$('div', element);

	var self = this;
	if (list.size() > 0) {
		var i=0;
		var size=list.size();
		list.each(function() {
			i++;
			this.listPosition=i+"/"+size;
			$(this).mouseenter(callback);
		});

		callback.call(list.get(0));
	}
}

function showPositionInGallery() {
	$('.showPositionGallery').text(this.listPosition);
}

function showPositionInProductList() {
	$('.showPositionProductList').text(this.listPosition);
}

function showPositionInColor() {
	$('.showPositionColor').text(this.listPosition);
}

function listHoverFadein(element, callback) {
	var span=$('li>a>span', element);

	span.hover(
		function() {
			$(this).stop(true).fadeTo('fast', 1);
		},
		function(){
			$(this).fadeTo('fast', 0.8);
		}
	);
}

function initializeSlider() {
	$("#slider").slider({
		animate: true,
		range: true,
		min: 0,
		max: 8,
		values: [0, 8],
		start: function(event, ui) {





			var this_slider=$(this).data("slider");
			this_slider.moveBothHandles=!$(event.originalEvent.target).is(".ui-slider-handle");
			

			if(!this_slider.super_slide) {
				this_slider.super_slide=this_slider._slide;
				
				this_slider._slide=function(event, index, newVal) {
					if(!this.moveBothHandles) {
						this.super_slide(event, index, newVal);
						return;
					}
					var position = { x: event.pageX, y: event.pageY };

					newVal=this._leftValueFromMouse(position);
					this.values([newVal, newVal+this.options.step]);
				};


				this_slider._leftValueFromMouse=function(position) {
					var pixelTotal, pixelMouse;
					if ('horizontal' == this.orientation) {
						pixelTotal = this.elementSize.width;
						pixelMouse = position.x - this.elementOffset.left - (this._clickOffset ? this._clickOffset.left : 0);
					} else {
						pixelTotal = this.elementSize.height;
						pixelMouse = position.y - this.elementOffset.top - (this._clickOffset ? this._clickOffset.top : 0);
					}
	
					var percentMouse = (pixelMouse / pixelTotal);
					if (percentMouse > 1) percentMouse = 1;
					if (percentMouse < 0) percentMouse = 0;
					if ('vertical' == this.orientation)
						percentMouse = 1 - percentMouse;
	
					var valueTotal = this._valueMax() - this._valueMin(),
						valueMouse = percentMouse * valueTotal,
						valueMouseModStep = valueMouse % this.options.step,
						normValue = this._valueMin() + valueMouse - valueMouseModStep;
	


					if(normValue>=this._valueMax())
						normValue -=this.options.step;
	


					return parseFloat(normValue.toFixed(5));
				};
			}
		},
		slide: function(event, ui) {

			if(ui.values[0]==ui.values[1]) return false;
			return true;
		},
		change: function(event, ui) {
			var this_slider=$(this).data("slider");
			if(this_slider.noPropagation==true || this_slider.noPropagation==ui.handle) return;
			

			$(".ui-slider-range", this).queue(function () {
				onIntensitySlider(ui.values[0], ui.values[1]);
				$(this).dequeue();
			});
		}
	});
	

	$("#slider .ui-slider-handle").each(function(i) { $(this).addClass("handle"+i); });
	
	refreshSliderValues();
	
	$("#resetButton>a").click(function() {
		var s=$("#slider");
		setSliderValues(s, [s.slider("option", "min"), s.slider("option", "max")], true, false);
	});
}

/**

 */
function refreshSliderValues(animated) {
	var s=$("#slider");
	
	var intensity=$("#intensity>li");
	setSliderValues(s, [(intensity.length>0 ? parseInt(intensity[0].innerHTML) : s.slider("option", "min")),
			(intensity.length>1 ? parseInt(intensity[1].innerHTML) : s.slider("option", "max"))], animated, true);
}

/**






 */
function setSliderValues(s, newValues, animated, noPropagation) {



	var this_slider=$(s).data("slider");
	
	var old_animateOff=this_slider._animateOff;
	if(!animated) this_slider._animateOff=!animated;
	
	var old_np=this_slider.noPropagation;
	if(noPropagation) this_slider.noPropagation=noPropagation;
	else this_slider.noPropagation=this_slider.handles[0];
	
	s.slider("values", newValues);
	
	this_slider._animateOff=old_animateOff
	this_slider.noPropagation=old_np;
}

