//functions for popups
function showBlackout() {
    if (!$('#blackout').length) {
    	if (!window.XMLHttpRequest) { //IE6-
    		$('select').hide();
		}
        $('body').append('<div id="blackout"></div>');
        if (!window.XMLHttpRequest) { //IE6-
    		$('#blackout').css('height', $('body').height()+"px");
		}
    }
}
function removeBlackout() {
	if (!window.XMLHttpRequest) { //IE6-
    	$('select').show();
	}
    $('#blackout').remove();
}
function centerElement(elementID) {
    if (window.innerWidth) {
        var windowWidth = window.innerWidth;
        var windowHeight = window.innerHeight;
    }
    else if (document.all) {
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
    }
    var elementWidth = $('#'+elementID).width();
    var elementHeight = $('#'+elementID).height();
    if (!window.XMLHttpRequest) { //IE6-
    	$('#'+elementID).css('position', 'absolute');
        window.scrollTo(0,0);
	}
	else {
		$('#'+elementID).css('position', 'fixed');
	}
    $('#'+elementID).css('z-index', '99999');
    $('#'+elementID).css('top', (windowHeight/2)-(elementHeight/2));
    $('#'+elementID).css('left', (windowWidth/2)-(elementWidth/2));
}

//used for the in-visible pseudo selector
(function($){$.belowthefold=function(element,settings){var fold=$(window).height()+$(window).scrollTop();return fold<=$(element).offset().top-settings.threshold;};$.abovethetop=function(element,settings){var top=$(window).scrollTop();return top>=$(element).offset().top+$(element).height()-settings.threshold;};$.rightofscreen=function(element,settings){var fold=$(window).width()+$(window).scrollLeft();return fold<=$(element).offset().left-settings.threshold;};$.leftofscreen=function(element,settings){var left=$(window).scrollLeft();return left>=$(element).offset().left+$(element).width()-settings.threshold;};$.inviewport=function(element,settings){return!$.rightofscreen(element,settings)&&!$.leftofscreen(element,settings)&&!$.belowthefold(element,settings)&&!$.abovethetop(element,settings);};$.extend($.expr[':'],{"below-the-fold":function(a,i,m){return $.belowthefold(a,{threshold:0});},"above-the-top":function(a,i,m){return $.abovethetop(a,{threshold:0});},"left-of-screen":function(a,i,m){return $.leftofscreen(a,{threshold:0});},"right-of-screen":function(a,i,m){return $.rightofscreen(a,{threshold:0});},"in-viewport":function(a,i,m){return $.inviewport(a,{threshold:0});}});})(jQuery);
//work out how much is visible
var amountVisible = function(c){
	//var fromTop = 300; <- jp thought i might need this, but didnt
	var a = $(window).scrollTop();
	var b = a + $(window).height();
	var e = c.offset().top;
	var d = e + c.height();
	if( d > a && d < b ) {
		return d - a;
	}
	else{
		return b - e;
	}
};
//work out the current slide item in the page to reflect the nav
var determineCurrentNodeAndSetNav = function(){
	var a = null;
	var b = $(".scrollItem:in-viewport");
	b.each(function(c){
		var d = $(this);
		if (a == null || amountVisible(d) >= amountVisible(a)) {
			a = d;
		}
	});
	
	current_index = $(".scrollItem").index(a);
	updateNavItem($('#subNav ul li a').eq(current_index));
};

function updateNavItem(item) {
	if (!item.is('.selected')) {
		$('.selected', $('#subNav')).removeClass('selected');
		$(item).addClass('selected');
		
		/*$('.currentBanner').removeClass('currentBanner').addClass('hidden');
		$('#'+$(item).attr('rel')).addClass('currentBanner').removeClass('hidden');*/
		$('.currentBanner').fadeOut().removeClass('currentBanner');
		$('#'+$(item).attr('rel')).fadeIn('normal', function() {
			$('body').data('doNotScrollEvent', false);
		}).addClass('currentBanner');
	}
}

// slide to a given slide (on the homepage)
function slideToSlide(newSlide) {
	var newLeft = newSlide * 940;
	$('#slider').stop().animate({ left: '-'+newLeft+'px' }, 800);
	$('#slider').data('curSlide', newSlide);
}

