/*
 * OBMG global scripts
 *
 * Targets jQuery 1.3.2.
 *
 * Copyright (c) 2009-2010 Dialect Communications Group (dialect.ca)
 *
 * $Package: OBMG $
 */

/*jslint browser: true, white: true, plusplus: true, newcap: true,
    eqeqeq: true, evil: true, nomen: false,  regexp: true, undef: true,
    onevar: true */

/*global $,pageTracker,window,console,Cufon */

var OBMG = {
	/* Domains for internal Google Analytics tracking */
	TRACKABLE_DOMAINS_RE : /[\.\/](obmg|painterslodge|aprilpoint|canadianprincess|mvcharlotteprincess|mvmarabell|mvsalmonseeker|kingsalmonresort|capesantamaria|pedderbaymarina|marinarestaurant)\./i,

	/* Default width for dropdown nav */
	NAV_WIDTH : 178,

	/*
	 * Add Google Analytics tracking across domains.
	 */
	addOutboundLinkTracking : function () {
		$('.fb-findus').bind('click.obmg', function () {
			OBMG.trackEvent('Social Links', 'Facebook', $(this).attr('href'));
			return true;
		});
		
		$('.twitter32').bind('click.obmg', function () {
			OBMG.trackEvent('Social Links', 'Twitter', $(this).attr('href'));
			return true;
		});
	
		//return;
		$('a').bind('click.obmg', function () {
			
			var href = $(this).attr('href');
			// don't track relative links
			if (href.indexOf('http:') !== 0 && href.indexOf('https:') !== 0) {
				return true;
			}

			if (OBMG.isTrackableLink(href)) {
				try {
					if ( typeof($.cookie) != 'undefined' ) {
						if ($.cookie('off_site_ref')) {
							pageTracker._link(this.href + '?src=' + $.cookie('off_site_ref'));
							return false;
						}
					}
					
					pageTracker._link(this.href);
					return false;
				} catch (e) { }
			}

			return true;
		});
	},


	/**
	 * Add hover and click behaviours to the inline child TOCs.
	 */
	initTOCs : function () {
		$('.toc li, #home_toc li').hover( 
			function () {
				OBMG.tocHighlight(this);
			},
			function () {
				OBMG.tocUnlight(this);
			}
		);
	},
	
	tocHighlight : function (el) {
		$(el).stop(false, true).animate({'backgroundColor': '#F4F4F4'}, 250);
		
	},

	tocUnlight : function (el) {
		$(el).stop(false, true).animate({'backgroundColor': '#FFF'}, 250);		
	},
	
	/**
	 * Global key handlers.  Debug only.
	 */
	initKeyHandlers : function () {
		return;
		$(document).bind('keypress.obmg', function (e) {
			if (e.which === 103) {
				$('body').toggleClass('debug');
			}
		});
	},
    
	/**
	 * Flash header gallery wireup.
	 */
    initHGal : function (xmlPath) {
		if (false) {
			return;
		}
        $('#hgal').flash({
            src : '/header/header.swf',
            width: 980,
            height: 405,
            wmode: 'opaque',
            flashvars: { xmlPath: xmlPath, slideDelay : 3000 }
        });
    },
    
	/**
	 * Flash homepage map wireup.
	 */
    initHomeMap : function () {
		if (false) {
			return;
		}
        $('#home_map').empty().flash({
            src : 'inc/map.swf',
            width: 350,
            height: 605,
            wmode: 'opaque',
			id: 'home_map_flash',
			name: 'home_map_flash'
        });

		var home_map_ref = $.browser.msie ? window['home_map_flash'] : document['home_map_flash'];

		$('#home_toc li').bind('mouseenter.obmg', function () {
			// TODO: error checking
			var pid = $(this).attr('id').substr(9);
			home_map_ref.highlight(pid);
		});
    },

	highlightFromHomeMap : function (val) {
		OBMG.tocHighlight('#home_toc_' + val);
	},

	unlightFromHomeMap : function (val) {
		OBMG.tocUnlight('#home_toc_' + val);
	},

	initTableStriping : function () {
		$('tbody tr:even').addClass('alt');
	},



	/**
	 * Drop-down nav, used on most pages
	 */
	initNav : function () {
	
		$('#nav>ul>li').hover(function () { 
			$(this).addClass('hover');
			var child = $('ul', this),
			    cw = child.width(),
			    pw = $(this).outerWidth();

			child.stop(false, true);
			
			if (OBMG.NAV_WIDTH > cw) {
				if (OBMG.NAV_WIDTH < pw) {
					child.width(pw - 2);
				} else {
					child.width(OBMG.NAV_WIDTH);
				}
			}
			
			child.slideDown(500);
        }, function () {
	        $('ul', this).stop(false, true).slideUp(500);
	        $(this).removeClass('hover');
        });  
	},
	
	/**
	 * Drop-down nav, used on ever page
	 */
	initNewsletterForm : function () {
		$('#newsletter').bind('submit.obmg', function () {
			var params = { email: $('#email').val(), xhr: true };
			$.post($(this).attr('action'), params, function (d, s) {
				if (d.success) {
					OBMG.trackPageView('/subscribe/complete');
					OBMG.trackEvent('Newsletter', 'Subscribe');				
					
					try {
					  FB.Insights.impression({
					     'id' : 6002845646866,
					     'h' : '4c46f80637'
					  });
					} catch (e) {}
				}

				$('#newsletter_status').html(d.msg);
			}, 'json');
			return false;
		});
	},
	
	trackPageView : function (href) {
		//console.debug('trackPageView', href);
		try {
			pageTracker._trackPageview(href);
		} catch (e) { }
	},
	
	trackEvent : function (c, a, l, v) {
		//console.debug('trackEvent', c, a, l, v);
		try {
			pageTracker._trackEvent(c, a, l, v);
		} catch (e) { }
	}
};


if ($) {
	$(document).bind('ready.obmg', function () {
		OBMG.addOutboundLinkTracking();
	});
}