/* Le nombre maximum de pages a afficher */
var nbrMaxGalleryDisplayedPages = 15 ;
/* Le nombre courrent de page pour la gallerie en cours */
var nbrGalleryPages = 0 ;
/* Le nombre de photo dans la pages */
var nbrPhotosPage = 0 ;
/* Date courrente de la gallerie */
var galleryDate = 0 ;
/* Tableau contenant les informations des thumbs */
var thumbsInfos = null ;
/* Mingnature selectionnee */
var selectedThumb = 0 ;
/* Page selecteionnee HTML OBJ*/
var selectedPage = 0 ;
/* Page selectionnee NBE */
var selectedNbrPage = 0 ;
/* Mingature preselectionne depuis un lien */
var preSelectedThumb = 0 ;
/* Page preselectionnee depuis un lien */
var preSelectedPage = 0 ;
/* Calendrier de la gallerie */
var calPhotos = null ;
/* Dates desactives */
var DISABLED_DATES = new Array() ;
/* Compteur de photo pour les pubs */
var viewedPics = 0 ;
/* Si on active les publicite dans la grande photo */
var enabelAds = false ;
/* Si on a lancer la premiere fois la gallerie photo cliente */
var galStarted = true ;

function get_gallery_nbr_pages_by_date(dteSrch)
{
	var xhr = get_xmlhttp() ;
	if(xhr==null) {
		return false ;
	}
	/* On assigne la date de la gallerie actuelle */
	galleryDate = dteSrch ;

	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			/* On recupere avec responseXML le nombre de page dans le tag nbrpages */
			var nbrPages = xhr.responseXML.getElementsByTagName('nbrpages').item(0).getAttribute('value') ;
				
			/* Si on trouve qqch, on supprime toutes la liste et on la recree */
			var ulPages = document.getElementById('ul_pages') ;
			var liPages = ulPages.getElementsByTagName("li")
			var nbrLi = liPages.length ;	
			for(var i=nbrLi-1; i>=0; i--) {
				ulPages.removeChild(liPages[i]) ;
			}
			nbrGalleryPages = nbrPages ;
			/* C'est maintenant que l'on recree la liste avec le nombre de pages */
			for(var i=0; i<nbrPages;i++) {
				var newLi = document.createElement('li');
				newLi.id = i ;
				
				/* Check can use attachEvent else use addEventListener */
				if ( newLi.attachEvent ) {
					newLi.attachEvent('onclick', get_pages_thumbs);
				} else {
					newLi.addEventListener('click', get_pages_thumbs, false );
				}
				/* On remplit le texte dans la li */
				var nbr = i + 1 ;
				newLi.innerHTML = " " + nbr + " ";
				/* J'insere la li dans le UL */
				/* todo : Correct bug with IE and order Page */
				ulPages.appendChild(newLi) ;
				//ulPages.insertBefore(newLi, ulPages.lastChild);
				/* Si l'on a charger la derniere pages, on appelle le chargement des thumbs de cette derniere */
				if(preSelectedPage == 0) {
					if(i == 0) {
						get_pages_thumbs(newLi) ;
					}
				} else {
					if(i == preSelectedPage) {
						get_pages_thumbs(newLi) ;
					}
				}
			}
		}
	}
	/* Build get url */
	/* todo : Send this value in POST */
	scriptAjax = '/scripts/ajax/get_gallery_pages_by_date.php?dteSrch='+dteSrch ;
	xhr.open("GET", scriptAjax, true) ;
	xhr.send(null) ;
}

