/*
 *  Kaza Animation
 * 
 * Copyright 2011 Alfazone Solution Web
 * All rights reserved. 
 *
 * Created by Sébastien Asselin
 *
 */

// SET THIS VALUE
var DURATION = 1000;		// Vitesse de l'animation
var BACKGROUND_WIDTH = 40;	// Nombre de pixel que le background doit se déplacer
var EASING = 'easeInQuart';

// NE PAS TOUCHER AU RESTE
var viewPortWidth;
var viewPortHeight;

/**
 * Permet de trouvé l'index du lien
 */

function find(arr, obj) {
	for (var i = 0; i < arr.length; i++) {
	  if (arr[i] == obj) {
	    found = i;
	    break;
	  }
	}
	return found;
}

/**
 * Récupère la largeur du viewport
 */
function setWindowSize() {
	if (jQuery.browser.msie){
		viewPortWidth = jQuery(window).width() + parseInt(jQuery('.entry-quote:first').css('margin-left').replace('px',''));
	} else {
		viewPortWidth = jQuery(window).width();
	}
}

/**
 * Lorsque jquery est prêt
 */
jQuery(document).ready(function($) {
	$('li#menu-item-387 a').addClass('iframe');
	
	// s = start of Link
	// e = end of Link
	// n = soustraction of e - s
	// d = distance
	// w = width
	var s,e,d,w;
	
	var animate = false; // flag permettant de savoir si l'animatione est terminé
	
	var aList = new Array();
	setWindowSize();
	
	// je stock l'index des liens
	$('#menu-menu-sidebar a').each(function(index) {
		aList[index] = $(this).text();
	});
	
	// resize window on change
	$('.kazacontent').css('width', viewPortWidth);
	$(window).resize(function (){
		setWindowSize();
		var l = parseInt(find(aList, $('#menu-menu-sidebar li.current_page_item a').text()));
			
		if (viewPortWidth > 850) {
			$('#kazawrap').css('margin-left', (viewPortWidth * l * (-1)));
			$('.kazacontent').css('width', viewPortWidth);
		}
		else {
			$('#kazawrap').css('margin-left', ('850' * l * (-1)));
			$('.kazacontent').css('width', '850');
		}
	});

	$('#menu-menu-sidebar li').click(function (e){

		var classLi = $(this).attr('class');
		var clickLi = $(this);

		if ($('a', clickLi).text() == 'BLOGUE') {
			e.preventDefault();
           	e.stopPropagation();
		}
		else {
			// si le lien n'est pas sélectionné et que l'animation est en stop
			if (classLi.search('current_page_item') == -1 && animate == false) 
			{
				animate = true;
				s = parseInt(find(aList, $('#menu-menu-sidebar li.current_page_item a').text()));
				e = parseInt(find(aList, $('a', clickLi).text()));			
				n = e - s;	
				if (viewPortWidth > 850) {
					d = n * viewPortWidth * (-1);
				}
				else {
					d = n * 850 * (-1);
				}
		
				w = parseInt($('#kazawrap').css('margin-left').replace('px','')) + d;
				
				// animation du contenu
				$('#kazawrap').animate({
					marginLeft: w
				}, DURATION, EASING, function() {
						$('#menu-menu-sidebar li.current_page_item').removeClass('current_page_item');
						clickLi.addClass('current_page_item');
						if ($('li#menu-item-317').hasClass("current_page_item")) {
       						$('#submenu').addClass("booyah");
    					} else { $('#submenu').removeClass("booyah"); }
						animate = false;
					}
				);			
				
				// animation du background
				$('body').animate({
					backgroundPositionX: parseInt($('body').css('background-position-x').replace('px','')) + (BACKGROUND_WIDTH * n)
				}, DURATION, EASING);	
			}
		}
		return false;

	});
});




