// page init
function initNavFix() {
	
	new touchNav({
		navBlock: 'nav'
		/*navBlock: 'list'*/
	});
}

if (window.addEventListener) window.addEventListener("load", initNavFix, false);
else if (window.attachEvent) window.attachEvent("onload", initNavFix);

// navigation accesibility module
function touchNav(options) {
	this.options = {
		mobileReg: /(ipad|iphone|ipod|android|blackberry|iemobile)/gi,
		hoverClass: 'hover',
		followLink: false,
		menuItems: 'li',
		menuOpener: 'a',
		menuDrop: 'div',
		navBlock: null
	}
	for(var p in options) {
		this.options[p] = options[p];
	}
	this.init();
}
touchNav.prototype = {
	init: function() {
		
		this.isMobile = (this.options.mobileReg).test(navigator.userAgent);
		if(typeof this.options.navBlock === 'string') {
			this.menu = document.getElementById(this.options.navBlock);
		} else if(typeof this.options.navBlock === 'object') {
			this.menu = this.options.navBlock;
		}
		
		if(this.menu) {
			this.getElements();
			this.addEvents();
		}
		
	},
	getElements: function() {
		this.menuItems = this.menu.getElementsByTagName(this.options.menuItems);
	},
	hideActiveDropdown: function() { //mobile only
		if(this.activeParent) {
			for(var i = 0; i < this.menuItems.length; i++) {
				this.removeClass(this.menuItems[i], this.options.hoverClass);
			}
			this.activeParent = null;
		}
	},
	getOpener: function(obj) { //mobile only
		for(var i = 0; i < obj.childNodes.length; i++) {
			if(obj.childNodes[i].tagName && obj.childNodes[i].tagName.toLowerCase() == this.options.menuOpener.toLowerCase()) {
				return obj.childNodes[i];
			}
		}
		return false;
	},
	getDrop: function(obj) { //mobile only
		for(var i = 0; i < obj.childNodes.length; i++) {
			if(obj.childNodes[i].tagName && obj.childNodes[i].tagName.toLowerCase() == this.options.menuDrop.toLowerCase()) {
				return obj.childNodes[i];
			}
		}
		return false;
	},
	addEvents: function() {
		// mobile event handlers
		if(this.isMobile) {
			for(var i = 0; i < this.menuItems.length; i++) {
				this.menuItems[i].touchNav = this;
				if(this.getDrop(this.menuItems[i])) {
					this.addHandler(this.getOpener(this.menuItems[i]), 'click', this.bind(this.clickHandler,this.menuItems[i]));
				}
			}
			this.addHandler(document.body, 'click', this.bind(this.outsideHandler, this));
			this.addHandler(document.body, 'touchstart', this.bind(this.outsideHandler, this));
			
			//show the active element
			var _active = $('#nav li a.active').parent().get();
			if(_active[0])  this.touchNav.addClass(_active[0], this.touchNav.options.hoverClass);
		
		}
		// desktop event handlers
		else {
			for(var i = 0; i < this.menuItems.length; i++){
				this.menuItems[i].touchNav = this;
				this.addHandler(this.menuItems[i], 'mouseover', this.mouseoverHandler);
				this.addHandler(this.menuItems[i], 'mouseout', this.mouseoutHandler);
			}
			
			//show the active element
			var _active = $('#nav li a.active').parent().get();
			if(_active[0]) this.addClass(_active[0], this.options.hoverClass);
		}
	},
	outsideHandler: function(e) { //mobile only
		var childFlag = false;
		if(this.activeParent) {
			this.outsideTarget = e.target || e.currentTarget || e.srcElement;
			while (this.outsideTarget.parentNode) {
				if(this.activeParent == this.outsideTarget) {
					childFlag = true;
					break;
				}
				this.outsideTarget = this.outsideTarget.parentNode;
			}
			if(!childFlag) {
				this.hideActiveDropdown();
			}
		}
	},
	mouseoverHandler: function() {
		this.touchNav.addClass(this, this.touchNav.options.hoverClass);
		
		//deactivate the active element
		var _active = $('#nav li a.active').parent().get();
		if(_active[0]) this.touchNav.removeClass(_active[0], this.touchNav.options.hoverClass);
	},
	mouseoutHandler: function() {
		
		this.touchNav.removeClass(this, this.touchNav.options.hoverClass);
		
		//activate the active element
		var _active = $('#nav li a.active').parent().get();
		if(_active[0]) this.touchNav.addClass(_active[0], this.touchNav.options.hoverClass);
		
	},
	clickHandler: function(e) { //mobile only
		// get current dropdown
		var tNav = this.touchNav;
		tNav.currentElement = e.currentTarget || e.srcElement;
		tNav.currentParent = tNav.currentElement.parentNode;

		// hide previous drop (if exists)
		if(tNav.activeParent && !tNav.isParent(tNav.activeParent, tNav.currentParent) && tNav.currentParent != tNav.activeParent) {
			tNav.hideActiveDropdown();
		}

		// handle current drop
		if(tNav.hasClass(tNav.currentParent, tNav.options.hoverClass)) {
			tNav.removeClass(tNav.currentParent, tNav.options.hoverClass);
			if(tNav.options.followLink) {
				window.location.href = tNav.currentElement.href;
			}
		} else {
			tNav.addClass(tNav.currentParent, tNav.options.hoverClass);
			tNav.activeParent = tNav.currentParent;
			return tNav.preventEvent(e);
		}
	},
	preventEvent: function(e) { //mobile only
		if(!e) e = window.event;
		if(e.preventDefault) e.preventDefault();
		if(e.stopPropagation) e.stopPropagation();
		e.cancelBubble = true;
		return false;
	},
	isParent: function(parent, child) { //mobile only
		while(child.parentNode) {
			if(child.parentNode == parent) {
				return true;
			}
			child = child.parentNode;
		}
		return false;
	},
	addHandler: function(object, event, handler) {
		if (typeof object.addEventListener != 'undefined') object.addEventListener(event, this.bind(handler,object), false);
		else if (typeof object.attachEvent != 'undefined') object.attachEvent('on' + event, this.bind(handler,object));
	},
	removeHandler: function(object, event, handler) {
		if (typeof object.removeEventListener != 'undefined') object.removeEventListener(event, handler, false);
		else if (typeof object.detachEvent != 'undefined') object.detachEvent('on' + event, handler);
	},
	hasClass: function(obj,cname) {
		return (obj.className ? obj.className.match(new RegExp('(\\s|^)'+cname+'(\\s|$)')) : false);
	},
	addClass: function(obj,cname) {
		if (!this.hasClass(obj,cname)) obj.className += " "+cname;
	},
	removeClass: function(obj,cname) {
		if (this.hasClass(obj,cname)) obj.className=obj.className.replace(new RegExp('(\\s|^)'+cname+'(\\s|$)'),' ');
	},
	bind: function(func, scope){
		return function() {
			return func.apply(scope, arguments);
		}
	}
}

