var News = {}

News.popup_photo = function(lang, popupWidth, popupHeight)
{
	if ($('photo_clearsite').style.display == "none")
		{
			$('title').style.display = "none";
			$('title_clear').style.height = "68px";
			$('photo_clearsite').style.display = "block";
			$('photo_clearsite').style.width = document.documentElement.scrollWidth + "px";
			$('photo_clearsite').style.height = document.documentElement.scrollHeight + "px";
			DOM.center($('photo_popup'),-300,-280);
			
			if ($('news_left_brochure') && lang)
			{
				brochure_div_content = $('news_left_brochure').innerHTML;
				$('news_left_brochure').innerHTML = "<img src='images/" + lang + "/brochure.jpg' />";
			}
			
			$('photo_popup').style.display = "block";
		}
		else
		{
			$('photo_clearsite').style.display = "none";
			$('photo_popup').style.display = "none";
			
			if (brochure_div_content != "");
			{
				$('news_left_brochure').innerHTML = brochure_div_content;
				brochure_div_content = "";
			}
			
			$('title').style.display = "block";
			$('title_clear').style.height = "0px";
			$('photo_clearsite').style.width = "0px";
			$('photo_clearsite').style.height = "0px";
		}	
};

News.send_subscription = function(current_form)
{
	News.email_input = current_form.newsalert;
	
	var req = new AJAX.Request
	(
		Request.get('basepath') + '/' + CURRENT_LANG + '/' + Request.get('module') + '/api_send_subscription/', 
		{
			method : 'POST',
			json : true,
			parameters: 
			[
				'email=' + escape(News.email_input.value)
			],
			onComplete : function(response)
			{
				News._send_subscription(response.responseJSON);
			}
		}
	);		
};

News._send_subscription = function(responseJSON)
{
	if(responseJSON.status == -1)
	{
		// Bad email
		News.email_input.className = 'news_left_alert_field_error';
	}
	else
	{
		News.email_input.className = 'news_left_alert_field';			
		
		if(responseJSON.status == 0)
		{
			// Email already
			var msg = PHPVars.get('subscribe_confirm_none');
		}
		else
		{
			// Email confirmed	
			var msg = PHPVars.get('subscribe_confirm');
		}
	
		News.popup_confirm(msg);
	}
};

News.close_confirm_popup = function()
{
	if ($('clearsite'))
	{
		$('clearsite').style.display 	= "none";
		$('clearsite').style.width 		= "0px";
		$('clearsite').style.height 	= "0px";
	}
	else if ($('video_clearsite'))
	{
		$('video_clearsite').style.display 	= "none";
		$('video_clearsite').style.width 	= "0px";
		$('video_clearsite').style.height 	= "0px";
	}
		
	$('confirm_popup').style.display 	= "none";
	$('title').style.display 			= "block";
	$('title_clear').style.height 		= "0px";
};

News.popup_confirm = function (msg)
{	
	if ($('confirm_popup').style.display == "none")
	{
		$('title').style.display = "none";
		$('title_clear').style.height = "68px";
		$('clearsite').style.display = "block";
		$('confirm_popup').style.display = "block";
		$('clearsite').style.width = document.documentElement.scrollWidth + "px";
		
		if (document.documentElement.scrollHeight < document.documentElement.clientHeight)
		{
			$('clearsite').style.height = document.documentElement.clientHeight + "px";
		}
		else
		{
			$('clearsite').style.height = document.documentElement.scrollHeight + "px";
		}
		
		$('confirm_popup_content_text').innerHTML = msg;
				
		DOM.center($('confirm_popup'), 0, 0);
	}
};

Browser.registerOnLoad(function(){
	
	var unsubscribe_frm = $('unsubscribe_frm');
	
	if (unsubscribe_frm)
	{
		DOM.addEvent(unsubscribe_frm, 'submit', function(event){
			
			DOM.stopEvent(event);
			
			var path = Request.get('basepath') + '/' + CURRENT_LANG + '/' + Request.get('module') + '/api_unsubscribe_email';
			
			var request = new AJAX.Request(path, {
				method : 'POST',
				json : true,
				parameters : AJAX.FormParams(unsubscribe_frm),
				onComplete : function(response){
					
					if (response.responseJSON.result)
					{
						$('unsubscribe_form').style.display = 'none';
						$('unsubscribe_confirm').style.display = '';
					}
					
				}
			});
		});
	}
	
});

News.Publications = {};

News.Publications.select = function(id, button)
{
	var input = $('publication_' + id),
		image = button.getElementsByTagName('img')[0],
		basepath = Request.get('basepath') + '/images/' + CURRENT_LANG + '/btn_selected_{state}.gif';
	
	if (input)
	{
		DOM.remove(input);
		image.src = basepath.fill({state:'off'});
	}
	else
	{
		input = DOM.create('input', {
			type : 'hidden',
			name : 'publications[]',
			id : 'publication_' + id,
			value : id
		});
		
		input.className = 'publicationSelected';
		
		$('frm_publications').appendChild(input);
		
		image.src = basepath.fill({state:'on'});
	}
};

Browser.registerOnLoad(function(){
	
	var frm_publications = $('frm_publications');		
	
	if (frm_publications)
	{
		Validation.registerForm(frm_publications, {
			'lastname' : [{type:'required'}],
			'society' : [{type:'required'}],
			'firstname' : [{type:'required'}],
			'address' : [{type:'required'}],
			'phone' : [{type:'required'}],
			'city' : [{type:'required'}],
			'email' : [{type:'required'}, {type:'email'}],
			'postalcode' : [{type:'required'}]
		}, false);
		
		DOM.addEvent(frm_publications, 'submit', function(event){
			
			var publicationSelected = false;
			
			for(var i = 0; i < $('frm_publications').length; i++)
			{
				if($('frm_publications')[i].className == 'publicationSelected')
				{
					var publicationSelected = true;	
				}
			}
			
			if (Validation.validateForm(frm_publications, true))
			{									
				if (!publicationSelected)
				{
					DOM.stopEvent(event);
					alert(PHPVars.get('choose_atleast_one_publication'));
				}
			}
			else
			{
				DOM.stopEvent(event);
			}
		});
	}
});

function publication_popup(edition) {
	if ($('publication_popup').style.display == "none")
	{
		$('title').style.display = "none";
		$('title_clear').style.height = "68px";
		$('clearsite').style.display = "block";
		$('clearsite').style.width = document.documentElement.scrollWidth + "px";
		if (document.documentElement.scrollHeight < document.documentElement.clientHeight)
		{
			$('clearsite').style.height = document.documentElement.clientHeight + "px";
		}
		else
		{
			$('clearsite').style.height = document.documentElement.scrollHeight + "px";
		}
		DOM.center($('publication_popup'), -175, -250);
		$('publication_popup_img').src = Request.get('basepath') + "/images/" + CURRENT_LANG + "/publications_trajectoire_" + edition + "_big.jpg";
		$('publication_popup').style.display = "block";
	}
	else
	{
		$('clearsite').style.display = "none";
		$('publication_popup').style.display = "none";
		$('title').style.display = "block";
		$('title_clear').style.height = "0px";
		$('clearsite').style.width = "0px";
		$('clearsite').style.height = "0px";
	}
}
