$(document).ready(function() {
	
  var t = '';
  
	/**
	 * Flyout Code
	 *
	 * Hover
	 */
	$('div').hover(function() {    
    if( $(this).hasClass('.homepage-tab-flexilist') ) {
      front_flyout($(this));
    } 
    else {
      if ( $(this).parents('.view-homepage .item-list .views-row').length === 0 ) {
			  front_hide_flyout();
		  } 
    }
	});
	
	/**
	 * Focus
	 */
	$('a').focus(function() {
    if ( $(this).hasClass('.homepage-tab-link') ) {
      front_flyout($(this));
    }
    else {
		  if ( $(this).closest('.flyout').length === 0 ) {
			  front_hide_flyout();
		  }
    }
	});

	front_flyout = function(object) {
		if ( object.closest('.views-row').find('.sfHover').length === 0 ) {
      $('ul.homepage-tab-show li.off').show();
      $('ul.homepage-tab-show li.on').hide();
      $('.flyout').hide().removeClass('sfHover');
      $('.views-row').removeClass('sfHover-homepage-tab');
      clearTimeout(t); 
      t = setTimeout(function() {
				front_flyout_action(object);
			}, 200);
		}
	}
  
  front_flyout_action = function(object) {
    if ( !$('.flyout').is(':animated') ) {
      $('.flyout').hide().removeClass('sfHover');
      object.closest('.views-row').find('ul.homepage-tab-show li.off').hide();
      object.closest('.views-row').find('ul.homepage-tab-show li.on').show();
      object.closest('.views-row').addClass('sfHover-homepage-tab');
      object.closest('.views-row').children('.flyout').fadeIn(500, function() {
        object.closest('.views-row').children('.flyout').addClass('sfHover');
      });
    }
    else {
      front_flyout(object);
    }
  }
	
	front_hide_flyout = function() {
		$('.views-row').removeClass('sfHover-homepage-tab');
		$('.flyout').hide().removeClass('sfHover');
		$('ul.homepage-tab-show li.off').show();
		$('ul.homepage-tab-show li.on').hide();
	}
	
  
  
	/**
	 * Segmentation Cookie
	 *
	 * If a link is clicked on, we need to set the segmentation cookie to the 
	 * correct segmentation
	 */
	//Problem on UAT, cookie not switching quick enough.
	//let us try deleting it when the user is on the homepage
	$.cookie("seg_cookie", null);
	 
	$('.view-homepage a').bind('mousedown keypress', function() {
		var segmentation = front_find_segmentation($(this));
		front_set_cookie(segmentation);
	});
	$('.form-text').bind('keypress', function(e) {
		if (e.keyCode == 13) { //If they press return
			var segmentation = front_find_segmentation($(this));
			front_set_cookie(segmentation);
		}
	});
	$('.form-submit').bind('click keypress', function(e) {
		if (e.type == 'click' || e.keyCode == 13) { //If they press return
			var segmentation = front_find_segmentation($(this));
			front_set_cookie(segmentation);
		}
	});
	
	/**
	 * Find the segmentation that the user is going to
	 */
	front_find_segmentation = function(object) {
		//find out what section the link is contained in
		var url = object.parents('.views-row').find('a.homepage-tab-link').attr('href');
		if ( url.search('/personal') != -1 ) {
			var segmentation = 'SG-Personal';
		} 
    else if ( url.search('/corporate') != -1 ) {
			var segmentation = 'SG-Corporate';
		} 
    else {
			var segmentation = 'SG-SME';
		}
		
		return segmentation;
	}
	
	/**
	 * Set the cookie
	 */
	front_set_cookie = function(segmentation) {
		//set the cookie value
		var rmlSegmentationName='seg_cookie';
		var url = window.location.host;
		if ( url.search('capgemini.com') != -1 ) {
			var rmlSegmentationOptions = { path: '/', domain: '.capgemini.com' };
		} 
    else {
			var rmlSegmentationOptions = { path: '/', domain: '.royalmail.com' };
		}
		$.cookie(rmlSegmentationName, segmentation, rmlSegmentationOptions);
		
		var seg = $.cookie(rmlSegmentationName);
	}
});
