$(document).ready(function(){
    
    $.ajaxSetup({cache:false});
    
    $('#logo').hide();
    
    var pagesmap = {
                        'photos'    :   'http://www.longirock.ch/news/lieu/index.php?nd #content',
                        'artistes'  :   'http://www.longirock.ch/artistes/?nd #content',
                        'accueil'   :   './',
                        'pressbook2k10' :   'http://www.longirock.ch/pressbook/?nd #content',
                        'pressbook' :   'pages/pressbook.html #content',
                        '2010'      :   'pages/edition2010.html #content',
                        '2012'      :   'http://www.longirock.ch/informations/?nd&douze #content',
                        'contact'   :   'pages/contact.html #content',
                        'forum'     :   'pages/forum.html #content',
                        'video'     :   'http://www.longirock.ch/video/?nd #content',
                        'audio'     :   'http://www.longirock.ch/podcast/?nd #content',
                        'liens'     :   'http://www.longirock.ch/liens/?nd #content',
//                         'news'      :   'pages/news.html #content'
                        'news'      :   'http://www.longirock.ch/informations/?nd&douze #content',
                        'partenaires2010' : 'http://www.longirock.ch/partenaires/?nd #content'
    };
    
    var pagescontent = {}; // NOT USED
    
    $(window).bind('hashchange', function(e){
//         alert('hashch');
        $('#menus li').removeClass('sel');
        var theli = ($('#menus li[nav="' + e.fragment + '"]').parents('li').length) ? ($('#menus li[nav="' + e.fragment + '"]').parents('li')) : ($('#menus li[nav="' + e.fragment + '"]'));
        theli.addClass('sel');
        
        if (!e.fragment || (typeof pagesmap[e.fragment] == 'undefined' && typeof pagescontent[e.fragment] == 'undefined')) {
            if (e.fragment && $('#'+e.fragment).length == 1) {
                $('.tohide').fadeOut(600, function(){
                    $('#contentwrapper').html('');
                    if ($('.tohide:visible').length == 0) {
                        // to make sure we don't fadeIn too early
                        $('#'+e.fragment).fadeIn();
                    }
                });
                return;
            }
            
//             alert('page does not exist: '+e.fragment);
            $('.tohide').fadeOut(600, function(){
                if ($('.tohide:visible').length == 0) {
                    $('#logo, #accueilnews').fadeIn();
                    $('#contentwrapper').html('');
                }
            });
            return;
        }
        
        var contenttoload = pagescontent[e.fragment];
        if (typeof contenttoload !== 'undefined') {
//             alert('local content'); // NOT USED
            $('.tohide').fadeOut(600, function(){
                $('#loading').fadeIn(600, function(){
                    $('#loading').fadeOut(600, function(){
                        $('#contentwrapper').html(pagescontent[e.fragment]).fadeIn();
                    });
                });
            });
            return;
        }
        
        var pagetoload = pagesmap[e.fragment];
        if (pagetoload.indexOf('#') == -1) {
//             alert('location.href');
            $('.tohide').fadeOut(600, function(){
                if ($('.tohide:visible').length == 0) {
                    $('#loading').fadeIn(600, function(){
                        document.location.href = pagetoload;
                    });
                }
            });
        }
        
        else {
//             alert('loading content');
            $('.tohide').fadeOut(600, function(){
                if ($('.tohide:visible').length == 0) {
                    $('#main').addClass('fixedsize');
                    $('#loading').fadeIn(600, function(){
                        $('#contentwrapper').load(pagetoload, function(response, status, xhr){
                            $('#loading').fadeOut(600, function(){
                                if (status == 'error') {
                                    $('#error').fadeIn(600);
                                }
                                else {
                                    redo_js();
                                    $('#main').removeClass('fixedsize');
                                    $('#contentwrapper').fadeIn(600);
                                }
                            });
                        });
                    });
                }
            });
        }
    });
    
    var austDay = new Date(Date.UTC(2010, 11, 15, 11));
    $.countdown.setDefaults($.countdown.regional['fr']);
    $('#countdown').countdown({until: austDay});
    
    $('h1').fadeOut(5000, function(){
        $('#countdown, input, h2.todisplay').fadeIn(2000, function(){
            window.setTimeout('h2invite_change()', 5000);
        });
    });
    
    $('.thanks button').click(function(){
        $('.thanks').fadeOut();
    });
    
    $('#menus ul li').hover(function(){
        $(this).find('ul').show();
    }, function(){
        $(this).find('ul').hide();
    });
    
    $('#menus li').click(function(event){
        var menu_el = $(this);
        $('#menus ul li ul').hide();
        document.location.href = '#' + menu_el.attr('nav');
        return false;
    });
    
    
    
    
    
    
    
    

    redo_js = function() {
        $('h2').click(function(){
            $('#photo' + $(this).attr('id')).slideToggle();
        });
        
        $('div.divphotoslieu a').fancybox({
            'width' : '90%',
            'height' : '90%',
            'autoScale' : true,
            'overlayShow' : false,
            'zoomSpeedIn' : 500,
            'zoomSpeedOut' : 500,
            'transitionIn' : 'fade',
            'transitionOut' : 'fade'
    	});    
        $('a.fancybox').fancybox({
            'zoomOpacity'			: true,
            'overlayShow'			: false,
            'zoomSpeedIn'			: 500,
            'zoomSpeedOut'			: 500,
            'transitionIn'          : 'elastic',
            'transitionOut'         : 'elastic',
            'hideOnContentClick'    : true
        });
        $('a.youtube').fancybox({
            'width' : 680,
            'height' : 405,
            'autoScale' : false,
            'overlayShow' : false,
            'zoomSpeedIn' : 500,
            'zoomSpeedOut' : 500,
            'transitionIn' : 'elastic',
            'transitionOut' : 'elastic',
            'type' : 'iframe'
        });
        $('a.youtubebeta').click(function(){
            $.fancybox({
                'padding'           : 0,
                'autoScale'         : true,
                'overlayShow'       : false,
                'zoomSpeedIn'       : 500,
                'zoomSpeedOut'      : 500,
                'transitionIn'      : 'fade',
                'transitionOut'     : 'fade',
                'href'              : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
                'type'              : 'swf',
                'swf'               : {
                    'wmode'             : 'transparent',
                    'allowfullscreen'   : 'true'
                }
            });
            return false;
        });
        $('div.artiste div').hide();
        $('div.artiste h2').click(function(){
            $('div.artiste div').addClass('tohandle');
            $(this).parent().children('div').removeClass('tohandle');
            $('div.artiste div.tohandle').slideUp();
            $(this).parent().children('div').slideToggle();
            $('div.artiste div').removeClass('tohandle');
        });
        $('.newz.clickable').click(function(){
            $(this).children('div.visible').slideDown();
            $(this).children('div.invisible').slideUp();
            $(this).removeClass('clickable');
        });
        $('form').submit(function(){
            if ($(this).find('select').val() == '' || $(this).find('input[name=nom]').val() == '' || $(this).find('textarea').val() == '') {
                alert('Veuillez remplir tous les champs.');
                return false;
            }
            return isValidEmail($(this).find('input[name="email"]').val());
        });
        $('#input_email').attr('value', 'adresse e-mail').css('color', '#666').focus(function(){
            if (this.value == 'adresse e-mail') {
                $(this).css({'color': '#000', 'font-weight': 'bold'}).attr('value', '');
            }
        });
    };
    
    
    
    
    $(window).trigger('hashchange');
    
});

h2invite_change = function() {
    if ($('h2').length == $('h2.todisplay').length) {
        $('h2').removeClass('todisplay');
    }
    var next = $('h2').not('.todisplay').eq(0);
    $('h2:visible').fadeOut(600, function(){
        next.addClass('todisplay').fadeIn();
    });
    window.setTimeout('h2invite_change()', 5000);
}
    
isValidEmail = function(str) {
	pos_at = str.indexOf("@");
	pos_dot = str.indexOf(".", pos_at);
	valid = true;
	if (str.indexOf('"') > -1) valid = false;
	if (str.indexOf("'") > -1) valid = false;
	if (str.indexOf("<") > -1) valid = false;
	if (pos_at < 1) valid = false;
	if (pos_dot < 3) valid = false;
	if (str.length - pos_dot < 2) valid = false;
	if (pos_dot - pos_at < 2) valid = false;
	if (!valid) {
	    alert('L\'adresse e-mail semble invalide. Veullez vérifier.');
	}
	return valid;
}

