var var_static = new Array();
var_static['debug_enable'] = 1;
var_static['animation_time'] = 2000;

var var_dynamic = new Array();
var_dynamic['default_values'] = new Array();
var_dynamic['error'] = 0;
var_dynamic['slideshow_enable'] = 1;

function debug(data) {
    if(var_static['debug_enable'] == 1) {
        if(window.console && console.debug) {
            console.debug(data);
        } else if(window.console && console.log) {
            console.log(data);
        }
    }
}
function error_show(message, target) {
    $('#bigbox5-error').html(message);
    var_dynamic['error'] = 1;
}
function error_hide() {
    $('#bigbox5-error').html('');
    var_dynamic['error'] = 0;
}
function tracking(_trackPageview) {
    debug('tracking ' + _trackPageview);
}
function form_missing(form) {
    $('label, .label').removeClass('error');
    missing = 0;
    $(form).find('.required').each(function(index) {
        id = $(this).attr('id');
        name = $(this).attr('name');
        type = $(this).attr('type');
        value = $(this).val();
        if(type == 'radio') {
            if($('input[name='+name+']:checked').length != 0) {
            } else {
                $(this).parent().siblings('.label').addClass('error');
                missing = 1;
            }
        } else if(type == 'checkbox') {
            if($(this).is(':checked')) {
            } else {
                $('label[for=' + id + ']').addClass('error');
                missing = 1;
            }
        } else {
            if(value == '' || value == var_dynamic['default_values'][name]) {
                $('label[for=' + id + ']').addClass('error');
                missing = 1;
            }
        }
    });
    if(missing) {
        error_show('Merci de remplir tous les champs');
    }
    return missing;
}
function form_values(form) {
    var params = {};
    $(form).find('input[type=text], input[type=hidden], input[type=password], input[type=image], input[type=submit], textarea, select').each(function() {
        name = $(this).attr('name');
        value = $(this).val();
        params[name] = value;
    });
    $(form).find('input[type=checkbox]').each(function() {
        name = $(this).attr('name');
        if($(this).is(':checked')) {
            params[name] = 1;
        }
    });
    $(form).find('input[type=radio]').each(function() {
        name = $(this).attr('name');
        value = $(this).val();
        if($('input[name='+name+']:checked').length != 0) {
            params[name] = $('input[name='+name+']:checked').val();
        }
    });
    return params;
}
function click_nav(href) {
    //debug(href + ' / ' + href.substring(1));
    //$.history.load(href.substring(1));
    $(window).scrollTo(href, 1200);
}

//masque
function mask_close() {
    $('#box-mask').fadeOut();
}
function mask_resize() {
    width = $(window).width();
    height = $(document).height();
    $('#box-mask').css({'width': width});
    $('#box-mask').css({'height': height});

    width_popin = $('#box-popin').width();
    margin_left = (width-width_popin)/2;
    $('#box-popin').css({'margin-left': margin_left});
}
function mask_open() {
    var ua = $.browser;
    if(ua.msie && ua.version.slice(0, 1) <= 7) {
    } else {
        mask_resize();
        $('#box-mask').css({'opacity': 0.7});
        $('#box-mask').fadeIn();
    }
}

function popin_click(href) {
    mask_open();
    $.ajax({
        async: false,
        cache: false,
        dataType: 'html',
        success: function(html) {
			//$(window).scrollTo('#bigboxheader', 800);
			$('#display-popin').html(html);
			$('#box-popin').fadeIn('slow');
			$('#box-popin').css("top", $(document).scrollTop()+230);
        },
        type: 'POST',
        url: href
    });
}
function popin_close() {
    mask_close();
    $('#box-popin').fadeOut('slow', function() {
        $('#display-popin').html('');
    });
}

function animate_slideshow(){
  //gestion du contenu
  var $active = $('#bigbox0-diaporama .actif');
  var $next = ($('#bigbox0-diaporama .actif').next().length > 0) ? $('#bigbox0-diaporama .actif').next() : $('#bigbox0-diaporama div:first');
  //gestion de la nav
  var $activeli = $('#bigbox0-paging .active');
  var $nextli = ($('#bigbox0-paging .active').next().length > 0) ? $('#bigbox0-paging .active').next() : $('#bigbox0-paging li:first');
  
  $active.fadeOut(function(){
    $active.removeClass('actif');
    $next.fadeIn().addClass('actif');
	
	$activeli.removeClass('active');
	$nextli.addClass('active');
  });
}

