// break out of frameset
if (top.location != location)
    top.location.href = document.location.href;


jQuery.noConflict();


// font sizes are meassured in 0 = small, 1 = medium and 2 = large for easier reduction factor calculation

var currentFontSize = 1;
var currentClass;

function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
// check for mobile device

if(window.location.hostname.indexOf("nexeninc.com") != -1)
{
   /*   if (bhawk['mobiledevice']() === true) {
	    if (!getParameterByName('mobiledetected'))
		  document.location.href = document.location.href + '?mobiledetected=true';*/
}
jQuery(function () {
	if (jQuery('table.TableHover tr') != null) {
		jQuery('table.TableHover tr').hover(
            function () {
            	jQuery(this).children().addClass('TableHoverRow');
            },
            function () {
            	jQuery(this).children().removeClass('TableHoverRow');
            }
        );
	}

	//New Grad Profile
	//ACCORDION BUTTON ACTION	
	jQuery('div.accordionButton').click(function() {
		jQuery('div.accordionContent').slideUp('normal');	
		jQuery(this).next().slideDown('normal');
	});

	//HIDE THE DIVS ON PAGE LOAD	
	jQuery("div.accordionContent").hide();

	// setup any tabs if they are on the page
	jQuery('#TabContainer').tabs({ onClick: TabClicked });
	TabClicked(null, null, null);


	// setup the top nav
	jQuery('a[class^=TopMenuHover]').hover(function () {
		var id = jQuery(this).attr('class').split('_');
		lastMenuClass = jQuery(this).attr('class');

		jQuery('div[id^=' + id[1].replace('-clicked', '') + ']').show();

		if (id[1] != 'Logo')
			jQuery('#main_0_content_0_contentright_0_c_ctl01_ctl00_ctl00').hide();
		highlightedId = id[1];

	}, function () {
		//var id = jQuery(this).attr('class').split('_');
		//jQuery('div[id^='+id[1]+']').hide();
		jQuery('div[class^=TopMenuDiv]').each(function () {
			jQuery(this).hide();
			jQuery('#main_0_content_0_contentright_0_c_ctl01_ctl00_ctl00').show();
		});

	});

	jQuery('div[class^=TopMenuDiv]').hover(function () {
		jQuery(this).show();
		jQuery('#main_0_content_0_contentright_0_c_ctl01_ctl00_ctl00').hide();

		jQuery('a.TopMenuHover_' + highlightedId).attr('class', jQuery('a.TopMenuHover_' + highlightedId).attr('class').replace('-clicked', '') + '-selected');

	}, function () {
		jQuery('div[class^=TopMenuDiv]').each(function () { jQuery(this).hide(); });
		jQuery('#main_0_content_0_contentright_0_c_ctl01_ctl00_ctl00').show();

		jQuery('a.TopMenuHover_' + highlightedId.replace('-clicked', '') + '-selected').attr('class', lastMenuClass);
		//jQuery('a.TopMenuHover_'+highlightedId+'-selected').attr('class','TopMenuHover_'+highlightedId);

	});

	jQuery(".HistoryNav").css('opacity', '0.5');
	jQuery(".HistoryNav ul").css('opacity', '1.0');
	jQuery('.HistoryNav').each(function () {
		jQuery(this).find('a:first').attr('class', 'active');
	});

	jQuery('.StoryboardThumbOverlay').css('opacity', '0.5');
	jQuery('.StoryboardThumbOverlay:first').css('opacity', '0');



	// Sustainability report on click function that reveals the download links underneath

	// Don't need this for now so commenting out - PQ - July 8, 2010

	/*     jQuery('#SRInfo a').click(function() {
	jQuery('#SRDownload').show();
	jQuery('#SRInfo').hide();
	return false;
	});

	jQuery('div.CloseLink > a').click(function() {
	jQuery('#SRDownload').hide();
	jQuery('#SRInfo').show();
	return false;
	});

	*/
	jQuery(function () { // DOM Ready Shortcut

		// Hide to start
		jQuery('#SRDownload').hide();

		if (jQuery('table.TableHover tr') != null) {
			jQuery('table.TableHover tr').hover(
	            function () {
	            	jQuery(this).children().addClass('TableHoverRow');
	            },
	            function () {
	            	jQuery(this).children().removeClass('TableHoverRow');
	            }
	        );
		}

		var activeSlideMenuItem = jQuery('#HomeBannerSlideMenu a').eq(0);


		// Setup banner's zindex
		var numberOfBanners = jQuery('.HomeBanner').length;
		jQuery('.HomeBanner').hide();

		var currentBanner = jQuery('.HomeBanner').eq(0).show();

		// Banner Page Numbers
		activeSlideMenuItem.addClass('active');
		jQuery('#HomeBannerSlideMenu a').click(function (e) {

			e.preventDefault();

			activeSlideMenuItem.removeClass('active');
			activeSlideMenuItem = jQuery(this).addClass('active');

			currentBanner.css({ 'z-index': 1 }).fadeOut(750);
			currentBanner = jQuery(this.hash).css({ 'z-index': 2 }).fadeIn(750);


		});


		// setup any tabs if they are on the page
		jQuery('#TabContainer').tabs({ onClick: TabClicked });
		TabClicked(null, null, null);


		// add a left class to the first for divs if the logo doesn't exist in the menu
		if (!jQuery('#TopMenu .Logo').length) {
			jQuery('.TopMenuDiv').slice(0, 3).addClass('TopMenuDivLeft');
		}
		// setup the top nav
		jQuery('#TopMenu a').hover(function () {
			var id = jQuery(this).attr('id').replace(/^TopMenuAnchor/, '');

			// Store previous menu item
			hoveredMenuItem = jQuery(this).eq(0);

			jQuery('#' + id).show();

			// No idea what element this but left it in as to not break anything - JP
			if (id != 'Logo') {
				jQuery('#main_0_content_0_contentright_0_c_ctl01_ctl00_ctl00').hide();
			}

		}, function () {

			// No idea what element this but left it in as to not break anything - JP
			jQuery('#main_0_content_0_contentright_0_c_ctl01_ctl00_ctl00').show();

			// Hide all Top Menu Divs
			jQuery('.TopMenuDiv').hide();
		});

		jQuery('.TopMenuDiv').hover(function () {
			// Keep this div open
			jQuery(this).show();

			// No idea what element this but left it in as to not break anything - JP
			jQuery('#main_0_content_0_contentright_0_c_ctl01_ctl00_ctl00').hide();

			// Add a hover class from the menu item that opened this div
			if (jQuery(hoveredMenuItem).length) hoveredMenuItem.addClass('hover');

		}, function () {

			// Hide all Top Menu Divs
			jQuery('.TopMenuDiv').hide();

			// No idea what element this but left it in as to not break anything - JP
			jQuery('#main_0_content_0_contentright_0_c_ctl01_ctl00_ctl00').show();

			// Remove the hover class from the menu item
			if (jQuery(hoveredMenuItem).length) hoveredMenuItem.removeClass('hover');

		});

		jQuery(".HistoryNav").css('opacity', '0.5');
		jQuery(".HistoryNav ul").css('opacity', '1.0');
		jQuery('.HistoryNav').each(function () {
			jQuery(this).find('a:first').attr('class', 'active');
		});

		jQuery('.StoryboardThumbOverlay').css('opacity', '0.5');
		jQuery('.StoryboardThumbOverlay:first').css('opacity', '0');


		//Duplicate code - see above
		/*
		// Sustainability report on click function that reveals the download links underneath
		jQuery('#SRInfo a').click(function() {
		jQuery('#SRDownload').show();
		jQuery('#SRInfo').hide();
		return false;
		});

		jQuery('div.CloseLink > a').click(function() {
		jQuery('#SRDownload').hide();
		jQuery('#SRInfo').show();
		return false;
		});

		*/
		// Replace all PopupLink with new window
		jQuery('a.PopupLink').each(function () {
			jQuery(this).click(function () {
				var left = (screen.width / 2) - (670 / 2);
				var top = (screen.height / 2) - (600 / 2);

				var new_window = window.open(jQuery(this).attr('href'), '', 'scrollbars=yes,menubar=no,height=600,width=670,resizable=no,toolbar=no,location=no,status=no,top=' + top + ',left=' + left + '\'');
				return false;
			});
		});

	});



});


