(function($){


/* FAQ PLUGIN
================================================================ */
	$.fn.faq = function() {
		return this.each(function(){
			var jThis = $(this);

			/* Add 'js' class to list (stylesheets refer to this) */
			jThis.addClass('js');

			/* Toggle questions */
			$('h3.question', jThis).toggle(
				function(){ $(this).next('div.answer').slideDown('slow'); },
				function(){ $(this).next('div.answer').slideUp('slow'); }
			);
		});
	}


/* EXTENDED COPY PLUGIN
================================================================ */
	$.fn.extendText = function() {
		return this.each(function(){
			var jThis = $(this);

			/* Hide extended text */
			jThis.hide();

			var jSpanTag = $('<span class="readmore">  Read more</span>').click(function(){ $(this).hide().parent().next().slideDown('slow'); });
			jThis.prev().append(jSpanTag);

		});
	}


/* ICAMPUS COUNTDOWN
================================================================ */
	$.icampus = function(){

		var	elems = {
			countdown_time : $('#countdown_time'),
			countdown_timer : $('#countdown_timer')
		};

		/* Countdown */
		function countdown(){
			var	curTime = new Date(),
				secs = (icampus.LIVESTARTSECONDS + icampus.TIMEOFFSET) - Math.round(curTime.getTime()/1000),
				calcTime = {
					d : ((Math.floor(secs/86400))%100000).toString(),
					h : ((Math.floor(secs/3600))%24).toString(),
					m : ((Math.floor(secs/60))%60).toString(),
					s : (secs % 60).toString()
				},
				prefixes = ['d','h','m','s'],
				i,
				output = '';
		
			if (secs <= 0) {
				return onServiceStart();
			}

			output = calcTime.d > 0 ? (calcTime.d + ' day') + (calcTime.d !== 1 ? 's ' : ' ') : '';
				output += (calcTime.h + ' hr') + (calcTime.h !== 1 ? 's ' : ' ');
				output += ((calcTime.m.length > 1 ? calcTime.m : ('0' + calcTime.m)) + ' min ');
				output += ((calcTime.s.length > 1 ? calcTime.s : ('0' + calcTime.s)) + ' sec');
		
			elems.countdown_timer.html(output);
		
			setTimeout("$.icampus.countdown()", 1000);
		}

		/* onServiceStart */
		function onServiceStart(){
			var	localTime = Math.round(new Date().getTime()/1000),
				secs = (icampus.LIVESTARTSECONDS + icampus.TIMEOFFSET) - localTime,
				secsLapsed = ((localTime + icampus.TIMEOFFSET) - icampus.LIVESTARTSECONDS),
				secsRemaining = icampus.LIVEDURATION - secsLapsed;

			setTimeout("$.icampus.onServiceEnd()", secsRemaining*1000);

			elems.countdown_timer.html("Join Us Now");
		}

		/* onServiceEnd */
		function onServiceEnd(){

			/* Get new icampus data */
			$.getJSON('/ajax/icampus-next', function(data, textStatus){

				var	weekdays = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],
					newTime = new Date(data.LIVESTART),
					prettyTime = weekdays[newTime.getDay()]
						+ ' '
						+ (newTime.getHours() > 12 ? (newTime.getHours()-12).toString() : newTime.getHours().toString())
						+ (newTime.getMinutes() > 0 ? (':' + newTime.getMinutes()) : '')
						+ ((newTime.getHours() < 12) ? 'am' : 'pm');

				/* Update icampus data */
				$.extend(icampus, data);

				/* Change the service time */
				elems.countdown_time.html(prettyTime);

				/* Start the countdown */
				countdown();
			});
		}

		/* Initialize */
		countdown();

		return {
			countdown : countdown,
			onServiceEnd : onServiceEnd
		};

	};


/* On document ready */
$(document).ready(function(){

/* Add 'hover' class to nav for IE 6
================================================================ */
	if ($.browser.msie && $.browser.version < 7) {
		$('ul#nav > li').hover(
			function(){
				$(this).addClass('hover');
			},
			function(){
				$(this).removeClass('hover');
			}
		);
	}

/* Add 'hover' class for clicks on mobile devices
================================================================ */
	if (navigator.userAgent.toLowerCase().indexOf('iphone') >= 0) {
		$('ul#nav > li').click(
			function(){
				var $this = $(this);
				if ($this.hasClass('hover')) {
					$('ul#nav > li').removeClass('hover');
				} else {
					$('ul#nav > li').removeClass('hover');
					$(this).addClass('hover');
				}
			}
		);
	}


/* icampus countdown
================================================================= */

	$.icampus = $.icampus();

});})(jQuery);