/*
 * alt tag on href will replace the text (or Link Name, the 2nd value (Event Action) in GA) 
 * Also, note that if the destination is not pre-defined below with "screenid", then alt will also be the Link Destination (3rd GA value (Event Label))
 * alt2 tag on href will replace the Link Destination (the 3rd value in GA (Event Label))
 * e.g. alt="View on Map" alt2="Map Detail"
*/


jQuery(document).ready(function($){
	var section = "";//first value in GA - Link Section
	//var URL = "";
	var filetypes = "";
	var ALT2 = "";
	var TEXT = "";
	var FILE = "";
	var MAPPING = "";
	var SEO_LDP = "";
	var SEO_WL = "";
	var menu_parent="";
	var screenid_name="";//second value in GA - Link Name
	var screenid="";//third value in GA - Link Destination
	var whichSection="";


	$('a').click(function() {
		section = $('div.left-area').attr('alt');
	});
	$('a').click(function() {
		section = $('.main-area').attr('alt');
	});
	$('div.menu a').click(function() {
		section = 'Header';
	});
	$('#vow_navigation a').click(function() {
		section = 'VOW Navigation';
	});
	$('#footer a').click(function() {
		section = 'Footer';
	});
	$('#page_footer a').click(function() {
		section = 'Footer';
	});
	$('#footer1 a').click(function() {
		section = 'Footer';
	});
	$('#footer2 a').click(function() {
		section = 'Footer';
	});
	$('div.right-sidebar a').click(function() {
		section = 'Right Navigation';
	});
	$('#ga-listing-summary a').click(function() {
		section = 'Listing Summary';
	});
	$('#ga-listing-detail a').click(function() {
		section = 'Listing Detail';
	});
	$('#rightNavigation a').click(function() {
		section = 'Right Navigation';
	});
	$('#right_rail a').click(function() {
		section = 'Right Navigation';
	});
	$('#mainlevel a').click(function() {
		section = 'Left Navigation';
	});
	$('#sidebar a').click(function() {
		section = 'Left Navigation';
	});
	$('#leftnav a').click(function() {
		section = 'Left Navigation';
	});
	$('#left_rail a').click(function() {
		section = 'Left Navigation';
	});
	$('#leftcol a').click(function() {
		section = 'Left Navigation';
	});
	$('#content_left a').click(function() {
		section = 'Left Navigation';
	});
	$('#header a').click(function() {
		section = 'Header';
	});
	$('#Tier1-Bg a').click(function() {
		section = 'Header';
	});
	$('#subnav a').click(function() {
		section = 'Header';
	});
	$('#topnav a').click(function() {
		section = 'Header';
	});
	$('#nav a').click(function() {
		section = 'Navigation';
	});
	$('#main_menu a').click(function() {
		section = 'Header';
	});

	$('a').click(function() {
		var URL = "";
		URL = $(this).attr('href');
		if (typeof console != "undefined") { 
			console.log('URL being evaluated: '+URL);
		}
		if(URL == ''){
			if (typeof console != "undefined") { 
				console.log('It got into the NO URL section');
			}
			URL = window.location.href;
			$(this).attr('href',URL);
		}
		//URL being evaluated
		filetypes = /\.(php|html|html|shtml|pdf|jsp)$/i;//static page filetypes
		ALT2 = $(this).attr('alt2');
		TEXT = $(this).attr('alt'); //text of link alt tag (mostly for images, but can be used to make specific text)
		if(!TEXT){
			TEXT = $(this).text(); //text of href URL
		}

		FILE = $.url(URL).attr('file'); //for static pages, gets file name ie buyer.php
		MAPPING = $.url(URL).attr('host'); //for mapping links
		SEO_LDP = $.url(URL).segment(1);//check for LDP for /homes/
		SEO_WL = $.url(URL).segment(2);//check for LDP for /show, email and info

		$('li:has(> ul)').addClass('parent') //adds class of parent to top li in menuing/navigation
		menu_parent = $(this).parents('li:has(ul)').children('a').text();//this gets the text of the main list item if there is another list underneath it
		if (URL.match(filetypes)){//static pages
			if(URL.indexOf('-homes') > -1){
				screenid_name = TEXT;
				screenid = 'Community Information';
			} else {
				if (typeof console != "undefined") { 
					console.log('static non-community page');
				}
				screenid_name = TEXT;
				screenid = TEXT;
			}
		} else if(ALT2){
			if (typeof console != "undefined") { 
				console.log('this has an alt2');
			}
			screenid_name = TEXT;
			screenid = ALT2;
		} else {
			if (typeof console != "undefined") { 
				console.log('this is a JSP link');
			}
			screenid = $.url(URL).param('ScreenID');//JSPs
			if(screenid != ''){
				//pretty name based on screenid if some sort of summary page
				if((screenid) == 'OFFICE_SUMMARY_P'){
					screenid_name = TEXT;
					screenid ='Office Summary';
				} else if((screenid) == 'SEARCH_LISTING_PALL'){
					screenid_name = TEXT;
					screenid ='Search All Listings';
				} else if((screenid) == 'AgentsSummary_Public'){
					screenid_name = TEXT;
					screenid ='Agent Summary';
				} else if((FILE) == 'viewer.jsp'){
					screenid_name = TEXT;
					screenid ='Listing Map';
				} else if((screenid) == 'LISTING_DETAIL_P'){
					if (typeof console != "undefined") { 
						console.log('non-seo LDP');
					}
					screenid_name = TEXT;
					screenid ='Listing Detail';
				} else if((screenid) == 'VOW_LOGIN'){
					if((TEXT).indexOf('Add') > -1){
						screenid_name = 'Add to Favorites - Not Logged In';
						screenid ='Add to Favorites';
					} else if((TEXT).indexOf('Current') > -1){
						screenid_name = 'Current Favorite';
						screenid ='VOW Home';
					} else {
						screenid_name = 'VOW Login';
						screenid ='VOW Login';
					}

				} else if((screenid) == 'PANO_XML_GEN'){
					screenid_name = TEXT;
					screenid = 'Flash Panorama Tour';
				} else if((screenid) == 'MEDIA_DETAIL_VR'){
					screenid_name = TEXT;
					screenid = 'QuickTime VR Tour';
				} else if((TEXT) == '...Read More'){
					screenid_name = TEXT;
					screenid = 'Listing Detail';


				} else if((SEO_LDP) == 'homes'){
					if((TEXT).indexOf('$') > -1){
						screenid_name = 'Listing Price';
						screenid ='Listing Detail';
					} else if(((SEO_WL) == 'show') || ((SEO_WL) == 'info') || ((SEO_WL) == 'email') || ((SEO_WL) == 'print')){
						screenid_name = TEXT;
						screenid = TEXT;
					} else {
						if (typeof console != "undefined") { 
							console.log('LDP link evaluation');
						}
						screenid_name = TEXT;
						screenid ='Listing Detail';
					}



				} else if(URL.indexOf('-homes') > -1){
					screenid_name = TEXT;
					screenid = 'Community Information';
				} else if(URL.indexOf('/slideshow') > -1){
					screenid_name = TEXT;
					screenid = 'Slide Show';
				} else {
					screenid_name = TEXT;
					screenid = TEXT;
				}
			} else if(URL.indexOf('addToFavorites') > -1){
				screenid_name = 'Add to Favorites - Logged In';
				screenid = 'Add to Favorites';
			} else if(URL.indexOf('removeFromFavorites') > -1){
				if (typeof console != "undefined") { 
					console.log('remove from favorites');
				}
				screenid_name = TEXT;
				screenid = 'Listing Detail';
			} else if(URL.indexOf('-homes') > -1){
				screenid_name = TEXT;
				screenid = 'Community Information';
			} else if((FILE) == 'viewer.jsp'){
				screenid_name = TEXT;
				screenid ='Listing Map';


			} else if((SEO_LDP) == 'homes'){
				if (typeof console != "undefined") { 
					console.log('seo detail page');
				}
				screenid_name = TEXT;
				screenid ='Listing Detail';


			} else {
				screenid_name = TEXT;
				screenid = TEXT;
			}
		}

		//alert('1st GA Value: '+section+'\n2nd GA Value: '+screenid_name+'\n3rd GA Value: '+screenid);
		if(menu_parent){
			if (typeof console != "undefined") { 
				console.log('1st GA Value: '+section+'\n2nd GA Value: '+menu_parent+' - '+screenid_name+'\n3rd GA Value: '+screenid);
			}
			vp_ga_tracking(section,menu_parent+' - '+screenid_name,screenid);
		} else {
			if (typeof console != "undefined") { 
				console.log('1st GA Value: '+section+'\n2nd GA Value: '+screenid_name+'\n3rd GA Value: '+screenid);
			}
			vp_ga_tracking(section,screenid_name,screenid);
		}
	});
});
