// *************************** jQuery Initialisation **************************** //

function slideSwitch() {
    var $active = $('#homeBanner a.active');
    if ( $active.length == 0 ) $active = $('#homeBanner a:last');
    var $next =  $active.next().length ? $active.next()
        : $('#homeBanner a:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

function demoSwitch() {
    var $active = $('#demoBanner a.active');
    if ( $active.length == 0 ) $active = $('#demoBanner a:last');
    var $next =  $active.next().length ? $active.next()
        : $('#demoBanner a:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}


$(document).ready(function() {

	// homepage banner animation
	$("#homeBanner a:first").attr("class", "active");
	$("#homeBanner").append('<a href="productOverview.asp"><img src="pics/homeBanner2.jpg" width="555" height="175" /></a>');
	$("#homeBanner").append('<a href="productOverview.asp"><img src="pics/homeBanner3.jpg" width="555" height="175" /></a>');
	$("#homeBanner").append('<a href="productOverview.asp"><img src="pics/homeBanner4.jpg" width="555" height="175" /></a>');
	$("#homeBanner").append('<a href="productOverview.asp"><img src="pics/homeBanner5.jpg" width="555" height="175" /></a>');
	$("#homeBanner").append('<a href="productOverview.asp"><img src="pics/homeBanner6.jpg" width="555" height="175" /></a>');
	$("#homeBanner").append('<a href="productOverview.asp"><img src="pics/homeBanner7.jpg" width="555" height="175" /></a>');
	$("#homeBanner").append('<a href="productOverview.asp"><img src="pics/homeBanner8.jpg" width="555" height="175" /></a>');
	$("#homeBanner").append('<a href="productOverview.asp"><img src="pics/homeBanner9.jpg" width="555" height="175" /></a>');
	setInterval( "slideSwitch()", 5000 );

	// demopage banner animation
	$("#demoBanner a:first").attr("class", "active");
	$("#demoBanner").append('<a href="productOverview.asp"><img src="pics/demo1.gif" width="770" height="459" /></a>');
	$("#demoBanner").append('<a href="productOverview.asp"><img src="pics/demo2.gif" width="770" height="459" /></a>');
	$("#demoBanner").append('<a href="productOverview.asp"><img src="pics/demo3.gif" width="770" height="459" /></a>');
	$("#demoBanner").append('<a href="productOverview.asp"><img src="pics/demo4.gif" width="770" height="459" /></a>');
	$("#demoBanner").append('<a href="productOverview.asp"><img src="pics/demo5.gif" width="770" height="459" /></a>');
	$("#demoBanner").append('<a href="productOverview.asp"><img src="pics/demo6.gif" width="770" height="459" /></a>');
	$("#demoBanner").append('<a href="productOverview.asp"><img src="pics/demo7.gif" width="770" height="459" /></a>');
	$("#demoBanner").append('<a href="productOverview.asp"><img src="pics/demo8.gif" width="770" height="459" /></a>');
	$("#demoBanner").append('<a href="productOverview.asp"><img src="pics/demo9.gif" width="770" height="459" /></a>');
	$("#demoBanner").append('<a href="productOverview.asp"><img src="pics/demo10.gif" width="770" height="459" /></a>');
	$("#demoBanner").append('<a href="productOverview.asp"><img src="pics/demo11.gif" width="770" height="459" /></a>');
	$("#demoBanner").append('<a href="productOverview.asp"><img src="pics/demo14.gif" width="770" height="459" /></a>');
	setInterval( "demoSwitch()", 3000 );

	// add the menus to the DOM
	for(key in menu) {
		var str = '<div class="subMenu">';
		var subMenu = menu[key];
		for(subKey in subMenu) {
			str += '<div><a href="' + subMenu[subKey] + '">' + subKey + '</a></div>';
		}
		str += '</div>';
		$("#" + key).append(str);
	}

	// show and hide the sub-menus on hover 
	var t = "";
	$("#priNav .menu").hover( function () {
		$(this).find(".subMenu").show();
		t = $(this).find("img").attr("title"); // hide the images title as was
		$(this).find("img").attr("title", ""); // causing mouseout probs in safari
	}, function () {
		$(this).find(".subMenu").hide();
		$(this).find("img").attr("title", t); // restore the image title
	} );
	// add a hover class - because ie doesn't support 
	// css :hover styles on anything other than an anchor
	$("#priNav .menu .subMenu div").hover( function () {
		$(this).addClass("hover");
	}, function () {
		$(this).removeClass("hover");
	} );

	// mouse over images
	$('#priNav img').imghover();
	$('#homeContact .btn').imghover();
	$('#secNav img').imghover();
	$('#marginContact .btn').imghover();
	$('.mailto img').imghover();

	// mailto obsfucation
	$('.mailto').mailtoObsfucator();

	// default form value restoration
	$('#homeContact input[type=text]').add('#homeContact textarea').each(function(){
		$(this).defaultRestore();
	});
	$('#marginContact input[type=text]').add('#marginContact textarea').each(function(){
		$(this).defaultRestore();
	});

	$('#prevNext div').hide();
	$('#prevNext div:first').show();
	$('#prevNext div').each(function(){
		var str = '<p class="prevNextNav">';
		if( !$(this).is(':first-child') ){
			str += '<a href="#" class="prev"><< previous</a>';
		}
		if( !$(this).is(':last-child') ){
			str += '<a href="#" class="next">next >></a>';
		}
		str += '</p>';
		$(this).append(str);
	});
	$('#prevNext div a.next').click(function(){
		$(this).parent().parent().next().show();
		$(this).parent().parent().hide();
		return false;
	});
	$('#prevNext div a.prev').click(function(){
		$(this).parent().parent().prev().show();
		$(this).parent().parent().hide();
		return false;
	});

});

