if (!PIVOT) var PIVOT = {};


/* DROPDOWN NAV */

PIVOT.NAV = function() {
	
	return {
		
	init: function() {
			
			var me = this;
			var activeMenu;
			
			$("#nav>li").hover(
				function() {
					if (me.activeMenu && me.activeMenu != this) $(me.activeMenu).find("ul").hide();
					$(this).find("ul").stop(true,true).delay(200).slideDown(100);
					me.activeMenu = this;
				},
				function() {
					$(this).find("ul").stop(true,true).delay(500).slideUp(100);
				}
			); 
		}
		
	}
}


/* IMAGE CAROUSEL */

PIVOT.CAROUSEL = function() {
	
	var config = {};
	var cid;
	var rotate_interval;
	
	return {
		
		init: function(o) {
			
			config = o;
			cid = config.containerId;
			
			var me = this;
			
			//Set window width
			$(cid+" .carousel-images").width($(cid+" .carousel-window").width() * $(cid+" .carousel-images > a").size());
		
			//On image hover
			$(cid+" .carousel-images > a").hover(
				function () {
					clearInterval(rotate_interval);
				},
				function () {
					me.rotateStart();
				}
			);
			
			$(cid+" .carousel-pages > a").hover(
				function () {
					clearInterval(rotate_interval);
				},
				function () {
					me.rotateStart();
				}
			);
			
			//On page click
			var me = this;
			$(cid+" .carousel-pages > a").click(function(e) {
				clearInterval(rotate_interval);
				me.rotate($(this));
				e.preventDefault();
			});
			
			// Activate first page link & start rotation
			$(cid+" .carousel-pages > a:first").addClass("active");
			this.rotateStart();
		},
		
		//Paging and Slider Function
		rotate: function(active_page_btn) {
			var imageId = active_page_btn.attr("rel") - 1;
			var image_reelPosition = imageId * ($(cid+" .carousel-window").width());
		
			$(cid+" .carousel-pages a.active").removeClass('active');
			active_page_btn.addClass('active');
			
			$(cid+" .carousel-images").animate({ left: -image_reelPosition}, 400 );
		},
		
		//Rotation and Timer
		rotateStart: function() {
			var me = this;
			rotate_interval = setInterval(function() {me.triggerRotate();}, config.delay*1000);
		},
		
		triggerRotate: function() {
			var active_page_btn = $(cid+' .carousel-pages a.active').next();
			if (active_page_btn.length == 0) {
				active_page_btn = $(cid+' .carousel-pages a:first');
			}
			this.rotate(active_page_btn);
		}
		
	}
	
}


/* TWITTER FEED */

PIVOT.TWEETS = function() {

	var config = {};

	return {
		
		init: function(cfg) {
			config = cfg;
			var me = this;
			$.ajax({
				url: config.src,
				dataType: "json",
				success: function(o) { me.twitterDataHandler(o) }
			});
		},
		
		twitterDataHandler: function(o) {
			var tweetData = [];
			var totalTweets = o.length;
			if (config.numTweets > totalTweets) config.numTweets = totalTweets;
			for (var i=0; i<config.numTweets; i++) {
				var tweet = o[i];
				tweetData.push({text:tweet.text, age:this.parseAge(tweet.created_at)});
			}
			this.output(tweetData);
		},
		
		parseAge: function(datetime_string) {
			var datetime_tweet = new Date(datetime_string);
			var datetime_now = new Date(new Date().toUTCString());
			var time_elapsed = datetime_now.getTime() - datetime_tweet.getTime();
			
			var d = Math.floor(time_elapsed / (1000 * 60 * 60 * 24));
			var h = Math.floor(time_elapsed / (1000 * 60 * 60));
			var m = Math.floor(time_elapsed / (1000 * 60));
			
			var age_text = '';
			
			if (m < 2) {
				age_text = "just now";
			} else if (m < 50) {
				age_text = m + " minutes ago";
			} else if (m < 100) {
				age_text = "about an hour ago";
			} else if (h < 20) {
				age_text = h + " hours ago";
			} else if (h < 36 ) {
				age_text = "about a day ago";
			} else if (h >= 36 || d > 1) {
				age_text = d + " days ago";
			}
			
			return age_text;
		},
		
		output: function(tweetData) {
			var tweetHtml = "<ul>";
			for (var i=0; i<tweetData.length; i++) {
				var tweet = tweetData[i];
				tweetHtml+= "<li>" + tweet.text + " <span>" + tweet.age + "</span></li>";
			}
			tweetHtml+= "</ul>";
			// activate links
			var linkRegex = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
			tweetHtml = tweetHtml.replace(linkRegex, ' <a target="_blank" href="$1">$1</a> ');
			var userRegex = /[\@]+([A-Za-z0-9-_]+)/gi;
			tweetHtml = tweetHtml.replace(userRegex, ' <a target="_blank" href="http://twitter.com/$1">@$1</a> ');
			var hashRegex = /(?:^| )[\#]+([A-Za-z0-9-_]+)/gi;
			tweetHtml = tweetHtml.replace(hashRegex, ' <a target="_blank" href="http://twitter.com/#!/search?q=%23$1">#$1</a> ');
			// output to page
			$(config.containerId).html(tweetHtml);
		}
	}
}




/* LIGHTBOX IMAGE GALLERY */

PIVOT.GALLERY = function() {
	
	return {
		
		init: function(targ) {
			
			var fancybox_settings = {
			'transitionIn'	: 'fade',
			'transitionOut'	: 'none',
			'titlePosition' : 'over',
			'overlayColor'	: '#fff',
			'overlayOpacity': '0.9',
			'titlePosition'	: 'inside',
			'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
				return '<div id="fancybox-title-over"><span>' + (currentIndex+1) + '/' + currentArray.length + '</span> ' + title + '</div>';
				}
			}
		
			var prevset = '';
			$(targ).each(function(index) {
				var set = $(this).attr('rel');
				if (set != prevset) {
					$("a[rel="+set+"]").fancybox(fancybox_settings);
				}
				prevset = set;
			});
			
		}
		
	}
	
}



/* IE6 WARNING MESSAGE */

PIVOT.IE6MESSAGE = function() {

	var config = {};

	return {
		
		init: function(cfg) {
			if ($.browser.msie) {
				config = cfg;
				var me = this;
				$.ajax({
					url: config.src,
					dataType: "html",
					success: function(o) { me.ajaxDataHandler(o) }
				});
			}
		},
		
		ajaxDataHandler: function(o) {
			$('<div id="ie6"></div>').insertBefore('#main');
			$('#ie6').html(o);
		}
	}
}

$(document).ready(function () {
	var pv_ie6message = new PIVOT.IE6MESSAGE();
	pv_ie6message.init({src:'/includes/html/ie6.html'});
});



