$(window).load(function()
{
	/*
	
	Home page Slideshow
	
	*/
	if($('#slideshow').length != 0)
	{
		// the left bar
		var leftBorderWidth = ($(window).width() / 2) - (930 / 2) + 10;
		
		// 
		var topP = $('#slideshow').offset().top + 71;
	
		$('#big-border').css({width: leftBorderWidth, top : topP });
	
		$("#rgCarousel ul").rgCarousel(
		{
			  easing : 'easeOutQuad'
			, scroll : 2		  
			, useLightBox : false
			
		});
	}
	
	
	/*
		
	TIPS AND TRICKS
		
	*/
	$('.tt_link').each(function()
	{
		$(this).click(function(e)
		{		
			$("html, body").animate({scrollTop: $($(this).attr('href')).position().top}, 2000);	
		})
	})
	
	$('.gotop').click(function(e)
	{
		e.preventDefault();
		$("html, body").animate({scrollTop: 0}, 500);	
	})
	
	/*
	
	Adjust background position
	
	*/
	$(window).smartresize(function(){  
		onResize();
	});
	
	// Everything you want to do on browser resize - You put it there
	function onResize(){
		// 3000 = background image width
		$('body').css({'background-position' : -(3000 - $(document).width()) + 'px -70px'});	
		
		if($('#slideshow').length != 0)
		{
			var leftBorderWidth = ($(window).width() / 2) - (930 / 2) + 10;		
			var topP = $('#slideshow').offset().top + 71;
			$('#big-border').css({width: leftBorderWidth, top : topP });
		}
	}
	
	onResize();

	// classes for round div

	var corner8 = {tl:{ radius:8},tr: { radius:8},bl: { radius:8},br: { radius:8}};
	var corner6 = {tl:{ radius:6},tr: { radius:6},bl: { radius:6},br: { radius:6}};
	var corner4 = {tl:{ radius:4},tr: { radius:4},bl: { radius:4},br: { radius:4}};
	var corner2 = {tl:{ radius:2},tr: { radius:2},bl: { radius:2},br: { radius:2}};

	var cornerR8 = {tl:{ radius:0},tr: { radius:8},bl: { radius:0},br: { radius:8}};
	var cornerR6 = {tl:{ radius:0},tr: { radius:6},bl: { radius:0},br: { radius:6}};
	var cornerR4 = {tl:{ radius:0},tr: { radius:4},bl: { radius:0},br: { radius:4}};
	var cornerR2 = {tl:{ radius:0},tr: { radius:2},bl: { radius:0},br: { radius:2}};

	var cornerL8 = {tl:{ radius:8},tr: { radius:0},bl: { radius:8},br: { radius:0}};
	var cornerL6 = {tl:{ radius:6},tr: { radius:0},bl: { radius:6},br: { radius:0}};
	var cornerL4 = {tl:{ radius:4},tr: { radius:0},bl: { radius:4},br: { radius:0}};
	var cornerL2 = {tl:{ radius:2},tr: { radius:0},bl: { radius:2},br: { radius:0}};

	
	$('.corner8').corner(corner8);
	$('.corner6').corner(corner6);
	$('.corner4').corner(corner4);
	$('.corner2').corner(corner2);
	
	$('.cornerRight2').corner(cornerR2);
	$('.cornerRight4').corner(cornerR4);
	$('.cornerRight6').corner(cornerR6);
	$('.cornerRight8').corner(cornerR8);
	
	$('.cornerLeft4').corner(cornerL4);
	$('.cornerLeft6').corner(cornerL6);
	$('.cornerLeft8').corner(cornerL8);

});

