var count_photos = 0;
var animate = 0;
var nom_photo = 0;

var direction = 1;
var time = 4000;
var varTimeout;

var count_rotation = 4;
var time_rotation_in_gallery = 500/4;

$(document).ready(function() {
	if ($('.widget').length > 0) {
		count_photos = $('.widget .photo img').length;
		
		$('.widget .photo a.arrow_prev').click (function () {
			prev_photo();
			return false;
		});
		
		$('.widget .photo a.arrow_next').click (function () {
			next_photo();
			return false;
		});
	}
	
	if ($('.gallery').length > 0) {
		count_photos = $('.gallery .small_photo').length;
		
		$('.gallery .big_photo a.arrow_prev, .gallery .arr_prev_link').click (function () {
			count_rotation = 4;
			prev_photo_gallery();
			return false;
		});
		
		$('.gallery .big_photo a.arrow_next, .gallery .arr_next_link').click (function () {
			count_rotation = 4;
			next_photo_gallery();
			return false;
		});
	}
	
	$("a.fancybox").fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'overlayShow': false
	});
	$("a.fancybox_ajax").fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'frameWidth': 800,
		'frameHeight': 500,
		'hideOnContentClick': false,
		'overlayShow': true
	}); 
});

$(window).load(function () {
	if ($('.widget').length > 0) {
		if (direction == 1) {
			varTimeout = setTimeout('next_photo()', time);
		} else {
			varTimeout = setTimeout('prev_photo()', time);
		}
	}
});

function prev_photo () {
	if (animate == 0) {
		animate = 1;
		direction = -1;
		clearTimeout(varTimeout);
		
		$('.widget .photo img:last').clone().insertBefore('.widget .photo img:first');
		$('.widget .photo img:last').remove();
		$('.widget .photo img.visible').animate({ opacity: '0' }, 500, 'linear', function () {
			$('.widget .photo img:first').addClass('visible');
			$('.widget .photo img.visible:not(":first")').removeClass('visible').css({ opacity: '1' });
			
			varTimeout = setTimeout('prev_photo()', time);
			animate = 0;
		});
	}
}

function next_photo () {
	if (animate == 0) {
		animate = 1;
		direction = 1;
		clearTimeout(varTimeout);
		
		$('.widget .photo img.visible').animate({ opacity: '0' }, 500, 'linear', function () {
			$('.widget .photo img.visible').removeClass('visible').css({ opacity: '1' });
			$('.widget .photo img:first').clone().insertAfter('.widget .photo img:last');
			$('.widget .photo img:first').remove();
			$('.widget .photo img:first').addClass('visible');
			
			varTimeout = setTimeout('next_photo()', time);
			animate = 0;
		});
	}
}

function prev_photo_gallery () {
	if (animate == 0) {
		animate = 1;
		$('.gallery .photos_list_inner .small_photo:last').clone().insertBefore('.gallery .photos_list_inner .small_photo:first');
		$('.gallery .photos_list_inner').css({ marginLeft: '-123px' });
		$('.gallery .photos_list_inner .small_photo:last').remove();
		
		var src = $('.gallery .photos_list_inner .small_photo:first').children('a').children('img').attr('src').replace(/small/, 'big');
		
		$('.gallery .photos_list_inner').animate({ marginLeft: '0px' }, time_rotation_in_gallery, 'linear', function () {
			animate = 0;
			count_rotation--;
			if (count_rotation > 0) {
				prev_photo_gallery();
			}
		});
	}
}

function next_photo_gallery () {
	if (animate == 0) {
		animate = 1;
		$('.gallery .photos_list_inner .small_photo:first').clone().insertAfter('.gallery .photos_list_inner .small_photo:last');
		
		$('.gallery .photos_list_inner').animate({ marginLeft: '-123px' }, time_rotation_in_gallery, 'linear', function () {
			$('.gallery .photos_list_inner').css({ marginLeft: '0px' }).children('.small_photo:first').remove();
			
			animate = 0;
			count_rotation--;
			if (count_rotation > 0) {
				next_photo_gallery();
			}
		});
	}
}

function change_photo_in_gallery (obj) {
	if (animate == 0) {
		animate = 1;
		var src = obj.children('img').attr('src').replace(/small/, 'big');
		var img = document.createElement('img');
		img.onload = function () {
			$('.gallery .big_photo img.hidden').attr('src', src);
			$('.gallery .big_photo img.hidden').removeClass('hidden').css({ opacity: '1' });
			$('.gallery .big_photo img.visible').animate({ opacity: '0' }, 500, 'linear', function () {
				$('.gallery .big_photo img.visible').removeClass('visible').addClass('hidden');
				$('.gallery .big_photo img:not(".hidden")').addClass('visible').css({ opacity: '1' });
				animate = 0;
			});
		};
		$(img).attr('src', src);
	}
}

