﻿function popUpWindow(URLStr, width, height) {
    window.open(URLStr, 'name', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=yes, width=' + width + ', height=' + height);
}

function ReplaceAll(data, stringToFind, stringToReplace) {
    var temp = data;
    var index = temp.indexOf(stringToFind);
    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}

jQuery(function ($) {
    //apply google tag to external hyperlinks including pdfs and docs
    //and to internal download links including those in sub-domains
    //**************************************
    var filetypes = /\.(zip|exe|pdf|docx?|xlsx?|pptx?|mp3)$/i;

    $('a').each(function () {
        try {
            var href = $(this).attr('href');

            if (href == undefined || href == '') return;

            if ((href.match(/^https?\:/i)) && (!href.match(/^https?\:\/\/(([^.]+)\.)?bhujmandir\.org/i))) {
                $(this).click(function () {
                    var buildUrl = href.replace(/^https?\:\/\//i, '');
                    //alert(buildUrl.toLowerCase());
                    _gaq.push(['_trackEvent', 'External Link', 'Click', buildUrl.toLowerCase()]);
                });
            } else if (href.match(filetypes)) {
                $(this).click(function () {
                    var buildUrl = href.replace(/^https?\:\/\//i, '');
                    var filePath = ReplaceAll(buildUrl, '_', '-');
                    filePath = ReplaceAll(filePath, '%20', '-');
                    //alert(filePath.toLowerCase());
                    _gaq.push(['_trackEvent', 'Download Link', 'Click', filePath.toLowerCase()]);
                });
            }
        } catch (err) { }
    });
});