$(document).ready(
	
	function()
	{
		// Home button transparent
		$('#homeButton').css('opacity',0);
		
		var w = $(window);
		// we hide the home page caroussel so it doesn't look bad when the page load
		// $("#rgCarousel ul").css('opacity',0);
		
		$('.entries_block:even').css({'background-color': '#f7f7f7'})
		// Instanciate the form validator
		$('form').validationEngine();		
		
		
		// The lightbox over the realisation images
		$("a.fancy").fancybox({
			'overlayShow'	: true,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic'
		});
	
		// index of the slideshow pictures
		var _currentPic = 0;
		
		// hide the slideshow texts
		$("#slideshow_slogan p").hide();
		
		$('.color-changer a').click(function(e)
		{
			e.preventDefault();				
			$('.color-changer-button:visible').fadeOut();
			$('.color-changer-button:hidden').fadeIn();			
		})
		
		// set colors values
		var cook = readCookie('ps_color');	
		var color_name = readCookie('ps_color_name');		
		
		// if the cookie is null we set color theme cookie
		if(!cook){
			createCookie('ps_color', '#a01523');
			createCookie('ps_color_name', 'red');
		}
		
		// variables
		var currentColor 		= readCookie('ps_color');
		var currentColorName	= readCookie('ps_color_name');
		
		$('#slideshow-imgs').find('img').each(function(){
			$('#slideshow-stars').append('<a class="imgCnt-normal"><img src="' + site_url + '/public/images/star-png.png" /></a>');
		});		
		
		function changeSlideShow(idx)
		{
			$('.imgCnt-normal').removeClass('imgCnt-active');
			$('.imgCnt-normal').removeAttr('style');			
			
			_currentPic = idx;
			
			if(_currentPic > $('#slideshow-imgs').find('img').size() - 1 ) _currentPic = 0;
				
			$('#slideshow-imgs').find('img:visible').stop(true,true).fadeOut(400);
			$("#slideshow_slogan").find("p:visible").stop(true,true).fadeOut(400);
				
			$('#slideshow-imgs').find('img').eq(_currentPic).stop(true,true).fadeIn(400,function(){
				$("#slideshow_slogan").find("p").eq(_currentPic-1).stop(true, true).fadeIn(1500);	
			});				
			
			$('.imgCnt-normal').eq(_currentPic).addClass('imgCnt-active');
			$('.imgCnt-normal').eq(_currentPic).css('background-color', currentColor);
					
			_currentPic++;
			
			$.doTimeout('slide_timer', 7000, function(){changeSlideShow(_currentPic)});
		}
		
		$('.imgCnt-normal').each(function(){
			$(this).click(function()
			{	
				$.doTimeout('slide_timer');
				changeSlideShow($(this).index());
			})
		})
		
		// when the user click the change color circles
		$(".color-changer-button li a").click(function(e)
		{
			e.preventDefault();
			
			// the color of the the parent <li ie.: <li class="pink"><a href=""><img src...
			var color = $(this).parent().attr('class');			
			
			setATagsHoverColor(color);

			// look for all elelment with the class 'changeable_c'
			$('.changeable_c').each(function()
			{
				// split the classes names
				var classes = $(this).attr('class').split(/\s+/);
				
				// we are looking for any class that starts with changeable_color_*
				var pattern = /^changeable_color_\w+\b/;
				
				for(var i = 0; i < classes.length; i++){
					var className = classes[i];
					// if we find a match
					if(className.match(pattern)){
						// we delete the current color class
						$(this).removeClass(className);						
					}
				}
			})
			
			$('.changeable_b').each(function()
			{
				var classes = $(this).attr('class').split(/\s+/);				
				var pattern = /^changeable_bg_\w+\b/;
				for(var i = 0; i < classes.length; i++){
					var className = classes[i];
					if(className.match(pattern)) $(this).removeClass(className);
				}
			})
			
			// we add the new color classes
			$('.changeable_c').addClass('changeable_color_' + color);
			$('.changeable_b').addClass('changeable_bg_' + color);
			
			currentColor = $('.changeable_b').css('background-color');
			
			$('.imgCnt-normal').removeAttr('style');
			$('.imgCnt-normal').eq(_currentPic-1).css('background-color', currentColor);
			

			// and we store the cookie for the user's next visit
			eraseCookie('ps_color');
			eraseCookie('ps_color_name');
			createCookie('ps_color', currentColor);
			createCookie('ps_color_name', color);
			
			
		})
		
		$('.changeable_b').css('background-color', currentColor);
		$('.changeable_c').css('color', currentColor);		
			
		changeSlideShow(_currentPic);
		
		function setATagsHoverColor(color)
		{
			// add the color class too all anchor tags
			
			var exclude = '.color-changer a, #service_navigation ul li a'
			
			$('a').not(exclude).hover(
				function(){
					$(this).addClass('changeable_color_' + color)
				}
				,function(){
					$(this).not('.active').removeClass('changeable_color_' + color)
				}
			);
			$('.active').addClass('changeable_color_' + color);
		}
		
		setATagsHoverColor(currentColorName);
		
		/*
		
		SECTION JOBS
		
		*/
		
		$('.other_P').hide();
		$('.other_R').hide();
		$('.div-region').hide();
		
		$('.province').change(function()
		{
			var idx = $(this).find("option:selected").index() - 1;
			var val = $(this).find("option:selected").val();
			
			if(idx >= 0)
			{
				var other = language == "fr" ? 'Autre : Spécifiez' : 'Other : Specify';
				var obj = [
							  ['Montréal','Laval','Québec','Gatineau','Laurentides','Estrie','Lanaudière','Mauricie','Montérégie','Saguenay-Lac-St-Jean', other]
							, ['Ottawa','Kanata','Toronto (GTA)', other]
							, ['Edmonton','Calgary', other]
							, ['Saskatoon','Regina', other]
							, ['Winnipeg', other]
							, ['Vancouver', other]
				];
			
			
				if(val == 'other')
				{
					// the province select box is not required anymore
					$('.province').removeClass('validate[required]');
					// the input other province is now required
					$('.other_province').addClass('validate[required]');							
					// we show the province container div
					$('.other_P').show();
					
					// we show the other region text input
					$('.other_region').addClass('validate[required]');
					// we show the region container div
					$('.other_R').show();
					
					// we remove the required class of the region select menu
					$('.region').removeClass('validate[required]');
					// we hide the region container div
					$('.div-region').hide();
				}
				else
				{
					// remove required class from the province select menu
					$('.province').removeClass('validate[required]');
					// we hide the province container div
					$('.other_P:visible').hide();
					
					// remove required class from the region select menu
					$('.other_region').removeClass('validate[required]');
					// we hide the region container div
					$('.other_R:visible').hide();
					
					// we show the other_region text input, add required class and empty the options
					$('.div-region:hidden').fadeIn();
					$('.region').addClass('validate[required]')
								.find('option')
								.empty()
								.remove();
			
					for(var x = 0; x < obj[idx].length; x++)
					{
						var last = x == obj[idx].length - 1 ? ' value="other"' : '';
						$(".region").append('<option' + last + '>' + obj[idx][x] + '</option>');
					}
					
					$('.region').change(function()
					{
						var val = $(this).find("option:selected").val();
						
						if(val == 'other')
						{
							$('.region').removeClass('validate[required]')
										.find('option')
										.empty()
										.remove();
							$('.div-region').hide();
							$('.other_region').addClass('validate[required]');
							$('.other_R').show();
						}
					})
				}
			}
			else
			{
				$('#div-region:visible').hide();
			}
		})
		

		
		/*
		
		JOBS

		*/
		var opt_sales = $('.opt_sales').clone();
		var opt_promo = $('.opt_promo').clone();

		$('.opt_sales').empty().remove();
		$('.opt_promo').empty().remove();
		
		
		$('#position').append(opt_promo);

		var emptyOPT = $('<option>- ' + (language == "fr" ? "Choisir" : "Choose") + ' - </option>');

		$("#cbPromo").click(function()
		{
			$('#position').find('option').remove()
			$('#position').append(opt_promo).prepend(emptyOPT);
			$('#position').find('option').eq(0).attr('selected', 'selected');
		});
		$("#cbSales").click(function()
		{
			$('#position').find('option').remove();
			$('#position').append(opt_sales).prepend(emptyOPT);
			$('#position').find('option').eq(0).attr('selected', 'selected');
		});
	}	
)

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/*
// debug
$("#screen").hide();

var screenbutton = $('<button id="screen_button">PSD</button>');
$('body').append($('#screen_button');
$('#screen_button')
.css({
	'position':'fixed',
	'top':10,
	'left':10,
	'width':50,
	'z-index' : 999999
})
.click(function(){
	$("#screen").css({
		position :'absolute',
		top:0,
		left: ($(window).width() - $("#screen").width()) / 2,
		opacity : 0.5,
		zIndex	: 999998
	}
)
$("#screen").toggle();
})
		*/