$(function() {
	$('#slider').data('curSlide', 0);
	
	$('#homeButtons a').bind('mouseover', function() {
		slideToSlide($(this).attr('rel'));
	});
	$('#homeButtons').bind('mouseout', function(e) {
		if ($(e.relatedTarget).is('.slide')) {
			slideToSlide(0);
			return false;
		}
	});
	
	$.smoothAnchors("normal", null, false);
	
	$('body').data('doNotScrollEvent', false);
	
	$('#subNav li a').bind('click', function() {
		$('body').data('doNotScrollEvent', true);
		updateNavItem($(this));
	});
	
	if (window.location.hash) {
		determineCurrentNodeAndSetNav();
	}
	
	if (!$.browser.msie || ($.browser.msie && $.browser.version.substr(0,1)>=7)) {
		$(window).scroll(function(){
			if (!$('body').data('doNotScrollEvent')) {
				determineCurrentNodeAndSetNav();
			}
		});
	}
	
	$('.showCustomerJourneyPopup').bind('click', function() {
		showBlackout();
		$('#customerJourneyPopup').show();
		centerElement('customerJourneyPopup');
		swfobject.embedSWF("/flash/InteractiveVideoPlayer.swf", "replaceCustomerJourney", "640", "360", "9.0.0","expressInstall.swf", {}, {}, {});
		return false;
	});
	$('.closeCustomerJourneyPopup').bind('click', function() {
		$('#customerJourneyPopup').hide();
		removeBlackout();
		return false;
	});
	
	/*$('.showWordFromFloorPopup').bind('click', function() {
		showBlackout();
		$('#wordFromFloorPopup').show();
		centerElement('wordFromFloorPopup');
		swfobject.embedSWF("/flash/InteractiveVideoPlayer.swf", "replaceWordFromFloor", "640", "360", "9.0.0","expressInstall.swf", {}, {}, {});
		return false;
	});
	$('.closeWordFromFloorPopup').bind('click', function() {
		$('#wordFromFloorPopup').hide();
		removeBlackout();
		return false;
	});*/
	
	$('.showVideoPopup').bind('click', function() {
		showBlackout();
		$('#videoPopup .popupTitle').attr('src', '/img/video-titles/'+$(this).attr('rel')+'.gif');
		if ($(this).attr('rel') == 'all') {
			$('#videoPopup').addClass('allGraduates');
		}
		else {
			$('#videoPopup').removeClass('allGraduates');
		}
		$('#videoPopup').show();
		centerElement('videoPopup');
		swfobject.embedSWF("/flash/VideoPlayer.swf", "replaceVideo", "480", "270", "9.0.0","expressInstall.swf", { "meta" : "/flash/playlists/"+$(this).attr('rel')+".json"}, {allowscriptaccess: 'sameDomain', allowfullscreen: 'true'}, {id: "replaceVideo", name: "replaceVideo"});
		return false;
	});
	$('.closeVideoPopup').bind('click', function() {
		$('#videoPopup').hide();
		sendMessage("replaceVideo", "stopVideo", "{}");
		removeBlackout();
		return false;
	});
	
	$('.showFlashPopup').bind('click', function() {
		showBlackout();
		$('#flashPopup').show();
		centerElement('flashPopup');
		swfobject.embedSWF("/flash/MatalanProgrammeSelector.swf", "flashReplace", "545", "600", "9.0.0","expressInstall.swf", {}, {}, {});
		return false;
	});
	$('.closeFlashPopup').bind('click', function() {
		$('#flashPopup').hide();
		removeBlackout();
		return false;
	});
	
	
	$('.askedQuestion h2').bind('click', function() {
		var myAnswer = $(this).siblings('.answer');
		
		if (!$(this).children('a').is('.viewing')) {
			$('.viewing').removeClass('viewing');
			$('.answer:visible').hide();
			myAnswer.show();
			$(this).children('a').addClass('viewing');
			//for IE6
			myAnswer.css('visibility', 'visible');
		}
		else {
			myAnswer.hide();
			$(this).children('a').removeClass('viewing');
		}
		return false;
	});
	/*$('.askedQuestion h2').bind('mouseout', function() {
		var myAnswer = $(this).siblings('.answer');
		myAnswer.hide();
		return false;
	});*/
	
	$('#refreshFact').bind('click', function() {
		$.post('/ajax/matalan.php', {'function' : 'getFact', 'current' : $('#currentFact').val()}, function(data) {
			var json = eval('('+data+')');
			$('#currentFact').val(json.id)
			$('#factText').html(json.text);
		});
		return false;
	});
});