function get_pages_thumbs(e)
{
	/* todo : Essayer de recuperer la position dans le UL au lieu d'utiliser evt.id*/
	var e=e? e : window.event;
	var el=e.target? e.target : e.srcElement;
	var xhr = get_xmlhttp() ;
	if(el) {
		e = el ;
	}
	if(xhr==null) {
		return false ;
	}

	if(selectedPage != 0) {	
		selectedPage.className = '' ;
	}
	selectedPage = e ;
	selectedNbrPage = e.id ;
	selectedPage.className = 'selected' ;
	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			/* Tout d'abord, je cache les miniatures */
			/* todo : Change 14 with PHP define var's */
			for(var i=1; i<=14; i++) {
				document.getElementById('thumbs_'+i).style.display = 'none' ;
			}
			/* Maintenant, on recupere dans le XML les photos que l'on doit mettre dans les mignatures */	
			thumbsInfos = null ;
			thumbsInfos = xhr.responseXML.getElementsByTagName('photo') ;
			nbrPhotosPage = thumbsInfos.length ;
			if(thumbsInfos.length > 7) {
				document.getElementById('thumbs_pages').style.marginTop = '90px' ;
			} else {
				document.getElementById('thumbs_pages').style.marginTop = '0px' ;
			}
			/* Je fais une boucle sur le nombre et j'attribue dans le inner HTML, les images */
			for(var i=1; i<=thumbsInfos.length; i++) {
				/* todo : CHange 75px with PHP define var */
				var nbr = i-1 ;
				document.getElementById('thumbs_'+i).innerHTML = "<img style='width: 75px;' onclick='set_photo("+nbr+")' src='/uploads/resized/"+thumbsInfos.item(nbr).getAttribute('value')+".jpg' />"+thumbsInfos.item(nbr).getAttribute('time')+"" ;
				document.getElementById('thumbs_'+i).style.display = 'block' ;
				
				if(i == preSelectedThumb + 1 && preSelectedThumb != 0) {
					set_photo(i-1) ;
				}
			}
			/* Des que l'on a fini de charger les thumbs, on affiche la premiere dans la big div */
			if(preSelectedThumb == 0) {
				set_photo(0) ;
			} else {
				preSelectedThumb = 0 ;
			} 
		}
	}
	
	/* Build get url */
	/* todo : Send this value in POST */
	scriptAjax = '/scripts/ajax/get_gallery_pages_thumbs.php?dteSrch='+galleryDate+'&pageSrch='+e.id ;
	xhr.open("GET", scriptAjax, true) ;
	xhr.send(null) ;
		
}

/* Fonction qui affiche la publicite */
function show_advert()
{
	document.getElementById('big_pic').style.display = 'none' ;
	document.getElementById('advert_pic').style.display = 'block' ;
	setTimeout("hide_advert()", 2000) ;
}

/* Fonction qui cache la publicite */
function hide_advert()
{
	viewedPics = 0 ;
	document.getElementById('advert_pic').style.display = 'none' ;
	document.getElementById('big_pic').style.display = 'block' ;
}

/* Attribution de l'image lors du clique sur une mignature */
function set_photo(i)
{
	var xhr = get_xmlhttp() ;
	viewedPics ++ ;
	if(viewedPics == 10 && enabelAds == true) {
		/* Si on a afficher 5 photos alors on affiche une petite pub */
		show_advert() ;	
	}
	/* Define page to PHP session */
	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			/* Tout d'abord, je cache les miniatures */
			preSelectedPage = 0 ;
			var nbr = i+1 ;
			if(selectedThumb != 0) {
				document.getElementById('thumbs_' + selectedThumb).className = '' ;
			}
			selectedThumb = nbr ;
			document.getElementById('thumbs_'+nbr).className = 'selected' ;
			document.getElementById('big_pic').innerHTML = "<img style='width: 570px;cursor:pointer !important; cursor:hand;' src='/uploads/resized/"+thumbsInfos.item(i).getAttribute('value')+".jpg' onClick='go_next_photo()' />" ;
			document.getElementById('pic_info').innerHTML = thumbsInfos.item(i).getAttribute('client') + ', le ' + thumbsInfos.item(i).getAttribute('date') + ' ' + thumbsInfos.item(i).getAttribute('time') ;
			if(thumbsInfos.item(i).getAttribute('comment') > 0) {
				get_photo_comments(thumbsInfos.item(i).getAttribute('value')) ;
			} else {
				document.getElementById('comments_pic').style.display = 'none';
			}
		}
	}
	
	scriptAjax = '/scripts/ajax/set_photo_view.php?picId='+thumbsInfos.item(i).getAttribute('value') ;
	xhr.open("GET", scriptAjax, true) ;
	xhr.send(null) ;
}

