var menu = new function() {
	this.slide_speed = 250;
	this.timer_delay = 1000;  //time it takes the main menu to close automatically after the mouse moves away
	
	this.menu_id = "main_menu";
	this.q = new Array();
	this.timer = 0;
	this.items = ['item1','item2','item3','item4','item5','item6'];
	
	this.setup = function() {
		for(var i=0; i<this.items.length; i++) {
				var item_id = this.items[i];
				
				jQuery('#' + this.menu_id + " li." + item_id)
					.data('item_id', item_id)
					.hover(
								function() { 
									menu.queue(jQuery(this).data('item_id'), 0); 
								}, 
								function() { 
									menu.queue(jQuery(this).data('item_id'), 1); 
								} 
							);	
		}
	};
	
	this.restart = function() {
		clearInterval(this.timer);
		this.timer = setInterval("menu.tick()", this.timer_delay);
	};
	
	this.queue = function(item_id, bHide) {
		if (bHide == 1) {
			var bFound = false;
			for(var j=0; j<this.q.length; j++) {
				if (this.q[j] == item_id) {
					bFound = true;	
					break;
				}
			}
			if (!bFound) {
				this.q.push(item_id);
			}
			this.restart();
		} else {
			if (jQuery("#" + this.menu_id + " li." + item_id).children('ul').length > 0) {
				jQuery("#" + this.menu_id + " li." + item_id).children('ul').slideDown(this.slide_speed);
				jQuery("#" + this.menu_id + " li." + item_id).addClass('dyn_active');
			
				var new_menu = new Array();
				for(var i=0; i<this.q.length; i++) {
					if (this.q[i] != item_id)
						new_menu.push(this.q[i]);
				}
				this.q = new_menu;
				
				this.close_all(item_id);
			}
		}
	};

	this.close = function(item_id) {
		jQuery("#" + this.menu_id  + " li." + item_id).removeClass('dyn_active');
		jQuery("#" + this.menu_id  + " li." + item_id).children('ul').slideUp(this.slide_speed);	
	};


	this.close_all = function(item_id) {
		for( var i=0; i<this.items.length; i++) {
			var cur_item = this.items[i];
			if (cur_item != item_id)
				this.close(cur_item);
		}
	};

	this.tick = function() {
		for(var i=0; i<this.q.length; i++) {
			this.close(this.q[i]);
		}
		this.q = new Array();
	};
	
};

var clock = new function() {
	this.container_selector = '#header';
	this.timer_delay = 1000;
	this.clock_css = 
				{
					color: '#a2a4a7',
					'font-size' : '12px',
					'text-transform' : 'uppercase',
					position: 'absolute',
					right: 0,
					top: 10
				};
				
	this.days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
	this.months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
	
	this.timer = 0;
	this.jq_clock = null;
	
	this.setup = function() {
		this.timer = setInterval("clock.tick()", this.timer_delay);
		this.setup_html();
		this.tick();
	};
	
	this.tick = function() {
		var d = new Date();
		
		var day_of_week = d.getDay();
		var month = d.getMonth();
		var day_of_month = d.getDate();
		var year = d.getFullYear();
		var hour = d.getHours();
		var minute = d.getMinutes();
		var am_pm = "a.m.";
		
		if (hour > 12) {
			hour -= 12;
			am_pm = "p.m.";
		}
		
		var html = this.days[day_of_week] + ", " + this.months[month] + " " + day_of_month + ", " + year + " | " + hour + ":" + this.format_two(minute) + " " + am_pm;
		
		this.jq_clock.html(html);
	};
	
	this.format_two = function(s) {
		s = s + "";
		if (s.length == 1)
			s = "0" + s;
		return s;
	}
	
	this.setup_html = function() {
		this.jq_clock = jQuery('<div />').css(this.clock_css);
		jQuery(this.container_selector).append(this.jq_clock);
	};
};

var image_rotate = new function() {
	this.image_delay = 8000;	//time (ms) between each slide change
	this.transition_time = 500; //time (ms) spent animating between slides
	this.base_dir = '/images/image/image-rotate/';
	this.container_selector = "#image-rotate-img";	

	this.slide_css =
		{
			position: "absolute",
			top: 0,
			right: 0,
			display: "none"
		};

	this.slides = ['img1.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg', 'img5.jpg'];
	this.index = 0; //current index
	this.timer = 0; //main timer	
	
	this.container = null; //jquery object
	this.container_slides = []; //array of jquery slides
	this.preload_arr = [];


	this.setup = function(slides) {
		var intro_played = '1'; //this.cookie_read("intro_played");
		if (intro_played == '1' && location.hash != '#play') {
			this.container = jQuery(this.container_selector);

			this.setupHTML();
			this.start();
			this.preload();
		} else {
			jQuery('#image-rotate').append('<div id="flash-container" style="position: absolute; top: 4px; left: 4px; overflow: hidden; width: 960px; height: 434px; z-index: 10;"><div style="position: absolute; top: -40px; "><div id="image-rotate-swf" ></div></div></div>');
			jQuery('#image-rotate-img').hide();
			jQuery('#image-rotate-message').hide();
		
			swfobject.embedSWF("/images/flash/intro.swf", "image-rotate-swf", "960", "540", "9.0.0", "", {}, {wmode: 'transparent'});
			this.cookie_create('intro_played', '1', 30 * 24 * 60 * 60 * 1000);
			
			this.timer_after_movie = setInterval("image_rotate.after_movie()", 12000);
		}
	};
	
	this.after_movie = function() {
		clearInterval(this.timer_after_movie);
		jQuery("#flash-container").remove();
		jQuery("#image-rotate").css({opacity: 0}).animate({opacity: 1}, 500);
		jQuery('#image-rotate-img').show();
		jQuery('#image-rotate-message').show();
		this.container = jQuery(this.container_selector);
		
		this.setupHTML();
		this.start();
		this.preload();

	};
	
	this.preload = function() {
		for(var i=0; i<this.slides.length; i++) {
			var img = new Image();
			img.src = this.base_dir + this.slides[i];
			this.preload_arr.push(img);
		}
	};


	this.change = function() {	
		var next_id = this.index + 1;
		if (next_id >= this.slides.length)
			next_id = 0;

		var current_slide = this.container_slides[this.index];
		var next_slide = this.container_slides[next_id];
		
		current_slide.css('z-index',10);
		next_slide.css('z-index',9);
		next_slide.show();
		
		current_slide.fadeOut(this.transition_time);
		
		this.index = next_id;
	};
		
	this.start = function() {
		this.timer = setInterval("image_rotate.tick()", this.image_delay);
	};
	
	this.setupHTML = function() {
		this.container.html("");
		
		for(var i=0; i<this.slides.length; i++) {
			var slide = jQuery('<div />').css(this.slide_css).html('<img src="' + this.base_dir + this.slides[i] + '" />');
			this.container_slides.push(slide);
			this.container.append(slide);
		}		
		this.container_slides[0].show();
	};
	
	this.tick = function() {
		this.change();
	};

	/*
	Create a cookie
	Expiration in MS
	*/
	this.cookie_create = function (name, value, expiration) {
		var date = new Date();
		date.setTime(date.getTime() + expiration);
		var expires = "; expires=" + date.toGMTString();

		document.cookie = name + "=" + value + expires + "; path=/";
	}

	/*
	Read a cookie
	*/
	this.cookie_read = function (name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
		}
		return null;
	}

};

function intro_complete() {
	image_rotate.after_movie();
}

jQuery(document).ready(function(){
	image_rotate.setup();								
	clock.setup();								
	menu.setup();								
});

