Cufon.replace('h2, #vote .latest h3, #gig h3, #gig .goto a');
$(document).ready(function(){

	map();
	validate();
	
	$('#nav-vote, #nav-apply, #nav-win').hover(function(){
	
		$(this).children('img').stop(true,false).show().fadeTo('fast',1);
	
	},function(){

		$(this).children('img').stop(true,false).fadeTo('slow',0,function(){$(this).hide();});
	
	}).children('img').css('opacity',0);

	$('a.icon').css('opacity',0.6).hover(function(){
	
		$(this).stop(true,false).fadeTo('fast',1);
	
	},function(){
	
		$(this).stop(true,false).fadeTo('slow',0.6);
	
	});

});

function map(){

	$('.city-info .inner').append('<a href="" class="closer">close</a>');

	$('.closer').click(function(){
	
		$('#maplist a').removeClass('selected')
	
		$('.city-info:visible').stop(true,true).animate({'opacity':0,'top':'200px'},400,'swing',function(){$('.city-info').hide();});
		
		return false;	
	
	});

	$('#maplist a').click(function(){
	
		$('#maplist a').removeClass('selected').filter(this).addClass('selected');
	
		var newCnt = $(this).attr('href');	
	
		$('.city-info:visible').stop(true,true).not(newCnt).animate({'opacity':0,'top':'200px'},400,'swing',function(){$(this).hide();});
		$(newCnt).stop(true,true).not(':visible').css({'top':'50px','opacity':0}).show().animate({'top':'100px','opacity':1},600,'swing');				
	
		return false;
	
	});

}


function validate(){


	$('form').submit(function(){
	
		//trigger change event on required elements. then check for invalid values
		$(this).find("[required='required']").change();
		if($(this).find('.invalid').length) { $(this).find('.invalid').shakeup(); return false; } else { return true; }
	
	});

	$('select').filter("[required='required']").change(function(){
	
		if($(this).val() != 0) { $(this).parent().removeClass('invalid'); } else { $(this).parent().addClass('invalid'); } 
	
	});

	$('input[type=text]').filter("[required='required']").change(function(){
		
		if($(this).val()) { $(this).parent().removeClass('invalid'); } else { $(this).parent().addClass('invalid'); } 
	
	});

	$('input[type=email]').filter("[required='required']").change(function(){
	
		if(emailcheck($(this).val())) { $(this).parent().removeClass('invalid'); } else { $(this).parent().addClass('invalid'); } 
	
	});
	
	$('input[type=tel]').filter("[required='required']").change(function(){
	
		if($(this).val()) { $(this).parent().removeClass('invalid'); } else { $(this).parent().addClass('invalid'); } 
	
	});


}

$.fn.shakeup = function(){
	$(this).css('position','relative');
	$(this).animate({left:'+=2'},100,'swing',function(){
		$(this).animate({left:'-=4'},100,'swing',function(){
			$(this).animate({left:'+=4'},100,'swing',function(){
				$(this).animate({left:'-=4'},100,'swing',function(){
					$(this).animate({left:'+=4'},100,'swing',function(){
						$(this).animate({left:'-=2'},100,'swing');
					});
				});
			});
		});
	});
}


function emailcheck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
		
	if (str.indexOf(at)==-1){return false;}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false;}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false;}
	if (str.indexOf(at,(lat+1))!=-1){return false;}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false;}
	if (str.indexOf(dot,(lat+2))==-1){return false;}
	if (str.indexOf(" ")!=-1){return false;}

	return true;					
}
