/*
 * Copyright (c) 2009 Night Agency LLC
 *
 */
if( typeof( NIGHT ) === 'undefined') {
	NIGHT = {};
}

NIGHT.common = function() {

	$(document).ready( function() {
		var location = document.location.href,
			slugMaker = function (text){
				return text.replace(/\s/g,'-').replace(/[^a-zA-Z0-9\-]/g,'').toLowerCase();
			},
			firstClickEventMediaCenter = false,
			debug = false;
				
		//open in new window, and attach page tracker to all external classed links
		$('a.external').attr('target', '_blank').click(function(){
			pageTracker._trackEvent('outbound', $(this).attr('href'), location);
		});

		// 3in1 page, sustability dialog
		$('a#sustainability').click(function(){
			pageTracker._trackEvent('dialog', 'sustainability', location);
		});
		
		// 3in1 page, product performance dialog
		$('a#product-performance').click(function(){
			pageTracker._trackEvent('dialog', 'product-performance', location);
		});
		
		// site wide news letter in footer
		$('a#newsletter').click(function(){
			pageTracker._trackEvent('dialog', 'newsletter-footer' + location);
		});
		
		//track any clicks on the navigators on product pages and the special Purex 2 Color Safe Bleach Page 
		$('#fragrance-navigator a, #purex2-navigator a').click(function() {
			var aTitle = slugMaker($(this).attr('title'));
			pageTracker._trackEvent('switch', aTitle, location);
		});
		
		//back to top button on product pages
		$('a#back-to-top-btn').click(function () {
			pageTracker._trackEvent('anchor', 'back-to-top-button', location);
		});
		
		//description button on 3in1 product page
		$('a[href=#description]').click(function () {
			pageTracker._trackEvent('anchor', 'description' + location);
		});
		
		
		//tabbed navigation on product page
		$('#product-info-navigator ul li a').click(function () {
			var text = slugMaker($(this).find('span').text());
			pageTracker._trackEvent('tabbed-nav-product-info', text, location);
		});
		
		//faq accordian headers on 3in1 product page
		$('#faq-wrapper h3.ui-accordion-header a').click(function () {
			var text = slugMaker($(this).text());
			pageTracker._trackEvent('3in1-faq-accordian', text, location);
		});
		
		//product review navigation links
		$('.reviews-nav .pagination ul li a').click(function () {
			var text = slugMaker($(this).text());
			pageTracker._trackEvent('review-navigation', text, location);
		});
		
		//product review post a button dialog
		$('#reviews-wrapper .post-button a').click(function () {
			pageTracker._trackEvent('dialog', 'post-a-review', location);
		});
		
		//track video clicks in media center page
		$('div.thumb-content a').click(function() {
			if(firstClickEventMediaCenter === false){
				firstClickEventMediaCenter = true; //a click event is fired on page load but no video is played, so dont count it
			} else { 
				var text = slugMaker($(this).parent('.thumb-content').find('div.movie-details h3').text());
				pageTracker._trackEvent('video', text, location);
			}
		});
		
		//track mailto links
		$('a[href^=mailto:]').click(function () {
			var text = $(this).text();
			pageTracker._trackEvent('email', text, location);
		});
		
		//track laundry symbol decoder clicks
		$('#symbols li a').click(function () {
			var text = slugMaker($(this).text());
			pageTracker._trackEvent('clothing-label-decoder', text, location);
		});
		
		//track fabric care clicks
		$('#fabric-selector li a').click(function () {
			var text = slugMaker($(this).text());
			pageTracker._trackEvent('fabric-care', text, location);
		});
				
	});
}();