////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// uni_widget.js
//
// This script provides all rich interface functionality for the 
// DG Student Finance Widget.
//
//
// Developed by Michael Fuchs,
//   Michael.Fuchs@directgov.gsi.gov.uk / fuchs@michaelfuchs.org
//
// _revision history_
// 2010.07.07, MF: first written
//
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////


// Set global var for referer (empty by default).

var my_referer = '';


// Get our referer from appended arg, if widget version provides this.
// (Cut off '?' at beginning.)

if ( location.search ) {

	my_referer = location.search.substring ( 1 );

}


// This is stuff jquery runs when the page is ready for manipulation.
// It includes positioning, rendering, and binding things.

$(document).ready(function() {


	// Register this as a view with tracking system, using helper fn.
	
	trackWidget ( 'v' );


	// Position the 'Get this widget' div.

	$('#dg_u_getwidget').position({

		of: $('#dg_u_widget'),
		my: 'left bottom',
		at: 'left bottom',
		offset: '10 -12',
		collision: 'none'

	});


	// Position the 'Back' div.

	$('#dg_u_back').position({

		of: $('#dg_u_widget'),
		my: 'left top',
		at: 'left top',
		offset: '8 8',
		collision: 'none'

	});


	// Pre-load all our images, w/utility function.
	
	preloadImages ( 'splash_logo-dg', 'splash_logo-nhs', 'splash_logo-bl', 'getwidget','getwidget-bg','gradient_top','splash_getstarted','splash_getwidget','splash_logos','tab_block_dark','tab_block_light','tab_point_bl','tab_point_dark','tab_point_dg','tab_point_light','tab_point_nhs','tab_point_plain' );


	// Bind the 'Get Started' button to get started.

	$('#dg_u_splash_getstarted').bind('click', function(e) {

		// getStarted ( );

	});


	// Bind tumescence to mousing over 'Get this widget' div...

	$('#dg_u_getwidget').mouseover(function() {

		$('#dg_u_getwidget').animate({width: '102px' }, 300 )

	});


	// ...and contraction to mousing out.

	$('#dg_u_getwidget').mouseout(function() {

		$('#dg_u_getwidget').animate({width: '23px' }, 300 )

	});	


	// Bind the link to widget page to 'Get this widget' div.

	$('#dg_u_getwidget').bind('click', function(e) {
		
		window.open('http://innovate-apps.direct.gov.uk/widgets/uni/');	

		return false;

	});
	
	
	// Bind all the main page tabs to show appropriate detail pages.

	$('.dg_u_tab_point_dark').each(function() {
	
		$(this).bind('click', function(e) {
	
			my_area = $(this).attr('id').substring(  13, $(this).attr('id').length  );
				
			showAreaScreen ( my_area );
			
			return false;
			
		});
	
	});
	
	// Bind the 'Back' button to show the main screen.
	
	$('#dg_u_back_img').bind('click', function(e) {
	
		// backToMain();
			
	});
	
	
	// Iterate over individual link tabs; apply links.
	
	$('.dg_u_tab_point_light').each(function() {
	
		// Get URL from title attribute, plus rollover class suffix.
		
		var my_url = $(this).attr('link');

		$(this).bind('click', function(e) {
		
			window.open(my_url, "redundancy_link");	

			return false;
			
		});

	});
	
	
	// For individual link tabs, apply hover state.
	
	$('.dg_u_dg_link').hover(
		function () {
			$(this).css('background-image','url(img/tab_point_dg.png)');
		},
		function () {
			$(this).css('background-image','url(img/tab_point_light.png)');
		}
	);

	$('.dgl_u_plain_link').hover(
		function () {
			$(this).css('background-image','url(img/tab_point_plain.png)');
		},
		function () {
			$(this).css('background-image','url(img/tab_point_light.png)');
		}
	);

	
});



// Gets started.

function getStarted ( ) {

	$('#dg_u_splash').hide('slide',{},500);

	setTimeout (  "$('#dg_u_main').show('slide',{ direction: 'right' },500)", 500  );


	// Show the 'Get this widget' div.

	setTimeout (  "$('#dg_u_getwidget').css('visibility', 'visible')", 300  );


	// Reel it back in to the left. - Only if not IE6!

	if ( ! ($.browser.msie && $.browser.version.substr(0,1)<7)  ) {

		setTimeout (  "$('#dg_u_getwidget').animate({width: '23px' }, 500 )", 1000  );

	}


	// If they've started, this is a 'use'; register with tracking system, using helper fn.

	trackWidget ( 'u' );


	// Set background (top blue bar) to main div, so it stays.

	$('#dg_u_widget').css('background-repeat', 'repeat-x');
	$('#dg_u_widget').css('background-image', 'url("img/gradient_top.png")');

		
}


// Displays appropriate Area Screen.

function showAreaScreen ( my_area ) {

	$('#dg_u_main').hide('slide',{},500);

	my_area_id = '#dg_u_area_' + my_area;

	setTimeout (  "$(my_area_id).show('slide',{ direction: 'right' },500)", 500  );


	// Show the 'Back' button.

	setTimeout (  "$('#dg_u_back').css('visibility', 'visible')", 300  );
	
}


// Re-displays Main screen.

function backToMain ( ) {

	$('.dg_u_area_screen').hide('slide',{ direction: 'right' },500);

	my_area_id = '#dg_u_area_' + my_area;

	setTimeout (  "$('#dg_u_main').show('slide',{ direction: 'left' },500)", 300  );


	// Hide the 'Back' button.

	setTimeout (  "$('#dg_u_back').css('visibility', 'hidden')", 300 );
	
}


// Utility function for pre-loading images.

function preloadImages () {

	var cache = [];

	var args_len = arguments.length;

	for (var i = args_len; i--;) {

		var cacheImage = document.createElement('img');
		cacheImage.src = 'img/' + arguments[i] + '.png';
		cache.push(cacheImage);

    }

}


// Utility function for registering views/uses in tracking system
// Type is either 'v' (view) or 'u' (use). Our widget num is 7.

function trackWidget ( type ) {

	$.ajax({

		type: "GET",
		url: "http://innovate-apps.direct.gov.uk/t.php",
		data: "w=7&t="+type+"&r="+my_referer,

		success: function(msg) {

			// On success, do nothing.

		},

		error: function(errormessage) {

			// On error, do nothing - it's not critical

		}

	});
	
}