function trim (string) {
	var filter  = /^\s*$/;
	if (filter.test(string)) {
		return '';
	} else {
		return string.replace(/^\s*([^\s].*)$/, '$1').replace(/^(.*[^\s])\s*$/, "$1");
	}
}

function checkEmail (text) {
	var filter  = /^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,4}$/;
	if (!filter.test(text)) {
		return false;
	}
	return true;
}

function submitRequestForm (formObj) {
	var alertText='';
	var lang = formObj.children('[name=lang]').val();
	
	if (lang == 'en') {
		var userInput = formObj.children('[name=user]');
		var userInputText = trim(userInput.val());
		if (userInputText == '' || userInputText == 'Your Name') {
			userInput.addClass('red');
			if (alertText == '') alertText = 'Enter your name';
		} else {
			userInput.removeClass('red');
		}
		
		var emailInput = formObj.children('[name=email]');
		var emailInputText = trim(emailInput.val());
		if (emailInputText == '' || emailInputText == 'Email Address') {
			if (alertText == '') alertText = 'Enter your e-mail';
			emailInput.addClass('red');
		} else {
			if (checkEmail(emailInputText) === false) {
				if (alertText == '') alertText = 'Bad e-mail format';
				emailInput.addClass('red');
			} else {
				emailInput.removeClass('red');
			}
		}
		
		var phoneInput = formObj.children('[name=phone]');
		var phoneInputText = trim(phoneInput.val());
		if (phoneInputText == '' || phoneInputText == 'Phone Number') {
			if (alertText == '') alertText = 'Enter your phone number';
			phoneInput.addClass('red');
		} else {
			phoneInput.removeClass('red');
		}
		
		var contentTextarea = formObj.children('[name=content]');
		var contentTextareaText = trim(contentTextarea.val());
		if (contentTextareaText == '' || contentTextareaText == 'Questions / Comments') {
			if (alertText == '') alertText = 'Enter your message';
			contentTextarea.addClass('red');
		} else {
			contentTextarea.removeClass('red');
		}
	} else {
		var userInput = formObj.children('[name=user]');
		var userInputText = trim(userInput.val());
		if (userInputText == '' || userInputText == 'Votre Nom') {
			userInput.addClass('red');
			if (alertText == '') alertText = 'Enter your name';
		} else {
			userInput.removeClass('red');
		}
		
		var emailInput = formObj.children('[name=email]');
		var emailInputText = trim(emailInput.val());
		if (emailInputText == '' || emailInputText == 'Courriel') {
			if (alertText == '') alertText = 'Enter your e-mail';
			emailInput.addClass('red');
		} else {
			if (checkEmail(emailInputText) === false) {
				if (alertText == '') alertText = 'Bad e-mail format';
				emailInput.addClass('red');
			} else {
				emailInput.removeClass('red');
			}
		}
		
		var phoneInput = formObj.children('[name=phone]');
		var phoneInputText = trim(phoneInput.val());
		if (phoneInputText == '' || phoneInputText == 'Telephone') {
			if (alertText == '') alertText = 'Enter your phone number';
			phoneInput.addClass('red');
		} else {
			phoneInput.removeClass('red');
		}
		
		var contentTextarea = formObj.children('[name=content]');
		var contentTextareaText = trim(contentTextarea.val());
		if (contentTextareaText == '' || contentTextareaText == 'Commentaires') {
			if (alertText == '') alertText = 'Enter your message';
			contentTextarea.addClass('red');
		} else {
			contentTextarea.removeClass('red');
		}
	}
	
	if (alertText != '') {
		alert(alertText);
	} else {
		var params = 'lang='+lang+'&name='+userInputText+'&email='+emailInputText+'&phone='+phoneInputText+'&content='+contentTextareaText;
		$.ajax({ type: 'POST', url: '/send-request.php', data: params, success: 
			function (result) {
				if (result != 1) {
					alert(result);
				} else {
					if (lang == 'en') {
						window.location = '/en/thank-you';
					} else {
						window.location = '/fr/merci';
					}
				}
			}
		});
	}
}

function initialize() {
	var map = new google.maps.Map2(document.getElementById("map"));
	map.setCenter(new google.maps.LatLng(45.507683, -73.616206), 15);
	
	var point = new GLatLng(45.507683, -73.616206);
	var marker = new GMarker(point, {title: '200 Willowdale Unite #14'});
	map.addOverlay(marker);
}

function initialize() {
	var map = new google.maps.Map2(document.getElementById("mapb"));
	map.setCenter(new google.maps.LatLng(45.507683, -73.616206), 15);
	
	var point = new GLatLng(45.507683, -73.616206);
	var marker = new GMarker(point, {title: '200 Willowdale Unite #14'});
	map.addOverlay(marker);
}
