$(function(){
	// Navigation
	$('#HRN ul li').mouseover(function(){

		// remove old hover thing
		if($('#HRN ul li.selected img').length > 0){
			sel_src = $('#HRN ul li.selected img').attr('src').split('.');
			if(sel_src[0].indexOf('w') > 0)
				$('#HRN ul li.selected img').attr('src', sel_src[0].substring(0, sel_src[0].length - 1) + '.' + sel_src[1]);
		}

		// highlight the tab
		$('#HRN ul li.selected').removeClass('selected');
		$(this).addClass('selected');
		
		// show the related sub-item
		$('#Nav ul.visible').removeClass('visible');
		$('#Nav_' + $(this).attr('id')).addClass('visible');

		//// change image
		old_src = $(this).find('img').attr('src').split('.');
		if(old_src[0].indexOf('w') < 0)
			$(this).find('img').attr('src', old_src[0] + 'w.' + old_src[1]);
	});
	
	$('#HContainer').mouseleave(function(){
		if($('.selected_orig').size() > 0){
			// revert to originals
			sel_src = $('#HRN ul li.selected img').attr('src').split('.');
			if(sel_src[0].indexOf('w') > 0)
				$('#HRN ul li.selected img').attr('src', sel_src[0].substring(0, sel_src[0].length - 1) + '.' + sel_src[1]);

			$('#HRN ul li.selected').removeClass('selected');
			$('#HRN ul li.selected_orig').addClass('selected');
			
			$('#Nav ul.visible').removeClass('visible');
			$('#Nav ul.selected_orig').addClass('visible');

			// change image for the original
			old_src = $('#HRN ul li.selected_orig img').attr('src').split('.');
			if(old_src[0].indexOf('w') < 0)
				$('#HRN ul li.selected_orig img').attr('src', old_src[0] + 'w.' + old_src[1]);
		}
	});

	// set selected image
	if($('#HRN ul li.selected img').length > 0){
		old_src = $('#HRN ul li.selected img').attr('src').split('.');
		$('#HRN ul li.selected img').attr('src', old_src[0] + 'w.' + old_src[1]);
	}

	// top page search
	$('#q').focus(function(){ 
		if($(this).val() == 'Search oneconnect.ca')
			$(this).val('');
	 });

	 // top page search
	$('#email').focus(function(){ 
		if($(this).val() == 'Enter email address')
			$(this).val('');
	 });

	// tutorials
	$('.tutorial').click( function(event){
		event.preventDefault();
		
		// replace content
		$('#dialog-content').html( $(this).next().html() );
		
		// replace video
		if($(this).next().next().html() == ''){
			$('#dialog-video').css('display', 'none');
		}else{
			$('#dialog-video').css('display', 'block');
			$('#dialog-video iframe').attr('src', 'http://www.youtube.com/embed/' + $(this).next().next().html());
		}
		
		$( "#dialog-modal" ).dialog({
			title: $(this).html(),
			width: 750,
			modal: true,
			buttons: {
				Ok: function() {
					$( this ).dialog( "close" );
				}
			}
		});
	});

	if(document.location.hash && $('a[href=\\'+ document.location.hash +']')){
		$('a[href=\\'+ document.location.hash +']').click();
	}
	
	// Misc Common Stuff
	$('.button').button();
	$('.tabs').tabs();
});

/// image rotator
var rotatorInt;
$(document).ready(function() {	
	rotatorInt = setInterval('swapMainImages()', 5000);
	$('area[href="\#rotate1"]').click(function(){ swapMainImages(1); });
	$('area[href="\#rotate2"]').click(function(){ swapMainImages(2); });
	$('area[href="\#rotate3"]').click(function(){ swapMainImages(3); });
	//$('area[href="\#rotate4"]').click(function(){ swapMainImages(4); });
});

function swapMainImages( switchTo ){
	if(switchTo){
		$('#main_rotate_img .active').removeClass('active');
		$($('#main_rotate_img a')[switchTo - 1]).addClass('active');
		clearInterval(rotatorInt);
		rotatorInt = setInterval('swapMainImages()', 5000);
		return;
	}

	$('#main_rotate_img .active').fadeOut(function(){
		var next = $('#main_rotate_img .active').next();
		if(next.length == 0)
			next = $('#main_rotate_img a:first');
			
		$('#main_rotate_img .active').removeClass('active');
		
		next.fadeIn().addClass('active')
	});
}
