$(window).load(function() {
	$('.logo').ifixpng().css('cursor', 'pointer');
	$('.SubNavigation li a').ifixpng().css('cursor', 'pointer');
	$('.orange-bullet-list li').ifixpng();
	$('td.ms-sbgo a img').ifixpng();
	// DAJW design haus added check for js var set inline before this file loaded. Var is only set in edit mode of Publishing page.
	var sifrDisable = (typeof (sifrDisableFlag) != "undefined");
	
	if (!sifrDisable) {
		$('.professional-services').children('ul').hide();
		$('.business-services').children('ul').hide();
		$('.maintenance-services').children('ul').hide();
	} else {
		$("body").addClass("editmode");
	}
	
	// make any sun catalogue links open in a new window
	$('a.suncat').bind('click', function() {
		selfPopup(this, 800, 600);
		return false;
	});
	// hover state for tables with listing class
	$('table.listing td').hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});
	
	// hover state for download panels for case studies
	$('.content-full-section').hover(function() {
		$(this).addClass("content-full-section-hover");
	}, function() {
		$(this).removeClass("content-full-section-hover");
	});
	
	// toggle the menu that selects the keyword filtering
	$('#csPop a[href="#"]').click(function() {
		g_bWarnBeforeLeave = false;
		$('#csPop ul').slideDown();
		return false;
	});
	
	$('#csPop ul a').click(function() {
		$('#csPop ul').slideUp();
		// set input value to href attr of clicked link
		$('input.casestudyfilterpicker').val($(this).attr('href'));
		g_bWarnBeforeLeave = true;
		return false;
	});
		
	var current = 0,
		visibleDuration = 2000,
		kids = $(".SubNavigation").children("li"),
		panels = {
			0: "managed-services-header",
			1: "professional-services-header",
			2: "maintenance-services-header",
			3: "business-services-header"
		}
		
	//slow down accordion anims
	$.extend($.ui.accordion.animations, {
		slowslide: function(options) {
			$.ui.accordion.animations.slide(options, { duration: 3500 }); 
		}
	});
	
	//remove margins for smoothness
	//$(".SubNavigation").find("ul").css("marginBottom", 0);
	
	//show the right panel at start
	$("#managed-services-header").css("zIndex", 200);
	$("#maintenance-services-header").css("zIndex", 0);
	
		
	//show next panel
	function rotate() {
		var next = (current < kids.length - 1) ? current + 1 : 0;
		$(".SubNavigation").accordion("activate", next);
		
		$(".SubNavigation").find(".on").removeClass("on");
		$(".SubNavigation").children("li").eq(next).addClass("on");
		
		//switch panels
		$('.homepage-cover').fadeIn(2000, function() {
		    $("#HomeHeaderInner").children("div").not(".homepage-fadein, .homepage-cover").css("zIndex", -2);
			$("#" + panels[next]).css("zIndex", 200);
			$('.homepage-cover').fadeOut(2000);
		});
	}
	
	//init ui accordion	
	$("#HomeHeaderInner .SubNavigation").accordion({
		autoHeight: false,
		animated: "slowslide",
		change: function() {
			(current < kids.length - 1) ? current++ : current = 0;
			
			setTimeout(function(){ rotate(); }, visibleDuration);
		}
	});
	
	//fix broken IE7 styling
	if ($.browser.msie) {
		if ($.browser.version === "7.0" || $.browser.version === "6.0") {
			$(".ui-accordion-header").css({
				position: "relative",
				left: -20,
				paddingTop: 0,
				paddingBottom: 0,
				outline: "none"
			});
			
			$(".ui-accordion-content").css("overflow", "hidden");
			
			$(".ui-accordion-content").find("a").css({
				zoom: 0
			});
		}
		
	}
	
	//auto rotate
	setTimeout(function(){ rotate(); }, visibleDuration);	
});


// Usage:
// renderGoogleMapEmbedded('IdOfInputControl'); - function will read value of input (hidden, text) control to geocode
// renderGoogleMapEmbedded('', 'Bitterne Road, Southampton'); - function will use supplied address to geocode
function renderGoogleMapEmbedded(mapDataElement, addressSupplied) {
	// render the google map
	var geocoder = null;
	var map = null;
	var lat=0.0;
	var lon=0.0;
	var address = "";
	var mapRenderElement = "map_canvas";
	
	// don't show in edit mode.
	if (typeof(disableGoogleMaps)!="undefined") {
		return;
	}
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(mapRenderElement));
		map.setUIToDefault();
		geocoder = new GClientGeocoder();
		geocoder.setBaseCountryCode("uk");
		// add marker to map
		map.clearOverlays();
		// get address data
		if (typeof(addressSupplied)=="undefined") {
			address = $("#"+mapDataElement).val();
		} else {
			address = addressSupplied;
		}
		geocoder.getLatLng(address, function(point) {
			if (!point) {
				//alert(address + " not found by google maps");
			} else {
				map.setCenter(point, 13);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				GEvent.addListener(marker , "click", function() {
					window.open("http://maps.google.co.uk/maps?q="+address);
				});
			}
		});
	}
}
/* selfPopup
* open popup window with href determined from the href attribute on the anchor:
* <a href="index.html" onclick="selfPopup(this,300,400); return false;">Index</a>
*/
function selfPopup( loc, x, y ) {
	LeftPosition = (screen.width) ? (screen.width-x)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-y)/2 : 0;
	window.open(loc.href,"","toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width="+x+",height="+y+',left=' +LeftPosition + ',top=' +TopPosition +"");
}
