/**

 */
var superInit=init;

init = function() {
	superInit();	
	
	$('.currentLanguage').click(function() {
		$('#ContentBox').unbind('click');
		
		if($('#otherLanguage').css('display') == 'none') {
			$('#otherLanguage').slideDown();
			$('#ContentBox').click(function(){
				$('#otherLanguage').slideUp();
			});
		}else {
			$('#otherLanguage').slideUp();
		}
	});
	
	$('#selectColor').click(function(){
		selectTab("color");
	});
	
	$('#selectProduct').click(function(){
		selectTab("product");
	});
	

	$('.contact[rel]').overlay({
		expose: {
			color: '#000',
			closeSpeed: 'slow',
			opacity: 0.5
 		},
		closeOnClick: true,
		closeOnEsc: true,
		api: true
	});
	
	$('.favorites').click(function(){
		$('.contact[rel]').overlay().close();
	});
	
	updateDownloadLink();
}
/**

 */
function initHoverAccordion() {
}

/**

 */
function initColorButtons() {
	$('#ColorSelector').addColorPicker({
		clickCallback: function(c) {
			onColorPicker(c);
		},
		colors: [	
		         '#CCC189', '#CEB487', '#D0B172', '#F2AD00', '#E4A700', '#C79600', 
		         '#D99300', '#E69400', '#AF8552', '#D8BA2E', '#E5DFCC', '#DFCEA1', 
		         '#E6D9BD', '#ECEA41', '#F6B256', '#FDDA38', '#A6937A', '#F2C000', 
		         '#B89650', '#A4861A', '#F7A11F', '#EBA557', '#D97604', '#BB4926', 
		         '#C13524', '#E8540D', '#F46F29', '#DB5316', '#D6654E', '#A02725', 
		         '#870A24', '#6C1B2A', '#581E29', '#402226', '#6D312B', '#791F24', 
		         '#C68872', '#CF7178', '#E3A0AC', '#AB392D', '#CC515E', '#CA3F51', 
		         '#BF111B', '#B01D42', '#A7323E', '#865D86', '#903F51', '#CA5B91', 
		         '#69193B', '#7D63A1', '#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', '#B2D8B4', '#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', '#765D4D', '#4F3B2B', '#EEE9DA', '#DADBD5', '#F8F9FB'
		         ]
	});
	
}
/**

 */
function initializeModules() {

	
	initializeCategory();
	
	initializeBigProductImg();
	
	initializeGalleryScroller();
	initializeProductListScrollable();
	initializeSlider();
}

/**

 */
function formatLightboxItem(item) {
	var row='<tr><td>' + item["image"] + '</td>' +
				'<td align="top">' + item["description"] +
				'</td></tr>';
	return row;
}

function initializeCategory() {
	showTransparent();
	$('#showPanel').click(function(){
		if($('#OverlayGallery').css('display')== 'none'){
			showTransparent();
		}else{
			hideTransparent();
		}
	});
	
	$('#GalleryBox').mouseleave(function(){
		hideTransparent();
	});
	
	$('#Content').click(function(){
		hideTransparent();
	});
}

function showTransparent() {
	$('#OverlayGallery').slideDown('slow');
	$('#OverlayGallery_background').slideDown('slow');
    $('#showPanel').css('background-image', 'url(images/fb-arrow_down.gif)');
}

function hideTransparent() {
	$('#OverlayGallery').slideUp('slow');
    $('#OverlayGallery_background').slideUp('slow');
    $('#showPanel').css('background-image', 'url(images/fb-arrow.gif)');
}

function selectTab(tabName) {

	var tabs={
		"product": ["#ProductBox", "#selectProduct"],
		"color": ["#ColorBox", "#selectColor", initializeColorScrollable]
	}
	
	if(tabs[tabName]==null) {
		alert("unknown tab '"+tabName+"'");
		return;
	}
	
	for(name in tabs) {
		if(name==tabName) {
			$(tabs[name][0]).show(); // content
			$(tabs[name][1]).addClass('selected');
			
			var callback=tabs[name].length>2 ? tabs[name][2] : null;
			if(callback) callback();
		} else {
			$(tabs[name][0]).hide(); // content
			$(tabs[name][1]).removeClass('selected');
		}
	}
}

/**

 */
function isCentered() {
    return false;
}

/**

 */
function shapeSelected(sceneObjectIndex) {
	selectSceneObject(sceneObjectIndex);
}

