/**
* homepage.js
*
* Auteur: Nicolas JARRAUD
* Date de création: 12.09.2008
* Dernière modification: 25.08.2009
*/

/*********************************************************
/* select_gallery_tab(type)
/* tab_off(type, init)
/* tab_on(type)
/* build_buttons(type, init)
/* select_button(btn, btnNumber)
/* display_gallery(gallery, btn, btnNumber)
/* open_contact_window()
/* close_contact_window()
/* send_email()
/* gallery_init()
/* fbs_click()
/********************************************************/

/*********************************************************
/*	  	 	Choix de la catégorie de la mini galerie
/********************************************************/
function select_gallery_tab(type, init) {
	if (type == 'news') {
		tab_off('ranking');
		tab_on('news');
		
		build_buttons('news', init);
	} else if (type == 'ranking') {
		tab_off('news');
		tab_on('ranking');
		
		build_buttons('ranking');
	}
}

/*********************************************************
/*	  	 	  Menu de la mini gallerie éteind
/********************************************************/
function tab_off(type) {
	$('gl-'+type+'Left').style.background = 'none';
	$('gl-'+type+'Right').style.background = 'none';
	$('gl-'+type+'Middle').style.background = 'none';
	$('gl-'+type+'Middle').style.color = '#000';
	$('gl-'+type+'Middle').style.textShadow = '#A0A0A0 0px 1px';
}

/*********************************************************
/*	  	 	  Menu de la mini gallerie allumé
/********************************************************/
function tab_on(type) {
	$('gl-'+type+'Left').style.background = 'url(homepage/views/img/gallery-top-menuLeft.png)';
	$('gl-'+type+'Right').style.background = 'url(homepage/views/img/gallery-top-menuRight.png)';
	$('gl-'+type+'Middle').style.background = 'url(homepage/views/img/gallery-top-menuMiddle.png)';
	$('gl-'+type+'Middle').style.color = '#F7FEFE';
	$('gl-'+type+'Middle').style.textShadow = '#000 0px 1px';
}

/*********************************************************
/*	  	 	  	Construit la barre de boutons
/********************************************************/
function build_buttons(type, init) {
	if (type == 'news') {
		txt  = '<div class="gallery-btnSep"></div>';
		txt += '<div class="gallery-btnBg" onClick="display_gallery(\'news\', 1, 1)" id="gl-btn1" style="padding-right:7px">'+strLastPhotosAdded+'</div>';
		txt += '<img src="homepage/views/img/gallery-btn-endOff.png" style="float:left" id="gl-btnEnd">';
		txt += '<br style="clear:both">';
		
		$('gallery-buttons').update(txt);
		
		if(!init) init = 0;
		if(init == '0') display_gallery('news', 1, 1);
	} else if (type == 'ranking') {
		txt  = '<div class="gallery-btnSep"></div>';
		txt += '<div class="gallery-btnBg" onClick="display_gallery(\'popularity\', 1, 3)" id="gl-btn1">'+strPopularity+'</div>';
		txt += '<div class="gallery-btnSep"></div>';
		txt += '<div class="gallery-btnBg" onClick="display_gallery(\'views\', 2, 3)" id="gl-btn2">'+strViews+'</div>';
		txt += '<div class="gallery-btnSep"></div>';
		txt += '<div class="gallery-btnBg" onClick="display_gallery(\'downloads\', 3, 3)" id="gl-btn3" style="padding-right:7px">'+strDownloads+'</div>';
		txt += '<img src="homepage/views/img/gallery-btn-endOff.png" style="float:left" id="gl-btnEnd">';
		txt += '<br style="clear:both">';
		
		$('gallery-buttons').update(txt);
		display_gallery('popularity', 1, 3);
	}
}

/*********************************************************
/*	  	 	  			Sélectionne un bouton
/********************************************************/
function select_button(btn, btnNumber) {
	for (i = 1; i <= btnNumber; i++) {
		$('gl-btn'+i).style.background = 'url(homepage/views/img/gallery-btn-off.png)';
		$('gl-btnEnd').src = 'homepage/views/img/gallery-btn-endOff.png';
	}
	
	$('gl-btn'+btn).style.background = 'url(homepage/views/img/gallery-btn-on.png)';
	if(btn == btnNumber) $('gl-btnEnd').src = 'homepage/views/img/gallery-btn-endOn.png';
}

