// *************************** 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');
        });
}




$(document).ready(function() {

	// homepage banner animation
	$("#homeBanner a:first").attr("class", "active");
	$("#homeBanner").append('<a href="m2m.asp"><img src="pics/homeBanner2.jpg" width="555" height="135" /></a>');
	$("#homeBanner").append('<a href="fixedIP.asp"><img src="pics/homeBanner3.jpg" width="555" height="135" /></a>');
	$("#homeBanner").append('<a href="bespokeSolutions.asp"><img src="pics/homeBanner4.jpg" width="555" height="135" /></a>');
	setInterval( "slideSwitch()", 5000 );

	// 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();
	});

});
