/*

    @project: Baker Tilly
    @file: script.js / @dev: cem@skyron.co.uk
    
    
*/


function toggleTextOut(e,txtval) {
	if(e.value=="") e.value = txtval;

}
function toggleTextIn(e,txtval) {
	if(e.value==txtval) e.value="";
	else e.select();
}

/*
    Dom is ready!
*/
$(document).ready(function() {

    setupGoogleTracking();

    // tint rows of table
    $('table tbody tr:odd').addClass('tableRowOdd');

    $(".jsusersonly").show();

    // fill empty table cells
    $("td:empty").html("&nbsp;");

    setupAnchorOperations();

    /* 
    disable submit buttons that id starts with nojs [nojs-sbmt] 
    */
    $("input[id^='nojs']").hide();

    $(".swap").hover(function() {
        if ($(this).attr("src").indexOf("-on.") == -1) {
            var newSrc = $(this).attr("src");
            newSrc = (newSrc.replace(/.jpg|.gif|.png/, '-on' + newSrc.substr(newSrc.length - 4)));
            $(this).attr("src", newSrc);
        }
    }, function() {
        if ($(this).attr("src").indexOf("-on.") > 0) {
            var oldSrc = $(this).attr("src").replace('-on.', '.');
            $(this).attr("src", oldSrc);
        }
    });

    // add tracking to all external links
    $(function() {
        $("a[href*='://']:not([href*='" + location.hostname + "'])").each(function() {
            var onclick = $(this).attr("onclick");
            if (onclick == null || onclick == "") {
                $(this).trackUrlOnClick($(this).attr("href"));
                //$(this).attr("onclick", "javascript:try{var pageTracker = _gat._getTracker('UA-13066537-1');pageTracker._trackPageview('" + this.href + "');}catch(err){}");
            }
        });
    });

    // add tracking to all mailto links
    $(function() {
        $("a[href^='mailto:']").each(function() {
            var onclick = $(this).attr("onclick");
            if (onclick == null || onclick == "") {
                var address = $(this).attr("href");
                $(this).buildOnClickTrackingUrl(window.location.pathname, [{ "key": "mailto", "value": address.substring(7)}]);
                //$(this).attr("onclick", "javascript:try{var pageTracker = _gat._getTracker('UA-13066537-1');pageTracker._trackPageview('" + window.location.pathname + "?mailto=" + address.substring(7) + "');}catch(err){}");
            }
        });
    });

    // add tracking to all pdf downloads
    $(function() {
        $("a[href$='.pdf']").each(function() {
            var onclick = $(this).attr("onclick");
            if (onclick == null || onclick == "") {
                $(this).trackUrlOnClick($(this).attr("href"));
                //$(this).attr("onclick", "javascript:try{var pageTracker = _gat._getTracker('UA-13066537-1');pageTracker._trackPageview('" + this.href + "');}catch(err){}");
            }
        });
    });

    // add rollover image to WD
    $(function() {
        $("img.imgCollapser").hover(function() {
            $(this).attr("src", $(this).attr("src").split(".").join("-on."));
        }, function() {
            $(this).attr("src", $(this).attr("src").split("-on.").join("."));
        });
    });

});

var css_browser_selector = function() {var ua=navigator.userAgent.toLowerCase(),is=function(t){return ua.indexOf(t) != -1;},h=document.getElementsByTagName('html')[0],b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?'gecko ff2':is('firefox/3')?'gecko ff3':is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';var c=b+os+' js'; h.className += h.className?' '+c:c;}();

/*****************************************************
****		Web Site Functions		******************
******************************************************/

function popPDF(pdfUrl, strTitle) {
    if (pdfUrl != null && pdfUrl.toString().length > 4 && pdfUrl != "Please select a country" && pdfUrl != "Please select an archive issue") {
		//alert(pdfUrl);
		try {
			var pageTracker = _gat._getTracker("UA-13066537-1");
			pageTracker._trackPageview(pdfUrl);
		} catch (err) { }
		window.open(pdfUrl);
	}
}

function popPDFFromDropdown(dropdownId, titlePrefix) {

	var dropdown = document.forms[0][dropdownId];	
	var pdfUrl = dropdown.options[dropdown.selectedIndex].value;
	var strTitle = titlePrefix + dropdown.options[dropdown.selectedIndex].text

	if (pdfUrl == null || pdfUrl == "" || strTitle.indexOf("Please select ") > -1) {
		return false;
	}

	if (pdfUrl != null && pdfUrl.toString().length > 4 && pdfUrl != "Please select a country" && pdfUrl != "Please select an archive issue") {
		try {
			var pageTracker = _gat._getTracker("UA-13066537-1");
			pageTracker._trackPageview(pdfUrl);
		} catch (err) { }
		window.open(pdfUrl);
	}

	return false; //Stop click event here
}

function getPrintParameter() {
   	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
   	var regexS = "[\\?&]" + "print" + "=([^&#]*)";
   	var regex = new RegExp(regexS);
   	var results = regex.exec(window.location.href);
   	if (results == null)
   		return "";
   	else
   		return "&print=1";
}

function setupGoogleTracking() {
    $('.jProfilePopup').click(function() {
        manualBuildClickTrackerUrl("/web/show-user-profile", [{ "key": "name", "value": $(this).html() }, { "key": "url", "value": location.pathname}])
    });
    // Unable to bind click to onclick method as text can change, so handle the click using an event and fire off the manual tracker.
    $('.jSearchButton').click(function() { manualConditionalBuildClickTrackerUrl($('.jSearchValue').val().length > 0, "/web/search", [{ "key": "searchterm", "value": $('.jSearchValue').val() }, { "key": "url", "value": location.pathname}]) });
}


