/*
 * ENABLE JS-ONLY STYLES
 */
jQuery('html').addClass('js-enabled');

/*
 * ONLOAD EVENTS
 */
jQuery(document).ready(function() {

	if(typeof(swfobject) !== 'undefined' && swfobject.hasFlashPlayerVersion("9.0.0")) {
		jQuery('html').addClass('flash-enabled');

	}

	/*
	 * External Links
	 */
	function externalLinks() {
		if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
				anchor.target = "_blank";
		}
	}
	externalLinks();
	
	/*
	 * PRIMARY NAVIGATION hover enhancement 
	 * 		(with IE 6 workarounds)
	 */
	jQuery('#primary-navigation').hover(
		function() {
			if (navigator.userAgent.match(/msie 6/i)) {
				jQuery(this).addClass('primary-navigation-hover');
			}
			var $nav = jQuery('#primary-navigation .navigation');
			$nav.stop().animate({ height: '190px' },'fast');
		},
		function() {
			if (navigator.userAgent.match(/msie 6/i)) {
				jQuery(this).removeClass('primary-navigation-hover');
			}
			var $nav = jQuery('#primary-navigation .navigation');
			$nav.stop().animate({ height: '30px' },'fast');			
		}
	);
	
	if (navigator.userAgent.match(/msie 6/i)) {
		jQuery('#primary-navigation .navigation dl').hover(
			function() {
				jQuery(this).addClass('primary-subnavigation-hover');
			},
			function() {
				jQuery(this).removeClass('primary-subnavigation-hover');
			}
		);
	};
	
	/*
		Executes proprietary command to force IE to cache all background images
			which prevents the browser from fetching the same image repeatedly when using CSS hover background repositioning
		(http://support.microsoft.com/?scid=kb;en-us;823727&spid=2073&sid=global)
	*/
	if (navigator.userAgent.match(/msie 6/i)) {
		try {
			document.execCommand("BackgroundImageCache",false,true);
		} catch(e) {
			// just in case
		}
	};

	/*
	 * SEARCH
	 */
	jQuery(function() {
		/* get value from label */
		var _searchVal = jQuery('.site-search label').html();
		if (_searchVal) {
			jQuery('.site-search input.text#search-text').val(_searchVal).focus(
				function() { if(jQuery(this).val().trim() == jQuery('.site-search label').html()){jQuery(this).val('');}}
			).blur(
			    function() { if(jQuery(this).val().trim() == ""){jQuery(this).val(_searchVal);}}
			);
		}
	});


	/*
	 * TABS
	 */

	jQuery(function () {
		//hide containers
		var tabContainers = jQuery('div.tabz > div');
		tabContainers.hide().filter(':first').show();
		var tabHeaders = jQuery('div.main-illus-switch');
		tabHeaders.hide().filter(':first').show();


		jQuery('div.tabz ul.navigation a').click(function () {
			tabContainers.hide();
			var selected = this.hash;
			var selectedImg = this.rel;
			jQuery('div.main-illus-switch').hide();
			jQuery('#'+selectedImg).show();
			jQuery(selected).show();
			jQuery('div.tabz ul.navigation a').removeClass('selected');
			jQuery(this).addClass('selected');
			return false;
		}).filter(':first').click();

	});

/*	$("img.main-illus-first").fadeIn("fast");
	$('img.main-illus').removeClass('main-illus-shown');
*/



	jQuery(function () {
		jQuery('.lifecycle-tabs .tab').append('<a class="tabover"><span class="tabspan">&nbsp;</span></a>').each(function () {
				var $span = jQuery('a.tabover', this).hide();
				jQuery(this).hover(function () {
				jQuery('a.tabover', this).fadeIn(100);
				jQuery('span.tabspan', this).fadeIn(100);
			},
			function () {
				jQuery('a.tabover', this).fadeOut(100);
				jQuery('span.tabspan', this).fadeOut(100);
			});
		});
	});



	/*
	 * EXPANDING PRODUCT TABS
	 */

	// Expands and hides product expanders, and toggles header style.
	jQuery(function() {

		//hide divs with expander class
		jQuery('div.expander > div').hide();
		jQuery('div.expander > h2').addClass('expanding_');

		//toggle first tab on and switch header class.
		jQuery('div.expander:eq(0)> h2').removeClass('expanding_');
		jQuery('div.expander:eq(0)> h2').addClass('expanded_');
		jQuery('div.expander:eq(0)> div').show();
		jQuery('div.expander > h2').click(function() {
				jQuery(this).next().slideToggle('fast');

				//If an expanding class is on, turn it off, if it's off turn it on.
				jQuery(this).toggleClass('expanding_');
				jQuery(this).toggleClass('expanded_');
			}
		)
	});

	// Search Suggestions
	jQuery(function() {
		if (jQuery('#search-text').suggest) {
			jQuery('#search-text').suggest('/html/portlet/ext/sappi_search/search_terms.jsp', {onSelect: function() {jQuery('#search-submit').click()}});
		}
	})
});


/*
 *  POPUPS - CALCULATORS
 */
function calculatorpopup(url) {

/*
	var popup = Liferay.Popup({
		title: "Calculator",
		modal:false,
		width:500,
		height:550
	});

	jQuery(popup).load(url);
*/

	newwindow = window.open(url,'name','height=550,width=500');
	if (window.focus) {
		newwindow.focus();
	}

	return false;
}

function yieldpopup(url) {

/*
	var popup = Liferay.Popup({
		title: "Calculator",
		modal:false,
		width:500,
		height:790
	});

	jQuery(popup).load(url);
*/

	newwindow = window.open(url,'name','height=790,width=500');
	if (window.focus) {
		newwindow.focus();
	}

	return false;
}

	function videopopup(url) { newwindow=window.open(url,'name','height=355,width=340'); if (window.focus) {newwindow.focus()} return false; }

/* 
 * FLASH COOKIE FUNCTIONS
 */

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);
}


jQuery(document).last(


	function() {
		// Why oh why does LR use javascript to set an inline style z-index value on the liferay dock???  Undo this.
		jQuery(".lfr-dock").css("z-index", "410");
	}
);


Liferay.Portlet.ready(

	/*
	This function gets loaded after each and every portlet on the page.

	portletId: the current portlet's id
	jQueryObj: the jQuery wrapped object of the current portlet
	*/

	function(portletId, jQueryObj) {

		// K.H. 3/20/2009 - Make sure to add the suggest feature to the search results portlet search
		// text box when it is loaded.
		if (portletId == 3) {
			jQuery(function() {
				console.log(portletId);
				jQuery('#results-search-text').suggest('/html/portlet/ext/sappi_search/search_terms.jsp', {onSelect: function() {jQuery('#results-search-submit').click()}});
			})
		}
	}
);