/**

 */
var galleryScroller=null;
function initializeGalleryScroller() {
	if(galleryScroller!=null) galleryScroller.stop();
	
	var carousel=$("#carousel");
	galleryScroller=new Scroller(carousel, {
		area: 50,
		velocity: 0.2,
		attachEvent: "mouseenter",
		detachEvent: "mouseleave"
	});
	galleryScroller.start();
	
	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 showPositionInGallery() {
	$('.showPositionGallery').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 initializeProductListScrollable() {
	var api=$("#ProductList").data("scrollable");

	if(api==null) {


		$("#aazone\\.productListZ").addClass("items");
		
		$("#ProductList").scrollable();
	}
	else {

		api.begin(0);

		$("#productNavi").empty();
	}
	
	adjustToChildrenSize($("#ProductList .items"));
	$("#ProductList").navigator();
}

function adjustToChildrenSize(element, vertical) {
	var list=element;
	var li=list.children();

	var self = this;
	if (li.size() > 0) {
		var wh = 0;
		li.each(function() {
			wh += !vertical ? $(this).outerWidth() : $(this).outerHeight();
		});

		list.css(!vertical ? 'width' : 'height', wh + 'px');
	}
}

function initializeColorScrollable() {




	var api=$("#ColorSelector").data("scrollable");
	if(api!=null) return;
	

	var itemsPerPage=12;
	
	var colorList=$("#ColorSelector");
	var colors=$("div", colorList);
	
	var items=$("<div class=\"items\"/>");
	colorList.append(items);
	var page=$("<div class=\"page\"/>");
	items.append(page);
	
    var added=0;
    colors.each(function(i) {
    	added++;
    	if(added>itemsPerPage) {
    		added=1;
    		page=$("<div class=\"page\"/>");
    		items.append(page);
        }
    	page.append($(this));
    });
	
    adjustToChildrenSize($(".items", colorList));
	colorList.scrollable().navigator();
}

function initializeSlider() {
	$("#slider").slider({
		animate: true,
		range: true,
		min: 0,
		max: 3,
		values: [0, 3],
		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;
}

function openInfosPDF(pdfname) {
	var parameter = "width=865px,scrollbars=yes,menubar=no,toolbar=no,status=no,resizable=no";
	infosPDF = window.open('data/'+pdfname+'.pdf','_blank', parameter);
	infosPDF.document.close();
}

var _ns4 = document.layers ? 1 : 0;

function initializeBigProductImg(){
	$('#ProductList ul>li').mouseenter(function(){
		var imageTitle = $(this).children('a').children('span').attr('rel_name');
		var imageKey = $(this).children('a').children('span').attr('rel_key');
		$('#currentProductDescription2').text(imageTitle);
		$('#currentProductKey2').text(imageKey);
		$('#bigProductImg').css('display', 'block');
		
		$(this).mousemove(updateBigImage);
	});
	$('#ProductList ul>li').mouseleave(function(){
		$(this).unbind("mousemove", updateBigImage);
	});
	
	$('#ProductList ul').mouseleave(function(){
		$('#bigProductImg').css('display', 'none');
	});
}

function updateBigImage(e) {
	if(!e) e = window.event; // Event-Definition fuer IE
	var mouseX = ((_ns4) ? e.pageX : e.clientX);
	var mouseY = ((_ns4) ? e.pageY : e.clientY);
	
	var image = $('#ProductSelectionBox');
	var pos=image.offset();
	


	var doc = document, body = doc.body, docElem = doc.documentElement,
	top  = (self.pageYOffset || jQuery.boxModel && docElem.scrollTop  || body.scrollTop ),
	left = (self.pageXOffset || jQuery.boxModel && docElem.scrollLeft || body.scrollLeft);
	


	var x=mouseX-pos.left+left;
	var y=mouseY-pos.top+top;
	
	var big=$('#bigProductImg');
	var innerHeight;
	if(typeof(window.innerWidth)!='undefined') {

		innerHeight=window.innerHeight;
	} else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {

		innerHeight=document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {

		innerHeight=document.body.clientHeight;
	}
	
	var remainingHeight=innerHeight-mouseY;

	if(remainingHeight-10<big.outerHeight()) y=y-big.outerHeight();
	else y+=10;
	
	big.css('top', y-20);
	big.css('left', x+30);
}
