
var ANSYS = {};

$(document).ready( function() {
	$('html').addClass('js');
	ANSYS.anchors();
	ANSYS.txtBoxClear();
	ANSYS.collapsibles();
	ANSYS.overlay();
	ANSYS.shortcutDropdown();
	
	ANSYS.slider('#mainFocus');
	ANSYS.slider('#spotlightBlocks');
	ANSYS.slider('#hdrFocus');
	
	$('#navLevelOne li').sfHover('sfhover');
});


/* =================================================================== */


/**
 * set up focus slider content
 */
ANSYS.slider = function(sElement) {
	// make sure the element exists before continuing. 
	if ($(sElement).length == 0) {
		return; 
	}

	// add wrapper to actual slider items
	$(sElement).wrapInner('<div class="sliderContent" />');
	
	// insert slider navigation
	$(sElement).prepend('<a href="#"><span class="prevSlide">Prev</span></a><a href="#"><span class="nextSlide">Next</span></a><ul class="navSlide"></ul>');
	
	// activate slider for this element
    $(sElement + ' .sliderContent').cycle({
        fx:      'fade',
        timeout:  7000,
        prev:    sElement+' .prevSlide',
        next:    sElement+' .nextSlide',
        pager:   sElement+' .navSlide',
        pagerAnchorBuilder: pagerFactory
    });
	
	function pagerFactory(idx, slide) {
        var s = idx > 2 ? ' style="display:none"' : '';
        return '<li'+s+'><a href="#">'+(idx+1)+'</a></li>';
    };
	
}


/* =================================================================== */


/**
 * set up Fancybox functionality
 */
ANSYS.overlay = function() {
	var $link = $('<span>View larger image</span>');
	
	var init = function() {
		$('a.larger').append($link).fancybox();
	}();
	
}



/* =================================================================== */



/**
 * set up the functionality to have expandable/collapsible sections of content on the site
 */
ANSYS.collapsibles = function() {
	var $link = $('<span class="collapseLink">&nbsp;</span>');
	
	/**
	 * initialize the functionality
	 * this runs automatically when calling the parent class
	 * Hides all the content to start
	 * Attaches all the proper events
	 */
	var init = function() {
		var $sections = $('.collapsibles .section');
		$sections.each( function() {
			$(this).contents().not('h1').wrapAll('<div class="hideable"></div>');
		});
		$sections.find('.hideable').hide();
		$sections.find('h1').append($link).click( function() {
			click($(this).closest('.section'));
		});		
	}();
	
	var click = function($obj) {
		var $hideable = $obj.find('.hideable');
		if ($hideable.is(':visible')) {
			collapse($hideable);
		} else {
			expand($hideable);
		}
	};
	
	var expand = function($hideable) {
		$hideable.slideDown().closest('.section').addClass('open');
		$hideable.closest('.section').find('#btnnewpage').attr("src", "/images/btn-newpage3-blue.gif");
	};
	
	var collapse = function($hideable) {
		$hideable.slideUp(function() {
			$(this).closest('.section').removeClass('open');
			$(this).closest('.section').find('#btnnewpage').attr("src", "/images/btn-newpage3.gif");
		});
	};
	
}


/* =================================================================== */



/**
 * Updated link functionality script using delegation to speed up
 * the page and handle dynamically added elements.
 * dependencies: jQuery
 */
ANSYS.anchors = function() {
	var init = function() {
		$('body').delegate('a', 'click', function(e) {
			if ($(this).attr('rel') == 'external' || $(this).hasClass('external') || $(this).hasClass('pdf') || $(this).hasClass('popupFull')) {
				// external links
				e.preventDefault();
				return openWin(this,"");
			} else if ($(this).hasClass('popup')) {
				// popup
				e.preventDefault();
				return openWin(this,"height=550,width=600,scrollbars=yes");
			} 
			
			if ($(this).attr('href') == location.href ) {
				// onstate
				$(this).addClass('onstate');
			}
		});
	}();
	
	var openWin = function(o,params) {
		window.open(o.href, "newwin","" + params + "");
		return false;
	};
}
/* =================================================================== */




/**
 * use this to automatically clear a text box of it's default value
 * if nothing is typed in the box, the script will put the default value back.
 * Useful on sites where search boxes don't have a separate label
 * you can use it for more than one box per page using the txtBoxes array
 * 
 */
