/*--AJAXY--*/
//Redirection if hash
if(window.location.hash != 0) {
	var base_url =	$('base').attr('href');
	var paramURL = window.location.hash;
	var uri = paramURL.replace("#", "");
	//var hash_uri = base_url+uri;
	var hash_uri = base_url+'/'+uri;
	window.location.href = hash_uri;
}

var cache = new Array();

function pageload(hash) {
	// hash doesn't contain the first # character.
	if(hash) {
		response = cache[hash];
		openPage(response, hash);
	} else {
		// start page
		response = cache['accueil'];
		openPage(response, hash);
	}
}

function initPage() {

	// PRELOAD IMAGE
	img1 = new Image(); img1.src="themes/actuvisu/img/btn_menu_1(hover).png";
	img2 = new Image(); img2.src="themes/actuvisu/img/btn_menu_2(hover).png";
	img3 = new Image(); img3.src="themes/actuvisu/img/btn_menu_3(hover).png";
	img4 = new Image(); img4.src="themes/actuvisu/img/btn_menu_4(hover).png";
	img5 = new Image(); img5.src="themes/actuvisu/img/btn_menu_5(hover).png";
	img6 = new Image(); img6.src="themes/actuvisu/img/btn_menu_6(hover).png";
	img7 = new Image(); img7.src="themes/actuvisu/img/btn_menu_7(hover).png";
	img8 = new Image(); img8.src="themes/actuvisu/img/btn_menu_8(hover).png";

	// HIDE ANIMATED ELEMENTS
	var top_nav_height = $('#top_nav').height();
	$('#top_nav_warp').css('margin-top', -top_nav_height);
	
		$('#top_nav_warp')
        .hover(function() {
            $('#top_nav').stop().animate({ marginTop: top_nav_height }, 400);
        }, function() {
           $('#top_nav').stop().animate({ marginTop: 0 }, 400);
        }
	);

	// INIT EFFECTS
	$("#logo").css('opacity', 0);
	$("#follow_AV").css('opacity', 0);
	$("#footer_fluid").css('opacity', 0);
	$("#texte").css('opacity', 0);
	if($('#sortable_otpions').html() == null) {
		$("#logo").css('margin-top', -300);
	}

	// NAVIGATION
	$(".rounded_menu_item").each(function() { $(this).css("opacity", 0); });

	var i = 1;
		
	init_rounded_menu();
	init_small_rounded_menu();

	function myfunction(elem) { $(elem).animate({ opacity: 1 }, 1200); }
	
	setTimeout(function() { var element = "#img_imagemap_1"; myfunction(element) }, 1100);
	setTimeout(function() { var element = "#img_imagemap_2"; myfunction(element) }, 1200);
	setTimeout(function() { var element = "#img_imagemap_4"; myfunction(element) }, 1300);
	setTimeout(function() { var element = "#img_imagemap_5"; myfunction(element) }, 1400);
	setTimeout(function() { var element = "#img_imagemap_6"; myfunction(element) }, 1500);
	setTimeout(function() { var element = "#img_imagemap_7"; myfunction(element) }, 1600);
	setTimeout(function() { var element = "#img_imagemap_8"; myfunction(element) }, 1700);
	setTimeout(function() { var element = "#img_imagemap_3"; myfunction(element) }, 1800);
	setTimeout(function() { var element = "#center_nav"; myfunction(element) }, 2300);

	if($('#sortable_otpions').html() != null) {
		$('#logo').css('opacity', 1);
	} else {
		$('#logo').animate({ marginTop: 40, opacity: 1 }, 1000, 'easeInOutCirc', function() {
			$('#follow_AV').animate({ opacity: 1 }, 700, 'easeInOutCirc');
			$('#texte').animate({ opacity: 1 }, 700, 'easeInOutCirc', function() {
				
			});
		});
	}
	
	setTimeout("$('#footer_fluid').animate({ opacity: 1 }, 700, 'easeInOutCirc');", 1000);
	
	if($('#sortable_otpions').html() != null) {
		if(jQuery.browser.mozilla) {
			$('#center_round').css('margin-left', '18px');
			$('#texte_center_round').css('margin-left', '78px');
		}
	}
	
	// Scanne tous les liens de la page pour initialiser l'action Ã  effectuer avec setOpenLink
	$('a, area').each(setOpenLink);
	$.historyInit(pageload);
	//$('#ajax_spinner').hide();

	
	// LOAD QUICKSAND GRID
	init_quicksand();
	init_quicksand_sortable();

	init_grid_clicks();
	
	init_comment();
	
}