var techSlider = {
	
	key: "",
    techState: "normal",
    techIconState: "",
    techSliderType: "",
    resetImage: "",
   
	init: function(key,techSliderType) {
		
		techSlider.key = key;
		techSlider.techSliderType = techSliderType;
		techSlider.resetImage = 'riposa_'+techSlider.key+'_tech.jpg';
		techSlider.resetText = 'techtext-default';
		
		 $('#techfooter-switcher').live({
		  click: function() {
		    techSlider.toggleTechView();
		  }
		});
	
	},
	
    preloadImage: function (img) {
    	
    	jQuery.preLoadImages(img, 'content/'+techSlider.key+'/'+img);
    	
    },
    toggleTechView: function () {
    	
    	if(techSlider.techState=='loading') return; //prevent techIconOver() and  multiple clicks during the slide
	
		var targetechState;
		if(techSlider.techState=='normal'){
			targetechState = 'tech';
			$('#crossfade-images-produkte').showItemSrc('content/'+techSlider.key+'/riposa_'+techSlider.key+'_tech.jpg');
			$('#techfooter-switcher').css('backgroundPosition', '0 0');
		}
		else{
			targetechState = 'normal';
			$('#crossfade-images-produkte').showItemSrc('content/'+techSlider.key+'/riposa_'+techSlider.key+'.jpg');
			$('#techfooter-switcher').css('backgroundPosition', '0 -36px');
		}
		
		techSlider.techState='loading';
		
		//slide up/down tech
		$('#techfooter').slideToggle('slow', function() {
	   		techSlider.techState = (targetechState=='normal')?'normal':'tech';
	 	 });
    },
	showTechView: function () { //show the view without animation
		
		techSlider.techState='tech';
		$("#techfooter").show();
	},
	techIconOver: function(techIcon){
		
		//prevent multiple fade in on the same techIcon
		if(techSlider.techIconState == techIcon) return;
		techSlider.techIconState = techIcon;
		
		//prevent mouse over effect on loading
		if(techSlider.techState=='loading') return;
		
		$("#debug").html('riposa_'+techSlider.key+'_'+techIcon+'.jpg');
		
		//set new image
		//alert('content/'+techSlider.key+'/riposa_'+techSlider.key+'_'+techIcon+'.jpg');
		$('#crossfade-images-produkte').showItemSrc('content/'+techSlider.key+'/riposa_'+techSlider.key+'_'+techIcon+'.jpg');
	
		//set new text
		$("#techfooter #content").html($("#techtext-"+techIcon).html());
		
		//set new icon next to the text
		if(techSlider.techSliderType == 'rueckenmatratzen' || techSlider.techSliderType == 'system'){
			$("#detail-tech-icon").css({'background-image' : 'url(img/tech_ico_'+techIcon+'.gif)'});
		}
		
	},
	techIconOut: function(){
		
		if(techSlider.techIconState=='') return; //techIconOut is already executing
		
		techSlider.techIconState = ''; //reset the techSlider.techIconState and wait..
		
		setTimeout( function(){techSlider.techIconReset()}, 1000);
		 
	},
	techIconReset: function(){
		
		if(techSlider.techIconState=='' && techSlider.techState=='tech'){ //if the techSlider.techIconState has not been set by a new mouseover, reset to initial tech view:
		  
	  		//reset image
	  		$('#crossfade-images-produkte').showItemSrc('content/'+techSlider.key+'/'+techSlider.resetImage);

			//reset text
			$("#techfooter #content").html($("#"+techSlider.resetText).html());
			
			//remove icon next to the text
			if(techSlider.techSliderType == 'rueckenmatratzen' || techSlider.techSliderType == 'system'){
				$("#detail-tech-icon").css({'background-image' : 'url(img/blank.gif)'});
			}
	  	}
	},
	setResetImage: function(resetImage){
		techSlider.resetImage = resetImage;
	},
	setResetText: function(resetText){
		techSlider.resetText = resetText;
	}
	
};


(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)


