
Haedus.Fnc.Voting = {
	
	watch : new Array,
	voting : null,
	vote : null,
	
	setEvent : function(container, vote) {
		
		var voting = Haedus.getDom(container);
		this.voting = voting;
		this.vote = Haedus.getDom(vote);
		
		if (null === voting || null === this.vote || 'voted' === voting.className) {
			return false;
		}
		
		if (document.cookie) {
			
			cookies = document.cookie.split(';');
			
			for (i = 0; i < cookies.length; i++) {
				
				if (cookies[i].match(/votingC/)) {
					
					cookie = cookies[i].split(/=/)[1].split(/|/);
					
					if (cookie[0] == voting.firstChild.id.replace(/\D/, "")) {
						
						this.vote.parentNode.innerHTML = '<img src="' + webaddress + '/website/std/lay/vote_confirm.jpg" border="0" class="voted" alt="Sorry, Du hast dein Vote bereits abgegeben.">';
						return false;
						
					}
					
				}
				
			}
			
		}
		
		Haedus.Event.addEvent(
			this.vote,
			'submit',
			Haedus.Fnc.Voting.sendVote,
			false
		);
		
		images = voting.getElementsByTagName('img');
		
		for (i = 0; i < images.length; i++) {
			
			this.watch.push(images[i]);
			
			Haedus.Event.addEvent(
				images[i],
				'mouseover',
				Haedus.Fnc.Voting.mouseover,
				false
			);
			
			Haedus.Event.addEvent(
				images[i],
				'mouseout',
				Haedus.Fnc.Voting.mouseout,
				false
			);
			
			Haedus.Event.addEvent(
				images[i],
				'click',
				Haedus.Fnc.Voting.click,
				false
			);
			
		}
		
	},
	
	clear : function() {
		
		Haedus.Event.rmvEvent(
			this.vote,
			'submit',
			Haedus.Fnc.Voting.sendVote,
			false
		);
		
		for (i = 0; i < this.watch.length; i++) {
			
			Haedus.Event.rmvEvent(
				this.watch[i],
				'mouseover',
				Haedus.Fnc.Voting.mouseover,
				false
			);
			
			Haedus.Event.rmvEvent(
				this.watch[i],
				'mouseout',
				Haedus.Fnc.Voting.mouseout,
				false
			);
			
			Haedus.Event.rmvEvent(
				this.watch[i],
				'click',
				Haedus.Fnc.Voting.click,
				false
			);
			
		}
		
		this.watch = new Array;
		
	},
	
	mouseover : function(e) {
		
		element = Haedus.Event.getTarget(Haedus.Event.getEvent(e), 'IMG');
		
		var selected = true;
		
		for (i = 0; i < element.parentNode.childNodes.length; i++) {
			
			switch (selected) {
				
				case true:
					element.parentNode.childNodes[i].src = element.parentNode.childNodes[i].src.replace(/icon_star\.gif/g, "icon_star_active.gif");
					break;
				
				case false:
					element.parentNode.childNodes[i].src = element.parentNode.childNodes[i].src.replace(/icon_star_active\.gif/g, "icon_star.gif");
					break;
				
			}
			
			if (element.parentNode.childNodes[i] == element) {
				selected = false;
			}
			
		}
		
	},
	
	mouseout : function(e) {
		
		element = Haedus.Event.getTarget(Haedus.Event.getEvent(e), 'IMG');
		
		var current = 0;
		
		if (element.parentNode.className.match(/selected/g)) {
			current = element.parentNode.className.replace(/\D/g, "");
		}
		
		for (i = 0; i < element.parentNode.childNodes.length; i++) {
			
			element.parentNode.childNodes[i].src	=	((i < current)
															?	element.parentNode.childNodes[i].src.replace(/icon_star\.gif/g, "icon_star_active.gif")
															:	element.parentNode.childNodes[i].src.replace(/icon_star_active\.gif/g, "icon_star.gif"));
			
		}
		
	},
	
	click : function(e) {
		
		element = Haedus.Event.getTarget(Haedus.Event.getEvent(e), 'IMG');
		
		var rating = 1;
		
		for (i = 0; i < element.parentNode.childNodes.length; i++) {
			
			if (element.parentNode.childNodes[i] == element) {
				break;
			}
			
			rating++;
			
		}
		
		element.parentNode.className = 'selected-' + rating;
		
	},
	
	sendVote : function(e) {
		
		if (e.preventDefault) {
			e.preventDefault()
		} else {
			e.returnValue = false;
		}
		
		var list = Haedus.Fnc.Voting.voting.getElementsByTagName('li');
		
		var votes = new Array;
		var counter = 0;
		for (i = 0; i < list.length; i++) {
			
			rating = 0;
			for (j = 0; j < list[i].firstChild.childNodes.length; j++) {
				
				if (list[i].firstChild.childNodes[j].src.match(/active\.gif/)) {
					rating = (j+1);
				}
				
			}
			
			votes.push('song[' + list[i].firstChild.id.replace(/\D/g, "") + ']=' + rating);
			
			if (0 == rating) {
				counter++;
			}
			
		}
		
		if (0 < counter) {
			
			if (!confirm("ACHTUNG! " + counter + " Songs wurde nicht Bewertet!\nSoll dennoch das Voting abgegeben werden? - Bitte bedenkt das Ihr nur einmal Voten könnt!")) {
				return false;
			}
			
		}
		
		if (1==2) {
			//mail = Haedus.getDom('voting_e-mail').value.match(/^([0-9a-zA-Z]+([-_\.]?[0-9a-zA-Z])+@[0-9a-zA-Z]+([-_\.]?[0-9a-zA-Z])+\.[a-z]{2,4})$/);
		}
		
		document.getElementById('ajax-loading').style.display = 'block';
		
		Ajax_SendRequest('GET', webaddress + '/ajax.php?page=' + Haedus.Fnc.Voting.vote.action.replace(webaddress, "") + '&do=vote&voting=' + Haedus.Fnc.Voting.voting.firstChild.id.replace(/\D/g, "") + '&email=' + Haedus.getDom('voting_e-mail').value + '&' + votes.join('&'));
		
		return false;
		
	},
	
	handleResult : function(xml) {
		
		if ('' == xml.getElementsByTagName('content').item(0).getAttribute('param')) {
			return false;
		}
		
		var expires = new Date( new Date().setDate( new Date().getDate() + 30) ).toGMTString();
		var votingC = xml.getElementsByTagName('content').item(0).getAttribute('param');
		document.cookie = votingC + '; expires=' + expires;
		
	}
	
};
