// ========= Allow multiple onload events
function addLoadEvent(func) { 
	var oldonload = window.onload; 
	if (typeof window.onload != 'function') { 
		window.onload = func; 
	} else { 
		window.onload = function() { 
			if (oldonload) { 
				oldonload(); 
			} 
			func(); 
		};
	} 
}

// ========= External site links open in new window
function externalLinks() {
	var anchors, i;
	if (!document.getElementsByTagName) { return; }
	anchors = document.getElementsByTagName("a");
	for (i=0; i<anchors.length; i++) {
		anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}

// ========= Email Scramblers
function email(name,text){
	var a,b,c,d,e,f;
	a='<a href=\"mai';
	b='bci-grp';
	a+='lto:';
	c='.com\">';
	if(text!=''){
		d=text;
	} else { 
		d=name+c+b+'.com';
	}
	e='</a>';
	f='@';
	document.write(a+name+f+b+c+d+e);
}

// ========= Copyright Date
function copyright() {
	var d, yr;
	d = new Date();
	yr = d.getFullYear();
	document.write(yr);
}

// ========= Load Analytics
function loadGAScript(){
	var gaJsHost, gaScript, domHead;
    /*Check browser for Dom compatibility*/
	if (!document.getElementsByTagName) { return false; }
	/*Determines whether the page is using a secure or unsecure protocol*/
	gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	/*Writes in the script to the document head*/
	gaScript = document.createElement("script");
	gaScript.setAttribute("src",gaJsHost +"google-analytics.com/ga.js");
	gaScript.setAttribute("type","text/javascript");
	domHead = document.getElementsByTagName("head")[0];
	domHead.appendChild(gaScript);
}
    loadGAScript();
/*Calls the analytics function*/
function analytics(){   
	var pageTracker = _gat._getTracker("UA-13226023-1"); // ***CUSTOM KEY HERE***
	pageTracker._initData();
	pageTracker._trackPageview();
}

// ========= Execute all the onload Events
addLoadEvent(externalLinks); 
addLoadEvent(analytics);

// ========= Jquery HoverIntent
(function($) {
	$.fn.hoverIntent = function(f,g) {
		var cfg, cX, cY, pX, pY, track, compare, delay, handleHover;
		cfg = {
			sensitivity: 7,
			interval: 100,
			timeout: 0
		};
		cfg = $.extend(cfg, g ? { over: f, out: g } : f );
		track = function(ev) {
			cX = ev.pageX;
			cY = ev.pageY;
		};
		compare = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
				$(ob).unbind("mousemove",track);
				ob.hoverIntent_s = 1;
				return cfg.over.apply(ob,[ev]);
			} else {
				pX = cX; pY = cY;
				ob.hoverIntent_t = setTimeout( function(){compare(ev, ob);} , cfg.interval );
			}
		};
		delay = function(ev,ob) {
			ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
			ob.hoverIntent_s = 0;
			return cfg.out.apply(ob,[ev]);
		};
		handleHover = function(onThis) {
			var p, ev, ob;
			p = (onThis.type == "mouseover" ? onThis.fromElement : onThis.toElement) || onThis.relatedTarget;
			while ( p && p != this ) { try { p = p.parentNode; } catch(onThis) { p = this; } }
			if ( p == this ) { return false; }
			ev = jQuery.extend({},onThis);
			ob = this;
			if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); }
			if (onThis.type == "mouseover") {
				pX = ev.pageX; pY = ev.pageY;
				$(ob).bind("mousemove",track);
				if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout( function(){compare(ev,ob);} , cfg.interval );}
			} else {
				$(ob).unbind("mousemove",track);
				if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout( function(){delay(ev,ob);} , cfg.timeout );}
			}
		};
		return this.mouseover(handleHover).mouseout(handleHover);
	};
})(jQuery);

// ========= Jquery Slider Functions
jQuery(function(){
	$("div.load").prepend("<p class='loading'><img src='/images/loader.gif' alt='loading...'/ ></p>");
});
var j = 0;
jQuery.fn.slider = function() {
	return this.each(function(){
		var container, panelHeight, panelCount, bannerHeight, cPanel;
		container = $(this);
		// Remove the preloader gif...
		container.find("p.loading").remove();
		// Self-explanatory...
		container.removeClass("load").addClass("banner");
		// Get the height of a panel, set from CSS...
		panelHeight = container.find("div.panel").height();
		// panelCount gives us a count of the panels in the container...
		panelCount = container.find("div.panel").size();
		// Calculate the height of all the panels when lined up end-to-end...
		bannerHeight = panelHeight*panelCount;
		// Use the above height to specify the CSS height for the panelContainer element...
		container.find("div.panelContainer").css("height" , bannerHeight);
		// Specify the current panel.
		cPanel = 1;
		
			
            function nextslide(){
				var position;
				if (cPanel == panelCount) {
					position = 0;
					cPanel = 1;
					$("div#slide-nav a.current").removeClass("current").parent().parent().find("a:eq(0)").addClass("current");
				} else {
					position = - (panelHeight*cPanel);
					cPanel += 1;
					$("div#slide-nav a.current").removeClass("current").parent().next().find("a").addClass("current");
				}
				$("div.panelContainer").animate({top: position}, 750);
				return false;
			}
			
			// autostart
			timerval = setInterval(nextslide, 10000);
			
			// function for reseting the autoplay
			function resetTimer(){
				clearInterval(timerval);
				timerval = setInterval(nextslide, 10000);
			} 
						
		// Create appropriate nav
		container.each(function(i) {
			if (panelHeight != bannerHeight) {				
				// Create the Tabs
				$(this).after("<div id='slide-nav'><ul><\/ul><\/div>");
				$(this).find("div.panel").each(function(n) {
					definition = $(this).attr('class').split(" ");
					$("div#slide-nav ul").append("<li class='btn" + (n+1) + " " + definition[1] +"'><a href='#" + (n+1) + "'>" + $(this).attr("title") + "<\/a><\/li>");
				});
			};
			
			// Tab nav
			$("div#slide-nav a").each(function(z) {
				// What happens when a nav link is clicked
				$(this).click(function () {
					var position;
					$(this).addClass("current").parent().parent().find("a").not($(this)).removeClass("current"); // wow!
					position = - (panelHeight*z);
					$(this).parent().parent().parent().prev().find("div.panelContainer").animate({top: position}, 750);
					cPanel = z + 1;
					resetTimer();
					return false;
				});
			});
		});
			
		// set first as current nav
		$("div#slide-nav a:eq(0)").addClass("current");
		
		j++;
  });
};

// ========= Close Modal Overlay
function modal() { document.getElementById('overlay').style.display='none' }

// ========= Jquery Functions and variables
function showNav() { $("ul", this).slideDown(200); }
function hideNav() { $("ul", this).slideUp(200); }

// ========= Jquery Onload Events
$(document).ready(function(){
	// ==== Cufon Font Replacement
	Cufon.replace('h1, #sidebar h3, .quote p, .quote cite, #footer dt, .sign-in h4, #home .widget h2', {
		hover: true
	});

	// ==== Dropdowns
	$("#navigation li.primary ul").css("visibility", "visible").css("display", "none");
	$("#navigation li.primary:hover ul").css("visibility", "visible");
	$("li.primary").hoverIntent({
		sensitivity: 5,
		interval: 100,
		over: showNav,
		timeout: 500,
		out: hideNav
	});

	$("div#banner").slider();


});