function get_last_photos()
{
	var xhr = get_xmlhttp() ;
	if(xhr==null) {
		return false ;
	}
	/* todo : Call this only if a new photo */
	var divOutput = document.getElementById('LastPicture') ;
	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			if(xhr.responseText!="") {
				/* If result, write in output div */
				divOutput.innerHTML = xhr.responseText ;
				var tLastPhotos=setTimeout("get_last_photos()",20000);
			}
		}
	}
	
	/* Build get url */
	scriptAjax = '/scripts/ajax/get_last_photos.php' ;
	xhr.open("GET", scriptAjax, true) ;
	xhr.send(null) ;
}

function get_photo_comments(pid)
{
	var xhr = get_xmlhttp() ;
	if(xhr==null) {
		return false ;
	}

	var divContent = document.getElementById('comments_pic') ;
	var divOutput = document.getElementById('comments_pic_content') ;
	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			if(xhr.responseText!="") {
				/* If result, write in output div */
				divOutput.innerHTML = xhr.responseText ;
				divContent.style.display = 'block' ;
			} 
		}
	}
	
	/* Build get url */
	/* todo : Send in POST */
	scriptAjax = '/scripts/ajax/get_photo_comment.php?pid='+pid ;
	xhr.open("GET", scriptAjax, true) ;
	xhr.send(null) ;
}

function dis_cal_date(m, y)
{
	var xhr = get_xmlhttp() ;
	if(xhr==null) {
		return false ;
	}
	
	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			disDate = xhr.responseXML.getElementsByTagName('disdate') ;
			for(var i=0; i<disDate.length; i++)
			{
				DISABLED_DATES[disDate.item(i).getAttribute('value')] = "true" ;
			} 
			calPhotos.args.disabled() ;
			calPhotos.redraw();
		}
	}
	/* Build get url */
	/* todo : Send in post */
	scriptAjax = '/scripts/ajax/get_calendar_disabled_date_by_mounth.php?m='+m+'&y='+y ;
	xhr.open("GET", scriptAjax, true) ;
	xhr.send(null) ;
}

function open_download(photo)
{
	var url = "http://www.buzzyou.ch/tpl/lightwindow/download/download.php?pid="+photo ;
	myLightWindow.activateWindow({
		title: 'T&eacute;l&eacute;charger la photo',
		href: url,
		height: 348,
		width: 657
	});
}

function open_facebook()
{
	var url = "http://www.buzzyou.ch/tpl/lightwindow/facebook/facebook.php?pid="+thumbsInfos.item(selectedThumb-1).getAttribute('value') ;
	myLightWindow.activateWindow({
		title: 'Envoyer la photo sur Facebook',
		href: url,
		height: 380,
		width: 680
	});
}

function open_comment()
{
	var url = "http://www.buzzyou.ch/tpl/lightwindow/comment/comment.php?pid="+thumbsInfos.item(selectedThumb-1).getAttribute('value') ;
	myLightWindow.activateWindow({
		title: 'Commenter la photo',
		href: url,
		height: 380,
		width: 680
	});
}

function open_comment_display()
{
	var url = "http://www.buzzyou.ch/tpl/lightwindow/comment_display/comment_display.php?pid="+thumbsInfos.item(selectedThumb-1).getAttribute('value') ;
	myLightWindow.activateWindow({
		title: 'Tous les commentaires de la photo',
		href: url,
		height: 380,
		width: 700
	});
}

function open_censure(photo)
{
	var url = "http://www.buzzyou.ch/tpl/lightwindow/censure/censure.php?pid="+photo ;
	myLightWindow.activateWindow({
		title: 'Censurer la photo',
		href: url,
		height: 450,
		width: 710
	});
}

