﻿var _gaq = _gaq || [];

$(document).ready(function() {

    // Header
    var $header = $('#header');
    $header.find('.loginlightbox').click(function(e) {
        _gaq.push(['_trackEvent', window.location.pathname, 'Header - Login']);
    });
    $header.find('.shopping_cart').click(function(e) {
        if (!$(this).hasClass('shopping_cart_open')) {
            _gaq.push(['_trackEvent', window.location.pathname, 'Header - Cart']);
        }
    });

    // Home page
    $('#content .anythingSlider a').click(function(e) {
        var src = $(this).children('img').attr('src');
        var banner_name = (-1 === src.indexOf('FileName=')) ? src : src.split('FileName=')[1].split('&')[0];
        _gaq.push(['_trackEvent', window.location.pathname, 'Home - Top-Banner', 'Banner - ' + banner_name]);
        DelayUnload(this, e);
    });
    $('#btnSearchWorkshop').click(function(e) {
        _gaq.push(['_trackEvent', window.location.pathname, 'Home - Search']);
    });
    $('#search_centre a.tire_change_button').click(function(e) {
        _gaq.push(['_trackEvent', window.location.pathname, 'Home - Appointment']);
        DelayUnload(this, e);
    });
    $('.startpage_campaign_container .viewall').click(function(e) {
        _gaq.push(['_trackEvent', window.location.pathname, 'Home - Stock']);
    });
    $('.news_letter_button').click(function(e) {
        _gaq.push(['_trackPageview', '/virtual/newsletter/']);
        DelayUnload(this, e);
    });

    // Tyre size form
    var $dimension = $('#dimension');
    if ($dimension.length > 0) {
        $dimension.find('select[id$="m_DDLSectionWidth"]').bind('change keyup', function() {
            var value = $(this).children("option:selected").val();
            _gaq.push(['_trackEvent', window.location.pathname, 'Tyre size form - Width', 'Width - ' + value]);
        });
        $dimension.find('select[id$="m_DDLAspectRatio"]').bind('change keyup', function() {
            var value = $(this).children("option:selected").val();
            _gaq.push(['_trackEvent', window.location.pathname, 'Tyre size form - Height', 'Height - ' + value]);
        });
        $dimension.find('select[id$="m_DDLDiameter"]').bind('change keyup', function() {
            var value = $(this).children("option:selected").val();
            _gaq.push(['_trackEvent', window.location.pathname, 'Tyre size form - Wheel size', 'Wheel size - ' + value]);
        });
        $dimension.find('select[id$="m_DDLLoadIndex"]').bind('change keyup', function() {
            var value = $(this).children("option:selected").val();
            _gaq.push(['_trackEvent', window.location.pathname, 'Tyre size form - Weight Index', 'Weight Index - ' + value]);
        });
        $dimension.find('select[id$="m_DDLSpeedRating"]').bind('change keyup', function() {
            var value = $(this).children("option:selected").val();
            _gaq.push(['_trackEvent', window.location.pathname, 'Tyre size form - Speed Index', 'Speed Index - ' + value]);
        });
        $dimension.find('input[id$="m_radioButtonSummer"],label[for$="m_radioButtonSummer"]').click(function() {
            _gaq.push(['_trackEvent', window.location.pathname, 'Tyre size form - Season Summer']);
        });
        $dimension.find('input[id$="m_radioButtonWinter"],label[for$="m_radioButtonWinter"]').click(function() {
            _gaq.push(['_trackEvent', window.location.pathname, 'Tyre size form - Season Winter']);
        });
        $dimension.find('input[id$="m_radioButtonStuds"],label[for$="m_radioButtonStuds"]').click(function() {
            _gaq.push(['_trackEvent', window.location.pathname, 'Tyre size form - Season Winter (no studs)']);
        });
        $dimension.find('.tiresearch_button').click(function() {
            _gaq.push(['_trackEvent', window.location.pathname, 'Tyre size form - Search']);
        });
    }
	

    // Footer
    var $footer = $('#footer');
    if ($footer.length > 0) {
        $footer.find('.countries li a').click(function(e) {
            var country = $(this).text().replace(/^\s+|\s+$/g, '');
            _gaq.push(['_trackEvent', window.location.pathname, 'Footer - Country selection', 'Country selection - ' + country]);
            DelayUnload(this, e);
        });
    }

    // Ebooking module
    $('.search_shop .black_button a').click(function() {
        _gaq.push(['_trackEvent', window.location.pathname, 'Shop - Search']);
    });
    $('.maps_results a.maplink').click(function() {
        _gaq.push(['_trackEvent', window.location.pathname, 'Shop - More Info']);
    });
    $('.maps_results a.maplink + a').click(function() {
        _gaq.push(['_trackEvent', window.location.pathname, 'Shop - See on map']);
    });
    $('a.timeBooking').click(function(e) {
        _gaq.push(['_trackEvent', window.location.pathname, 'Shop - Appointment']);
        _gaq.push(['_trackPageview', '/virtual/appointment/']);
        DelayUnload(this, e);
    });

    // Search result page and Product page
    $('.buy_button').click(function() {
        _gaq.push(['_trackEvent', window.location.pathname, 'Product - Add to Cart'])
        _gaq.push(['_trackPageview', '/virtual/add-to-cart/']);
    });

    // Order process
    var $body = $('body');
    if ($body.hasClass('p-checkout')) {
        $('.campaign_code input[type="submit"]').click(function() {
            _gaq.push(['_trackEvent', window.location.pathname, 'Process - Promotional Code']);
        });
        $('.CheckOutLoginForm input[type="submit"]').click(function() {
            _gaq.push(['_trackEvent', window.location.pathname, 'Process - Sign In']);
        });
        $('input[id$="c_RegisterCustomerAccount"],label[for$="c_RegisterCustomerAccount"]').click(function() {
            _gaq.push(['_trackEvent', window.location.pathname, 'Process - Account creation']);
        });
        $('input[id$="c_RegisterForNewsletter"],label[for$="c_RegisterForNewsletter"]').click(function() {
            _gaq.push(['_trackEvent', window.location.pathname, 'Process - Sign up Newsletters']);
        });
    } else if ($body.hasClass('p-ermservicebooking')) {
        $('.orderflow_next_prev .right a').click(function() {
            var region = $('select[id$="DropDownListRegionWorkShop"]').val();
            var $reservationInfo = $('.iframe_booking strong');
            var centre = $reservationInfo.eq(0).text();
            var dateTime = $reservationInfo.eq(1).text().split(' ');
            var weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
            var weekday = dateTime.length > 1 ? weekdays[(new Date(dateTime[0])).getDay()] : 'unknown';
            var hour = dateTime.length > 1 ? dateTime[1].substring(0, 2) : 'unknown';
            _gaq.push(['_trackEvent', window.location.pathname, 'Process - Region', 'Region - ' + region]);
            _gaq.push(['_trackEvent', window.location.pathname, 'Process - Centre', 'Centre - ' + centre]);
            _gaq.push(['_trackEvent', window.location.pathname, 'Process - Week day', 'Week day - ' + weekday]);
            _gaq.push(['_trackEvent', window.location.pathname, 'Process - Time period', 'Time period - ' + hour]);
        });
    }

    // Order confirmation page


	//Business - Home page
	$('#news_letter .black_button a').click(function(e) {
        _gaq.push(['_trackEvent', window.location.pathname, 'Business home - Sign up for Newsletters']);
        DelayUnload(this, e);
    });
	$('#ResellerMap area.hotspot').click(function(){
		var mapArea = $(this).attr('title');
		_gaq.push(['_trackEvent', window.location.pathname, 'Business home - Seller map: ' + mapArea]);
	});
	
	
	//Buisness - News letter
	$('#main_column').find(':checkbox').filter('[name*="newslistRepeater"]').click(function() {
		var newsLetterType = $(this).next('span').html();
		_gaq.push(['_trackEvent', window.location.pathname, 'Business news letter - Type: ' + newsLetterType]);
    });
	$('#main_column').find('input[type="submit"]').filter('[name*="newslistRepeater"]').click(function(e) {
		_gaq.push(['_trackEvent', window.location.pathname, 'Business news letter - Registration button']);
		DelayUnload(this, e);
    });

	//Reseller - Apply for user account
	$('#resellertextpuff .first_div a:first').click(function(e){
		_gaq.push(['_trackEvent', window.location.pathname, 'Reseller home - Apply image']);
		DelayUnload(this, e);
	});
	$('#resellertextpuff .black_button a').click(function(e){
		_gaq.push(['_trackEvent', window.location.pathname, 'Reseller home - Apply button']);
		DelayUnload(this, e);
	});
	$('input[id$="Content_MainCol_c_buttonSend"]').click(function(e) {
		_gaq.push(['_trackEvent', window.location.pathname, 'Reseller apply - Send button']);
		DelayUnload(this, e);
    });
	
	//Reseller (Logged in) - Quicksearch
	var $resellerQuickSearch = $('#resellerquicksearch');
    if ($resellerQuickSearch.length > 0) {
        $resellerQuickSearch.find('#sectionWidth input').focusout(function() {
           	var value = $(this).val();
            _gaq.push(['_trackEvent', window.location.pathname, 'Reseller quicksearch - Width', 'Width - ' + value]);
        });
		$resellerQuickSearch.find('#aspectRatio input').focusout(function() {
            var value = $(this).val();
	        _gaq.push(['_trackEvent', window.location.pathname, 'Reseller quicksearch - Height', 'Height - ' + value]);
        });
		$resellerQuickSearch.find('#diameter input').focusout(function() {
			var value = $(this).val();
	        _gaq.push(['_trackEvent', window.location.pathname, 'Reseller quicksearch - Wheel size', 'Wheel size - ' + value]); 
        });
		$resellerQuickSearch.find('input[id$="m_radioButtonSummer"]').click(function() {
            _gaq.push(['_trackEvent', window.location.pathname, 'Reseller quicksearch - Season Summer']);
        });
        $resellerQuickSearch.find('input[id$="m_radioButtonWinter"]').click(function() {
           _gaq.push(['_trackEvent', window.location.pathname, 'Reseller quicksearch - Season Winter']);
        });
        $resellerQuickSearch.find('input[id$="m_radioButtonStuds"]').click(function() {
            _gaq.push(['_trackEvent', window.location.pathname, 'Reseller quicksearch - Season Winter (no studs)']);
        });
        $resellerQuickSearch.find('input[id$="ResellerQuickSearch_btnSearchValue"]').click(function() {
    		_gaq.push(['_trackEvent', window.location.pathname, 'Reseller quicksearch - Search']);
        });
		$resellerQuickSearch.find('.black_button a').click(function(e) {
           	_gaq.push(['_trackEvent', window.location.pathname, 'Reseller quicksearch - Show all products']);
        });
	}
	
	//Reseller (logged in) - Order History
	$('.order_history_container input.smallBtn').click(function(){
		_gaq.push(['_trackEvent', window.location.pathname, 'Reseller Order History - Get history']);
	});
	
	//Reseller - Shop
	var $carSpecBox = $('#car_spec_box');
	if ($carSpecBox.length > 0) {
		
		//Tyres
		$('ul.category li[id^="tyre_maingroup"] a').click(function() {
           	var category = $(this).html();
			_gaq.push(['_trackEvent', window.location.pathname, 'Reseller shop - Tyre category', 'Tyre category - ' + category]);
        });
		$carSpecBox.find('input[id$="m_textBoxSearchSectionWidth"]').focusout(function() {
           	var selectedCategory = $carSpecBox.find('strong.selectedCategory').html();
			var value = $(this).val();
            _gaq.push(['_trackEvent', window.location.pathname, 'Reseller tyre shop - Width', 'Width - ' + value + ' Category - ' + selectedCategory]);
        });
		$carSpecBox.find('input[id$="m_textBoxSearchAspectRatio"]').focusout(function() {
           	var selectedCategory = $carSpecBox.find('strong.selectedCategory').html();
			var value = $(this).val();
            _gaq.push(['_trackEvent', window.location.pathname, 'Reseller tyre shop - Height', 'Height - ' + value + ' Category - ' + selectedCategory]);
        });
		$carSpecBox.find('input[id$="m_textBoxSearchDiameter"]').focusout(function() {
           	var selectedCategory = $carSpecBox.find('strong.selectedCategory').html();
			var value = $(this).val();
           	_gaq.push(['_trackEvent', window.location.pathname, 'Reseller tyre shop - Wheel size', 'Wheel size - ' + value + ' Category - ' + selectedCategory]);
        });
		$carSpecBox.find('select[id$="m_dropDownListBrands"]').bind('change keyup', function() {
           	var selectedCategory = $carSpecBox.find('strong.selectedCategory').html();
			var value = $(this).children("option:selected").html();
            _gaq.push(['_trackEvent', window.location.pathname, 'Reseller tyre shop - Brand', 'Brand - ' + value + ' Category - ' + selectedCategory]);
        });
		$carSpecBox.find('input[id$="m_speedRating"]').focusout(function() {
           	var selectedCategory = $carSpecBox.find('strong.selectedCategory').html();
			var value = $(this).val();
            _gaq.push(['_trackEvent', window.location.pathname, 'Reseller tyre shop - Speed rating', 'Speed rating - ' + value + ' Category - ' + selectedCategory]);
        });
		$carSpecBox.find('input[id$="m_studs"]').click(function() {
           	var selectedCategory = $carSpecBox.find('strong.selectedCategory').html();
			_gaq.push(['_trackEvent', window.location.pathname, 'Reseller tyre shop - Season Winter (no studs)', 'Category - ' + selectedCategory]);
        });
		$carSpecBox.find('.black_button a').click(function() {
           	var selectedCategory = $carSpecBox.find('strong.selectedCategory').html();
			_gaq.push(['_trackEvent', window.location.pathname, 'Reseller tyre shop - Search', 'Category - ' + selectedCategory]);
        });
		
		//Rims
		$('ul.category li[id^="rim_maingroup"] a').click(function() {
	    	var category = $(this).html();
			_gaq.push(['_trackEvent', window.location.pathname, 'Reseller shop - Rim category', 'Rim category - ' + category]);
	    });
		$carSpecBox.find('input[id$="_Content_MainCol__searchText"]').focusout(function() {
           	var selectedCategory = $carSpecBox.find('strong.selectedCategory').html();
			var value = $(this).val();
			_gaq.push(['_trackEvent', window.location.pathname, 'Reseller rim shop - Description', 'Description - ' + value + ' Category - ' + selectedCategory]);
        });
		$carSpecBox.find('input[id$="_Content_MainCol_RimSearchDimension"]').focusout(function() {
           	var selectedCategory = $carSpecBox.find('strong.selectedCategory').html();
			var value = $(this).val();
			_gaq.push(['_trackEvent', window.location.pathname, 'Reseller rim shop - Wheel size', 'Wheel size - ' + value + ' Category - ' + selectedCategory]);
        });
		$carSpecBox.find('.black_button a[id$="_Content_MainCol_LinkButton1"]').click(function() {
           	var selectedCategory = $carSpecBox.find('strong.selectedCategory').html();
            _gaq.push(['_trackEvent', window.location.pathname, 'Reseller rim shop - Search', 'Category - ' + selectedCategory]);
        });
    }

    // By delaying a click or submit Google Analytics will have some time to report the event.
    function DelayUnload(element, event) {
        if ('click' === event.type && element.href) {
            event.preventDefault();
            setTimeout(function() {
                document.location = element.href;
            }, 100); // milliseconds
        } else if ('submit' === event.type) {
            event.preventDefault();
            setTimeout(function() {
                $(element).unbind('submit');
                element.submit();
            }, 100); // milliseconds
        }
    }
});
