jQuery.noConflict();

// Document Ready Event
jQuery(document).ready(function(){

	// Add dropdown class to links that have dropdowns
	jQuery('.menu > li > ul, .submenu > li > ul').each(function(i,li) {
		jQuery(li).parent('li').addClass('dropdown');
	});
	
	// Add dropdown class to links that have dropdowns
	jQuery('.menu > li > ul, .submenu > li > ul').each(function(i,li) {
		jQuery(li).parent('li').addClass('dropdown');
	});

	// Drop down menus - level 2
	jQuery(".menu > li,.submenu > li").hover(
	function(){
		var menu = jQuery(this).children(':parent > ul');
		var offset = jQuery(this).offset();
		var bodywidth = jQuery('body').width();
		
		// Check to make sure the dropdown won't go off screen
		if(offset.left + 189 > bodywidth) {
			menu.css({
				left: 'auto',
				right: '0px'
			});
		}
		
		menu.css({visibility: "visible",display: "none"}).slideDown(268);
		// menu.slideDown(268);
	},
	function() {
		var menu = jQuery(this).children(':parent > ul');
		menu.css({visibility: "hidden"});
	});
	
	// Drop down menus - level 3
	jQuery(".menu > li > ul > li, .submenu > li > ul > li").hover(
	function(){
		var menu = jQuery(this).children(':parent > ul');
		var offset = jQuery(this).offset();
		var bodywidth = jQuery('body').width();
		
		// Check to make sure the dropdown won't go off screen
		if(offset.left + 378 > bodywidth) {
			menu.css({
				left: '-189px'
			});
		}
		
		// menu.slideDown(268);
		menu.css({visibility: "visible",display: "none"}).slideDown(268)
	},
	function() {
		var menu = jQuery(this).children(':parent > ul');
		menu.css({visibility: "hidden"});
	});
	
	
	// Featured Slide Thing
	
	var items = jQuery('.featured li');
	var item_width = 630;
	var max_margin = items.length * item_width - item_width;
	var animstatus = false;
	var user_click = false;
	var animation_speed = 1000;
	var auto_speed = 7500;
	
	
	jQuery('.featured .fthumbs a').each(function(i,link) {
		var index = i+1;
		jQuery(link).click(function(e) {
			e.preventDefault();
			user_click = true;
			
			moveFeature(index);
		});
	});
	
	function moveFeature(location) {
		if(animstatus == false) {
			animstatus = true;
			var feature = jQuery('.featured ul');
			var pos = parseInt(jQuery(feature).css('left'));
			
			if(location) {
				if(location=='next') {
					if(pos == -max_margin){
						left = 0;
					}
					else {
						left = pos-item_width;
					}
				}
				else if(location=='back') {
					if(pos == 0){
						left = -max_margin;
					}
					else {
						left = pos+item_width;
					}
				}							
				else {
					left = -(item_width*location)+item_width;
				}
				
				var item = Math.abs(left/item_width);
				jQuery('.featured .fthumbs a img').removeClass('active');
				jQuery('.featured .fthumbs a:eq('+item+') img').addClass('active');
				
				feature.animate({left: left},animation_speed,"swing",function() {
					animstatus = false;
				});
			}
		}
	}
	
	function feature_automove() {
		if(user_click == true) {
			user_click = false;
		}
		else {
			moveFeature('next');
		}
	}
	var feature_auto = setInterval(feature_automove, auto_speed);
	
	// cookie functions http://www.quirksmode.org/js/cookies.html
	function createCookie(name,value,days)
	{
		if (days)
		{
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
	function readCookie(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 eraseCookie(name)
	{
		createCookie(name,"",-1);
	}
	// /cookie functions
	
	/**
	* Styleswitch stylesheet switcher built on jQuery
	* Under an Attribution, Share Alike License
	* By Kelvin Luck ( http://www.kelvinluck.com/ )
	**/

	jQuery(document).ready(function() {
		jQuery('.styleswitch').click(function()
		{
			switchStylestyle(this.getAttribute("rel"));
			return false;
		});
		var c = readCookie('style');
		if (c) switchStylestyle(c);
	});

	function switchStylestyle(styleName)
	{
		jQuery('link[@rel*=style][title]').each(function(i)
		{
			this.disabled = true;
			if (this.getAttribute('title') == styleName) this.disabled = false;
		});
		createCookie('style', styleName, 365);
	}
	
	jQuery(".atabs .tabs a").each(function(i) {
		jQuery(this).click(function(e){
			e.preventDefault();
							
			jQuery(".atabs .tabs a").removeClass('active');
			jQuery(this).addClass('active');
			
			jQuery('#atabs_content').empty().html(jQuery(this.getAttribute("rel")).html());
		});
	});
});

function bookmark(url, title){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all) // ie
		window.external.AddFavorite(url, title);
	else
		alert('Your browser does not support this function.');
};

	// JavaScript Document

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(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;
}
			

jQuery(document).ready(function(){

	// Attention Grabber Mancave bar

	jQuery.fn.attentionGrabber = function(options) {
	
		var	defaults = {
			duration	: 300,
			showAfter	: 0,
			borderSize	: 3,
			height		: 39,
			easing		: "linear"
		},
		settings = jQuery.extend({}, defaults, options);
		
		if( settings.easing == "swing" ){ settings.easing = ''; } 
		
		settings.totalHeight	= parseInt( settings.height, 10 ) + parseInt( settings.borderSize, 10 );
		settings.duration		= parseInt( settings.duration, 10 );
		settings.showAfter		= parseInt( settings.showAfter, 10 )*1000;
		
		// Main elements
		var	wrap					= jQuery(this),
			grabber					= wrap.find("#attentionGrabber"),
			link					= grabber.find(".link"),
			
			closeBtn				= grabber.find("#closeAttentionGrabber"),
			openBtn					= wrap.find("#openAttentionGrabber"),
			
			animationParam			= {},
			animationProperty		= "",
			
			buttonAnimationParam	= {},
			buttonAnimationProperty = "",
			
			showOpenButton		= function(){
				buttonAnimationParam[buttonAnimationProperty] = settings.totalHeight;
				openBtn.animate( buttonAnimationParam, (settings.duration/2), settings.easing );
				wrap.removeClass("openGrabber");
			},
			hideOpenButton		= function(){
				buttonAnimationParam[buttonAnimationProperty] = - Math.abs(34 - settings.height);
				openBtn.animate( buttonAnimationParam, (settings.duration/2), function(){ showGrabber(); } );
			},
			
			// The show/hide animations
			showGrabber			= function(){
				animationParam[animationProperty] = 0;
				wrap.animate( animationParam , settings.duration, settings.easing ,function(){
					wrap.addClass("openGrabber");
				} );
			},
			hideGrabber			= function(){
				animationParam[animationProperty] = -settings.totalHeight;
				wrap.animate( animationParam , settings.duration, function(){
					showOpenButton();
				} );
			};
		
		if( readCookie('attentionGrabber_active') == 'false' ){
			var foundCookie = true;
		}else{
			var foundCookie = false;
		}
		
		// Initialize the property for the slide animation
		animationProperty = "bottom";
		buttonAnimationProperty = "bottom";
		
		// Remove it from the DOM
		wrap.detach();
		
		// Move it to the right position
		wrap.prependTo("body").css({ display : "block" });
		
		
		if( foundCookie ){
			// Show only the open button
			setTimeout( function(){ showOpenButton(); }, settings.showAfter );
		} else {
			// Show the grabber for the first time
			setTimeout( function(){ showGrabber(); }, settings.showAfter );
		}
		
		// Close grabber
		closeBtn.click(function(){
			hideGrabber();
			toggleMancaveMenu('close');
			setCookie();
		});
		
		// Open grabber
		openBtn.click(function(){
			hideOpenButton();
			setCookie();
		});
		
		// Set the cookie
		function setCookie(){
			if( !foundCookie ){
				createCookie('attentionGrabber_active', 'false', 30)
				foundCookie = true;
			}else{
				createCookie('attentionGrabber_active', 'true', 30)
				foundCookie = false;
			}
		}
		
	}


	jQuery("#attentionGrabberWrap").attentionGrabber();
	
	// Mancave menu

	jQuery('#mancave_m').click( function () {
		toggleMancaveMenu('none');
	});
	
	function toggleMancaveMenu (action) {
		mcMenu = jQuery('#mancave_menu');
		var mcCurrClass = mcMenu.attr('class');
		if (mcCurrClass == 'mancave_menu_open' || action == 'close') {
			mcMenu.fadeOut();
			mcMenu.attr('class' , 'mancave_menu_closed');
		} else {
			mcMenu.fadeIn();
			mcMenu.attr('class' , 'mancave_menu_open');
		}
	}
	
});

			
