/*
 * Core controller
 *
 * @package PROSPEK
 * @category Controllers
 * @author Frédéric Trudeau <ftrudeau@prospek.ca>
 * @copyright Copyright (c) 2010-2011 Prospek Inc. All rights reserved.
 */



    var Controller = (function() {

        // -- CONTROLLER DISPATCH
        return {

            // Called method
            method: null,

            // Current language
            language: 'fr',

            /**
            * Init method
            *
            * @author Frédéric Trudeau <ftrudeau@prospek.ca>
            * @return called method
            */
            init: function ()
            {
				this.contactForm();
                if (typeof o !== "undefined") {
                    this.method = o.method;
                    $(".extlink").attr("target", "_blank");
                    if (typeof this[o.method] === 'function') {
                	    return this[o.method]();
				    }
                }
            },

            home: function ()
            {

				var p = 1;
                var max_p = $(".block_slide").length;
                var playslideshow = true;
                var time = 4000;
                var timerAuto;
                var focusLost = false;


                // Start the animation if there is more than one slide
                if(max_p > 1)
                {
                    showNextSlide();
                }

                $(window).focus(function() {
					if(focusLost == true)
	                {
	                    showNextSlide();
	                	focusLost = false;
	                }
				});

				$(window).blur(function() {
					focusLost = true;
					clearTimeout(animTimer);
				});


                //Lorsque l'on clique sur un bouton de la navigation
                $(".block_slideshow ul li a").click (  function () {

                	// To ensure we dont click on the same slide twice
                	if(p != $(this).html()){

	                    clearTimeout(timerAuto);

	                    //Change current page value
	                    p = $(this).html();

	                    //Animate the slide
	                    animateSlide();

	                    //Si un bouton à été cliqué on attend puis on repart la fonction automatique
	                    playslideshow = false;

	                    timerAuto = setTimeout (  function () {
	                            playslideshow = true;
	                            showNextSlide();
	                        },
	                        time
	                    )
					}
                } );

                //Fonction qui passe à la prochaine slide automatiquement
                function showNextSlide () {

                    animTimer = setTimeout (
                        function () {
                            if(playslideshow == true) {
                                p++;

                                if(p > max_p) {
                                    p = 1;
                                }

                                //Animate the slide
                                animateSlide();
                                showNextSlide();
                            }

                        }, time
                    );
                }

                //Fonction qui anime le slideshow et donne au bouton courant une classe current
                function animateSlide () {
                    $(".block_slideshow ul li a").removeClass("current");
                    $(".block_slideshow ul li a#b" + p).addClass("current");

                    $(".block_slideshow div").fadeOut(500);
                    $(".block_slideshow div#s" + p).fadeIn(500);
                }
			},

			contactForm: function ()
			{
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

				$('form#contactform .submit').click ( function (){

					$('form#contactform input').focus ( function () {
						$('.block_form_error').fadeOut();
					});


					if(!$('input[name="form_name"]').val())
					{
						$('.block_form_error.empty').fadeIn();
						return false;
					}

					if(!$('input[name="form_phone"]').val())
					{
						$('.block_form_error.empty').fadeIn();
						return false;
					}

					if(!$('input[name="form_email"]').val())
					{
						$('.block_form_error.empty').fadeIn();
						return false;
					}

					if(reg.test($('input[name="form_email"]').val()) == false)
					{
						$('.block_form_error.email').fadeIn();
						return false;
					}

					// Show loader and submit form
					$('form#contactform').submit();

				});
			}
        }
    });

    var controller = new Controller();
    controller.init();

