﻿var globals = [];
var previous_location_hash = null;

$(document).ready(function() {
    initGlobals();

    if (isHomePage())
        homepageInit();

    if (isClientsPage())
        clientsInit();

    if (isContactPage())
        contactInit();

    if (isFAQPage() || isReglesPage())
        rulesLayoutInit();
});

// PROTOTYPE 

function initGlobals(){
    globals['faq_previous_link'] = null;
    globals['regles_previous_link'] = null;
}

function isUndefined(obj){
    return obj == null || obj == 'undefined';
}

// ------- HOMEPAGE ------- //

function isHomePage() {
    return $('.Accueil').length > 0;
}

function homepageInit() {
    $("div#Full").scrollable({
        vertical: true,
        clickable: false, 
        size: 1
    }).mousewheel().circular().navigator({
        navi: "#Teasers",
        naviItem: 'dl',
        activeClass: 'on'
    }).autoscroll({
        step: 1,
        interval: 5000,
        autopause: 1
    });
};

// ------- FAQ ------- //

function isFAQPage() {
    return $('.faq').length > 0;
}

function displayTheme(tId, link) {    
    if (!isUndefined(globals['faq_previous_link'])) {
        $(globals['faq_previous_link']).removeAttr('class');
    } else {
        $('.questions .teasers ul li a').removeAttr('class');
    }
    $(link).attr('class', 'on');
    globals['faq_previous_link'] = link;
    $('.questionReponse').fadeOut();
    $('#' + tId).fadeIn();    
}

// ------- REGLES ------- //

function rulesLayoutInit() {
    var maxheight = 0;
    $('.fullslist .full').each(function() {        
        if ($(this).height() > maxheight)
            maxheight = $(this).height();
    });
    $('.fullslist').height(maxheight);
    setInterval(function() {
        if (self.location.hash.length > 0) {
            var item = $(self.location.hash);
            if (item.length > 0 && !item.hasClass('on')) {
                item.click();
            }
        }    
    }, 350);
}

function isReglesPage() {
    return $('.questions:not(.faq)').length > 0;
}

function displayRule(rId, link) {    
    if (!isUndefined(globals['regles_previous_link'])) {
        $(globals['regles_previous_link']).removeAttr('class');
    } else {
        $('.questions .teasers ul li a').removeAttr('class');
    }
    $(link).attr('class', 'on');
    globals['regles_previous_link'] = link;
    $('.rule').fadeOut();
    $('#' + rId).fadeIn();
    return false;
}

// ------- CLIENTS ------ //

function isClientsPage() {
    return $('.clients').length > 0 && $('.scrollable').length > 0;
}
function clientsInit() {
    $("div.scrollable").scrollable().mousewheel();
    $('.items li a').each(function(pos) {
        $(this).click(function() {
            $('.items li:not(:eq(' + pos + ')) img').fadeTo('medium', 0.5).removeClass('on');
            $('.full').hide().eq(pos).fadeIn();
            $(this).children('img').fadeTo('fast', 1).addClass('on');
            self.location.hash = $(this).attr('href');
            previous_location_hash = self.location.hash;
            return false;
        });
    });    
    if (self.location.hash.length > 0) {
        CheckLocationChanged();
    } else {
        $('li img:first').click();
    }
    setInterval(function() { CheckLocationChanged(); }, 100);
};
// ------ CONTACT ------ ///
function isContactPage() {
    return $('.contacts').length > 0;
}
function contactInit() {
    if ($('#ContactFormError').length > 0) {

    }    
    $('#dixxit').change(function() {        
        if (this.selectedIndex == 0) {
            $('#more_dixxit').text('');
            $('#dixxit_more_dixxit').hide();
        } else {
            $('#dixxit_more_dixxit').show();
        }
    });
    $('#notes').focus(function() {
        if ($(this).text() == 'Décrivez-le ici') $(this).text('');
    }).blur(function() {
        if ($(this).text() == '') $(this).text('Décrivez-le ici');
    });    
    var triggers = $(".affiche").overlay({
        expose: '#fff'
    });
    if (self.location.hash.indexOf('#form') > -1) {
        $('.affiche:eq(0)').click();
    }
    var CheckIsValidField = function(fieldobj) {        
        var field = $(fieldobj);
        var val = field.val();
        fieldobj.onkeyup = null;
        if (val.trim() == '' || (field.hasClass('email-validate') && !isValidEmail(val))) {
            field.addClass('bad');
            fieldobj.onkeyup = function() {
                CheckIsValidField(this);
            };
            return false;
        } else {
            field.removeClass('bad');
            return true;
        }
    };
    $("#prompt form").submit(function(e) {
        var missing = new Array();
        var fields = $('#NeededFields').val().split(',');
        for (var i = 0; i < fields.length; i++) {
            var f = document.getElementById(fields[i]);
            if (!CheckIsValidField(f)) {
                missing.push($(f).attr('name'));
            }
        }
        var isvalid = missing.length === 0;
        if (!isvalid) {
            if (missing.length == 1) {
                msg = 'Le champ ' + missing[0] + ' n\'a pas été correctement renseigné.';
            } else {
                msg = 'Certains champs n\'ont pas été correctement renseignés';
            }
            $('#formMsg').html(msg).show();
        } else {
            $('#formMsg').hide();
        }
        if (isvalid) {
            triggers.eq(0).overlay().close();
        }
        return missing.length === 0;
    });
};

function isValidEmail(argvalue){
  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("@") == -1)
    return false;
  else if (argvalue.indexOf("@") == 0)
    return false;
  else if (argvalue.indexOf("@") == (argvalue.length-1))
    return false;

  var arrayString = argvalue.split('@');

  if (arrayString[1].indexOf(".") == -1)
    return false;
  else if (arrayString[1].indexOf(".") == 0)
    return false;
  else if (arrayString[1].charAt(arrayString[1].length-1) == ".") {
    return false;
  }

  return true;
}

function CheckLocationChanged() {
    if (self.location.hash != previous_location_hash) {
        previous_location_hash = self.location.hash;
        var item = $(self.location.hash);
        if (item.length > 0) item.find('img:first').click();
        else {
            $('li img:first').click();
            self.location.hash = '';
        }
    }
}