// image preview
var ImagePreview  = {
	
	defaultHeight:	244,
	
	init:		function(profilePicture)	{
		this.profilePicture = profilePicture;
		this.profilePictureContext = profilePicture + '_context';		// handles the mouseover/out stuff
		$(this.profilePictureContext).onmouseover = this.mouseOver.bind(this);
		$(this.profilePictureContext).onmouseout = this.mouseOut.bind(this);
		
		enablePngTransparency('profileImageBorder_context','/img/imageMagnifier.png');
		$('profilePicturePreview').addEvent('load', this.mainImageLoaded.bind(this));

	},
	
	mainImageLoaded: 	function()	{
		if ( $('profilePicturePreview').offsetHeight > 244)	{
			$('profileImageBorder_context').setStyle('display','block');
		}
		/*
		var height = $('profilePicturePreview').offsetHeight;
		var width = $('profilePicturePreview').offsetWidth;
		var newWidth = 325;
		var newHeight = ( newWidth / width ) * height; 
		$('profilePicturePreview').height = newHeight;
		$('profilePicturePreview').width = newWidth;
		*/
		
	},
	
	mouseOver:	function()	{
		var height = $('profilePicturePreview').scrollHeight;
		if(height <= this.defaultHeight)	return;	
		$('profileImageBorder').setStyle('height','auto');
	},
	mouseOut: function()	{
		$('profileImageBorder').setStyle('height',this.defaultHeight + 'px');
	}
};


