// module depends on the following globals set in the page:
// ga_event_tracking -> the Google Analystics page tracker instance
// clack_page_url -> the page on which we are calling the tracking
// clack_pagetype -> the page or pagetype we plan to use for the event tracking Category
	
// a link to clack
function clink(s, l, aat) {
	this.selector = s;
	this.label = l;
	this.addText = aat;
}

// an array of additional links to track per page in addition to the global defaults
var clinks_to_clack = new Array();

// method to add more links to clack
function addLink(selector, label, addtext) {
	clinks_to_clack.push(new clink(selector, label , addtext));
};

// setup a bunch of default clacking (the easily configurable ones)
addLink("#gr_logo,#cp_logo a", "logo", true);
addLink("#firstlevel_tabs_container a", "topnav/firstlevel", true);
addLink("#secondlevel_tabs_container a", "topnav/secondlevel", true);
addLink("#thirdlevel_tabs_container a", "topnav/breadcrumbs", true);
addLink("#left_nav_user a", "left/left_nav_user", true);
addLink("#left_nav_content a", "left/left_nav_content", true);

//clack_debug set's event.preventDefault() on the bind()s
var clack_debug = false;

function clack(modulename) {
	// debugging
	/*
	console.debug("category: " + clack_pagetype);
	console.debug("action: " + modulename);
	console.debug("label: " + clack_page_url);
	console.debug(" --- ");
	*/
	if (typeof ga_event_tracking != 'undefined') {
		ga_event_tracking._trackEvent(clack_pagetype, modulename, clack_page_url); 
	}
};

function clackMore(s,l,at) {
	$(s).bind("click", function(e) {
		if ( clack_debug) { e.preventDefault(); }
		clack(l + (at ? '/'+jQuery.trim($(this).text()) : ''));
	}); // end bind()
}

