// 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", false);
addLink("#left_nav_content a", "left/left_nav_content", false);

//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);
            });
    });
    
    // track that we loaded clacking on this page
    clack("pageready");
    
};