﻿// Fichier JScript

var OBJPhotos = Class.create()
OBJPhotos.prototype = {
	initialize:function(){
		this.photos = new Array()
		this.setImgTemp = function(p){
			emptyBox('imgTemp');
			var i = p.image.cloneNode(true)
			$('imgTemp').appendChild(i)
		}
		
		this.goupe = ''
		this.groupeType='' //0 --> Groupe Normal --> Groupe topoupas
		
		this.setGrandePhoto = function(p){
		    if (p.profil.etat < 2) document.location='fr/default_ProfilNonValide.aspx'
			emptyBox('photo');
			emptyBox('marqueurTomates');
			$('animtomate').style.display='none'
			var i = p.image.cloneNode(true)
			i.id = 'princImg'
			$('photo').appendChild(i)
			p.image.width=i.clientWidth
			p.image.height=i.clientHeight
			$('linkTransmettre').href='fr/vote/transmettreprofil.aspx?pk=' + p.profil.mediapk
			$('linkTomate').href='fr/vote/sendTomates.aspx?pk=' + p.profil.mediapk
			$('linkDenoncer').href='fr/contact/denoncer_photo.aspx?mediapk=' + p.profil.mediapk
			if (p.profil.kdos){
				var t = p.getKdo('Tomates')
				Event.observe('princImg', 'load', function(e){setTomate(t)}, true)
			}
		}
	
		this.setPetitePhoto = function(p){
			$('moyenne').style.display='block'
			emptyBox('petitePhoto');
			$('petitePhoto').appendChild(resizeImg(p.image,120,0))
			$('nbvote').innerHTML=p.profil.nb
			$('avg').innerHTML = p.profil.avg
			$('depuis').innerHTML=p.profil.depuis
			$('linkTransmettre1').href='fr/vote/transmettreprofil.aspx?pk=' + p.profil.mediapk
			
		}
		
		this.display = function(){ //Affiche les images dans les différents container en fonction de leur place dans le tableau
			//if (this.photos.length==1){this.setImgTemp(this.photos[0]);return}
			if (this.photos.length==2){this.setGrandePhoto(this.photos[1]);return}
			if (this.photos.length==3){
				//this.setImgTemp(this.photos[0])
				this.setGrandePhoto(this.photos[1])
				this.setPetitePhoto(this.photos[2])
				return
			}
		}
	},
	
	add:function(profilPk){
		var o = new OBJPhoto(profilPk, this.groupe, this.groupeType)
		this.photos.unshift(o)		
		if (this.photos.length > 3) this.photos.pop()
		this.display()
	},
	setNote:function(note){this.photos[1].setNote(note)}
}

var OBJPhoto = Class.create()
OBJPhoto.prototype={
	initialize:function(profilPk, groupePk, groupeType){
		var params = 'action=1' + '&sexe=' + $F('sexe') + '&age=' + $F('age')
		if (profilPk) params += '&pfk=' + profilPk
		if (groupePk){params += (groupeType == '0')?'&gpk=':'&phfk='; params += groupePk}

		var a = new Ajax.Request('voter.ajax',{
			method:'post',
			postBody:params,
			onException:function(o, err){alert(err)},
			asynchronous:false,
			onLoading: function(el){document.body.style.cursor='wait'}
		})
		
		this.profil = eval(a.transport.responseText)
		this.image = new Image()
		this.image.src = 'getPhoto.file?pk=' + this.profil.mediapk
		this.image.alt = 'TopOuPas'
	},
	setNote:function(note){
		var a = new Ajax.Request('voter.ajax', {method:'post',
											postBody:'action=2&mediapk=' + this.profil.mediapk + '&note=' + note,
											asynchronous:false})
	    
	    var el = eval(a.transport.responseText)
	    if (!el.result){alert(ex.message);retrun}
	    if (el.count<0) alert('Vous ne pouvez pas voter pour le même profil dans un laps de temps inférieur à 6 heures.\nVotre n\'a pas été pris en compte.')
	},
	hasKdo:function(nomKdo){this.profil.kdos.each(function(o){if(o.nom==nomKdo) return true});return false},
	getKdo:function(nomKdo){
		var a = new Array();
		this.profil.kdos.each(function(o){if(o.nom==nomKdo) a.push(o)})
		return a
	}
}

function initNotes(){
	notes = $A(document.getElementsByName('note'))
   	notes.each(function(el){el.checked=false})
} 

function setNote(bidule){
	// Traite le clic sur une note.
	var tg = Event.element(bidule)
	if (tg.tagName=='A') {listePhotos.add();return}
	listePhotos.setNote(tg.value)
	listePhotos.add()
	window.setTimeout('initNotes()', 200)
}

function setTomate(kdos){
	
	kdos.each(function(o){
		switch(o.delais){
			case'compteur': 
				var y = (parseInt(o.nombre) <= 5)?o.nombre:5;
				var im = new Image()
				im.src='images/ong_noirorangeG.gif'
				$('marqueurTomates').appendChild(im)
				for (var i=1;i<=y; i++){
					im = new Image()
					im.src = 'images/ico_tomate.gif'
					$('marqueurTomates').appendChild(im)
				}
				break;
			case'expire':
				var d = CreateFlash2('animtomate', 'fla/tomate.swf', '');
    			//document.body.appendChild(d)
    			d.style.display='block'
				resizeTomate()
				Event.stopObserving('princImg', 'load', setTomate)
				break;

		}
	})
}