/* 
	Bee Clean - jQuery Site Enhancements
	August 2010
	Atomic Crayon / karen@atomiccrayon.com / SMRT
*/

/* Sections flagged with =
	BODY CLASSES
	LINKS
	LISTINGS & TABLES
	NAVIGATION
	FONT RESIZE
*/

$(document).ready(function(){
	/* =BODY CLASSES
	----------------------------------------------------*/
	// check for CMS form
	
	var CMS = false;

	if ($('form.cmsForm').length > 0) {
		CMS = true;
	}
	
	var IE = false;

	if ($.browser.msie && ($.browser.version < 9.0)) {
		IE = true;
	}
	
	var FR = false;
	
	if ($("body.lang-fr").length > 0) {
		FR = true;
	}
		
	/* =LINKS
	----------------------------------------------------*/
	
	// open rel="external" links in new windows
	
	$("a[rel~='external']").click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	


	/* =LISTINGS & TABLES
	----------------------------------------------------*/
	
	// make boxes clickable
	/*
	$("#latest-news ul li").addClass('click-box').click(function(){
		  window.location=$(this).find("a").attr("href"); return false;
	});
	*/
	
	// stripe tables
	/*
	$('.table-striped tr:even').addClass('even');
	*/
	/* =NAVIGATION
	----------------------------------------------------*/
	$('#main-navigation ul li.parent a').append('<span class="arrow"></span>');
	
	$('#main-navigation ul li.parent ul li a .arrow').remove();
	
	$('#main-navigation ul ul li:last-child').addClass('last');


	/* =META NAVIGATION
	----------------------------------------------------*/
	
	$('#meta-navigation li').append('<span class="separator"> | </span>');
	$('#meta-navigation li:last .separator').remove();

	
	/* =SEARCH
	----------------------------------------------------*/
	/*
	var default_value = 'Enter search topic...';
	
	if (FR) {
		default_value = 'TRANS Enter search topic...';
	}
	
	var searchField = $('#search #query');
	
	if (searchField.val() == '') {
		searchField.val(default_value);
	}
	
	searchField.focus(function() {
        if (searchField.val() == default_value) {
		  searchField.removeClass('blur');
            searchField.val('');
        }
    });
    searchField.blur(function() {
        if (searchField.val() == '') {
 		  searchField.addClass('blur');
           searchField.val(default_value);
        }
    });
*/
    	/* =EMPLOYMENT FORM
	----------------------------------------------------*/
	
	Array.prototype.in_array = function(p_val) {
		for(var i = 0, l = this.length; i < l; i++) {
			if(this[i] == p_val) {
				return true;
			}
		}
		return false;
	}
	
	$("#js-warning").remove();
	
	var Cities = $('.section-join-our-team #content form .city');
	var Regions = ['ab', 'ac', 'bc', 'mb', 'on', 'qc', 'sk' ];
	var Choose = $(".section-join-our-team #content form select[name='location_region']");
	var ChooseValue = $(Choose + ":selected").val();
	
	function ShowCity() {
		$(Cities).addClass('hide');
		if (Regions.in_array(ChooseValue)) {
			var City = '.section-join-our-team #content form div#' + ChooseValue;
			var Label = City + ' label';
			var Select = City + ' select';
			$(City).removeClass('hide');
			$(Label).prepend('<span class="req">*</span> ');
			$(Select).addClass('required');
		}		
	}

	ShowCity();
	
	
	$(Choose).change(function(){
		ChooseValue = $(Choose + ":selected").val();
		ShowCity();
	});
    
	
/* =SITEMAP
----------------------------------------------------*/
	
	if ($('body').hasClass('section-sitemap')) {
		
		// sliders & controls
		
		if (!FR) {
		$("#sitemap").prepend('<div id="sitemap-intro"><p id="activate"><a id="show-all" href="javascript:void(0);">Show All</a> | <a id="hide-all" href="javascript:void(0);">Hide All</a></p></div>');
		}
		else {
		$("#sitemap").prepend('<div id="sitemap-intro"><p id="activate"><a id="show-all" href="javascript:void(0);">Afficher tout</a> | <a id="hide-all" href="javascript:void(0);">Cacher tout</a></p></div>');
		}
	
		$("#sitemap .slider ul li ul.sitemap-1").hide();
		$("#sitemap .slider ul li h3 a").addClass("closed");
		//if (IE) {	curvyCorners.redraw(); }
	
		$("a#hide-all").click(function () {
			$("#sitemap .slider ul li ul.sitemap-1").hide();
			$("#sitemap .slider ul li h3 a").removeClass("open");
			$("#sitemap .slider ul li h3 a").addClass("closed");
			if (IE) {	curvyCorners.redraw(); }
			return false;
		});
		
		$("a#show-all").click(function () {
			$("#sitemap .slider ul li ul.sitemap-1").show();
			$("#sitemap .slider ul li h3 a").removeClass("closed");
			$("#sitemap .slider ul li h3 a").addClass("open");
			if (IE) {	curvyCorners.redraw(); }
			return false;
		});
		
		$("#sitemap .slider ul li h3 a").click(function () {
			$(this).parents("li").children("ul.sitemap-1").toggle();
			$(this).toggleClass("closed");
			$(this).toggleClass("open");
			if (IE) {	curvyCorners.redraw(); }
			return false;
		});

	}

	/* =FONT RESIZE
	----------------------------------------------------*/
	
	
	// add code
	
	var sizeText = 'Text size';
	
	if (FR) {
		sizeText = 'Taille du texte';
	}
	
	$('#language-switch').after('<p id="text-size"> <!-- open TEXT-SIZE -->				<strong>' + sizeText + '</strong> <a href="#" class="size smaller">A-</a> | <a href="#" class="size larger">A+</a></p> <!-- close TEXT-SIZE -->');
	
	
	var CookieName = 'textSize';
	
	// Font Size
	var fontSize;
	var textBase = 76;
	var increment = 10;
	
	// Check for cookie
	
	var cookieSize = $.cookie(CookieName);
	
	if (cookieSize > 0) {
		fontSize = Number(cookieSize);
		$('body').css('font-size', fontSize + '%');
	}
	else {
		fontSize = textBase;
		$('body').css('font-size', fontSize + '%');
	}
	
	// Check for clicks
	
	$('.smaller').click(function(){
		fontSize = fontSize - increment;
		$('body').css('font-size', fontSize + '%');
		$.cookie(CookieName, fontSize, { path: '/', expires: 365 });
		if (IE) {	curvyCorners.redraw(); }
		return false;
	});
	
	$('.larger').click(function(){
		fontSize = fontSize + increment;
		$('body').css('font-size', fontSize + '%');
		$.cookie(CookieName, fontSize, { path: '/', expires: 365 });
		if (IE) {	curvyCorners.redraw(); }
		return false;
	});
	

	/* =HOME
	----------------------------------------------------*/

		
	if ($("body.page-home").length > 0) {
		// if Testimonial is taller than Locations, make Locations the same height
		
		var cHeight = parseInt($('#content').height());
		var tHeight = parseInt($('#testimonial').height());
		if (tHeight > cHeight) { $('#content').height(tHeight); }

	}
	

});
