
// Requires a reference to JQuery ( src="http://jquery.com/src/latest/" )

$(function(){

	// Read landscape image attributes and collapse it before animating it:
	var $divLandscapeImg = $("#divLandscapeImg");
	var heightTo = parseInt($divLandscapeImg.css("height"));
	var marginTo = parseInt($divLandscapeImg.css("marginTop"));
	$divLandscapeImg.css({marginTop:marginTo+heightTo+20, height:0});
	$("#divFooterAd").hide();

	var elemIdFactory = 0;

	// Animate landscape image when page loads:
	$divLandscapeImg.each(function(){

			// For IE, workaround display:block anim quirk by auto-correcting using onpropertychange:
			// (Only necessary on elements that are not display:block to start with)
			if($.browser.msie && $(this).css("display") != 'block') {
				$(this)
					.attr("_origDisplay", $(this).css("display"))
					.bind("propertychange", function(){ if(this.currentStyle.display != this._origDisplay) this.style.display = this._origDisplay; })
			}

			$(this).animate({marginTop:marginTo, height:heightTo}, 1000);

		})
	;

	$("#divFooterAd").slideDown("slow");


	// Disable any links that self-reference the current page:
	var trailingSlash = /\/$/;
	var thisPage = document.location.href.split('?')[0].replace(trailingSlash,'').toLowerCase();

	$("A").each(function(){

		if(this.href.split('?')[0].replace(trailingSlash,'').toLowerCase() == thisPage) {
			var text  = $(this).html();
			var title = this.title || this.alt;
			var linkReplacement = '<span title="' + title + '">' + text + '</span>';
			$(this).after(linkReplacement).remove();
		}

	});


	// Randomly reorder lists tagged with the clsRotatingLinks className:
	var $listEl = $("DL.clsRotatingLinks");
	randomiseJQryArray($("DT", $listEl));

	// Hide all clsRotatingLinks then show them one by one:
	// (Only proceed if page includes a reference to the jQuery orderedEffect plugin.)
	if(jQuery.fn.orderedEffect) {

		$("DT", $listEl)
			.hide().lt(5)
			.each(function(){
				// Fix IE quirk that produces blurry text when Windows ClearType is enabled:
				if($.browser.msie && $(this).css("backgroundColor")=='transparent') $(this).css("backgroundColor", "white");
			})
			.orderedEffect("show", "slow");

	};

	// Activate the .clsMoreInfo hover links:
	$("SPAN.clsMoreInfo").each(function(){

		var $el = $(this);

		// Explicitly set element height & width: (to workaround show/hide quirk)
		$el.css({ width: this.offsetWidth, height: this.offsetHeight });

		// Explicitly set element background color: (to workaround show/hide quirk on WinIE with ClearType enabled) 
		if($.browser.msie && $el.css("backgroundColor") == 'transparent') $el.css("backgroundColor", "white");

	}).hide(1, function(){

		$(this).parent("DT")
		.hover(function(e){
			$("SPAN.clsMoreInfo:hidden", this).each(function(){
				var tmrID = setTimeout(getJQrySyntax(this) + '.show("slow")', 500);
				$(this).attr("showHideTmrID", tmrID);
			})
		},function(e){
			$("SPAN.clsMoreInfo:hidden", this).each(function(){
				if( $(this).attr("showHideTmrID") )
					window.clearTimeout( $(this).attr("showHideTmrID") );
			})
		})

	});


	// Add brand word-colouring to every tag containing 'software unity':
	$("H1, SPAN, LEGEND").filter(":contains('software unity')").each(function(){
		// If text is 'software unity' then replace it with pretty coloured equivalent:
		// (Fixed: Bug in JQuery prevented a search for both words from working because of the space between the words.
		var skip = {'HTML':true, 'HEAD':true, 'BODY':true, 'SCRIPT':true};
		if(this.innerHTML.toLowerCase().indexOf('software unity') == 0 && !skip[this.nodeName]) {
			$(this).html( this.innerHTML.replace("software unity", "<span class='clsSuNameSw'>software</span><span class='clsSuNameUnity'>unity</span>") );
			//alert(this.innerHTML);
		}
	});


	// Enable 'mailto' links by converting {at} to a real symbol: (Unicode for 'at' symbol is u0040)
	$(function(){
		$("DD:contains(' {at} ')").each(function(){
			var a = $(this).text().replace(' {at} ', '\u0040');
			$(this).html("<a href='mailto:" + a + "'>" + a + "</a>");
		})
	});


	function getJQrySyntax(el){
	// Return JQuery selector syntax string to return the specified element.
	// Eg: If el is myElement then this returns '$("#myElement")'

		// Ensure element has an id to use in the jquery syntax:
		if(!el.id) el.id = el.uniqueID || 'su__id' + elemIdFactory++ ;
		return '$("#' + el.id + '")';

	}

	function randomiseJQryArray($elems){
	// Randomly reorder the items in a jQuery array: (and in the DOM)
		if($elems && $elems.size() > 0){
			var i = $elems.size(), rnd;
			while(--i){
				rnd = Math.floor( Math.random() * (i+1) );
				$($elems[rnd]).before($elems[i]);
			}
		}
		return $elems;
	}

});