// method to add all the events
function addClacking() {
	switch (clack_pagetype) {
		case 'platformpage':
		case 'homepage':
			addLink("#browse_games a", "center/browse_games", false);
			addLink("#top_stories a", "center/top_stories", false);
			addLink("#latest_news a", "center/latest_news", false);
			addLink("#latest_screens a", "center/latest_screens", false);
			addLink("#latest_features a", "center/latest_features", false);
			addLink("#latest_previews a", "center/latest_previews", false);
			addLink("#latest_reviews a", "center/latest_reviews", false);
			break;
		case 'cheatplanet':
			addLink(".browse_cheats a", "center/browse_cheats")
			addLink("#top_stories a", "center/top_stories", false);
			addLink(".latest_platform_cheats a", "center/latest_platform_cheats", false);
			break;
		default:
			break;
	}
	
	// add all the links in the to_clack array
	if (clinks_to_clack != 'undefined' && clinks_to_clack.constructor == Array) {
		for(var i = 0; i < clinks_to_clack.length; i++) {
			clackMore(clinks_to_clack[i].selector, clinks_to_clack[i].label, clinks_to_clack[i].addText);
		} // end for
	} // endif

	// TODO -- cms content change -- need id=something
	// on ".side_bar_blocks div.nav_links_bg" for module name
	$("#left_nav_links .side_bar_blocks").each( function(i) {
		var mod = $(this).find("div").attr("id") || "sidebarhtml";
		$(this).find("a").bind("click",
			function(event) {
				if (clack_debug) { event.preventDefault(); }
				clack("left/" + mod);
			});
	});
	
	$("#left_nav_links .tablets").each( function(i) {
		$(this).find("a").bind("click",
			function(event) {
				if (clack_debug) { event.preventDefault(); }
				var img = $(this).find("img").attr("src");
				var paths = img.split('/');
				var mod = paths[paths.length-1].split('.')[0];
				clack("left/" + mod);
			});
	});

	// Right
	$(".right_module").each( function(i) {
		var mod = $(this).attr("id");
		$(this).find("a").bind("click",
			function(event) {
				if (clack_debug) { event.preventDefault(); } 
				clack("right/" + mod);
		});
	});

	// TODO -- cms content change -- need id=something
	// on ".side_bar_blocks div.nav_links_bg" for module name
	$("#right_content .side_bar_blocks").each( function(i) {
		var mod = $(this).find("div").attr("id") || "sidebarhtml";
		$(this).find("a").bind("click",
			function(event) {
				if (clack_debug) { event.preventDefault(); }
				clack("right/" + mod);
		});
	});

	$("#right_content .tablets").each( function(i) {
		$(this).find("a").bind("click",
			function(event) {
				if (clack_debug) { event.preventDefault(); }
				var img = $(this).find("img").attr("src");
				var paths = img.split('/');
				var mod = paths[paths.length-1].split('.')[0];
				clack("right/" +  mod);
		});
	});

	clackMore(".articleBody a", "center/article_link", false);
	clackMore("#commentsLinkArticle a", "center/comments_button", false);
	clackMore("#emailLinkArticle a", "center/email_button", false);
	clackMore("#shareLinkArticle a", "center/bookmark_button", false);
	$("#shareArticle a").click(function(){
		var bookmarkTxt=(jQuery.trim($(this).text())).toLowerCase();
		clack("center/bookmark/"+bookmarkTxt);
	});
	clackMore(".gr_article_pagination a", "center/article_pagination", false);
	clackMore(".comments_ordering a", "center/article_comments", true);
	clackMore(".comments_pagination a", "center/article_comments/pagination", false);
	$(".user_comments a").click(function(){
		var userCommentsClick = jQuery.trim($(this).text());
		if (((userCommentsClick).toLowerCase())=="report") {clack("center/article_comments/report");}
		else {clack("center/article_comments/username");}
	});
	clackMore(".cheats_summary_links a", "center/cheats_level_navigation", true);
	clackMore("#game_level_faq a", "center/game_level_cheats/faq", false);
	clackMore("#game_level_cheats a", "center/game_level_cheats/cheats", false);
	clackMore("#game_level_glitches a", "center/game_level_cheats/glitches", false);
	clackMore("#game_level_hints a", "center/game_level_cheats/hints", false);
	clackMore("#game_level_unlockables a", "center/game_level_cheats/unlockables", false);
	clackMore("#game_level_eastereggs a", "center/game_level_cheats/eastereggs", false);

	clackMore("#game_summary_videos a", "center/game_summary/videos", false);
	clackMore("#game_summary_screenshots a", "center/game_summary/screenshots", false);
	clackMore("#game_summary_stories a", "center/game_summary/stories", false);
	clackMore("#game_summary_cheats a", "center/game_summary/cheats", false);
	clackMore("#game_summary_features a", "center/game_summary/features", false);
	clackMore("#game_summary_news a", "center/game_summary/news", false);
	clackMore("#game_summary_reviews a", "center/game_summary/reviews", false);
	clackMore("#game_summary_previews a", "center/game_summary/previews", false);
	clackMore("#game_summary_comments a", "center/game_summary/comments", false);

	clackMore("#game_level_thumbnail_list a", "center/game_level_article_list/thumbnails", false);
	clackMore("#game_level_blog_list a", "center/game_level_article_list/blog", false);
	clackMore(".list_section_footer .gr_pagination a", "center/game_level_article_list/pagination", false);

	clackMore(".screenshots_list_tab a", "center/game_level_screenshots", true);
	clackMore("#screenshots_thumbnail a", "center/game_level_screenshots/thumbnail", false);
	clackMore("#screenshots_thumbnail_pagination a", "center/game_level_screenshots/pagination", false);

	clackMore("#game_level_videos a", "center/game_level_videos/latest_videos", false);

	clackMore(".summary_links a", "center/game_level_nav", true);
	clackMore(".complete_summary_links a", "center/game_level_nav", true);

	clackMore("#multi_list_page .filter_games a", "center/list_page_filters/game_name", false);
	clackMore("#multi_list_page .input_image", "center/list_page_filters/go_button", false);
	clackMore("#multi_list_page .multi_list_tab a", "center/list_page_filters/view_tabs", false);
	clackMore("#multi_list_page .gr_pagination a", "center/list_page/pagination", false);
	clackMore("#multi_list_page .sort_input_select", "center/list_page/sort", false);
	clackMore("#multi_list_block a", "center/list_page/link", false);

	clackMore("#multi_thumbnail_page .filter_games a", "center/list_thumbnail_filters/game_name", false);
	clackMore("#multi_thumbnail_page .input_image", "center/list_thumbnail_filters/go_button", false);
	clackMore("#multi_thumbnail_page .multi_list_tab a", "center/list_thumbnail_filters/view_tabs", false);
	clackMore("#multi_thumbnail_page .gr_pagination a", "center/list_thumbnail/pagination", false);
	clackMore("#multi_thumbnail_page .sort_input_select", "center/list_thumbnail/sort", false);
	clackMore("#multi_thumbnail_block a", "center/list_thumbnail/link", false);

	clackMore("#multi_blog_page .filter_games a", "center/list_blog_filters/game_name", false);
	clackMore("#multi_blog_page .input_image", "center/list_blog_filters/go_button", false);
	clackMore("#multi_blog_page .multi_list_tab a", "center/list_blog_filters/view_tabs", false);
	clackMore("#multi_blog_page .gr_pagination a", "center/list_blog/pagination", false);
	clackMore("#multi_blog_page .sort_input_select", "center/list_blog/sort", false);
	clackMore("#multi_blog_block a", "center/list_blog/link", false);
	
	clackMore("#no_cheats_guides a", "center/no_cheats_guides/link", false);
	clackMore("#feature_guide_articles .cp_list_thumbnail_items a", "center/guide_articles/link", false);
	
	
	
	// track that we loaded clacking on this page
	clack("pageready");
};