function open_send(photo)
{
	var url = "http://www.buzzyou.ch/tpl/lightwindow/send/send.php?pid="+photo ;
	myLightWindow.activateWindow({
		title: 'Envoyer &agrave; un ami',
		href: url,
		height: "400px",
		width: "680px"
	});
}

function send_facebook(pid)
{
	if(document.getElementById("read_chart_facebook").checked != false) {
	var xhr = get_xmlhttp() ;
	if(xhr==null) {
		return false ;
	}
	document.getElementById('facebook_comment').style.display = 'none' ;
	document.getElementById('facebook_send').style.display = 'block' ;	
	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			document.getElementById('facebook_send').style.display = 'none' ;
			document.getElementById('facebook_success').style.display = 'block' ;	
		}
	}
	
	/* Build get url */
	var params = 'pid='+pid+'&comment='+encodeURI(document.getElementById('facebook_comment_user').value) ;
	/* Build get url */
	scriptAjax = '/scripts/ajax/send_facebook.php' ;
	xhr.open("POST", scriptAjax, true) ;
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", params.length);
	xhr.setRequestHeader("Connection", "close")	
	xhr.send(params) ;
	} else {
		alert("Merci de lire et d'accepter la charte d'utilisation") ;
	}
}

function send_comment(pid)
{
	if(document.getElementById("read_chart_comment").checked != false) {
	var xhr = get_xmlhttp() ;
	if(xhr==null) {
		return false ;
	}

	document.getElementById('comment_comment').style.display = 'none' ;

	/* Bug FIX first comment on pic */
	thumbsInfos.item(selectedThumb-1).setAttribute('comment',"1") ;
	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			document.getElementById('comment_success').style.display = 'block' ;
			get_photo_comments(pid) ;
		}
	}
		
	/* Build get url */
	var params = 'pid='+pid+'&pseudo='+encodeURI(document.getElementById('comment_pseudo').value)+'&comment='+encodeURI(document.getElementById('comment_comment_user').value) ;
	/* Build get url */
	scriptAjax = '/scripts/ajax/add_photo_comment.php' ;
	xhr.open("POST", scriptAjax, true) ;
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", params.length);
	xhr.setRequestHeader("Connection", "close")	
	xhr.send(params) ;
	} else {
		alert("Merci de lire et d'accepter la charte d'utilisation") ;
	}
}

function send_censure(pid)
{
	if(document.getElementById("read_chart_censure").checked != false) {
	var xhr = get_xmlhttp() ;
	if(xhr==null) {
		return false ;
	}
	
	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			var state = xhr.responseXML.getElementsByTagName('state') ;
			if(state.item(0).getAttribute('value') == 1) {
				document.getElementById('censure_form').style.display = 'none' ;
				document.getElementById('censure_success').style.display = 'block' ;
			} else {
				document.getElementById('censure_msg').innerHTML = state.item(0).getAttribute('msg') ;
			}
		}
	}
	var params = 'pid='+pid+'&firstname='+encodeURI(document.getElementById('censure_firstname').value)+'&lastname='+encodeURI(document.getElementById('censure_lastname').value)+'&email='+encodeURI(document.getElementById('censure_email').value)+'&tel='+encodeURI(document.getElementById('censure_tel').value)+'&reason='+encodeURI(document.getElementById('censure_reason').value) ;
	/* Build get url */
	scriptAjax = '/scripts/ajax/censure_photo.php' ;
	xhr.open("POST", scriptAjax, true) ;
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", params.length);
	xhr.setRequestHeader("Connection", "close")	
	xhr.send(params) ;
	} else {
		alert("Merci de lire et d'accepter la charte d'utilisation") ;
	}
}