function init_rounded_menu() {
	var i = 1;
	//alert('Grand : '+i);
	$(".rounded_menu_item").each(function() {
		var map_id = '#map_imagemap_'+i;
		var mage_id = '#img_imagemap_'+i;
		if(i <= 8) {
			//alert($('#map_imagemap_'+i).attr('name'));
			//alert($('#map_imagemap_'+i).attr('name'));
			//$('#map_imagemap_'+i).css('display', 'none');
			//$('#img_imagemap_'+i).css('display', 'none');
			/*
			$(map_id).unbind('click');
			$(map_id).click(function() {
				alert('ok');
			});
			*/
			$(map_id).hover(function() {
					var item_num = $(this).attr('rel');
					$(mage_id).css("z-index", 10);
					$(mage_id).attr("src", "themes/actuvisu/img/btn_menu_"+item_num+"(hover).png");
					$('#texte_center_round').html($(mage_id).attr('alt'));
				},
				function() {
					var item_num = $(this).attr('rel');
					$(mage_id).attr("src", "themes/actuvisu/img/btn_menu_"+item_num+".png");
					$(".rounded_menu_item").each(function() { $(this).css("z-index", 5); });
					$(mage_id).css("z-index", 0);
				}
			);
		 }
		
		i = i + 1;
	});
}

function init_small_rounded_menu() {
	var i = 1;
	$(".rounded_menu_small_item").each(function() {
		var map_id = '#mapsmall_imagemap_'+i;
		var mage_id = '#imgsmall_imagemap_'+i;
		
		$(map_id).hover(function() {
				var item_num = $(this).attr('rel');
				$(mage_id).css("z-index", 10);
				$(mage_id).attr("src", "themes/actuvisu/img/round_menu_small/rounded_menu_"+item_num+"(hover).png");
				$('#texte_center_round').html($(mage_id).attr('alt'));
			},
			function() {
				var item_num = $(this).attr('rel');
				$(mage_id).attr("src", "themes/actuvisu/img/round_menu_small/rounded_menu_"+item_num+".png");
				$(".rounded_menu_item").each(function() { $(this).css("z-index", 5); });
				$(mage_id).css("z-index", 0);
			}
		);

		i = i + 1;
	});
}

function init_quicksand_sortable() {
	$('#filter_popular_on').click(function() {
		$(this).find('img').attr('src', 'themes/actuvisu/img/filter_popular_1.png');
		$('#filter_popular_off').find('img').attr('src', 'themes/actuvisu/img/filter_popular_2.png');
	});
	$('#filter_popular_off').click(function() {
		$(this).find('img').attr('src', 'themes/actuvisu/img/filter_popular_4.png');
		$('#filter_popular_on').find('img').attr('src', 'themes/actuvisu/img/filter_popular_3.png');
	});
}

//INIT VISU CLICKS
function init_grid_clicks() {
	$('.grid-visu').each(function() {
		$(this).click(function() {
			$('#preview_visu_title').html($(this).attr('title'));
			$('#preview_visu_teaser').html($(this).attr('visu-teaser'));
			$('#btn_visualisez').css('display', 'block');
			$('#btn_visualisez').attr('href', $(this).attr('href'));
			return false;
		});
	});
}

