 
var track = (function(){
	
	var get_element_value = function(context)
	{
		return context.val();
	};
	
	var get_checkbox_value = function(context)
	{
		if (context.attr('checked') == true)
		{ 
			return context.val();	 
		}
	};
	
	var get_checkbox_state = function(context)
	{
		if (context.attr('checked') == true)
		{ 
			return 1;	 
		}
		return 0;
	};
	
	var get_link_href_and_img = function(context)
	{
		var $img = context.find('img');
				
		if ($img.length)
		{
			var img_src = $img.attr('src');
			img_src = img_src.replace(/^.*(\\|\/|\:)/, '');
			return img_src + ' - ' + context.attr('href');	
		}
		// might be a link wrapping text like video links
		return context.text() + ' - ' + context.attr('href');
	};
	
	var get_slider_value = function(context)
	{
		return jQuery('input#rating_driving').val();
	};
	
	var get_search_keywords = function(context)
	{
		return jQuery('input[name="keywords"]').val();
	};
	
	var get_nav_dealer_search = function(context)
	{
		return jQuery('#nav input[name="location"]').val();
	};

	var get_prod_name_order = function(context)
	{
		return 'order_'+ jQuery("#order_button").attr("title");
	};
	var get_prod_name_request = function(context)
	{
		return 'request_proposal_'+ jQuery("#request_button").attr("title");
	};   
	var get_prod_name_fb_like = function(context)
	{
		return 'fb_like_'+ jQuery("#fblike_button").attr("title");
	};
	var get_prod_name_tweet = function(context)
	{
		return 'tweet_'+ jQuery("#tweet_button").attr("title");
	}; 
	var get_prod_emailafriend = function(context)
	{
		return 'emailafriend_'+ jQuery("#email_button").attr("rel");
	}; 
	var get_prod_details_tab = function(context)
	{
		return 'prod_details_tab_'+ jQuery("#tabs-1").attr("title");
	};      
	
	var track_event = function(category, action, label, value)
	{
		if (typeof _gaq == 'undefined')
		{
			return false;
		}
		window.console && console.log('sending GA event...');
		value = value ? parseInt(value) : null;
		_gaq.push(['_trackEvent', category, action, label, value]);
		return true;
	};
	
	var bind_event_tracking = function()
	{	
		jQuery.each(elements, function(element_name, item) {
			var $el = jQuery(item.selector);
			if ($el.length)
			{
				window.console && console.log('binding ', item.selector);
				
				var bind = item.bind ? item.bind : 'click';
				
				$el.data('category', item.category);
				$el.data('action', item.action);
				$el.data('label', item.label);
				$el.data('value', item.value);
				
				// we have the element, bind the click
				$el.live(bind, function(e){
					
					var category = $el.data('category');
					var action = $el.data('action');
					var label = $el.data('label');
					var value = $el.data('value');
					
					if (typeof label == 'function')
					{
						label = label.call( jQuery(this), jQuery(this));
						window.console && console.log('function called for label. result: ', label);
					}
					if (typeof value == 'function')
					{
						value = value.call( jQuery(this), jQuery(this));
						window.console && console.log('function called for value. result: ', value);
					}
					window.console && console.log('event registered! ', item.selector, category, action, label + '', value + '');
					track_event(category, action, label, value);
				});
			}
		});
	};
	
	var elements = {
		// begin home elements
		home_hero_one: {
			selector:	'#hero_one .hero_text_container .get_more_info_btn',
			category:	'home',
			action: 	'more_info',
			label:		'falcon'
		},
		home_hero_two: {
			selector:	'#hero_two .hero_text_container .get_more_info_btn',
			category:	'home',
			action: 	'more_info',
			label:		'save'
		},
		home_hero_three: {
			selector:	'#hero_three .hero_text_container .get_more_info_btn',
			category:	'home',
			action: 	'more_info',
			label:		'reduce'
		},
		home_hero_four: {
			selector:	'#hero_four .hero_text_container .get_more_info_btn',
			category:	'home',
			action: 	'more_info',
			label:		'provide'
		},
		home_hero_five: {
			selector:	'#hero_five .hero_text_container .get_more_info_btn',
			category:	'home',
			action: 	'more_info',
			label:		'diminish'
		},
        	home_video_one: {
			selector:	'#orientation_video',
			category:	'home',
			action: 	'video',
			label:		'orientation_video'
		},
        	home_video_two: {
			selector:	'#maintenance_video',
			category:	'home',
			action: 	'video',
			label:		'maintenance_video'
		},
        	home_video_three: {
			selector:	'#installation_video',
			category:	'home',
			action: 	'video',
			label:		'installation_video'
		},
		falcon_logo: {
                    selector:	'#home_logo',
                    category:	'header',
                    action: 	'click',
                    label:	'logo'
                },
                
                facebook_footer: {
                    selector:	'#facebook',
                    category:	'footer',
                    action: 	'click',
                    label:	'fb_like'
                },
                
                twitter_footer: {
                    selector:	'#twitter',
                    category:	'footer',
                    action: 	'click',
                    label:	'tweet'
                },
		why_diff_economic_toggler: {
                    selector:	'#economic_toggle',
                    category:	'why_different',
                    action: 	'click',
                    label:	'economic'
                },
                
                why_diff_enviro_toggler: {
                    selector:	'#enviromental_toggle',
                    category:	'why_different',
                    action: 	'click',
                    label:	'enviromental'
                },
                
                why_diff_hygiene_toggler: {
                    selector:	'#hygiene_toggle',
                    category:	'why_different',
                    action: 	'click',
                    label:	'hygiene'
                },
                why_diff_indie_research: {
                    selector:	'#view_independent_research',
                    category:	'why_different',
                    action: 	'click',
                    label:	'indie_research'
                },
		savings_indie_research: {
                    selector:	'#view_independent_research_savings',
                    category:	'savings',
                    action: 	'click',
                    label:	'indie_research'
                },
		product_warranty_link: {
                    selector:	'#product_warranty_link',
                    category:	'products',
                    action: 	'click',
                    label:	'10year'
                },
		product_install_video: {
                    selector:	'#product_install_video',
                    category:	'products',
                    action: 	'video',
                    label:	'installation_video'
                },
                product_maint_video: {
                    selector:	'#product_maint_video',
                    category:	'products',
                    action: 	'video',
                    label:	'maintenance_video'
                },
		product_detail_order: {
                    selector:	'#order_button',
                    category:	'product_detail',
                    action: 	'click',
                    label:	get_prod_name_order
                },
		product_detail_request_proposal: {
                    selector:	'#request_button',
                    category:	'product_detail',
                    action: 	'click',
                    label:	get_prod_name_request
                },
		product_detail_fb_like: {
                    selector:	'#fblike_button',
                    category:	'product_detail',
                    action: 	'click',
                    label:	get_prod_name_fb_like
                },
		product_detail_tweet: {
                    selector:	'#tweet_button',
                    category:	'product_detail',
                    action: 	'click',
                    label:	get_prod_name_tweet
                },
		product_detail_emailafriend: {
                    selector:	'#email_button',
                    category:	'product_detail',
                    action: 	'click',
                    label:	get_prod_emailafriend
                },
		product_detail_details_tab: {
                    selector:	'#tabs_button_1',
                    category:	'product_detail',
                    action: 	'click',
                    label:	get_prod_details_tab
                },
		resources_lit_tab: {
                    selector:	'#lit_tab',
                    category:	'resources',
                    action: 	'click',
                    label:	'lit_tab'
                },  
                resources_videos_tab: {
                    selector:	'#videos_tab',
                    category:	'resources',
                    action: 	'click',
                    label:	'videos_tab'
                },  
                resources_links_tab: {
                    selector:	'#links_tab',
                    category:	'resources',
                    action: 	'click',
                    label:	'links_tab'
                },  
                resources_gsa_tab: {
                    selector:	'#gsa_tab',
                    category:	'resources',
                    action: 	'click',
                    label:	'gsa_tab'
                },  
                resources_rebates_tab: {
                    selector:	'#rebates_tab',
                    category:	'resources',
                    action: 	'click',
                    label:	'rebates_tab'
                },  

		landing_form_step_1: {
                    selector:	'#form_step_1',
                    category:	'landing_page',
                    action: 	'click',
                    label:	'form_step_1'
                },    

		landing_form_step_2: {
                    selector:	'#form_step_2',
                    category:	'landing_page',
                    action: 	'click',
                    label:	'form_step_2'
                },   

		landing_form_step_3: {
                    selector:	'#step_form #contact_submit',
                    category:	'landing_page',
                    action: 	'click',
                    label:	'form_step_3'
                }, 



		whatwhat: {
			selector: 'body.home #sub_nav li:eq(0)',
			category: 'header',
			action: 'click',
			label: 'asdf'
		}
	};
	
	jQuery(document).ready(function($){bind_event_tracking();});
	
	return {
		// @params: Category, Action, Label, Value
		//   Category (required) The name you supply for the group of objects you want to track.
		//   Action (required) A string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object.
		//   Label (optional) An optional string to provide additional dimensions to the event data.
		//Value (optional) An integer that you can use to provide numerical data about the user event.
		   
		event: function(category, action, label, value){
			return track_event(category, action, label, value);
		}
	};
}(_gaq));
