/*
 * javascript for essl websites
 * @requires jQuery
 */
jQuery(function($){
	/* replaces interceptclick etc. from old templates */
	$('a[class^=img]').click(function(){
		return false;
	});
	
	if ($("a[href$='mp3']").length) {
		$.getScript("http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js", function(){
			$("a[href$='.mp3']").each(function(i){
		        var mp3_file = $(this).attr("href");
		        var player_ID = "mp3_player"+i;
		        $(this).parent('p').before('<div id="'+player_ID+'"></div>');
				var flashParams = {wmode:"opaque"};
		    	var flashVars = {playerID:player_ID,soundFile:mp3_file};
				var flashAttributes = {name:player_ID,style:"outline: none"};
				var playerURL = "/assets/flash/player.swf";
                swfobject.embedSWF(playerURL, player_ID, "100%", "24", "7", false, flashVars, flashParams, flashAttributes);
			});
		});
	}

	/* goto select element from new corporate templates */
	$('#goto').change(function(){
		if ($(this).val() !== "") {
    		window.location = $(this).val();
		}
	});
	$('#goto').next().hide();
	
	/* google analytics file download and linking trackers */
	if (pageTracker) {
        var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
        $('a').each(function(){
            var href = $(this).attr('href');
            if (href) {
                if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
                    $(this).click(function() {
                        var extLink = href.replace(/^https?\:\/\//i, '');
                        pageTracker._trackEvent('External', 'Click', extLink);
                    });
                } else if (href.match(/^mailto\:/i)){
                    $(this).click(function() {
                        var mailLink = href.replace(/^mailto\:/i, '');
                        pageTracker._trackEvent('Email', 'Click', mailLink);
                    });
                } else if (href.match(filetypes)){
	                $(this).click(function() {
	                    var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
	                    var filePath = href.replace(/^https?\:\/\/(www.)mydomain\.com\//i, '');
	                    pageTracker._trackEvent('Download', 'Click - ' + extension, filePath);
	                });
                }
	        }
        });
	}
});

