
/**
 * Utility functions
 * To get around with IE7 bug for slideUp and slideDown()
 */
$.fn.showMe = function() {
    $(this).each(function(){
    	$(this).css('display', 'block');
    });
}

$.fn.hideMe = function() {
    $(this).each(function(){
    	$(this).css('display', 'none');
    });
}

var Site = {

	currentStep: 1,
	
	fullBaseUrl: null,
	
	submitCount:0,

	onReady: function() {

		// Set base path
		this.fullBaseUrl = document.location.protocol + '//' + document.location.host;

		for (var selector in this.elements) {
			var $elms = $(selector);

			if ($elms.size() > 0) {
				this.elements[selector]($elms);
			}
		}
	},

	
	elements:
	{
		
			/*------------------------------------------------------
			  Start: Send results
			--------------------------------------------------------*/
			
			'.button-send-to-self, .button-send-to-friend':function(elms){
				
				height = 340;
				if ($('.button-send-to-self').attr('class').indexOf('button-send-to-friend')){
					height = 450;
				}
				
				$(elms).fancybox({
					'type'				: 'iframe', // 'ajax',
					'titleShow'			: false,
					'scrolling'			: 'no',
					'width'				: 674,
					'height'			: height,
					'padding'			: 0,
					'onStart'			: startFancy,
					'onClosed'			: closeFancy,
					'onComplete'		: completeFancy
				});
				
				
				function startFancy() {
					//	Hide content
					$("#fancybox-overlay").css({'display' : 'block', 'opacity' : '0'});
					$("#fancybox-inner").css({'display' : 'block', 'opacity' : '0'});
					$("#fancybox-overlay").fadeTo("slow", 0.9,
					function() {
						}
					);
				
				}
				
				function closeFancy() {
				}
				
				// Will be called once the content is displayed
				function completeFancy() {
					
					if ($.browser.msie && $.browser.version.substr(0,1)<7) {
						$(".fancybox-ie").addClass("fancybox-ie6");
					}else if ($.browser.msie && $.browser.version.substr(0,1)==7) {
						$(".fancybox-ie").addClass("fancybox-ie7");
					}
	
					Cufon.replace('h2', { fontFamily: 'BellCentennialStd-Address'});
					$("#fancybox-inner").css({'overflow-x':'hidden'});
					$("#fancybox-inner").fadeTo("slow", 1.0,
						function() {
						}
					
					);
					bindSubmit();
				}
				 
				 
				 var bindSubmit = function() {
					 	//Binding the form after load
						$("#fancybox-content form").bind("submit", 
							function() {
								$.fancybox.showActivity();
	
								var data = $(this).serializeArray();
								var action	= $(this).attr('action');
	
								//submit form
								$('#fancybox-inner').load(
									action,
									data,
									function() {
										$.fancybox.hideActivity();
										bindSubmit();
										//	Don't $.fancybox.resize() as it sets weird size
										//Cufon.replace('h2', { fontFamily: 'LubalinGraphStd-Book'});
									}
								);
								return false;
							}
						);						
				 }
			},
			/*------------------------------------------------------
			
			
			
			/*------------------------------------------------------
			  Start: Feedback form
			--------------------------------------------------------*/
			'.feedback-link':function(elms){
				
				height = 450;
				$(elms).fancybox({
					'type'				: 'iframe', // 'ajax',
					'titleShow'			: false,
					'scrolling'			: 'no',
					'width'				: 674,
					'height'			: height,
					'padding'			: 0,
					'onStart'			: startFancy,
					'onClosed'			: closeFancy,
					'onComplete'		: completeFancy
				});
				
				
				function startFancy() {
					//	Hide content
					$("#fancybox-overlay").css({'display' : 'block', 'opacity' : '0'});
					$("#fancybox-inner").css({'display' : 'block', 'opacity' : '0'});
					$("#fancybox-overlay").fadeTo("slow", 0.9,
					function() {
						}
					);
				
				}
				
				function closeFancy() {
				}
				
				// Will be called once the content is displayed
				function completeFancy() {
					
					if ($.browser.msie && $.browser.version.substr(0,1)<7) {
						$(".fancybox-ie").addClass("fancybox-ie6");
					}else if ($.browser.msie && $.browser.version.substr(0,1)==7) {
						$(".fancybox-ie").addClass("fancybox-ie7");
					}
	
					Cufon.replace('h2', { fontFamily: 'BellCentennialStd-Address'});
					$("#fancybox-inner").css({'overflow-x':'hidden'});
					$("#fancybox-inner").fadeTo("slow", 1.0,
						function() {
						}
					
					);
					bindSubmit();
				}
				 
				 
				 var bindSubmit = function() {
					 	//Binding the form after load
						$("#fancybox-content form").bind("submit", 
							function() {
								$.fancybox.showActivity();
	
								var data = $(this).serializeArray();
								var action	= $(this).attr('action');
	
								//submit form
								$('#fancybox-inner').load(
									action,
									data,
									function() {
										$.fancybox.hideActivity();
										bindSubmit();
										//	Don't $.fancybox.resize() as it sets weird size
										//Cufon.replace('h2', { fontFamily: 'LubalinGraphStd-Book'});
									}
								);
								return false;
							}
						);						
				 }
			},
			/*------------------------------------------------------
			
			
			
			
			
			
			
			/*------------------------------------------------------
			  Start: Section
			--------------------------------------------------------*/
			'#ajax-form':function($elms){
				//	....
			},
			/*------------------------------------------------------*/
			



			
			/*------------------------------------------------------
			  Start: 
			--------------------------------------------------------*/
			'body': function($elms) {
				Cufon.replace('h1, h2, .introduction p, .header .logo a, .header .logo .tagline ', { fontFamily: 'LubalinGraphStd-Book'});
				
				Cufon.replace('.nav .main-nav li a', { fontFamily: 'LubalinGraphStd-Book', hover: 'true' });
				Cufon.replace('.header .message', { fontFamily: 'LubalinGraphStd-Book',  textShadow: '0px 1px #b7a735'});
				Cufon.replace('.button .title', { fontFamily: 'LubalinGraphStd-Book',  textShadow: '0px 1px #848484'});
				Cufon.replace('.page-calculator .column-2 .button .title, .page-calculator .column-1 .button .title, .page-calculator .risk-preview ul li a', { fontFamily: 'LubalinGraphStd-Book'});

				if ($.browser.msie && $.browser.version <= 6 ){
					$($elms).supersleight({shim:'/img/clear.gif'});

					// Diplay warning if it's IE6
					checkAlert = function(){
						justVisit = $.cookie('just_visit');
						if (justVisit == null){
							text = 'We have detected you have an older version of IE, please be aware that you may not fully experience the sites features while using this browser.';
							alert(text);
							$.cookie('just_visit', 'true', { expires: 365, path: '/' });
						}
					}
					checkAlert();
				}
			},
			/*------------------------------------------------------
			  End: 
			--------------------------------------------------------*/

			
			
			/*------------------------------------------------------
			  Start: Nav
			--------------------------------------------------------*/
			'.nav': function($elms) {
				$('.nav .main-nav li:first').addClass('first');
				$('.nav .main-nav li:last').addClass('last');
			},
			/*------------------------------------------------------
			  End: Nav
			--------------------------------------------------------*/

			
			
			/*------------------------------------------------------
			  Start: Nav
			--------------------------------------------------------*/
			'.page-faq': function($elms) {
				$('.page-faq .column-1 h3:first').css({margin : '0'});

				//Hide (Collapse) the toggle containers on load
				$(".toggle_container").hide();
				//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
				$("h3.trigger").click(function(){
					$(this).toggleClass("active").next().slideToggle("slow");
					var action= $('.action', this);
					$(action).text($(action).text() == "Click for more" ? "Click to hide" : "Click for more");
					return false; //Prevent the browser jump to the link anchor
				});

			},
			/*------------------------------------------------------
			  End: Nav
			--------------------------------------------------------*/

			/*------------------------------------------------------
			  Start: Text Size
			--------------------------------------------------------*/
			'.text-size': function($elms) {
					$('.text-size a').click(function(){
						var ourText = $('.layout-standard .content .column-1 p');
						var currFontSize = ourText.css('fontSize');
						var currLineHeight = ourText.css('line-height');

						var finalNum = parseFloat(currFontSize, 10);
						var stringEnding = currFontSize.slice(-2);

						var lineFinalNum = parseFloat(currLineHeight, 10);
						var lineStringEnding = currLineHeight.slice(-2);

						if ($(this).is('.increase')) {
							finalNum *= 1.2;
							lineFinalNum *= 1.2;
						}
						else if ($(this).is('.decrease')) {
							finalNum /=1.2;
							lineFinalNum /=1.2;
						}
						ourText.css({'fontSize' : finalNum + stringEnding, 'line-height' :  lineFinalNum + lineStringEnding });
					});
			},
			/*------------------------------------------------------
			  End: Nav
			--------------------------------------------------------*/


			/*------------------------------------------------------
			  Start: Print
			--------------------------------------------------------*/
			'.print': function($elms) {
					$('a', $elms).click(function() {
							window.print();
							stopPropataion();
							preventDefault();
							return false;
					});
			},
			
			// On calculator result pages
			'.page-calculator a.print-button':function($elms){
					$($elms).click(function() {
							window.print();
							stopPropataion();
							preventDefault();
							return false;
					});
			},
			/*------------------------------------------------------
			  End: Print
			--------------------------------------------------------*/

			/*------------------------------------------------------
			  Start: Tooltip
			--------------------------------------------------------*/
			'a[tooltip]':function($elms){
				// Expect <a href='' tooltip='text goes here'>xxx</a>
				 $elms.each(function()
				   {
				      $(this).qtip({
				         content: $(this).attr('tooltip'),
							style: {
								classes: 'ui-tooltip-blue ui-tooltip-shadow',
								tip: {
									corner: "leftBottom",
									width: 10,
									height: 8
								}
							},
							position: {
								at: "top right",
								my: "left bottom"
							}
				      });

					  $(this).click(function(event) {
						  event.preventDefault();
						});

				   });

			}
			/*------------------------------------------------------
			  End: Tooltip
			--------------------------------------------------------*/

			
	}

}

		
/*------------------------------------------------------
Start: JQuery start function
--------------------------------------------------------*/


$(document).ready(
	function() {
		Site.onReady();
	}
);

/*------------------------------------------------------
End: JQuery start function
--------------------------------------------------------*/


