﻿/****************************************************************************************	
[GLOBAL JQUERY]
*****************************************************************************************/
$(document).ready(function() {

    // Creating custom :external selector
    $.expr[':'].external = function(obj) {
        return !obj.href.match(/^mailto\:/) && (obj.hostname.toLowerCase().replace("www.", "") != location.hostname.toLowerCase().replace("www.", ""))
    };
    // Add 'external' image to all external links
    $('a:external')
    //.append('<span class="external"></span>')
    //.attr('target','_blank')
    // Add GA track for clicking external links
	.bind('mousedown', function() {
	    try {
	        var uri = parseUri($(this).attr('href'));
	        var thisPath = location.pathname + '/external-links/' + $(this).attr('href').replace(uri["protocol"] + '://', '');
	        thisPath = thisPath.replace('//', '/');
	        _gaq.push(['_trackPageview', thisPath]);
	    }
	    catch (err) {
	    }
	    return true;
	})
	;


    // Clear text input values
    var swap_text_boxes = [];
    jQuery.each($("input[type='text'].autoclear"), function() {
        swap_text_boxes[$(this).attr('id')] = $(this).attr('value');
        $(this).bind('focus', function() {
            if ($(this).val() == swap_text_boxes[$(this).attr('id')]) {
                $(this).val('');
            }
        });
        $(this).bind('blur', function() {
            if ($(this).val() == '') {
                $(this).val(swap_text_boxes[$(this).attr('id')]);
            }
        });
    });

    // Enable forms to be submitted via ENTER key
    var AreaSelector = "#content-wrapper, .search, #aside";
    var ButtonSelector = "input[type='submit'],input[type='image'],button";
    jQuery.each($(AreaSelector), function() {
        $(this).keypress(function(e) {
            if (e.which == 13 && e.target.type != 'textarea') {
                var arrItems = $(this).find(ButtonSelector);
                if (arrItems.length > 0) {
                    $(this).find(ButtonSelector)[0].click();
                }
                return false;
            }
        });
    });

    // Append WAI-ARIA landmark roles
    $("#header").attr("role", "banner");
    $(".search").attr("role", "search");
    $("#nav-main").attr("role", "navigation");
    $("#content-main").attr("role", "main");
    $("#aside").attr("role", "complementary");
    $("#footer").attr("role", "contentinfo");

});

// Parse Uri
function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};
