var ieversion, base_href; // Hold base href globally for IE's benefit

jQuery(window).load(function()
	{
	// Stretch text so that select boxes are below main image on story/school/library pages
	// ------------------------------------------------------------------------------------
        jQuery('#story .richtext-container, .article .richtext-container').css('min-height', jQuery('#story-main-graphics img').height());

        // Rounded corners classnames
	// --------------------------
	jQuery('.rounded').each(function()
		{
		var wrapperDiv;
	        wrapperDiv = document.createElement('div');
        	wrapperDiv.className = 'rounded';
		this_JQ = jQuery(this);
                this_JQ.wrap(wrapperDiv).parent().css('float', this_JQ.css('float')).css('margin-top', this_JQ.css('margin-top')).css('margin-left', this_JQ.css('margin-left')).css('margin-right', this_JQ.css('margin-right')).css('margin-bottom', this_JQ.css('margin-bottom')).append('<div class="top-right"></div><div class="top-left"></div><div class="bottom-right"></div><div class="bottom-left"></div>');
              	this.style.margin = '0';
		});
	});

jQuery(document).ready(function() {

	var base;
	var subNavLis, subNavWidth, liMinWidth, liMaxWidth, liTotalWidth, liBorderPadding, equalWidth, liCurrentWidth, liCurrentTotalWidth, i, width, widthRemainder, lastOffsetLeft, wrappedFlag, liWidths;

	// Test for IE
	// -----------
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) //test for MSIE x.x;
		ieversion=new Number(RegExp.$1);
	else
		ieversion = null;
		
        // base href for IE
        // ----------------
        base = document.getElementsByTagName('base');
        if (base && base[0] && base[0].href) base_href = base[0].href;
        else base_href = 'http://'+document.domain;

	
	// FLIR
	// ----
	FLIR.init({ path: base_href+'assets/templates/goingandcoming/facelift/' });
	if (document.getElementById('gallery')) FLIR.replace('div#gallery h3', new FLIRStyle({ cLine: 1.1, mode: 'wrap' }));
	function flir_script()
		{
		FLIR.replace('div#content h2', new FLIRStyle({ cLine: 1.1, mode: 'wrap' }));
		jQuery('#select-user, #select-school, #select-library').find('label, h3').each(function() { FLIR.replace(this); });
		}
	if (ieversion == null || ieversion >= 7) flir_script();
	else setTimeout(flir_script, 100);

	// ClueTip
	// -------
	jQuery('a.tip').cluetip({
		cluetipClass: 'gandc', 
		splitTitle: '|',
		arrows: true
	});

	// JS only elements
	// ----------------
	jQuery('.JS').removeClass('JS');

	// Stretch and attempt to equalise sub-nav tabs
	// --------------------------------------------

	subNavWidth = document.getElementById('sub-nav').offsetWidth;
	liMinWidth = 5000;
	liMaxWidth = 0;
	liTotalWidth = 0;

	subNavLis = jQuery('#sub-nav li');

	if (subNavLis.length > 1)
		{

		// Get widths of <li>s in #sub-nav and detect wrapping of tabs to next line
		liWidths = new Array();
		lastOffsetWidth = -1;
		wrappedFlag = false;
		subNavLis.each(function(i) {
			liTotalWidth += (liWidths[i] = this.offsetWidth);
			if (liWidths[i]<liMinWidth) liMinWidth = liWidths[i];
			if (liWidths[i]>liMaxWidth) liMaxWidth = liWidths[i];
			if (this.offsetLeft < lastOffsetLeft) wrappedFlag = true;
			lastOffsetLeft = this.offsetLeft;
			});

		if (wrappedFlag)
			{
			// Tabs have wrapped - need to reduce in size.
			// -------------------------------------------
			subNavLis.css('padding', '0').find('span,a').css('padding', '0');
			document.getElementById('sub-nav').style.width = liTotalWidth+'px'; // fallback - stretch #sub-nav
			}
		else
			{
			// Tabs need to be stretched (or already fit perfectly)
			// ----------------------------------------------------
			
			// Difference between width and offsetWidth
			liBorderPadding = subNavLis.eq(0).outerWidth() - subNavLis.eq(0).width();

			// Check for simplest case - all <li>s able to be the same width or within 1px of the same width
			// Note that not all browsers handle fractional pixels so we must take care of this.
			equalWidth = Math.floor(subNavWidth / liWidths.length);
			if (equalWidth >= liMaxWidth)
				{
				subNavLis.width(width = equalWidth - liBorderPadding);
			
				// Check last <li> is really on the rhs - some, but never all, <li>s may have to be widened by 1px
				if (widthRemainder = subNavWidth - equalWidth * liWidths.length) subNavLis.filter(':lt('+widthRemainder+')').width(width+1);
				}

			else
				{
				// Awkward case - Increment <li>s starting with the narrowest.

				liCurrentTotalWidth = liTotalWidth;

			widthScan:
				for (liCurrentWidth = liMinWidth; liTotalWidth<subNavWidth; ++liCurrentWidth)
					{
					for (i=0; i<liWidths.length; ++i)
						{
						if (liWidths[i] == liCurrentWidth)
							{
							++liWidths[i];
							++liCurrentTotalWidth;
							}

						if (liCurrentTotalWidth == subNavWidth)
							// Finished! break out of loops
							break widthScan;
						}
					}

				subNavLis.each(function(i)
					{
					this.style.width = (liWidths[i] - liBorderPadding).toString().concat('px');
					});
				}
			}
		}
	});
