/*
	display.js
	
	Javascripts for Creative Automation website
*/

jQuery(document).ready(function($) {
	// home banner rotator
	if (document.getElementById('home_banner')) { rotator(); }
	// home banner rotator
	function rotator() {
		tick = 5; // number of seconds between image changes.
		fade = 500; // duration of fade-in, in milliseconds
		var comment = $('#home_banner').html();
//	alert(comment);
		var bgurl = $('#home_banner').css('background-image');
		if (!bgurl) { return; }
		var imgpath = bgurl.substring(bgurl.indexOf('(')+1,bgurl.lastIndexOf('/')+1) + 'home_banner/';
		if (isIE()>=6) { // IE inserts extra quotemarks into the background-image value. Or moz/saf strip them. Hard to say.
			var imgpath = bgurl.substring(bgurl.indexOf('(')+2,bgurl.lastIndexOf('/')+1) + 'home_banner/';
		} else {
			var imgpath = bgurl.substring(bgurl.indexOf('(')+1,bgurl.lastIndexOf('/')+1) + 'home_banner/';
		}
		var imgstr = comment.substring(5,comment.indexOf(' -->',6));
		imgstr = jQuery.trim(imgstr);
		imglist = imgstr.split(','); // should be a list of the actual imgs to be used. Roll with it.
		if (!imgstr || imglist.length<1) { return; }
		preloader = [];
		var lastimg = imglist.length - 1;
		var hbhtml = $('#home_banner').html();
		for (var a=0;a<imglist.length;a++) { // preload the images in the rotator.
			preloader[a] = new Image();
			preloader[a] = imgpath + imglist[a];
			hbhtml += '<img src="wp-content/themes/creative_automation/images/home_banner/'+imglist[a]+'" id="home_banner_'+a+'" class="slideshow" style="display:none;" width="950" height="339" />';
		}
		$('#home_banner').html(hbhtml);
		$(function() {
			setInterval(slides,tick*1000 );
		});
	}
	function slides() {
		var $active = $('#home_banner img.live');
		if ($active.length==0) { $active = $('#home_banner img:last'); }
		var $next = $active.next().length ? $active.next() : $('#home_banner img:first');
		$active.css('z-index',1).addClass('remove').removeClass('live');
		$next.css('z-index',2).fadeIn(fade,function() { $('#home_banner img.remove').css('display','none').removeClass('remove'); }).addClass('live');
	}
	// generic rollover function and preloader
	rpload = [];
	$('.rollover').each(function() {
		// preload the images to appear on rollover
		rpload.push(new Image());
		var a = rpload.length;
		a = a-1;
		rpload[a] = $(this).attr('src').replace(/(\.(jpg|png|gif)$)/,"_over$1");
	}).hover(function() {
		var oldsrc = $(this).attr('src');
		var newsrc = oldsrc.replace(/(\.(jpg|png|gif)$)/,"_over$1");
		$(this).attr('src',newsrc);
	},function() {
		var oldsrc = $(this).attr('src');
		var newsrc = oldsrc.replace(/_over(\.(jpg|png|gif)$)/,"$1");
		$(this).attr('src',newsrc);
	});
	// compensating for the faults of Internet Explorer
	if (isIE() && isIE()<8) {
		$(':last-child').addClass('last-child');
		$(':first-child').addClass('first-child');
		if (isIE()<=6) {
			$('#nav li').hover(function(){$(this).addClass('hover');},function(){$(this).removeClass('hover');});
		}
	}
});


// Tests for MSIE version. Returns version whole number
function isIE() {/*@cc_on
if(@_jscript_version < 5.6){return 5;}if(@_jscript_version < 5.7){return 6;}if(@_jscript_version < 5.8){return 7;}if(@_jscript_version < 5.9){return 8;}
@*/ return 0;}

function launchAll() {
	if (!document.getElementById) { return; }
}
window.onload = launchAll;

/* EOF */

