var ie = (navigator.userAgent.indexOf('MSIE')>=0) ? true : false;
var ie6 = (navigator.userAgent.indexOf('MSIE 6')>=0) ? true : false;

/*** JavaScript Enabled ***/

var jsEnabled = function(){
	$('body').removeClass('no-js');
};

/*** Modal Window ***/

$.fn.modalPanel = function(options){
	options = jQuery.extend({
		className: 'modal-window',
		callback: function(){},
		interstitial: false
	},options);

	var overlay = $(document.createElement('div')).attr('id','overlay').fadeTo(0, '.85'),
		modalWindow = $(document.createElement('div')).attr('id','modal-window').addClass(options.className);

	function handleEscape(e){
		if(e.keyCode == 27){
			modalHide();
		}
	}

	function modalHide(){
		$(document).unbind('keydown', handleEscape);
		var remove = function(){ $(this).remove(); };

		overlay.fadeOut(400, remove);
		modalWindow.fadeOut(400, remove).empty();

		if(typeof document.body.style.maxHeight === 'undefined'){
			$('body','html').css({ 'height' : 'auto', 'overflow' : 'auto', 'width' : 'auto' });
		}
	}

	function positionModal(){
		modalWindow.css({
			left: ($(document.body).width() - modalWindow.width()) / 2 + 'px',
			top: ($(window).height() - modalWindow.height()) / 2 + 'px'
		});
	}
	
	return this.each(function(){
		$(this).click(function(e) {
			e.preventDefault();

			$(document).keydown(handleEscape);

			$('body').append(overlay.click(function(){modalHide();})).append(modalWindow);

			overlay.fadeIn(400);

			if(options.interstitial){
				var modalWindowContent = modalWindow.html($('div.modal-interstitial').html()),
					targetLink = $(this).attr('href');

				modalWindow.append($('<a class="btn" id="btn-modal-continue" target="_blank"><span class="offscreen">Continue</span></a>').attr('href', targetLink).click(function(){
					modalHide();
				}));
				modalWindow.append($('<a class="btn" id="btn-modal-cancel"><span class="offscreen">Cancel</span></a>').click(function(){
					modalHide();
				}));
			}else{
				var modalWindowContent = modalWindow.html($(this).siblings('div.modal-content').html());
				
				modalWindow.append($('<a class="btn" id="btn-modal-close"><span class="offscreen">Close</span></a>').click(function(){
					modalHide();
				}));
			}

			if(ie6){
				modalWindowContent.show().append('<div class="ie-fix"></div>');
			}else if(ie){
				modalWindowContent.show();
			}else{
				modalWindowContent.fadeIn(400);
			}

			positionModal();

			options.callback.call(this);
		});

		$(window).bind('resize', function(){
			positionModal();
		});

		if(typeof document.body.style.maxHeight === 'undefined' || navigator.platform == "Nintendo Wii"){
			overlay.css({
				height: ($(document.viewport).height() > $(document.body).height() ? $(document.viewport).height() : $(document.body).height()) + 'px'
			});
		}
	});
};

/*** Content Slider ***/

$.fn.contentScroller = function(options){
	options = jQuery.extend({
		wrapperClass: 'wrapper',
		scrollOffset: 1,
		scrollSpeed: 400,
		scrollEase: 'easeOutQuad'
	},options);
  
	return this.each(function(){
		var elWrapper = $('> div.' + options.wrapperClass, this).css('overflow', 'hidden'),
			elScroller = elWrapper.find('> ul'),
			elItems = elScroller.find('> li'),
			elSingleItem = elItems.filter(':first'),
			singleWidth = elSingleItem.outerWidth(), 
			visible = Math.ceil(elWrapper.innerWidth() / singleWidth),
			currentPage = 1,
			pages = Math.ceil(elItems.length / options.scrollOffset);

		var elLinkContainer = $(document.createElement('ul')).addClass('scroller-links'),
			elBtnPrev = $(document.createElement('li')).addClass('btn-prev').append($(document.createElement('a')).attr('href', '#Previous').text('Previous').addClass('btn')),
			elBtnNext = $(document.createElement('li')).addClass('btn-next').append($(document.createElement('a')).attr('href', '#Next').text('Next').addClass('btn'));

		elItems.filter(':first').before(elItems.slice(- visible).clone().addClass('cloned'));
		elItems.filter(':last').after(elItems.slice(0, visible).clone().addClass('cloned'));
		elItems = elScroller.find('> li');

		elWrapper.scrollLeft(singleWidth * visible);
		
		function gotoPage(page) {
			var dir = page < currentPage ? -1 : 1,
				n = Math.abs(currentPage - page),
				left = singleWidth * dir * options.scrollOffset * n;

			$(elItems[page]).find('> div.drawer').css({ 'bottom' : '-' + elItems.find('> div.drawer').height() + 'px' }).children();

			elWrapper.filter(':not(:animated)').animate(
				{ scrollLeft: '+=' + left },
				options.scrollSpeed, options.scrollEase,
				function(){
					if (page == 0) {
						elWrapper.scrollLeft(singleWidth * (pages + visible - options.scrollOffset));
						page = pages;
					} else if (page > pages) {
						elWrapper.scrollLeft(singleWidth * visible);
						page = 1;
					};

					currentPage = page;
					$(elItems[page]).find('> div.drawer').css({ 'bottom' : '-' + elItems.find('> div.drawer').height() + 'px' }).animate(
						{ 'bottom' : '0'},
						{ queue: false, duration: 300 }	
					);
				}
			);
			return false;
		}
		
		elWrapper.after($(elLinkContainer).append(elBtnPrev, elBtnNext));
		
		elBtnPrev.click(function(){
			return gotoPage(currentPage - 1);				
		});
		
		elBtnNext.click(function(){
			return gotoPage(currentPage + 1);
		});
		
		$(this).bind('goto', function(event, page){
			gotoPage(page);
		});
	});
};

/*** Tab Switcher ***/

var tabSwitcher = function(options){
	options = jQuery.extend({
		elTab: $('a.tab'),
		elTabsWrapper: $('div.tab-wrapper'),
		elTabContent: $('div.tab-content'),
		elTabIntro: $('div.tab-intro'),
		showAllAnchor: '#show-all'
	},options);

	var elCurrentTab = options.elTab.filter('.on').attr('href');

	options.elTabContent.hide().filter('.on').show();

	if(elCurrentTab == options.showAllAnchor){
		options.elTabContent.show();
	}

	options.elTab.click(function(e){
		e.preventDefault();

		var elNextTab = $(this),
			elCurrentTab = options.elTab.filter('.on').attr('href'),
			elTargetTabId = elNextTab.attr('href');

		options.elTab.removeClass('on').addClass('off');

		$(elNextTab).removeClass('off').addClass('on');

		function showTab(){
			if(elTargetTabId == options.showAllAnchor){
				options.elTabContent.fadeIn(400).addClass('on');
			}else{
				$(elTargetTabId).fadeIn(400).addClass('on');
			}
		};

		if(options.elTabIntro){
			options.elTabIntro.remove();
		}

		if(elTargetTabId != elCurrentTab){
			options.elTabContent.removeClass('on').hide();

			if(elCurrentTab == undefined){
				showTab();
			}else if(elCurrentTab == options.showAllAnchor){
				options.elTabContent.fadeOut(400, function(){
					showTab();
				});
			}else{
				$(elCurrentTab).fadeOut(400, function(){
					showTab();
				});
			}
		}
	});
};

/*** Load On DOM Ready ***/

$(function(){
	jsEnabled();
});