function send_friend(pid)
{
	if(document.getElementById("read_chart_send").checked != false) {
	var xhr = get_xmlhttp() ;
	if(xhr==null) {
		return false ;
	}
	
	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			var state = xhr.responseXML.getElementsByTagName('state') ;
			if(state.item(0).getAttribute('value') == 1) {
				document.getElementById('send_form').style.display = 'none' ;
				document.getElementById('send_success').style.display = 'block' ;
			} else {
				document.getElementById('send_msg').innerHTML = state.item(0).getAttribute('msg') ;
			}
		}
	}
	var params = 'pid='+pid+'&my_email='+encodeURI(document.getElementById('my_email').value)+'&your_email='+encodeURI(document.getElementById('your_email').value)+'&comment='+encodeURI(document.getElementById('comment').value) ;
	/* Build get url */
	scriptAjax = '/scripts/ajax/send_photo.php' ;
	xhr.open("POST", scriptAjax, true) ;
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Content-length", params.length);
	xhr.setRequestHeader("Connection", "close")	
	xhr.send(params) ;
	} else {
		alert("Merci de lire et d'accepter la charte d'utilisation") ;
	}
}


function prepare_file_to_download(type, file)
{
	if(document.getElementById('read_chart_download').checked == true) {
	document.getElementById('download_msg').style.display = 'block' ;	
	var xhr = get_xmlhttp() ;
	if(xhr==null) {
		return false ;
	}
	
	xhr.onreadystatechange=function()
	{
		if(xhr.readyState==4 && xhr.status==200) {
			document.getElementById('download_msg').style.display = 'none' ;	
			var pInfos = xhr.responseXML.getElementsByTagName('photo') ;
			window.open('/uploads/prepared/'+pInfos.item(0).getAttribute('fname'),'_blank') ;
		}
	}
	
	/* Build get url */
	/* todo : Send in POST */
	scriptAjax = '/scripts/ajax/prepare_file_to_download.php?type='+type+'&pid='+file ;
	xhr.open("GET", scriptAjax, true) ;
	xhr.send(null) ;
	} else {
		alert("Merci de lire et d'accepter la charte d'utilisation") ;
	}

}

function go_next_photo()
{
	if(nbrPhotosPage == selectedThumb) {
		if(selectedNbrPage < nbrGalleryPages -1) {
			var nextId = parseInt(selectedNbrPage) + 1;
			get_pages_thumbs(document.getElementById(nextId)) ; 
		}	
	} else {
		set_photo(selectedThumb) 
	}
	
}

/* Cette fonction recupere */
function get_random_photo()
{
	var preLoad1 = document.getElementById('photo_preload1') ;
	var preLoad2 = document.getElementById('photo_preload2') ;
	var photoView = document.getElementById('photo_view') ;

	var xhr1 = get_xmlhttp() ;
	var xhr2 = get_xmlhttp() ;
	var xhrv = get_xmlhttp() ;
	
	var clientWidth = screen.width ;
	var clientHeight = screen.height ;
		
	var scriptAjax = '/scripts/ajax/get_random_photo.php?w='+clientWidth+'&h='+clientHeight ;
		
	xhr1.onreadystatechange=function()
	{
		if(xhr1.readyState==4 && xhr1.status==200) {
			preLoad1.innerHTML = xhr1.responseText ;
		}
	}
	xhr2.onreadystatechange=function()
	{
		if(xhr2.readyState==4 && xhr2.status==200) {
			preLoad2.innerHTML = xhr2.responseText ;
		}
	}
	xhrv.onreadystatechange=function()
	{
		if(xhrv.readyState==4 && xhrv.status==200) {
			photoView.innerHTML = xhrv.responseText ;
		}
	}
	
	if(galStarted) {
		galStarted = false ;
		xhr1.open("GET", scriptAjax, true) ;
		xhr1.send(null) ;
		xhr2.open("GET", scriptAjax, true) ;
		xhr2.send(null) ;
		xhrv.open("GET", scriptAjax, true) ;
		xhrv.send(null) ;
	} else {
		photoView.innerHTML = preLoad2.innerHTML ;
		preLoad2.innerHTML = preLoad1.innerHTML ;
		xhr1.open("GET", scriptAjax, true) ;
		xhr1.send(null) ;
	}	
	
	setTimeout("get_random_photo()", 7000) ;
}