// voting
var voting = {

	match: false,
	myVote: myVote,
	otherVote: otherVote,
	otherSex: otherSex,
	heartClass: '',
	defaultHeartClass: null,
	ajaxInProgress: false,
	animationStatus: 0,
	animationSpeed: 200,
	animation: false,
	
	onReady:	function()	{
		this.imageYes = new Image();
		this.imageYes.src = '/img/voteButtonYes.gif';
		this.imageYesI = new Image();
		this.imageYesI.src = '/img/voteButtonYes_i.gif';
		this.imageNo = new Image();
		this.imageNo.src = '/img/voteButtonNo.gif';
		this.imageNoI = new Image();
		this.imageNoI.src = '/img/voteButtonNo_i.gif';
		
		//this.imageHearts = new Image();
		//this.imageHearts.src = '/img/voteStatusMatch.gif';
		//this.imageHeartsI = new Image();
		//this.imageHeartsI.src = '/img/voteStatusNoVotes.gif'
		
		
		// default class for hearts
		if (this.myVote == 2 && this.otherVote == 2)	{
			this.match = true;
		};
	
		// set required styles
		$('profileVotingMatch').setStyle('overflow','hidden');
		$('profileVotingVote').setStyle('overflow','hidden');
		
		
		// set up click/mouseover stuff
		if (!outsideView)	{
			$('voteButtonYes').onmouseover = this.mouseOverYes.bind(this);
			$('voteButtonNo').onmouseover = this.mouseOverNo.bind(this);
			$('voteButtonYes').onmouseout = this.mouseOutYes.bind(this);
			$('voteButtonNo').onmouseout = this.mouseOutNo.bind(this);
			$('voteButtonYes').onclick = this.mouseClickYes.bind(this);
			$('voteButtonNo').onclick = this.mouseClickNo.bind(this);
			$('profileVotingMatchDelete').onclick = this.mouseClickNo.bind(this);	// match delete link	
		} else {
			// outer stuff
			$('voteButtonYes').onmouseover = this.mouseOverYes.bind(this);
			$('voteButtonNo').onmouseover = this.mouseOverNo.bind(this);
			$('voteButtonYes').onmouseout = this.mouseOutYes.bind(this);
			$('voteButtonNo').onmouseout = this.mouseOutNo.bind(this);
			$('voteButtonYes').onclick = welcome.showSignUp.bind(welcome);
			$('voteButtonNo').onclick = this.mouseClickNo.bind(this);
		}
		
		
	},
	
	mouseOverYes:	function()	{
		if (this.ajaxInProgress)	return;
		
		// higlight yes button if vote not already yes
		if (this.myVote != 2)	$('voteButtonYes').src = this.imageYes.src;
		
	},
	
	mouseOverNo:	function()	{
		if (this.ajaxInProgress)	return;
		
		// higlight no, if not already no 
		if (this.myVote != 1)	$('voteButtonNo').src = this.imageNo.src;	
		
	},
	
	mouseOutYes:	function()	{
		if (this.ajaxInProgress)	return;
		
		// reset if not already yes
		if (this.myVote != 2)	$('voteButtonYes').src = this.imageYesI.src;
		
	},
	
	mouseOutNo:		function()	{
		if (this.ajaxInProgress)	return;
		
		// reset if not already no 
		if (this.myVote != 1)	$('voteButtonNo').src = this.imageNoI.src;
	},
	
	mouseClickYes:	function()	{
		if (this.ajaxInProgress)	return;
		// check if vote was not already yes
		if (this.myVote == 2)	{
			this.checkAutomaticNextProfile();
			return;
		}
		this.storeVote(2);
	},
	
	mouseClickNo:	function()	{
		if (this.ajaxInProgress)	return false;
		if (this.myVote == 1)	{
			this.checkAutomaticNextProfile();
			return false;
		}
		this.storeVote(1);
		return false;
	},
	
	storeVote:		function(vote)	{
		// prevent more clicks
		this.ajaxInProgress = true;
		
		// start animation
		this.animationStatus = 1;
		this.animation = true;
		this.animationVote = vote;
		
		// remove old vote button
		if (vote == 2)	{
			$('voteButtonNo').src = this.imageNoI.src;
		} else if (vote == 1) {
			$('voteButtonYes').src = this.imageYesI.src;
		}
		
		// show loading animation on match delete
		if (vote == 1 && this.otherVote == 2 && this.myVote == 2 )	{
			$('profileVotingMatchAnimation').setStyle('display','inline');
		}
		
		this.animate.delay(100, this);
		
		// store vote via ajax
		this.ajaxVote = vote;
		if (!outsideView)	{
			// var url = '/ajax/vote/vote/' + vote + '/userID/' + userID + '/?' + SID;
			var url = '/ajax/vote/?' + SID;
			var postBody = { vote: vote, userID: userID };
			var options = { postBody: postBody, onComplete: this.ajaxComplete.bind(this) };
			var ajax = new Ajax(url, options).request();	
		} else {
			this.ajaxComplete.bind(this).delay(1000);
		}
		
		
		
		
	},
	
	hideMatch:		function()	{
		$('profileVotingMatch').setStyle('display','none');
		$('profileVotingVote').setStyle('height','auto');
	},
	
	showMatchDiv:	function()	{
		$('profileVotingMatch').setStyle('display','block');
		$('profileVotingVote').setStyle('display','none');
		$('profileVotingMatch').effect('height', {duration: 200}).custom(0, $('profileVotingMatch').scrollHeight);
		
		this.checkAutomaticNextProfile.bind(this).delay(100);
		
		
	},
	
	showVoteButtons:	function()	{
		$('profileVotingMatch').setStyle('display','none');
		$('profileVotingVote').setStyle('display','block');
		$('profileVotingVote').effect('height', {duration: 200}).custom(0, $('profileVotingVote').scrollHeight);		
		
		this.checkAutomaticNextProfile.bind(this).delay(100);
	},
			
	ajaxComplete:	function(request)	{
		var vote = this.ajaxVote;
		this.animation = false;
		
		var oldVote = this.myVote;
		
		// set yes no
		if (vote == 1)	{
			$('voteButtonNo').src = this.imageNo.src;
		} else {
			$('voteButtonYes').src = this.imageYes.src;
		}
		this.myVote = vote;
		
		// check for match? 
		this.match = false;
		if (this.myVote == 2 && this.otherVote == 2)	{
			// hide buttons and show match div
			this.match = true;
			$('profileVotingMatchAnimation').setStyle('display','none');
			$('profileVotingMatch').setStyle('height',0);
			$('profileVotingVote').effect('height', {duration: 200, onComplete: this.showMatchDiv.bind(this)}).custom($('profileVotingVote').offsetHeight, 0);
		} else if (oldVote == 2 && this.otherVote == 2 && this.myVote == 1)	{
			// hide match div and show buttons
			
			// show half match icon		
			$('voteStatus').src = '/img/voteHalfMatch_sex' + this.otherSex + '.gif';
			$('voteStatus').title = 'JA Vote für Dich :: Dieses Mitglied hat Dich mit JA bewertet';
			// show layer
			$('profileVotingVote').setStyle('height',0);
			$('profileVotingMatch').effect('height', {duration: 200, onComplete: this.showVoteButtons.bind(this)}).custom($('profileVotingMatch').offsetHeight, 0);
		} else if (this.myVote == 2 && this.otherVote != 2)	{
			// show fav icon
			$('voteStatus').src = '/img/voteStatusFav.gif';
			$('voteStatus').title = 'Favorit :: Du hast dieses Mitglied mit JA bewertet';
			this.checkAutomaticNextProfile.bind(this).delay(100);
		} else if (this.myVote == 1 && this.otherVote != 2)	{
			// show nothing
			$('voteStatus').src = '/img/transparent.gif';
			$('voteStatus').title = '';
			this.checkAutomaticNextProfile.bind(this).delay(100);
		} else {
			this.checkAutomaticNextProfile.bind(this).delay(100);
		}
		
		
		// reactive buttons
		this.ajaxInProgress = false;
		
	},
	
	animate:	function()	{
		if (!this.animation)	return;
		
		
		if (!this.match)	{
			// animate buttons
			if (this.animationVote == 1)	{
				// toggle no button
				if (this.animationStatus == 1)	{
					$('voteButtonNo').src = this.imageNoI.src;
				} else {
					$('voteButtonNo').src = this.imageNo.src;
				};
				
			} else {
				// toggle yes button
				if (this.animationStatus == 1)	{
					$('voteButtonYes').src = this.imageYesI.src;
				} else {
					$('voteButtonYes').src = this.imageYes.src;
				};
			}
				
		} else {
			// nope
			
		}
		
		this.animationStatus++;
		if (this.animationStatus == 3)	this.animationStatus = 1;
			
		
		// call yourself
		this.animate.delay(200, this);
		
		
	},
	
	checkAutomaticNextProfile:	function()	{
		if (($('profileNextVoteCheckbox').checked))	{
			this.showNextProfile();
		} 
	},
	
	showNextProfile: function()	{
		if (lastPos == 1)	return;
		if ($('profileNextVoteCheckbox').checked)	this.nextProfileChecked = 1;		
		var url = '/search/nextProfile/pos/' + searchPos + '/checked/' + this.nextProfileChecked +  '/?' + SID;
		document.location = url;
	},
	
	showPreviousProfile: function()	{
		var newPos = searchPos - 2;
		if (newPos < -1 )	return false;
		if ($('profileNextVoteCheckbox').checked)	this.nextProfileChecked = 1;		
		var url = '/search/nextProfile/pos/' + (newPos) + '/checked/' + this.nextProfileChecked +  '/?' + SID;
		document.location = url;
	},
	
	nextVoteCheckboxClick: function()	{
		return;  
	}
	
	
};
window.onDomReady(voting.onReady.bind(voting));
