if(typeof Disney === 'undefined') {
	Disney = {};
}

(function($){
	Disney.CP = function(){
		this.currentIndex = 0;
		this.modal = new CP.utils.Modal({
			showClose: true,
			contentCloseDelegate: '.modal-close',
			onOpenComplete: function() {},
			onCloseComplete: function() {},
			onCloseStart: function() {},
			onOpenStart: function() {}
		});
	};
	
	
	Disney.CP.prototype.showRules = function(lang) {
		var self = this;
		$('#modal-content').load('rules-overlay.html #rules-wrap', function(){
			//open modal
			self.modal.open('', function(){
				self.initRules();
			}, function(){
				clearInterval(self.rulesInterval);
			});
		});
	};
	
	Disney.CP.prototype.setTimer = function(currentIndex) {
		var self = this;
		self.rulesInterval = setInterval(function(){
			self.currentIndex = (self.currentIndex+1) % $("#rules ul li").length;
			self.showRule(self.currentIndex);
		}, 5000);
	};
	
	Disney.CP.prototype.initRules = function(){
		var self = this;
		if ($("#rules").length > 0) {
			$("#rules ul li").mouseover(function(){
				self.currentIndex = $("#rules ul li").index(this);
				self.showRule(self.currentIndex);
			});

			$("#rules ul li").mouseenter(function(){
				clearInterval(self.rulesInterval);
			}).mouseleave(function(){
				self.currentIndex = $("#rules ul li").index(this);
				self.setTimer(self.currentIndex);
			});
			self.setTimer(0);
		}
	};

	Disney.CP.prototype.showRule = function(index) {
		$("#rules ul li").removeClass("active");
		$($("#rules ul li")[index]).addClass("active");
		
		$("#rules-container").html(	$($("#rules ul li")[index]).html());
	};
	
})(window.jQuery);

window.jQuery(document).ready(function(e){
	window.cp = new Disney.CP();
});
