// Init
var available = true;

// Clic on all the hidden news_blocks
var initAllJsNewsBlockHidden = function(){
	$("a.jsNewsBlockHidden").each(function(){
		// Get the id
		myIds = $(this).attr('id').split("_");
		myId = myIds[1];
		myParentId = myIds[2];
		initJsNewsBlockHidden(myId, myParentId);
	});
};

// Clic on a hidden news_block (defined by an id)
var initJsNewsBlockHidden = function(myId, myParentId){
	$("#nbh_"+myId+"_"+myParentId).click(function(){
		// Get the news_block_visible_line tpl html
		if (available){
			available = false;
			$.ajax({
				type: "POST",
				url: '/'+siteaccess+'/layout/set/ajax/news/getNewsBlock/'+myId+'/'+myParentId+'/v/',
				data: "",
				success: function(responseTxt){
					// Add it to the news_block list
					$("#content").prepend(responseTxt);
					// Modify opacity & property of news_block_additional
					$("#nba_"+myId).removeClass("hidden");
					// Delete the hidden news_block
					$("#nbh_"+myId+"_"+myParentId).fadeOut().remove();
					// Update the css class if needed
					checkJsNewsBlockVisibleCount();
					// Declare the new actions
					initJsNewsBlockVisible(myId, myParentId);
					
					available = true;
				},
				complete: function(){},
				error: function(){}
			});
		}
	});
};

// Clic on all the visible news_blocks
var initAllJsNewsBlockVisible = function(){
	$("a.jsNewsBlockVisible").each(function(){
		// Get the id
		myIds = $(this).attr('id').split("_");
		myId = myIds[1];
		myParentId = myIds[2];
		initJsNewsBlockVisible(myId, myParentId);
	});
};

//Clic on a hidden news_block (defined by an id)
var initJsNewsBlockVisible = function(myId, myParentId){
	$("#nbvr_"+myId+"_"+myParentId).click(function(){
		// Get the news_block_hidden_line tpl html
		if (available){
			available = false;
			$.ajax({
				type: "POST",
				url: '/'+siteaccess+'/layout/set/ajax/news/getNewsBlock/'+myId+'/'+myParentId+'/h/',
				data: "",
				success: function(responseTxt){
					// Add it to the news_block list
					$("#news-block-hidden").append(responseTxt);
					// Modify opacity & property of news_block_additional
					$("#nba_"+myId).addClass("hidden");
					// Delete the hidden news_block
					$("#nbv_"+myId+"_"+myParentId).fadeOut().remove();
					// Update the css class if needed
					checkJsNewsBlockVisibleCount();
					// Declare the new actions
					initJsNewsBlockHidden(myId, myParentId);
					
					available = true;
				},
				complete: function(){},
				error: function(){}
			});
		}
	});
};

//Check the number of news_block in the content node
var checkJsNewsBlockVisibleCount = function(){
	newsBlockCount = $("#content").children(".news-block").length;
	if (newsBlockCount == "1"){
		$("#content").addClass("news-block-left");
	}else{
		$("#content").removeClass("news-block-left");
	}
};

//Change the news gallery portal source
var initAllJsNewsPortalChange = function(myId){
	$("#submitPortalNews").hide();
	$("#portalNews").change(function(){
		// Redirect
		$("#fPortalNews").submit();
	});
};


//Change the news gallery portal source
var initAllJsFindInTheCityCityChange = function(myId){
	$("#submitFindInTheCityCity").hide();
	$("#findInTheCityCities").change(function(){
		// Redirect
		$("#findInTheCityCities").submit();
	});
};

//Change the news gallery portal source
var initAllJsBusStationChange = function(myId){
	$("#submitChangeStation").hide();
	$("#bus-station").change(function(){
		// Redirect
		$("#bus-station-switcher").submit();
	});
};