// COMMENTS
function init_comment() {
	$('#comments').hide();
	$('#show_hide_comment').toggle(function() {
		$('#comments').slideDown();
	}, function() {
		$('#comments').slideUp();
	});
}
	

	
function setOpenLink() {

	// Récupération de l'attribut "href" du lien scanné
	var linkHref = $(this).attr('href');

	// Récupération de l'attribut "rel" du lien scanné qui correspond au type d'action à faire
	var linkRel = $(this).attr('rel');
	
	// Si pas d'attribut "rel", ouverture du lien avec l'animation openLink
	if (linkRel == null || linkRel == '' || linkRel == 'scale' || linkRel == 'blog') {
		$(this).click(openLink);
	}

}

function openLink() {

	// Génération de nombre aléatoire pour faire un appel unique Ã  chaque clique (HACK pour le cahce d'IE)
	var intParam = Math.random();
	var linkHash = $(this).attr('href');
	var linkRel = $(this).attr('rel');
	var linkHref = $(this).attr('href')+'?ajax=true&'+intParam;
	
	// Google Analytics tracker
	//pageTracker._trackPageview(linkHash);
	_gaq.push(['_trackPageview', linkHash]);
	
	var paramURL = window.location.hash;
	var current_hash = paramURL.replace("#", "");
	
	// Si le "href" commence par "http://" ou "mailto:" c'est un lien non ajax et on redirige vers ce lien
	var exp_http = new RegExp();
	var exp_mailto = new RegExp();
	exp_http.compile("^[A-Za-z]+://");
	exp_mailto.compile("^mailto:");
	if (exp_http.exec(linkHash) || exp_mailto.exec(linkHash)) {
		// Redirection
		window.location = linkHash;
		return false;
	}
	
	if(linkRel == "scale") {
		$('#page_content').css('position', 'absolute');
		$('#page_content').animate({scale: '+=1'}, {queue: false, duration: 300});
		$('#page_content').animate({opacity: 0 }, 300);
	}
	
	
	if($.browser.webkit) {
		if(linkRel == "scale" || linkHash == "accueil") {
			setTimeout('window.location.href = linkHash;', 400);
			return;
		}
	}
	
	
	// Si l'url appelée n'est pas la page courrante...
	if(current_hash != linkHash) {
		//$('#ajax_spinner').show();
		// Si l'url est enregistrée dans le cache...
		if(cache[linkHash]) {
		
			response = cache[linkHash];
			$.historyLoad(linkHash);
			//$('#ajax_spinner').hide();
			
		} else { // Sinon, requête Ajax...
		
			$.ajax({
				url: linkHref,
				success: function(response) {
					cache[linkHash] = response;
					$.historyLoad(linkHash);
				}
			});
			
		}
		
	}
	
	return false;
} // END function openLink()

function openPage(response, linkHash) {

	//$.scrollTo( '0px', 300);
	
	$('#dynamic_content').animate({ opacity: 0 }, 200, function() {
		
		$('#dynamic_content').html('');
		$('#dynamic_content').html(response);
		
		if($('#sortable_otpions').html() != null) {
			if(jQuery.browser.mozilla) {
				$('#center_round').css('margin-left', '18px');
				$('#texte_center_round').css('margin-left', '78px');
			}
		}
		
		//$('#dynamic_content a, #dynamic_content area').unbind();
		
		if($('#sortable_otpions').html() != null) {
			init_quicksand();
			init_grid_clicks();
			init_quicksand_sortable();
		}
		//$('#ajax_spinner').hide();
		
		$('#dynamic_content').animate({ opacity: 1 }, 500, function() {
			
			if($('#nav').html() != null) { init_rounded_menu(); }
			if($('#nav_small').html() != null) { init_small_rounded_menu(); }
			
			
			$('#dynamic_content a, #dynamic_content area').each(setOpenLink);
			
			init_comment();
			
			//$('#ajax_spinner').hide();
		});
		
	});

}

