/**
 * Utils and helper methods specific to eos3 templates
 * @namespace EOS
 * @author Beat Stadler, Namics AG
 */
var EOS = {
	// init
	init: function() {
		// teaser with scrolling
		EOS.scroller.init();

		// subnav flyout
		EOS.subnav.init();

		// search focus
		EOS.search.init()

		// button hover
		EOS.form.btn.init();

		// mapDynamic
		EOS.mapDynamic.init();

		// team
		EOS.team.init();
	},

	// teaser scroller
	scroller: {
		CONTENTWIDTH: 960,// teaser width
		DEFAULTSLOT: 1,// default slot
		currentSlot: this.DEFAULTSLOT,
		maxSlot: function() {// dynamic max slots
			var _l = $("#scroller").children().length;
			$("#scroller").css("width",_l*this.CONTENTWIDTH);
			return _l;
		},
		x: function() {// dynamic position for content
			var _x = this.CONTENTWIDTH - (this.currentSlot * this.CONTENTWIDTH);
			return _x;
		},
		prev: function() {
			if (this.currentSlot > 1) {
				this.currentSlot = this.currentSlot - 1;
			} else {
				this.currentSlot = this.maxSlot();
			}
			var _w = this.x();
			$("#scroller").stop().animate({left: _w + "px"},500,this.processHighlighting());
		},
		next: function() {
			if (this.currentSlot < this.maxSlot()) {
				this.currentSlot = this.currentSlot + 1;
			} else {
				// when auto cycling start with first after last
				if (this.cycle.running) {
					this.currentSlot = 1;
				}
			}

			var _w = this.x();
			$("#scroller").stop().animate({left: _w + "px"},500,this.processHighlighting());
		},
		jumpTo: function(target) {
			var _t = isFinite(parseInt(target)) ? parseInt(target) : this.DEFAULTSLOT;
			this.currentSlot = _t;
			var _w = this.x();
			$("#scroller").stop().animate({left: _w + "px"},100,this.processHighlighting());
		},
		cycle: {
			running: false,
			auto: true,
			interval: null,
			speed: 9000,

			// cycle step, called in intervalls when set to auto
			go: function() {
				if (EOS.scroller.cycle.auto) {
					EOS.scroller.cycle.interval = setInterval(
						function() {
							EOS.scroller.next();
						},
						EOS.scroller.cycle.speed
					);
					EOS.scroller.cycle.running = true;
				}
			}
		},
		processHighlighting: function() {
			$("#scroller").next().find(".jump").each(function(j) {
				if (EOS.scroller.currentSlot == j+1) {
					$(this).addClass("act");
				} else {
					$(this).removeClass("act");
				}
			});
		},
		init: function() {
			this.maxSlot();
			this.jumpTo(this.currentSlot);
			this.cycle.go();

			// click handler: scroll left
			$(".teaser_ctrl").delegate(".prev","click",function(e) {
				e.preventDefault();
				clearInterval(EOS.scroller.cycle.interval);
				EOS.scroller.prev();
			});

			// click handler: scroll right
			$(".teaser_ctrl").delegate(".next","click",function(e) {
				e.preventDefault();
				clearInterval(EOS.scroller.cycle.interval);
				EOS.scroller.next();
			});

			// click handler: jump to
			$(".teaser_ctrl").delegate(".jump","click",function(e) {
				e.preventDefault();
				clearInterval(EOS.scroller.cycle.interval);
				EOS.scroller.jumpTo($(this).attr("id").substr(3,2));// id=scr10 -> scr | 10
			});
		}
	},

	// topnav flyout subnav
	subnav: {
		// state helpers
		openL: 0,
		activeL: 0,
		hideSubL1: function() {
			var _t = $('.subnav_bg .l1 > li > a');
			_t.removeClass('act').siblings('ul').hide();
			EOS.subnav.openL = 1;
		},
		hideSubL2: function() {
			var _t = $('.subnav_bg .l2 > li > a');
			_t.removeClass('act').siblings('ul').hide();
			EOS.subnav.openL = 2;
		},
		hideSubL3: function() {
			var _t = $('.subnav_bg .l3 > li > a');
			_t.removeClass('act').siblings('ul').hide();
			EOS.subnav.openL = 3;
		},

		// time helpers
		timeout: 300,
		hidetimer: 0,
		timer: function(level) {
			switch(level) {
				case 1:
					EOS.subnav.hidetimer = window.setTimeout(EOS.subnav.hideSubL1, EOS.subnav.timeout);
				break;

				case 2:
					EOS.subnav.hidetimer = window.setTimeout(EOS.subnav.hideSubL2, EOS.subnav.timeout);
				break;

				case 3:
					EOS.subnav.hidetimer = window.setTimeout(EOS.subnav.hideSubL3, EOS.subnav.timeout);
				break;
			}
		},
		stopTimer: function() {
			if (EOS.subnav.hidetimer) {
				window.clearTimeout(EOS.subnav.hidetimer);
				EOS.subnav.hidetimer = null;
			}
		},

		// init
		init: function() {
			// topnav hover
			if ($.browser.msie && $.browser.version == 7) {
				// ie7 workaround for messed up event target onmouseleave
				// click toggle instead of hover
				$('.topnav .top').toggle(
					function(e) {
						var _t = $(this);

						// trigger any open subnavs
						EOS.subnav.hideSubL3();
						EOS.subnav.hideSubL2();
						EOS.subnav.hideSubL1();

						// remove any hov states
						_t.siblings().removeClass('hov');

						// avoid embed overlay, browser specific
						$('#scroller,.teaser_ctrl').hide();

						$(this).addClass('hov');

						EOS.subnav.openL = 1;
						EOS.subnav.activeL = 1;
					}, function(e){
						// do not close topnav but follow href if subnav link is event target
						if ($(e.target).is('a')) {
							window.location = $(e.target).attr('href');
						}
						var _t = $(this);
						if (_t.not('.hov')) {
							_t.siblings().removeClass('hov');
						}
						_t.removeClass('hov');

						// restore embed
						$('#scroller,.teaser_ctrl').show();
					}
				);
			} else {
				$('.topnav .top').hover(
					function(e) {
						// trigger any open subnavs
						EOS.subnav.hideSubL3();
						EOS.subnav.hideSubL2();
						EOS.subnav.hideSubL1();

						// avoid embed overlay, browser specific
						if ($.browser.msie || $.browser.webkit) {
							$('#scroller iframe').hide();
						} else {
							$('#scroller iframe').css("z-index",-1);
						}

						$(this).addClass('hov');

						EOS.subnav.openL = 1;
						EOS.subnav.activeL = 1;
					}, function(e){
						$(this).removeClass('hov');

						// restore embed
						if ($.browser.msie || $.browser.webkit) {
							$('#scroller iframe').show();
						} else {
							$('#scroller iframe').css("z-index",1);
						}
					}
				);
			}

			// level1 hover
			$('.subnav_bg .l1 > li > a').each(function(i,el) {
				var _t = $(this);
				_t.hover(
					function(e) {
						if (e.target == this) {
							_t.siblings('ul').hide();
							_t.siblings('a').removeClass('act');
							if (EOS.subnav.openL <= 2) {
								EOS.subnav.stopTimer();
							}
							if (EOS.subnav.openL >= 1) {
								_t.next('ul').show();
							} else {
								_t.next('ul').hide();
							}
							EOS.subnav.openL = 2;
							EOS.subnav.activeL = 1;
							_t.addClass('act');
						}
					}, function(e) {
						if (e.target == this) {
							EOS.subnav.timer(1);
						}
					}
				)
			});

			// level2 hover
			$('.subnav_bg .l2 > li > a').each(function(i,el) {
				var _t = $(this);
				_t.hover(
					function(e) {
						if (e.target == this) {
							_t.siblings('ul').hide();
							_t.siblings('a').removeClass('act');
							if (EOS.subnav.openL <= 3) {
								EOS.subnav.stopTimer();
							}
							if (EOS.subnav.openL >= 2) {
								_t.next('ul').show();
							} else {
								_t.next('ul').hide();
							}
							EOS.subnav.openL = 3;
							EOS.subnav.activeL = 2;
							_t.addClass('act');
						}
					}, function(e) {
						if (e.target == this) {
							EOS.subnav.timer(2);
						}
					}
				)
			});

			// level3 hover
			$('.subnav_bg .l3 > li > a').each(function(i,el) {
				var _t = $(this);
				_t.hover(
					function(e) {
						if (e.target == this) {
							_t.siblings('ul').hide();
							_t.siblings('a').removeClass('act');
							if (EOS.subnav.openL <= 4) {
								EOS.subnav.stopTimer();
							}
							if (EOS.subnav.openL >= 3) {
								_t.next('ul').show();
							} else {
								_t.next('ul').hide();
							}
							EOS.subnav.openL = 4;
							EOS.subnav.activeL = 3;
							_t.addClass('act');
						}
					}, function(e) {
						if (e.target == this) {
							EOS.subnav.timer(3);
						}
					}
				)
			});

			// level4 hover
			$('.subnav_bg .l4 > li > a').each(function(i,el) {
				var _t = $(this);
				_t.hover(
					function(e) {
						if (e.target == this) {
							_t.siblings('ul').hide();
							if (EOS.subnav.openL <= 4) {
								EOS.subnav.stopTimer();
							}
							if (EOS.subnav.openL == 4) {
								_t.next('ul').show();
							} else {
								_t.next('ul').hide();
							}
						}
					}, function(e) {
					}
				)
			});
		}
	},

	// search focus
	search: {
		// init
		init: function() {
			$('.search').focusin(function() {
				$(this).addClass('act');
			});
			$('.search').focusout(function() {
				$(this).removeClass('act');
			});
		}
	},

	// form specific
	form: {
		// buttons
		btn: {
			// init
			init: function() {
				$('.ct_btn').hover(
					function() {
						$(this).addClass('act');
					},
					function() {
						$(this).removeClass('act');
					}
				);
			}
		}
	},

	// dynamics google map
	mapDynamic: {
		// gmap placeholder
		m: null,

		// properties placeholder (set inpage)
		address: '',
		zoom: 12,
		infowindowHTML: '',
		imgSrc: '',
		marker: null,
		infowindow: null,

		// init
		init: function() {
			// moved to map template
		}
	},

	// team cards
	team: {
		init: function() {
			// show desc, when images loaded exchange via fade
			$('.team_vis img').load(function() {
				var _vis = $(this).parent();
				var _desc = _vis.siblings('.team_desc');
				// leave desc for empty visual
				if ($(this).attr('src').length > 0) {
					_desc.fadeOut(function() {
						_vis.fadeIn();
					});
				}
			}).each(function() {
				// trigger needed to kick in for cached images
				if (this.complete) {
					$(this).trigger("load");
				};
			});

			// hover handler: switch desc/img
			$('.ct_team').delegate('.team_tog','hover', function(e) {
				var _vis = $('.team_vis',this);
				// stop if visual empty
				if (_vis.children('img').attr('src').length > 0) {
					var _desc = $('.team_desc',this);
					if (e.type === 'mouseenter') {
						_vis.hide();
						_desc.show();
					} else {
						_desc.hide();
						_vis.show();
					}
				} else {
					return;
				}
			});
		}
	},

	// array shuffle
	shuffle: function(array) {
		var j, tempi, tempj, i = array.length;
		if (i == 0) {
			return false;
		}
		while (--i) {
			j = Math.floor( Math.random() * ( i + 1 ) );
			tempi = array[i];
			tempj = array[j];
			array[i] = tempj;
			array[j] = tempi;
		}
	}
};