$(document).ready(function() {
    /*$.history.init(function(hash){
        debug('history hash:' + hash);
        if(hash == "") {
        } else {
            click_nav('#' + hash);
        }
    },
    { unescape: ",/" });*/

    //mask
    $(window).resize(function() {
        mask_resize();
    });
    $('#box-mask').click(function(e) {
        e.preventDefault();
        popin_close();
    });

    $('#player-interview').click(function(e) {
        e.preventDefault();
		var_dynamic['slideshow_enable'] = 0;
        interval = clearInterval(interval);
    });

    //popin
    $(document).keydown(function(e) {
        if(e == null) { // ie
            keycode = event.keyCode;
        } else { // mozilla
            keycode = e.which;
        }
        if(keycode == 27) {
            popin_close();
        }
    });
    $('.popin_close').live('click', function(e) {
        e.preventDefault();
        popin_close();
    });
    $('.popin').live('click', function(e) {
        e.preventDefault();
        href = $(this).attr('href');
        popin_click(href);
    });

    $('#bigbox0-paging a').click(function(e) {
        e.preventDefault();
        href = $(this).attr('href');

		if(var_dynamic['slideshow_enable'] == 0) {
			interval = setInterval('animate_slideshow()', var_static['animation_time']);
		}

        $('#bigbox0-paging li').removeClass('active');
        $(this).parent().addClass('active');

        $('.diaporama').hide().removeClass('actif');
        $(href).fadeIn().addClass('actif');
    });

	var interval = setInterval('animate_slideshow()', var_static['animation_time']);
	$('#bigbox0').hover(function() {
		interval = clearInterval(interval);
	}, function() {
		if(var_dynamic['slideshow_enable'] == 1) {
			interval = setInterval('animate_slideshow()', var_static['animation_time']);
		}
	});
	
    $('#bigboxheader li a, .nav a').click(function(e) {
        e.preventDefault();
        href = $(this).attr('href');
        click_nav(href);
    });

    $('.nav li').mouseenter(function(e) {
        if($(this).hasClass('active')) {
        } else {
            $(this).addClass('over');
            $(this).find('a').show();
        }
    });

    $('.nav li').mouseleave(function(e) {
        if($(this).hasClass('active')) {
        } else {
            $(this).removeClass('over');
            $(this).find('a').hide();
        }
    });

    $('.volets-nav li a').click(function(e) {
        e.preventDefault();
        href = $(this).attr('href');
        rel = $(this).attr('rel');

        $('#' + rel).find('.volets-nav li').removeClass('active');
        $(this).parent().addClass('active');

        $('#' + rel).find('.volet').hide();
        $(href).show();
    });

    $('#bigbox5-form .inputtext, #bigbox5-form .textarea').each(function(index) {
        name = $(this).attr('name');
        value = $(this).attr('value');
        var_dynamic['default_values'][name] = value;
    });
    $('#bigbox5-form .inputtext, #bigbox5-form .textarea').focus(function(e) {
        name = $(this).attr('name');
        value = $(this).attr('value');
        if(value == var_dynamic['default_values'][name]) {
            $(this).attr('value', '');
        }
    });
    $('#bigbox5-form .inputtext, #bigbox5-form .textarea').blur(function(e) {
        name = $(this).attr('name');
        value = $(this).attr('value');
        if(value == '') {
            $(this).attr('value', var_dynamic['default_values'][name]);
        }
    });

    $('#bigbox5-form form').submit(function(e) {
        error_hide();

        missing = form_missing($(this));
        error = 0;

        var new_string = new String($('#contact-email').val());
        if(!new_string.match('^[-_\.0-9a-zA-Z]{1,}@[-_\.0-9a-zA-Z]{1,}[\.][0-9a-zA-Z]{2,}$') && $('#contact-email').val() != '' && $('#contact-email').val() != var_dynamic['default_values']['email']) {
            error = 1;
            error_show('Merci de vérifier votre email');
        }

        if(missing == 0 && error == 0) {
            var params = form_values($(this));
            $.ajax({
                cache: false,
                data: params,
                dataType: 'html',
                success: function(html) {
					switch(html) {
						case 'captcha error': $('#bigbox5-error').html(html); break;
						default : popin_click('merci.php'); break;
					}
                },
                type: 'POST',
                url: $(this).attr('action')
            });
			// popin_click('merci.php');
        }
        e.preventDefault();
    });
	
	
	$('#news-tab').bind("click", function(e){
		if($('#box-news').css("right")=="-182px"){
			$('#box-news').fadeIn(100, function(e){
				$('#box-news').animate({right:"0px"}, 200);
				$('#news-tab').animate({right:"182px"}, 200);
				$('#news-tab-off').fadeOut(200);
			});
			
		}
		if($('#box-news').css("right")=="0px"){
			$('#box-news').animate({right:"-182px"}, 200);
			$('#news-tab').animate({right:"0px"}, 200);
			$('#news-tab-off').fadeIn(200);
			$('#box-news').fadeOut(100);
		}
		
	});
	
	

});
