//SET NOCONFLICT TO WORK WITH OTHER LIBRARIES
jQuery.noConflict();

//CUSTOM JQUERY FUNCTIONS
jQuery(document).ready(function(){


	// ----------------------------------------------------------------
	// map replacement on click
	jQuery(".mapTrigger.fredericton").click(
		function () {
			jQuery('#mapDisplay').fadeOut(1000,function()
				{ jQuery("#mapDisplay").attr("src", '/maps/fredericton').fadeIn(2000); });
			jQuery('.heading.mapHeading').fadeOut(0,function()
				{ jQuery(".heading.mapHeading").replaceWith("<p class='heading mapHeading'>Fredericton Office:</p>").fadeIn(0); });
		}
	);

	jQuery(".mapTrigger.charlottetown").click(
		function () {
			jQuery('#mapDisplay').fadeOut(1000,function()
				{ jQuery("#mapDisplay").attr("src", '/maps/charlottetown').fadeIn(2000); });
			jQuery('.heading.mapHeading').fadeOut(0,function()
				{ jQuery(".heading.mapHeading").replaceWith("<p class='heading mapHeading'>Charlottetown Office:</p>").fadeIn(0); });
		}
	);
	
	jQuery(".mapTrigger.manchester").click(
		function () {
			jQuery('#mapDisplay').fadeOut(1000,function()
				{ jQuery("#mapDisplay").attr("src", '/maps/manchester').fadeIn(2000); });
			jQuery('.heading.mapHeading').fadeOut(0,function()
				{ jQuery(".heading.mapHeading").replaceWith("<p class='heading mapHeading'>Manchester Office:</p>").fadeIn(0); });
		}
	);



	// ----------------------------------------------------------------
	// superfish dropdowns
	jQuery("ul.sf-menu").superfish({ 
        autoArrows:    	false
    });


	// -------------------------------------------------------------------------------------------------------------------
	// animate scrolling
	jQuery(".linkIndustry").anchorScroll();
	jQuery(".linkCapabilities").anchorScroll();
	jQuery(".linkAbout").anchorScroll();
	jQuery(".linkCareers").anchorScroll();
	jQuery(".linkContact").anchorScroll();
	jQuery(".linkHome").anchorScroll();


	// -------------------------------------------------------------------------------------------------------------------
	// 	when "industryReplace" is clicked then we replace "hideIndustry" with the "industry" page based on the rel of the
	//	link then we append a new hashtag to the URL of the site
	
	jQuery(".industryReplace").click(
		function () {
			jQuery(".hideIndustry").fadeOut('slow');
	  		jQuery(".displayIndustry").fadeIn('slow');

			// defines the frameId as the value in the rel
			var frameId = jQuery(this).attr('rel');
					
			// displays div based on frameId of the link clicked
			jQuery(".displayIndustry").load('/industry/' + frameId + '/' );
			
			// appends rel to hashtag of URL
			window.location.hash = jQuery(this).attr('rel');

		}
	);

	// -------------------------------------------------------------------------------------------------------------------
	// 	when "capabilitiesReplace" is clicked then we replace "hideCapabilities" with the "Capabilities" page based on the rel of the
	//	link then we append a new hashtag to the URL of the site
	
	jQuery(".capabilitiesReplace").click(
		function () {
			jQuery(".hideCapabilities").fadeOut('slow');
	  		jQuery(".displayCapabilities").fadeIn('slow');

			// defines the frameId as the value in the rel
			var frameId = jQuery(this).attr('rel');
					
			// displays div based on frameId of the link clicked
			jQuery(".displayCapabilities").load('/capabilities/' + frameId + '/' );
			
			// appends rel to hashtag of URL
			window.location.hash = jQuery(this).attr('rel');

		}
	);


	// -------------------------------------------------------------------------------------------------------------------
	// hides back to top button when user scrolls to top of site
	jQuery(window).load(
		function(){
			jQuery(".backtotop").addClass("hidden");
		});

	jQuery(window).scroll(
		function(){
		    if (jQuery(this).scrollTop() === 0) {
		        jQuery(".backtotop").addClass("hidden");
		    } 
		    else {
		        jQuery(".backtotop").removeClass("hidden");
		    }
		});





	// ----------------------------------------------------------------
	// career tabs
	jQuery( "#careersDisplay" ).tabs();


	// ----------------------------------------------------------------
	// shows random image in the banner
    jQuery(".banner").hide();

    var elements = jQuery(".banner");
    var elementCount = elements.size();
    var elementsToShow = 1;
    var alreadyChoosen = ",";
    var i = 0;
    while (i < elementsToShow) {
        var rand = Math.floor(Math.random() * elementCount);
        if (alreadyChoosen.indexOf("," + rand + ",") < 0) {
            alreadyChoosen += rand + ",";
            elements.eq(rand).show();
            ++i;
        }
    }


	// ----------------------------------------------------------------
	// clears input when selected
	jQuery('input, textarea').clearInput();

});

//clearInput function
jQuery.fn.clearInput = function(){
	return this.focus(function(){
		if(this.value == this.defaultValue){
			this.value = "";
		}
	}).blur(function(){
		if(!this.value.length){
			this.value = this.defaultValue;
		}
	});
};

//clearForm function
jQuery.fn.clearForm = function() {
	return this.each(function() {
		var type = this.type, tag = this.tagName.toLowerCase();
		if (tag == 'form')
			return jQuery(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};
