/**
* general.js
*
* Auteur: Nicolas JARRAUD
* Date de création: 07.06.2008
* Dernière modification: 26.01.2009
*/

function getPageSize() {
	var xScroll, yScroll;
		
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
		
	var windowWidth, windowHeight;
		
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
			windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
		
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

/*********************************************************
/*	  				   			CALENDRIER
/********************************************************/
function display_events(day, month, year) {
	var arrayPageSize = getPageSize();
	
	$('overlay').setStyle({width: arrayPageSize[0]+"px",
						   height: arrayPageSize[1]+"px",
						   display: "block"
	});
	
	var url = url_relative+'includes/read_events.ajax.php';
	new Ajax.Request(url, {
		method: 'post',
		postBody: $H({day: day,
					  month: month,
					  year: year}).toQueryString(),
		onSuccess: function(transport) {
			JSONdata = transport.responseText.evalJSON();
			
			txt  = '<div id="calendar_window" style="left:'+(85+(arrayPageSize[0]-450)/2)+'px">';
			txt += '<div id="calendar_content">';
			for(i=0;i<JSONdata.events.length;i++) {
				txt += '<img src="'+url_relative+'template/images/calendar-arrow.gif" class="calendar_arrow">';
				txt += '<div class="calendar_event">'+JSONdata.events[i].content+'</div>';
				txt += '<br style="clear:both;">';	
			}
			txt += '</div>'; 
			txt += '<div id="calendar_date">'+JSONdata.stringDate+'</div>'; 
			txt += '<img src="'+url_relative+'template/images/calendar-close.png" id="close_calendar" onClick="close_opaque();">';
			txt += '</div>';
	
			new Insertion.After('overlay', txt);
		}
	});
}

function close_opaque() {
	$('calendar_window').remove();
	$('overlay').style.display = "none";
}

function create_calendar(month, year) {
	var url = url_relative+'includes/create_calendar.ajax.php';
	new Ajax.Request(url, {
		method: 'post',
		postBody: $H({month: month,
					  year: year}).toQueryString(),
		onSuccess: function(transport) {
			JSONdata = transport.responseText.evalJSON();
			
			nb_jour = parseInt(JSONdata.nb_jour);
			premier_jour = parseInt(JSONdata.premier_jour);
			
			jour_semaine = premier_jour;
			numero_jour = 1;
			txt  = '<table border=0 cellpadding=0 cellspacing=0 class="calendar_tab">';
			txt += '<tr class="calendar_label" height=15>';
			txt += '<td width=22 align=center>Lu</td><td width=21 align=center>Ma</td><td width=22 align=center>Me</td>';
			txt += '<td width=21 align=center>Je</td>';
			txt += '<td width=22 align=center>Ve</td><td width=21 align=center>Sa</td><td width=21 align=center>Di</td>';
			txt += '</tr>';
			nombre_ligne = Math.ceil((nb_jour + (premier_jour - 1)) / 7);
			for(i=1;i<=nombre_ligne;i++) {
				txt += '<tr height=15>';
				for(j=1;j<=7;j++) {
					txt +="<td height=10 align=center>"
					if(j == jour_semaine && numero_jour <= nb_jour) {
						if(numero_jour < 10) txtJour = "0"+numero_jour;
						else txtJour = numero_jour;
						
						if(JSONdata.jours[numero_jour-1] == '1') {
							txt += '<span style="color:#9f0090;font-weight:bold;cursor:pointer;"';
							txt += ' onClick="display_events('+numero_jour+','+month+','+year+');">';
							txt += txtJour;
							txt += '</span>';
						} else txt += txtJour;
						
						if(jour_semaine == 7) jour_semaine = 1;
						else jour_semaine++;
						
						numero_jour++;
					}
					txt += '</td>';
				}
				txt += '</tr>';
			}
			txt += '</table>';
			
			if(month < 12) {
				nextMonth = month + 1;
				nextYear = year;
			} else {
				nextMonth = 1;
				nextYear = year + 1;
			}
			
			if(month < 2) {
				prevMonth = 12;
				prevYear = year - 1;
			} else {
				prevMonth = month - 1;
				prevYear = year;
			}
			
			txt += '<img src="'+url_relative+'template/images/calendar-down.jpg" style="float:left;margin-left:5px;cursor:pointer"';
			txt += ' onClick="create_calendar('+prevMonth+','+prevYear+')">';
			txt += '<div id="calendar_legend">'+JSONdata.mois+' '+year+'</div>';
			txt += '<img src="'+url_relative+'template/images/calendar-up.jpg" style="float:left;cursor:pointer"';
			txt += ' onClick="create_calendar('+nextMonth+','+nextYear+')">';
			txt += '<br style="clear:both;">';
			
			$('calendar').update(txt); 
		}	
	});
}

/*********************************************************
/*	  				  			 SPONSORS
/********************************************************/
function display_sponsors(page) {
	var url = url_relative+'includes/read_sponsors.ajax.php';
	new Ajax.Request(url, {
		method: 'post',
		postBody: $H({page: page}).toQueryString(),
		onSuccess: function(transport) {
			JSONdata = transport.responseText.evalJSON();
			
			txt = '';
			for(i=0;i<JSONdata.sponsors.length;i++) {
				txt += '<div class="bd-sp-list">';
				txt += '<div><a href="'+JSONdata.sponsors[i].link+'" target=_blank>'+JSONdata.sponsors[i].name+'</a></div>';
				if(JSONdata.sponsors[i].address_1 != '') txt += '<div class="bd-sp-address">'+JSONdata.sponsors[i].address_1+'</div>';
				if(JSONdata.sponsors[i].address_2 != '') txt += '<div class="bd-sp-address">'+JSONdata.sponsors[i].address_2+'</div>';
				if(JSONdata.sponsors[i].address_3 != '') txt += '<div class="bd-sp-address">'+JSONdata.sponsors[i].address_3+'</div>';
				txt += '</div>';
			}
			txt += '<div style="width:140px;margin-top:8px;">';
			txt += '<img src="'+url_relative+'template/images/sponsors_down.jpg" style="float:right;cursor:pointer"';
			txt += ' onClick="display_sponsors('+JSONdata.page_down+')">';
			txt += '<img src="'+url_relative+'template/images/sponsors_up.jpg" style="float:right;cursor:pointer"';
			txt += ' onClick="display_sponsors('+JSONdata.page_up+')">';
			txt += '<br style="clear:both">';
			txt += '</div>';
			$('bd-sponsors').update(txt);
		}
	});
}

/*********************************************************
/*	  				   			 VIDEOS
/********************************************************/
function del_video_comment(video_id, com_id) {
	var url = url_relative+'includes/delete_video_comment.ajax.php';
	new Ajax.Request(url, {
		method: 'post',
		postBody: $H({com_id: com_id,
					  video_id: video_id}).toQueryString(),
		onSuccess: function(transport) {
			window.location.href = window.location;
		}
	});
}

function check_nickname() {
	if($('video-comNN').value == "Votre surnom ici... (20 caractères maximum)") $('video-comNN').value = "";
}

/*********************************************************
/*	  				    		NAVIGATION
/********************************************************/
function navCat_on(num, url) {
	$('nav-'+num).setStyle({color:'white',fontWeight:'bold',background:'url('+url+'template/images/nav-BG2.gif)'});
}

function navCat_off(num, url) {
	$('nav-'+num).setStyle({color:'black',fontWeight:'normal',background:'url('+url+'template/images/nav-BG1.gif)'});
}