/*********************************************************
/*	  	 	 Affiche la mini gallerie sélectionnée
/********************************************************/
function display_gallery(gallery, btn, btnNumber) {
	select_button(btn, btnNumber);
	gallery_init();
	
	var url = 'homepage/controllers/ajax/miniGallery.php';
	new Ajax.Request(url, {
		method: 'post',
		postBody: $H({type: gallery}).toQueryString(),
		onSuccess: function(transport) {
			JSONdata = transport.responseText.evalJSON();
			
			for (i = 1; i <= 5; i++) {
				$('gl-flagLink'+i).href = 'gallery/'+JSONdata.photos[(i-1)].countryID+'.html';
				$('gl-imgLink'+i).href = 'gallery/'+JSONdata.photos[(i-1)].id+'.html';
			
				$('gl-img'+i).src = 'data/cache/homepage/'+gallery+'/large/'+JSONdata.photos[(i-1)].id+'.jpg';
				$('gl-flag'+i).src = 'homepage/views/img/flags32/'+JSONdata.photos[(i-1)].countryID+'.png';
				
				info  = '<a href="./gallery/'+JSONdata.photos[(i-1)].id+'.html">';
				info += '<span style="font-weight:bold">'+JSONdata.photos[(i-1)].title+'</span>';
				info += '</a>';
				info += '<br /><a href="./gallery/member-'+JSONdata.photos[(i-1)].author_id+'.html">'+JSONdata.photos[(i-1)].nickname+'</a>';
				info += '<a href="./gallery/'+JSONdata.photos[(i-1)].countryID+'.html">';
				info += '<br>'+JSONdata.photos[(i-1)].countryTXT;
				info += '</a>';
				$('gl-info'+i).update(info);
				
				$('gl-special'+i).update(JSONdata.photos[(i-1)].special);
			}
			
			for (i = 6; i <= 14; i++) {
				if (i == 6 || i == 7 || i == 8) frame = 1;
				else if (i == 9 || i == 10 || i == 11) frame = 2;
				else frame = 3;

				$('gl-flagLink'+i).href = 'gallery/'+JSONdata.photos[(i-1)].countryID+'.html';
				$('gl-imgLink'+i).href = 'gallery/'+JSONdata.photos[(i-1)].id+'.html';
			
				$('gl-img'+i).src = 'data/cache/homepage/'+gallery+'/small/'+JSONdata.photos[(i-1)].id+'.jpg';
				$('gl-flag'+i).src = 'homepage/views/img/flags16/'+JSONdata.photos[(i-1)].countryID+'.png';
				
				info  = '<a href="./gallery/'+JSONdata.photos[(i-1)].id+'.html">';
				info += '<span style="font-weight:bold">'+JSONdata.photos[(i-1)].title+'</span>';
				info += '</a>';
				info += '<br><a href="./gallery/member-'+JSONdata.photos[(i-1)].author_id+'.html">'+JSONdata.photos[(i-1)].nickname+'</a>';
				info += '<br>'+JSONdata.photos[(i-1)].special;
				$('gl-info'+i).update(info);
				
				$('gl-country'+i).update('<a href="./gallery/'+JSONdata.photos[(i-1)].countryID+'.html">'+JSONdata.photos[(i-1)].countryTXT+'</a>');
			}
		}
	});
}

/*********************************************************
/*	  	 			 Affiche la boîte de contact
/********************************************************/
function open_contact_window() {
	if($('contact-box').getStyle('display') == 'none') {
		$('contact-name').value = '';
		$('contact-email').value = '';
		$('contact-msg').value = '';
		$('contact-error').update();
		new Effect.BlindDown($('contact-box'),{duration:0.5});
	}
}

/*********************************************************
/*	  	 			 	Ferme la boîte de contact
/********************************************************/
function close_contact_window() {
	if($('contact-box').getStyle('display') == 'block') {
		new Effect.BlindUp($('contact-box'),{duration:0.5});
	}
}

/*********************************************************
/*	  	 			 			Envoi du message
/********************************************************/
function send_email() {
	name = $('contact-name').value;
	email = $('contact-email').value;
	msg = $('contact-msg').value;

	var url = 'homepage/controllers/ajax/email.php';
	new Ajax.Request(url, {
		method: 'post',
		postBody: $H({name: name,
									email: email,
									msg: msg}).toQueryString(),
		onSuccess: function(transport) {
			JSONdata = transport.responseText.evalJSON();
			
			$('contact-error').update(JSONdata.msg);
		}
	});
}

/*********************************************************
/*	  	 Initialise les images de la mini galerie
/********************************************************/
function gallery_init() {
	for(i=1;i<=14;i++) {
		$('gl-img'+i).src = 'homepage/views/img/empty.png';
		$('gl-flag'+i).src = 'homepage/views/img/empty.png';
	}
}

/*********************************************************
/*	  	 							Partage facebook
/********************************************************/
function fbs_click() {
	u = location.href;
	t = document.title;
	
	url  = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(u);
	url += '&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436';
	window.open(url);
	return false;
}