﻿/**
 *  JavaScript
 *  www.regina.ca
 *  City of Regina
 *  Requires:
 *    - jQuery 1.4.4
 *    - Google Analytics (asynchronous ga.js)
 *
 *  @author: Dav Jones
 *
 *  [documentation at http://wiki/index.php/Category:Regina.ca]
 *
 */

// preload images
jQuery.preloadImages = function() {   
	for (var i = 0; i < arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);   
	}
}

$(document).ready(function() {
													 
	// search button
	$("div#search_box a").click(function(e) {
		if ($("#search_field").val().length > 0) {																		 
			$("form#search_form").submit();
		} else {
			$("#search_field").focus();
		}
		e.preventDefault();
	});
	
	// preload images
	$.preloadImages(
		"/system/modules/ca.regina.opencms.adetemplate/resources/images/widget_top_ro.png",
		"/system/modules/ca.regina.opencms.adetemplate/resources/images/widget_bottom_ro.png",
		"/system/modules/ca.regina.opencms.adetemplate/resources/images/menu/business_and_investors_ro.png",
		"/system/modules/ca.regina.opencms.adetemplate/resources/images/menu/residents_ro.png",
		"/system/modules/ca.regina.opencms.adetemplate/resources/images/menu/students_and_teachers_ro.png",
		"/system/modules/ca.regina.opencms.adetemplate/resources/images/menu/visitors_ro.png"
	);
	
	// external links selector
	$.expr[':'].external = function(obj) {
		return (!obj.href.match(/^mailto\:/)) && ($.trim($(obj).text()).length > 0) && (obj.hostname) && (obj.hostname !== location.hostname);
	};

	// pdf link selector
	$.expr[':'].pdf = function(obj) {
		return (/\.pdf$/.test(obj.href));
	}
	
	// pdf link selector
	$.expr[':'].email = function(obj) {
		return obj.href.match(/^mailto\:/);
	}
	
	// Google Analytics event tracking
	$('div#content a:pdf').each (function () {
		if (!$(this).is(":external")) {
			$(this).click(function() {
				//alert("_gaq.push(['_trackEvent', 'Regina.ca PDF', 'Download', '"+$(this).attr("href")+"'])");
				_gaq.push(['_trackEvent', 'Regina.ca PDF', 'Download', $(this).attr("href")])
			});
		}
	});
	
	// style external links
	$('div#content a:external').addClass("external").attr("target", "_blank").append(" <img src='/system/modules/ca.regina.opencms.adetemplate/resources/images/icons/external.png' />");
	
	// style pdf links
	$('div#content a:pdf').addClass("pdf").attr("target", "_blank");
	
	// style email links
	$('div#content a:email').addClass("email");
	
	// style class="table_1"
	$("div#content table.table_1 tr:odd").addClass("even");	

});
