// Get a new src
function getSizeSrc(src, size) {
	var matches = src.match(/(.+\/)([A-Za-z0-9]+)\-[0-9]+\.(jpg|png|gif)$/);
	return matches[1] + matches[2] + size + matches[3];
}

// IMG SWITCH 
function switchimg(){
	var $xunit = $(window).width();
	var size = '';
	if($xunit > 1800) {
		size = '.';
	}
	else if( ($xunit<= 1800) && ($xunit>=1100) ) {
		size = '-600.';
	}
	else if ( ($xunit<= 1100) && ($xunit>=800) ) {
		size = '-400.';
	} else {
		size = '-300.';
	}
	$('section img').each(function(){
		this.src = getSizeSrc(this.src, size);
	});
}

$(window).resize(function(){
	switchimg();
	$('body').css('height', $(window).height());
}).resize(); // force first resize

(function($){
  $(function(){
  

	// Starter
	$('.enter , body:not(.bopen)').click(function(){
		$('body').addClass('bopen');
		$('#menu').show();
	});

	var images = [ 'avara-home.gif','improvisation.gif','map.gif','carnets.gif','alchimie_livre.gif', 'fork.gif','RL.gif', 'bleuModal.jpg', 'formes1.gif'];
	$('body').css({'background-image': 'url(img/' + images[Math.floor(Math.random() * images.length)] + ')'});	

    // "MORE" Openers
    $('.open').click(function() {        
        if ($(this).hasClass('opened')){
			$(this).text('more');
			$(this).removeClass('opened');
			$(this).next('.inside').hide();        
        }else{
			$(this).text('less');
			$(this).addClass('opened');
			$(this).next('.inside').show();        
        }
    });
    
    function openSection(section, callback) {
        $('#left').prepend(section);
        section.hide();
        if (callback) callback();
        section.show().addClass('fresh');
        setTimeout(function(){
            section.removeClass('fresh');
        },500);
    }
    
    // jQuery History
    $.history.init(function(hash){
        if(hash !== "") {
            // restore the state from hash
            $('body').addClass('bopen');
            openSection($('#' + hash));
            $('h1').click();
        }
    },
    { unescape: ",/" });
    
    $('#menu a, #topmenu a').click(function(e){
        e.preventDefault();
        var curHash = $(this).attr('href').slice(1); // slice(1) = supprimer le '#'
        openSection($('#'+curHash), function(){
            $.history.load(curHash);
        }); // $(this).attr('href') === '#aude' 
    });


    // RANDOM BUTTON
	jQuery.fn.random = function() {
		var randomIndex = Math.floor(Math.random() * this.length);  
		return jQuery(this[randomIndex]);
	};

    $('#getrandom').click(function(e){
    	$('#menu a').not('.norand').random().click();
		randomcolor('#getrandom i');
	});


    // RANDOM BUTTON COLOR
	function randomcolor(word){
		$(word).each(function () {
			var hue = 'hsl(' + (Math.floor((256)*Math.random()) + 0) + ' ,' + (Math.floor((50)*Math.random())+ 30) + '% ,' + (Math.floor((0)*Math.random()) + 50) + '% )';
			$(this).css("color", hue);
		});
	}

    // IMG SHOW
	$('.show img').css('cursor', 'e-resize');
	
	$('.showbox').each(function(){
		var $this = $('.show', this),
        	li = $this.children('li').filter(":gt(0)").hide().end(),
			activeli = li.filter(':visible');
		
		li.click(function() {
			var $currentLi = $(this);
			var $next = $currentLi.next();
			if ($next.length === 0) {
				$next = li.filter(':first');
			}
			$currentLi.hide();
			$next.show();
		});

		$('.next', this).click(function() {
			li.filter(':visible').click();
		});
    });

	//VIDEO SUPPORT - fitVids plugin
    $("section").fitVids();

  });
})(jQuery);