ANSYS.txtBoxClear = function() {
	var txtBoxes = ['searchField'];
	
	var attachEvents = function(txtBox) {
		txtBox.onfocus = function() {
			if (txtBox.value == txtBox.defaultVal) { txtBox.value = ''; } 
		};
		txtBox.onblur = function() {
			if (txtBox.value == '') { txtBox.value = txtBox.defaultVal; }
		};
	};
	
	var init = function() {
		for (i=0;i<txtBoxes.length;i++) {
			var oCurrentTxtBox = document.getElementById(txtBoxes[i]);
			if (!oCurrentTxtBox) { continue; }
			oCurrentTxtBox.defaultVal = oCurrentTxtBox.defaultValue;
			attachEvents(oCurrentTxtBox);
		}
	}();
}



/* =================================================================== */



/**
 * Show/Hide Select box
 */ 
ANSYS.shortcutDropdown = function() {

	var listHeight = Number;
	var listYPos = Number;		
	var windowOffset = Number;

	var init = function() {
		$('.fauxSelect').wrapInner('<a href="#" class="showSelect"></a>').click( function(e) {
			e.preventDefault();
			e.stopPropagation();
			clickEvent(e, this);
		});		
	}();
	
	var clickEvent = function(e, obj) {
		var $select = $(obj).next('.selectList');
		
		listHeight = $select.height();
		listYPos = $select.prev('.fauxSelect').offset().top;
		windowOffset = $('html').scrollTop();
		
		if ($select.is(':visible') ) {
			closeDropdown($select);
		} else {
			openDropdown(e, obj, $select);
		}
	};
	
	var openDropdown = function(e, obj, $select) {
		// hide any visible lists first
		$('ul.selectList:visible').each( function() {
			closeDropdown($(this));
		});
		
		// some calculations to help us figure if it should open up or down
		var iWhereItHappened = (e.pageY - windowOffset);	
		var innerHeight = '';
		if (!window.innerHeight) {
			innerHeight = document.documentElement.clientHeight;
		} else {
			innerHeight = window.innerHeight;
		}
		
		// if where it happened on the page + the list height is greater than viewport height
		// we need to open the list up, not down.
		if ( (iWhereItHappened + listHeight + 20) > innerHeight) {
			var totalHeight = (listHeight + $select.prev('.fauxSelect').height()) + 5;
			$select.css({'left': '-999em', 'display': 'block'});
			if ( $select.data('ansys.top') ) {
				var offsetTop = $select.data('ansys.top');
			} else {
				var offsetTop = $select.position().top;
				$select.data('ansys.top', offsetTop);
			}
			$select.css({'left': '0', 'display': 'none'});
			var top = totalHeight - offsetTop;
			if ($.browser.mozilla) {top += 40;}
			$select.addClass('up').animate(
				{  'height': 'toggle', 'top': '-'+top+'px' }, 
				{ duration: "fast" }
			);
		} else {
			$select.parent().siblings().css('z-index','0');
			$select.parent().css('z-index','100');
			$select.slideDown('fast');
		}
		$('body').bind('click.fauxselect', function(e) {
			closeDropdown($select);
			$('body').unbind('click.fauxselect');
		});
	};
	
	var closeDropdown = function($select) {
		if ( $select.hasClass('up') ) {
			var totalHeight = (listHeight + $select.prev('.fauxSelect').height()) + 5;
			var offsetTop = $select.data('ansys.top');
			var top = totalHeight - offsetTop;
			if ($.browser.mozilla) {	top += 40;}
			$select.removeClass('up').animate(
				{ "height": "toggle", 'top': '+='+top+'px' }, 
				{ duration: "fast" }
			);
		} else {
			$select.slideUp('fast');
			$select.parent().siblings().css('z-index','0');
			$select.parent().css('z-index','0');
		}
		$('body').unbind('click.fauxselect');
	};

}



/* =================================================================== */




jQuery.fn.clog = function(msg) {
	var txt = msg ? msg : "";
	if (console && typeof console.log != "undefined") {
		console.log('%o ', this, txt);
	}
	return this;
}



/* =================================================================== */

/* =================================================================== */
// for suckerfish dhtml menus
// dependencies: jQuery

jQuery.fn.sfHover = function(className) {
	$(this).each( function() {
		$(this).hover( function() {
			$(this).addClass(className);
		}, function() {
			$(this).removeClass(className);
		});
	});
	return this;
};
