/*
 * units.js | www.vasalopet.se 
 * http://www.vasalopet.se/
 *
 * Author: Johan Björtin | Daddy Produktion
 *
 * Date: 2009-02-27 12:14:00
 * Revision: 1
 */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	LANDING RACE FILER
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function landingRaceFilter (e)
{
	
	var settings = {};
	
	settings.numHidden = $('.landingRaceFilterList .item.hide').size();
	settings.contest	= ($('#landingRaceFilter_contest').val() != '-') ? $('#landingRaceFilter_contest').val() : null;
	settings.gender	= ($('#landingRaceFilter_gender').val() != '-') ? $('#landingRaceFilter_gender').val() : null;
	settings.age			= ($('#landingRaceFilter_age').val() != '-') ? $('#landingRaceFilter_age').val() : null;
	settings.season	= ($('#landingRaceFilter_season').val() != '-') ? $('#landingRaceFilter_season').val() : null;
	
	settings.cleanUp = function ()
	{
		$('.landingRaceFilterList .item.hide .shade').css({opacity: 0});
		settings.filter();
	}
	
	settings.filter = function ()
	{
		$('.landingRaceFilterList .item.hide .shade').hide();
		$('.landingRaceFilterList .item.hide').removeClass('hide');
		
		if(settings.contest || settings.gender || settings.age || settings.season)
		{
	
			if(settings.contest)
			{
				$('.landingRaceFilterList .item').each(
					function ()
					{
							if($('input[name=c][value*='+settings.contest+']', this).length == 0)
								$(this).addClass('hide');
					}
				);
			}
			
			
			if(settings.gender)
			{
				$('.landingRaceFilterList .item').each(
					function ()
					{
							if($('input[name=g][value*='+settings.gender+']', this).length == 0)
								$(this).addClass('hide');
					}
				);
			}
			
			if(settings.age)
			{
				$('.landingRaceFilterList .item').each(
					function ()
					{
							if($('input[name=a][value*='+settings.age+']', this).length == 0)
								$(this).addClass('hide');
					}
				);
			}
			
			if(settings.season)
			{
				$('.landingRaceFilterList .item').each(
					function ()
					{
							if($('input[name=s][value*='+settings.season+']', this).length == 0)
								$(this).addClass('hide');
					}
				);
			}
		}
		
		$('.landingRaceFilterList .item.hide .shade').show().fadeTo(300, 0.8);
	}
	
	
	
	if(settings.numHidden > 0)
		settings.cleanUp();
	else
		settings.filter();
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	TIP A FRIEND
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function tipAFriend (e)
{
	var pos = $(e.target).offset();
	
	switch(e.type)
	{
		case 'click' :
					
					$('#tipAFriend :input').not('button, textarea,hidden').each(
						function ()
						{
							this.value = this.alt;
							$(this).parent().parent().removeClass('error');
						}
					);
					$('#tipAFriend').css({top: pos.top - 235, left: pos.left}).show();
					$('#tipAFriend input:first').focus();
					break;
	}
	
	return false;
}




/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	TIP A FRIEND VALIDATION
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function tipAFriendValidation(e)
{
	
	switch (e.target.id)
	{
		case 'tipAFriend_targetEmail' :
					($(e.target).validate({type: 'email'})) ? $(e.target).parents('.inputText').removeClass('error') : $(e.target).parents('.inputText').addClass('error');
					break;
					
		case 'tipAFriend_name' :
					($(e.target).validate({type: 'notEmpty'})) ? $(e.target).parents('.inputText').removeClass('error') : $(e.target).parents('.inputText').addClass('error');
					break;
					
		case 'tipAFriend_senderEmail' :
					($(e.target).validate({type: 'email'})) ? $(e.target).parents('.inputText').removeClass('error') : $(e.target).parents('.inputText').addClass('error');
					break;
					
		case 'tipAFriend_submit' : 
					
					var recipient = false;
					var sender_name = false;
					var sender_email = false;
					
					if($('#tipAFriend_targetEmail').validate({type: 'email'}))
					{
						$('#tipAFriend_targetEmail').parents('.inputText').removeClass('error');
						recipient = true;
					}
					else
					{
						$('#tipAFriend_targetEmail').parents('.inputText').addClass('error');
					}
					
					
					if($('#tipAFriend_name').validate({type: 'notEmpty'}))
					{
						$('#tipAFriend_name').parents('.inputText').removeClass('error');
						sender_name = true;
					}
					else
					{
						$('#tipAFriend_name').parents('.inputText').addClass('error');
					}
					
					
					if($('#tipAFriend_senderEmail').validate({type: 'email'}))
					{
						$('#tipAFriend_senderEmail').parents('.inputText').removeClass('error');
						sender_email = true;
					}
					else
					{
						$('#tipAFriend_senderEmail').parents('.inputText').addClass('error');
					}
					
					if(recipient && sender_name &&sender_email)
					{
						var elms = $('#tipAFriend :input').not('button');
						tipAFrinedPostData(elms);
					}
					else
						return false;
					break;
					
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	TIP A FRIEND POST DATA
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function tipAFrinedPostData (elms)
{
	var qry = '';
	
	$(elms).each(
		function (index)
		{
			qry += (index == 0) ? this.name+'='+this.value : '&'+this.name+'='+this.value;
		}
	);
	
	$.ajax({
   type: "GET",
   url: "/wb/MailerServlet",
   data: qry,
   success : function(msg)
	 					{
							$('#tipAFriend .tipAFriendSuccess .header').html("Ditt mail är påväg!");
     					$('#tipAFriend .tipAFriendStates').animate({top: 0}, 300, function () {$(this).animate({opacity: 1}, 2000, function () {$('#exit').trigger('click');});});
   					},
	 error: function (msg)
	 					{
							$('#tipAFriend .tipAFriendError .header').html("Ett fel inträffade, försök igen lite senare.");
							$('#tipAFriend .tipAFriendStates').animate({top: -400}, 300, function () {$(this).animate({opacity: 1}, 2000, function () {$(this).animate({top: -200}, 300);});});
						}
 });
	
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	PAGE FOOTER EMAIL VALIDATION
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function pageFooterEmailValidation (e)
{
	switch(e.target.id)
	{
		case 'newsletter' :
					($(e.target).validate({type: 'email'})) ? $(e.target).parents('.inputText').removeClass('error') : $(e.target).parents('.inputText').addClass('error');
					break;
			
		case 'newsletter_submit' :
					
					var newsletter = false;
					
					if($('#newsletter').validate({type: 'email'}))
					{
						$('#newsletter').parents('.inputText').removeClass('error');
						newsletter = true;
					}
					else
					{
						$('#newsletter').parents('.inputText').addClass('error');
					}
					
					return (newsletter) ? true : false;		
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	SELECT MAP POINT
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function selectMapPoint(pointId, offset) 
{
	
	var url = null;
	var data = null;
	var target = null;
	offset = (offset == null) ? 1 : offset;
	$('#currentDistance').val(pointId);
	
	
	if($('#resultListTable').length == 1)
	{
		url = "/Templates/Public/_dummy/map.json";
		data = "pointId="+pointId+'&offset='+offset;
		target = "resultListTable";
	}
	else if ($('#participantCompetitorsTable').length == 1)
	{
		url = "/Templates/Public/_dummy/map1.json";
		data = "pointId="+pointId;
		target = "participantCompetitorsTable";
	}
	
	$.ajax({
	type:			"GET",
	url:			url,
	data: 		data,
	dataType:	"json",
	error: 		function (res)
						{
							return false;
						},
	success: 	function (res)
						{
							processSelctMapPoint(res, target);
						}
	});

}

function processSelctMapPoint(res, target)
{
	
	targets = {};
	
	targets.resultListTable = function (res)
	{
		var table = $('#resultListTable');
		$('tr:has(td)', table).remove();
		$('th:first', table).html(res.header.position+' - '+res.header.span+' - '+res.header.contest+' - '+res.header.gender+' - '+res.header.distance+' - '+res.header.year);
		
		$.each(res.results, 
			function ()
			{
				table.append('<tr><td class="first">'+this.position+'</td><td>'+this.time+'</td><td><a href="'+this.link+'">'+this.firstName+' '+this.lastName+'</a></td><td class="last">'+this.club+'</td></tr>');
				$('tr:gt(1):odd', table).addClass('odd');
				$('tr:gt(1):even', table).addClass('even');
			}
		);
	}
	
	targets.participantCompetitorsTable = function (res)
	{
		var table = $('#participantCompetitorsTable');
		$('tr:has(td)', table).remove();
		$.each(res.results, 
			function ()
			{
				table.append('<tr><td class="first"><a href="'+this.link+'">'+this.firstName+' '+this.lastName+'</a></td><td>'+this.position+'</td><td>'+this.time+'</td><td>'+this.id+'</td><td class="last">'+this.club+'</td></tr>');
				$('tr:gt(1):odd', table).addClass('odd');
				$('tr:gt(1):even', table).addClass('even');
			}
		);
	}
	
	targets[target](res);
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	TO POSITION
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function toPosition (e)
{
	var pos = $(e.target).position();
	$('#toPosition').css({top: parseInt(pos.top)-70, left: parseInt(pos.left)-9});
	return false;
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	TOPLIST
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function topList (e)
{
	var gender = $('input[name=topList_gender]').val();
	var contest = $('#topList_contest').val();
	var year = $('#topList_year').val();
	var url = "/Templates/Public/_dummy/topList.json";
	var data = "gender="+gender+'&contest='+contest+'&year='+year;
	
	$.ajax({
	type:			"GET",
	url:			url,
	data: 		data,
	dataType:	"json",
	error: 		function (res)
						{
							return false;
						},
	success: 	function (res)
						{
							processTopList(res);
						}
	});
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	TOPLIST VALIDATION
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function topListValidation(e)
{
	switch (e.target.id)
	{
		case 'topList_contest' :
					var coresponding = $('#i_'+e.target.id).parents('.inputSelect');
					($(e.target).validate({type: 'not', value: '-'})) ? $(coresponding).removeClass('error') : $(coresponding).addClass('error');
					break;
					
		case 'topList_year' :
					var coresponding = $('#i_'+e.target.id).parents('.inputSelect');
					($(e.target).validate({type: 'not', value: '-'})) ? $(coresponding).removeClass('error') : $(coresponding).addClass('error');
					break;
					
		case 'topList_submit' :
					
					var topList_contest = true;
					var topList_year = true;
					
					if($("#topList_contest").validate({type: 'not', value: '-'}))
					{
						$(".inputSelect:has(#i_topList_contest)").removeClass('error');
					}
					else
					{
						$(".inputSelect:has(#i_topList_contest)").addClass('error');
						topList_contest = false;
					}
					
					if($("#topList_year").validate({type: 'not', value: '-'}))
						$(".inputSelect:has(#i_topList_year)").removeClass('error');
					else
					{
						$(".inputSelect:has(#i_topList_year)").addClass('error');
						topList_year = false;
					}
					
					if(topList_contest, topList_year)
					{
						topList();
					}
					else
					{
						return false;
					}
					
					break;
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	PROCESS TOPLIST
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function processTopList (res)
{
	var table = $('#topListTable');
	$('tr:has(td)', table).remove();
	
	$.each(res.results, function()
		{
			$(table).append('<tr><td class="first">'+this.position+'</td><td>'+this.time+'</td><td class="last"><a href="'+this.link+'">'+this.firstName+' '+this.lastName+'</a></td></tr>');
		}
	);
	$('tr:gt(0):odd', table).addClass('odd');
	$('tr:gt(0):even', table).addClass('even');
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	TO POSITION VALIDATE
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function toPositionValidate(e)
{
	switch(e.target.id)
	{
		case 'setPosition' :
								($(e.target).validate({type: 'numeric'})) ? $(e.target).parents('.inputText').removeClass('error') : $(e.target).parents('.inputText').addClass('error');
								break;
		
		case 'setPositionSubmit' :
					
					var toPosition = false;
					
					if($('#setPosition').validate({type: 'numeric'}))
					{
						$('#setPosition').parents('.inputText').removeClass('error');
						toPosition = true;
					}
					else
					{
						$('#setPosition').parents('.inputText').addClass('error');
					}
					
					if(toPosition)
					{
						selectMapPoint($('#currentDistance').val(), $('#setPosition').val());
						$('#toPosition').css({left: '-999em'});
					}
					else
						$('#setPosition').focus();		
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	VALIDATE POLL
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function validatePoll(e)
{
	var valid = ($('input[name=poll]:checked').length == 1) ? true : false;
	($('input[name=poll]:checked').length == 1) ? $('input[name=poll]').parent().removeClass('error')  : $('input[name=poll]').parent().addClass('error');
	(valid) ? viewPollResults(e) : null;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	VIEW POLL RESULTS
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function viewPollResults(e)
{
	
	var url = "/Templates/Public/_dummy/poll.json";
	var data = (e.target.id == 'poll_vote') ? "poll="+$('input[name=poll]:checked').val() : null;
	
	$.ajax({
	type:			"GET",
	url:			url,
	data:			data,
	dataType:	"json",
	error: 		function (res)
						{
							return false;
						},
	success: 	function (res)
						{
							var result = $('ul.results');
							$('li', result).remove();
							
							$.each(res.items,
								function (index)
								{
									result.append('<li class="clearfix"><div class="label">'+this.label+'</div><div class="bar clearfix"><div class="clearfix"><span style="width: '+parseInt(this.percent)+'%;"><span></span></span></div><strong class="percent">'+parseInt(this.percent)+'%</strong></div></li>');
								}
							);
							
							
							$('#pollResult').show();
							
						}
	});
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	EVENTS
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function unitsEventHandlers()
{
	$('#tipAFriendLink').bind('click', tipAFriend);
	$('#exit').bind('click', function (){$('#tipAFriend').hide(); return false;});
	$('#landingRaceFilter_contest').bind('change', landingRaceFilter);
	$('#landingRaceFilter_gender').bind('change', landingRaceFilter);
	$('#landingRaceFilter_age').bind('change', landingRaceFilter);
	$('#landingRaceFilter_season').bind('change', landingRaceFilter);
	$('#tipAFriend :text').bind('blur', tipAFriendValidation);
	$('#tipAFriend button').bind('click', tipAFriendValidation);
	$('#newsletter').bind('blur', pageFooterEmailValidation);
	$('#newsletter_submit').bind('click', pageFooterEmailValidation);
	$('.resultList .toPosition').bind('click', toPosition);
	$('.resultList #setPosition').bind('keyup', toPositionValidate);
	$('.resultList #setPositionSubmit').bind('click', toPositionValidate);
	$('#topList_submit').bind('click', topListValidation);
	$('.topList select').bind('change', topListValidation);
	$('#poll_vote').bind('click', validatePoll);
	$('#poll_results').bind('click', viewPollResults);
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	JQUERY CONSTRUCT
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
$(function () { unitsEventHandlers(); });