// Public functions

function TabClicked(clicked, content, hidden) {
	if (clicked == null) {
		if (jQuery('li.tabs-selected') != null)
			clicked = jQuery('li.tabs-selected >a').attr('href');
	}
	if (clicked != null) {
		var id = clicked.toString();
		id = id.split('#');
		var link = jQuery('#link-' + id[1]).attr('value');
		jQuery('a.PagePrintLink').attr('href', link + '?p=1');
	}
}


function SwitchStoryboardTab(id, el) {
	jQuery('.StoryboardThumbOverlay').css('opacity', 0.5);
	jQuery('.StoryboardBannerIndividualBanner').fadeOut();
	jQuery('.' + id).fadeIn();
	el.css('opacity', '0.1');
	return false;
}

function SetFontSize(size) {
	/*if(currentFontSize != size && jQuery('div#StoryboardLayout').text().length==0) {*/
	if (currentFontSize != size) {

		if (currentFontSize == 0)
		{ currentClass = 'small'; }
		else if (currentFontSize == 1)
		{ currentClass = 'medium'; }
		else if (currentFontSize == 2)
		{ currentClass = 'large'; }

		if (size == 0)
		{ newClass = 'small'; }
		else if (size == 1)
		{ newClass = 'medium'; }
		else if (size == 2)
		{ newClass = 'large'; }


		jQuery('#ContentWide').removeClass(currentClass).addClass(newClass);
		jQuery('#ContentLeft').removeClass(currentClass).addClass(newClass);
		jQuery('#ContentLeftStretch').removeClass(currentClass).addClass(newClass);


		currentFontSize = size;
		createCookie("FontSize", size);

		Cufon.replace('#Content h1');
		Cufon.replace('#Content h2');
		Cufon.replace('#NexenOperations h1');
		Cufon.replace('#Events h1');
		Cufon.replace('#News h1');
		Cufon.replace('#SRInfo h1');
		Cufon.replace('#SRDownload h1');
		Cufon.replace('#emergency h1');
		Cufon.replace('#emergency h2');
	}
	return false;
}



