/* COMMON
--------------------------------------------------------------------------------------------------------------------------------------*/

$(function(){
	$('.display_if_no_javascript').css('display', 'none');
});


$(function(){
	if($('.project')[0]) initProjectsHover();
	
	if($('#carousel')[0]){
		$('#carousel').jCarouselLite({
			btnNext: ".carousel-next",
			btnPrev: ".carousel-prev",
			speed: 500
		});
	}
});

$(function() {
	var selectedDiv;
	$('.box_projects').mouseover(function() {
		selectedDivID = $(this).attr('id');
		$('#'+selectedDivID+' .pop_in').css('visibility', 'visible');
	});
	$('.box_projects').mouseout(function() {
		selectedDivID = $(this).attr('id');
		$('#'+selectedDivID+' .pop_in').css('visibility', 'hidden');
	});
});

$(function() {
	$('.avoidspam').click(function() {
		$(this).attr('href', $(this).attr('href').replace(/avoidspam/, ''));
	})
}) 

/**
 * REALISATIONS
 */
var sectors = new Array();
var objects = new Array();
var ourActions = new Array();
var targetAjax;

$(function() {
	var selectSectorId;
	var selectObjectId;

	$('#sector li a').click(function() {
		selectSectorId = $(this).attr('id');
		runCommand(sectors, selectSectorId, 'sector');
		return false;
	});
	
	$('#object li a').click(function() {
		selectObjectId = $(this).attr('id');
		runCommand(objects, selectObjectId, 'object');
		return false;
	});
	
	$('#ouraction li a').click(function() {
		selectActionId = $(this).attr('id');
		runCommand(ourActions, selectActionId, 'ouraction');
		return false;
	});
});

function runCommand(data, selectId, ulId) {
	handleModel(data, selectId);
	drawSelectItems(data, ulId);
	startAjax();
}

function handleModel(list, id) {
	var index = isInList(list, id);
	if(index == '-1') {
		list.push(id);
	} else {
		list.splice(index, 1);
	}
}

function drawSelectItems(list, ulId) {
	$('#' + ulId + ' li a').removeClass('active');
	for(var i in list) {
		$('#' + ulId + ' li #'+list[i]).addClass('active');
	}
}

function isInList(list, id) {
	for(var i in list) {
		if(list[i] == id) {
			return i;
		}
	}
	return '-1';
}

function setTargetAjax(urlTarget) {
	targetAjax = urlTarget;
}

function startAjax() {
	$.ajax({
		type: "POST",
		url: targetAjax,
		data:"sectorList="+sectors+"&objectList="+objects+"&ourActionList="+ourActions,
		error:function(msg){
			$('#ajax').text(msg.status);
	   	},
	    success:function(data){
	   		$('#ajax').html(data);
	   	}
	});
}

function drawDefaultMenuItem() {
	var selectSectorId = $('#sector li a:first').attr('id');
	runCommand(sectors, selectSectorId, 'sector');
}

function keepStateMenuItemSector(strList, ulId) {
	sectors = strList.split(',');
	drawSelectItems(sectors, ulId);
	startAjax();
}

function keepStateMenuItemObject(strList, ulId) {
	objects =  strList.split(',');
	drawSelectItems(objects, ulId);
	startAjax();
}

function keepStateMenuItemAction(strList, ulId) {
	ourActions = strList.split(',');
	drawSelectItems(ourActions, ulId);
	startAjax();
}

/**
 * MAP
 */
function loadMap(customIcon) {
	var latLng = new google.maps.LatLng(48.8548673, 2.4036538);
	var options = {
		zoom: 16,
		center: latLng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	var gmap = new google.maps.Map(document.getElementById("gmap"), options);
	var markerImage = new google.maps.MarkerImage(customIcon);
	
	var myMarker = new google.maps.Marker({
		position: latLng, 
		map: gmap,
		icon: markerImage,
		title: "Entreprise Buzzaka"
	});

}
/* FUNCTIONS
--------------------------------------------------------------------------------------------------------------------------------------*/

function initProjectsHover(){
	$('.project').hover(function(){
		$(this).find('.info').stop(true, true).fadeIn(500);
	}, function(){
		$(this).find('.info').stop(true, true).fadeOut(500);
	});
}

function menuItemOver(pageId) {
	$('.page-item-'+pageId).addClass('current_page_item');
} 

