$(function () {
    var nightrace = {
        init: function () {
			//Tracking Events Page Load
            _gaq.push(['_trackEvent', 'tpe_gnr_2011', 'load', 'page']);
        },
        initNav: function () {
            $('#mainNav').onePageNav();

			//Tracking Events Nav			
            $('#mainNav > li > a').bind('click', function (event) {
                var id = event.target.id;

                switch (id) {
	                case 'home':
	                    _gaq.push(['_trackEvent', 'tpe_gnr_2011_ main_nav', 'click', 'night_race']);
	                break;

	                case 'about':
	                    _gaq.push(['_trackEvent', 'tpe_gnr_2011_ main_nav', 'click', 'about']);
	                break;

	                case 'signUp':
	                    _gaq.push(['_trackEvent', 'tpe_gnr_2011_ main_nav', 'click', 'sign_up']);
	                    break;

	                case 'photos':
	                    _gaq.push(['_trackEvent', 'tpe_gnr_2011_ main_nav', 'click', 'photos']);
	                break;   
                }
            });

            //Tracking Events Social Net	
            $('#shareSocial > a').bind('click', function (event) {
   				var $this = $(this),
					className = $this.attr("class");
				
                switch (className) {
                	case 'addthis_button_facebook at300b':
                    	_gaq.push(['_trackEvent', 'tpe_gnr_2011_exit', 'click', 'facebook']);
                    break;

                	case 'addthis_button_twitter at300b':
                    	_gaq.push(['_trackEvent', 'tpe_gnr_2011_exit', 'click', 'twitter']);
                    break;

					case 'addthis_button_email at300b':
                    	_gaq.push(['_trackEvent', 'tpe_gnr_2011_email', 'click', 'share']);
                    break;
                }
            });
        },
        initYouTubeTabs: function () {
            $('.about-video-menu > li').bind('click', function () {
                var $this = $(this);
                $('.about-video-menu > li').each(function () {
                    var $this = $(this);
                    $this.removeClass('active');
                });
                $this.addClass('active');
            });
        },
        initMapData: function () {
            $.getJSON('data/map-data.json', function (data, textStatus) {
                // Iterate through Data
                $.each(data, function (k, val) {
                    $('#signUpMap').append(
						"<div data-name=" + "'" + val.name + "'" + 
						"id=" + "'" + val.id + "'" + 
						"class='map-coordinate'" + "style=top:" + val.ypos + "px;" + 
						"left:" + val.xpos + "px;" + ">" +
						"<a href=" + "'" + val.link + "'" + "id=" + "'" + val.id + "'" + "data-confirmed=" + "'" + val.confirmed + "'" + "target='_blank'"+ "class='map-link'>" + val.name + "</a>" +
						"</div>"
					);
                });

				$('#signUpMap > div').each(function (k, data) {
					var $this = $(this),
						mapCity = $this.attr('id');
					
					$this.hover(
						function(){
					 		$this.css({'z-index':25});
													   
							//Tracking for hover event
						   	_gaq.push(['_trackEvent', 'tpe_gnr_2011_map', 'hover', mapCity]);
						}, 
						function(){
							 $this.css({'z-index':1});
						}
					);
				});
				
				if(textStatus == "success") {
					$('#signUpMap > div > a').each(function(){ 
						var $this = $(this),
							confirmedRace = $this.attr('data-confirmed'),
							linkCity = $this.attr('id');
						
						if(confirmedRace == "false") {
							$this.css({
							   'font-size':11,
							   'padding-top':10,
							   'color':'#BCBCBC',
							   'height':42 
							});
							
							//Remove these attributes since no value is given
							$this.removeAttr('target');
							$this.removeAttr('href');
						}
						
						$this.bind("click", function(){
							 _gaq.push(['_trackEvent', 'tpe_gnr_2011_map', 'click', linkCity]); 
						});
					});
				}
            });
        },
        initPhotoGallery: function () {
			//Tracking Events Photo Gallery Prev/Next
            $('#prevButton, #nextButton').bind('click', function (event) {
                var id = event.target.id;

                switch (id) {
                	case 'prevButton':
                    	_gaq.push(['_trackEvent', 'tpe_gnr_2011_photos', 'click', 'left']);
                    break;

                	case 'nextButton':
                    	_gaq.push(['_trackEvent', 'tpe_gnr_2011_photos', 'click', 'right']);
                    break;
                }
            });

            $('.callback .moreItems .jCarouselLite').jCarouselLite({
                btnNext: ".moreItems .next",
                btnPrev: ".moreItems .prev",
                speed: 800,
                visible: 1,
				beforeStart: function(a) {},
				afterEnd: function(a) {
					var id = a.attr('id'),
						cptIdTxt = $('#carouselCptTxt');
										
					switch(id){
						case 'image1':
							cptIdTxt.text('Hundreds of Energizer Night Race runners shine in the darkness.');
						break;
						
						case 'image2':
							cptIdTxt.text('Studying by Solar lights in Adulam, Tres Arroyos.');
						break;
						
						case 'image3':
							cptIdTxt.text('Young girl with Solar Light in Ethiopia.');
						break;
						
						case 'image4':
							cptIdTxt.text('Barrett, One Million Lights Representative in Ethiopia.');
						break;
						
						case 'image5':
							cptIdTxt.text('Young boys checking out solar lights, Ethiopia.');
						break;
						
						case 'image6':
							cptIdTxt.text('A sea of Energizer Night Race runners brighten the night.');
						break;
						
						case 'image7':
							cptIdTxt.text('Anna, One Million Lights Founder, donating a light, Kenya.');
						break;
						
						case 'image8':
							cptIdTxt.text('One Million Lights preparing for a solar lights distribution, Kenya.');
						break;
						
						case 'image9':
							cptIdTxt.text('Girls reading by solar lights, Rajasthan, India.');
						break;
						
						case 'image10':
							cptIdTxt.text('Young girl and her new solar light in southern Costa.');
						break;
						
						case 'image11':
							cptIdTxt.text('Energizer Night Race runners take over the streets.');
						break;
						
						case 'image12':
							cptIdTxt.text('One Million Lights distribution by High School Students, Cataduanes, Philippines.');
						break;
						
						case 'image13':
							cptIdTxt.text('6-year-old Ricardo Rojas student Pedro draws by the light of his donated Energizer solar lantern. ');
						break;
						
						case 'image14':
							cptIdTxt.text('Ricardo Rojas students learn how to work their new solar lights. ');
						break;
						
						case 'image15':
							cptIdTxt.text('Energizer\'s Mariano Gavito and Anna Sidana, founder of One Million Lights, explain how to hook up the solar panels to the lights.');
						break;
						case 'image16':
							cptIdTxt.text('Runners keep going as the sun begins to set.');
						break;
						
						case 'image17':
							cptIdTxt.text('Parents, teachers and students show off their new lights with representatives from Energizer, One Million Lights and APAER.');
						break;
						
						case 'image18':
							cptIdTxt.text('Mariano, from Energizer, sits with students and their solar lights outside the Ricardo Rojas school.');
						break;
						
						case 'image19':
							cptIdTxt.text('11-year-old Mauricio studies into the evening with the help of his new solar lantern.');
						break;
						
						case 'image20':
							cptIdTxt.text('The donated lights will allow children and their families to extend their day so they have more time to work, study and play.');
						break;
						
						case 'image21':
							cptIdTxt.text('Our first runners to cross the starting line light the way for the rest of the group.');
						break;
						case 'image22':
							cptIdTxt.text('By the light of the Energizer lantern, 6-year-old Pedro and his father Hugo complete their project.');
						break;
						
						case 'image23':
							cptIdTxt.text('Together Energizer and One Million Lights are working to shed light on the need for safe lighting in rural communities around the world. ');
						break;
						
						case 'image24':
							cptIdTxt.text('Energizer and One Million Lights are working with local people like Yolanda (center) to help bring light to communities around the world. ');
						break;
						
						case 'image25':
							cptIdTxt.text('Energizer Night Race runners blaze a trail.');
						break;
						
						case 'image26':
							cptIdTxt.text('Mariano from Energizer demonstrates the light settings to 10-year-old student Laura.');
						break;
						
						case 'image27':
							cptIdTxt.text('10-year-old Facundo demonstrates how he\'s learned to connect his solar lantern to the charging panels.');
						break;
						case 'image28':
							cptIdTxt.text('One Million Lights founder, Anna Sidana, shows Ricardo Rojas students some benefits of their new lights.');
						break;
						
						case 'image29':
							cptIdTxt.text('Ricardo Rojas students hold up their new solar lights, donated by Energizer in partnership with the global charity One Million Lights.');
						break;
						
						case 'image30':
							cptIdTxt.text('Runners get charged up with Mr. Energizer.');
						break;
					}
				}
            });
        },
        initTracking: function () {
            //Tracking Events One Million Lights / Donation
            $('.about-links > a').bind('click', function(event) {
                var id = event.target.id;

                switch (id) {
                	case 'oneMillionLink':
                    	_gaq.push(['_trackEvent', 'tpe_gnr_2011', 'click', 'oml_logo']);
                    break;

                	case 'donationLink':
                    	_gaq.push(['_trackEvent', 'tpe_gnr_2011', 'click', 'donation']);
                    break;
                }
            });

            //Tracking Events Lantern
            $('#lanternLink, #energizerLink, #footerLogo').bind('click', function(event) {
                var id = event.target.id;

                switch (id) {
                	case 'lanternLink':
                    	_gaq.push(['_trackEvent', 'tpe_gnr_2011_exit', 'click', 'lantern']);
                    break;

                	case 'energizerLink':
                    	_gaq.push(['_trackEvent', 'tpe_gnr_2011_exit', 'click', 'energizer.com_text_redirect']);
                    break;

					case 'footerLogo':
                    	_gaq.push(['_trackEvent', 'tpe_gnr_2011_exit', 'click', 'energizer.com_logo_redirect']);
                    break;
                }
            });

 			$('#google_translate_element').bind('mousedown', function(){
	           _gaq.push(['_trackEvent', 'tpe_gnr_2011', 'click', 'google_translate']);
			});
        }
    } 

    nightrace.init();
    nightrace.initNav();
    nightrace.initYouTubeTabs();
    nightrace.initMapData();
    nightrace.initPhotoGallery();
    nightrace.initTracking();
});