function createCookie(name, value) {
	document.cookie = name + "=" + value + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var results = document.cookie.split(';');
	for (var i = 0; i < results.length; i++) {
		var c = results[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 checkCookieFontSize() {
	var CookieFontSize = readCookie("FontSize");
	if (CookieFontSize != null) {
		if (currentFontSize != CookieFontSize) {
			SetFontSize(CookieFontSize);
		}
	}
}

function fncPrint(objLink) {
	strUri = document.location.href;
	if (strUri.indexOf('?') == -1)
		jQuery(objLink).attr('href', strUri + '?p=1');
	else
		jQuery(objLink).attr('href', strUri + '&p=1');
	jQuery(objLink).attr('target', '_blank');
	return true;
}
// search section cannot be isolated via asp:panel, since black bar nav used XSL, not ASP.NET, this is the workaround
function fncSearchSection_Submit() {
	/* SECTION SEARCH */
	var intLen = document.forms[0].q.length;
	for (var i = 0; i < intLen; i++)
		if (document.forms[0].q[i])
			if (document.forms[0].q[i].value != document.forms[0].q[i].defaultValue)
				break;

	if (i != intLen) { // if site section submitted then redirect
		document.location.href = "/SearchResults.aspx?q=" + document.forms[0].q[i].value + "&sk=" + jQuery(document.forms[0].q[i]).attr('sec');

		return false;
	}

	/* FORMSTACK - Contact */
	if (jQuery("script[src*='formstack']").length > 0)
		document.forms[0].action = 'http://www.formstack.com/forms/index.php';
	else if (jQuery("script[src*='formspring']").length > 0)
		document.forms[0].action = 'http://www.formspring.com/forms/index.php';
}
function fncSearchSection_Click(objLink, strInput, strName) { /* on hyperlink */
	if(jQuery('#'+strInput).val().toLowerCase()=='search this section')
		jQuery('#' + strInput).val('');
	document.location.href = "/SearchResults.aspx?q=" + jQuery('#' + strInput).val() + "&sk=" + strName;
}
function fncSearchSection_Blur(objInput) {
	if (jQuery(objInput).val().length == 0)
		jQuery(objInput).val('SEARCH THIS SECTION');
}
function fncSearchSection_Focus(objInput) {
	if (jQuery(objInput).val().toLowerCase() == 'search this section')
		jQuery(objInput).val('');
}
function fncPrintSitemap() {
 // see print.js
}
    jQuery(document).ready(function () {
        SetVertSpacer();
    });

    function SetVertSpacer() {
        if (jQuery('.flexvertspacer') == null) { return; }
        jQuery('.flexvertspacer').each(function (index) {
            var divParent = jQuery(this).parent();
            var height = divParent.height() - 40;
            jQuery(this).height(height);
        });
    }



