/*
Version 2 Homepage
8-11-11: Had to switch out many tree traversing with jQuery for IE6/7 support.
*/
carousel = function() {
	var position = 0;
	var imgWidth = 1135;
	var dom = document;
	var bgloop;
	var bg;
	var moveBackground = function(indexPosition) {
		updateText(indexPosition);
		var cw = dom.childNodes[1].clientWidth;
		var centerWidth = (cw-imgWidth)/2;
		var positionPadded = ((indexPosition+4)*-1)*imgWidth;
		position = indexPosition;
		autocenter.stop();
		if(centerWidth > 0) {
			var m = positionPadded+centerWidth;
		} else {
			var m = positionPadded;
		}
		$(bg).stop(true).animate({marginLeft:m},1000,'easeInOutCubic',function() {
			autocenter.start();
		});
	}
	var updateText = function(index) {
		var o = $("#carv2 .c-item");
		o.hide();
		$(o[index]).show();
	}
	var setTriggerState = function(index) {
		var a = dom.getElementById("event_triggers").childNodes;
		for(i in a) {
			if(a[i].className == "selected") {
				$(a[i]).attr("class","");
				break;
			}
		}
		// $($ faster than :nth-child fot this case
		$($("#event_triggers a")[index]).attr("class","selected");
	}
	var autocenter = {
		start:function() {
			bgloop = setInterval(function() {
				var cw = dom.childNodes[1].clientWidth;
				var centerWidth = (cw-imgWidth)/2;
				var positionPadded = ((position+4)*-1)*imgWidth;
				if(centerWidth > 0) {
					$(bg).css("marginLeft",(centerWidth+positionPadded)+"px");
				} else {
					$(bg).css("marginLeft",(positionPadded)+"px");
				}
			},70);
		},
		stop:function() {
			clearInterval(bgloop);
		}
	}
	var cloneElements = function() {
		$("#bg ul li:nth-child(1)").clone().appendTo("#bg ul");
		$("#bg ul li:nth-child(2)").clone().appendTo("#bg ul");
		$("#bg ul li:nth-child(3)").clone().appendTo("#bg ul");
	}
	return {
		init:function() {
			// set selected state
			$(dom.getElementById("event_triggers").children[0]).attr('class','selected');
			// set bg width, had to change to jquery due to compatibiltiy issues
			bg = $("#bg ul")[0];
			// style.width not crossbrowser
			$(bg).width((($("#event_triggers a").length*3)*imgWidth)+"px"); // *2 to count for clone
			// more elements
			cloneElements();cloneElements();
			// auto center background
			autocenter.start();
		},
                jump:function(i) {
                     setTriggerState(i);
                     moveBackground(i);
                     updateText(i);
                }
	}
}();
var rotation = function() {
   function rotation() {
      var o = this;
      // def
      this.duration = 5000;
      this.rotate = 0;
      this.index = 0;
      // start carousel UI
      this.carousel = carousel;
      this.carousel.init();
      // events
      $("#event_triggers a").click(function(e) {
         e.preventDefault();
         o.jump($(this).index());
         // restart rotation
         o.stop();
         o.start();
      });
      // begin rotation
      this.start();
   }
   rotation.prototype = {
      stop:function() {
         clearInterval(this.rotate);
      },
      start:function() {
         var o = this;
         this.rotate = setInterval(function() {
            o.next();
         },this.duration);
      },
      jump:function(i) {
         this.index = i;
         this.carousel.jump(i);
      },
      next:function() {
         this.index = (this.index == 2 ? 0 : ++this.index);
         this.jump(this.index);
      }
   }
   return rotation;
}();
$(document).ready(function(){new rotation();});
