/* Author: 
	-KDD WCST
*/	

$(function(){
	
	
	var section			=		$('body').attr('class')
	,	drinks			=		[]
	,	fileType		=		'.html' //This can change to php once Toaster is in place
	,	currentView		=		0
	,	cNav			=		{}
	,	emailOptions	=		{	beforeSubmit : validate,	success	: showResp	}
	,	noPacity		=		$.support.opacity
	,	isLit			=		false
	,	popped			=		false
	,	audioUrl		=		'/player'
	;
	
//Global
	
	//Handle orientation-change glitch
	if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
  		var viewportmeta = document.querySelector('meta[name="viewport"]');
  		if (viewportmeta) {
    	    viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0';
   		    document.body.addEventListener('gesturestart', function() {
      	    	viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
    	    }, false);
	  	}
	}
	
	//Set active state on Main Nav
	//$('a.n-'+section).addClass(section+'-active');
	
	//Initialize - determine current section
	if( section == 'age-gate-env' ){
		
		initGate();
		
	}else if( section == 'where' ){
		
	
	}else if( section == 'how' ){
	
		initHow();
		
	
	}else if( section == 'cocktails' ){
	
		initCocktails();
	
	}
	
	//Handle Age Gate
	function initGate(){
	
		$('#age input').focus(function(evt){
			
			if( !isLit ){
				
				$('#age button').css({
					
					color : "#f06548"
					
				});
			
			}
			
			var that 	=	 $(this)
			,	val		=	that.attr('value')
			;
			
			if(that == 'MM' || 'DD' || 'YYYY'){
				
				that.attr('value','');
						
			}
			
		}).blur(function(){
		
			var _that 	= 	$(this)
			,	_valID	=	_that.attr('id')
			,	_valAT	=	_that.attr('value')
			;
			
			if(  _valID == 'age-month' &&  _valAT == ''){
			
				_that.attr('value','MM');
			}else if(  _valID == 'age-day' &&  _valAT == ''){
			
				_that.attr('value','DD');
			}else if(  _valID == 'age-year' &&  _valAT == ''){
			
				_that.attr('value','YYYY');
			}	
			
		});
		
		/*.keydown( function(evt){
			
			var key 	=	String.fromCharCode( evt.keyCode )
			,	rex		=	/[0-9]|\./;
						
			if( !rex.test( key ) && evt.keyCode != 9 ){
			
				return false;
			
			}
					
		});*/
		
		
	
	}
	
	//Handle Audio PopOut
	$('.popper').click( function(){
		
		//Popout new window
		var _pop = window.open( audioUrl ,'RadioReykjavik','directories=no,menubar=no,status=no,titlebar=no,toolbar=no,height=225,width=400');
		if (window.focus) {_pop.focus()}
				
	});
	
	// Handle Form
	$('#boxer').focus(function(){
		
		if( $(this).attr('value') == 'Join...' ){
			$(this).attr('value' , '');
		}
		
	})
	
	$('#mailer-form').submit(function(){
		
		$(this).ajaxSubmit( emailOptions );
		return false;
			
	});
		
	$('#sbmt').click(function(){
			
		$('#mailer-form').ajaxSubmit( emailOptions );
		return false;
	});
		
	
	function validate(formData , jqForm , options){
		
		var test 	=	$('#boxer').attr('value')
		,	filter 	=	/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
		;
				
		if( !filter.test( test ) ){
			
			
			//Show Error
			$('.error').slideDown(125);
			return false;
		
		}else{
		
			$('.error').slideUp(125);
			//$('.error').css({"display":"none"});
			
			return true;
			
		}
		
					
	}
		
	function showResp(responseText , statusText , xhr , $form){
		
		trace(xhr);
		
		$('#boxer').css({'color':'#47ba47'}).attr('value','Thank You!');
		
		setTimeout(function(){
			
			$('#boxer').css({'color':'#5d6f7c'}).attr('value','Join...');
			
		},3000);
		
	
	}
		
			

//Cocktails Section
	function initCocktails(){
		
		cNav = $('.cocktail-nav li');
		
		//Need an array of sub-sections (possibly from template)
		cNav.each( function(i){
			
			drinks.push({ el : $(this) , path : $(this).attr('data-set') , id : i })
			
			if( !noPacity && i>0 ){
				$('img' , this).animate({"opacity":"0.4"},0);
			}
				
		});	
		
		
		// Set the first drink to 'active'
		drinks[0].el.css({'opacity':'1'}).addClass('active');
		
		
		
		//$('.cocktail-nav img').animate({"opacity":"0.1"},222);		
		/*
		.animate({filter: 'alpha(opacity=100)',height: '300px',top: newTop},{duration: 300});
		*/
		
		
		//Mouse |&| Touch Interatctions
		if(	!Modernizr.touch ){

			cNav.hover( function(){
			
				if( !$(this).hasClass('active') ){
					
					if( !noPacity ){
			
						$('img' , this).animate({"opacity":"1"},110);
			
					}
					
					$(this).css({'opacity':'1'});
				}
				
			}, function(){

				if( !$(this).hasClass('active') ){
					$(this).css({'opacity':'.4'});
					if( !noPacity ){
			
						$('img' , this).animate({"opacity":"0.4"},110);
			
					}
				}
				
			});
		
		}
		
		
		cNav.click( function(){
			
			var match	=	lookupID( $(this).attr('data-set') );
			
			if( match != currentView ){
				
				window.scrollTo(0,0);
				
				if( !noPacity ){
					$('img' , drinks[currentView].el).animate({"opacity":"0.4"},110);
				}
				
				drinks[currentView].el.css({'opacity':'0.4'}).removeClass('active');
				
				
				
				$('.prime-col').html('<div class="fetch"></div>');	
				
				currentView = match;
				
				
				
				if( !noPacity ){
					$('img' , drinks[currentView].el).animate({"opacity":"1"},110);
				}
				
				drinks[match].el.css({'opacity':'1'}).addClass('active');
				
				fetch( drinks[match] );
				
			}
			
		});
		
		
		//Initial Content Grab
		fetch( drinks[currentView] );
		
		
	}
	
	function lookupID( match ){
		
		var	i 		=	0
		,	len		=	drinks.length
		;		
		
		for(i; i<len; ++i){
			
			if(drinks[i].path == match){
				
				return i;
				
			}
			
		}
		
			
	}
	
	function fetch( params ){
		
		
		//Manage Assets
		$.ajax({
			
			url			:	'/cocktail/'+params.path,
			dataType	:	"html",
			success : function( data ){
				
				
				$('.fetch').fadeOut(220, function(){
					
					$(this).remove();
					$('.prime-col').fadeOut(0);
					
					setTimeout(function(){
						
												
						$('.prime-col').html( data ).fadeIn(220);
						
					},120);
					
				});
				
				
				
			},
			error : function( err ){
				
				trace('ERR')
			
			}
			

		});	
	
	}
	
	
// ** End Cocktails Section	
		
// How Section	
	function initHow(){
		
		
		$('.how-inner h3').click(  function(){	toggleHowState(1); return false;});
		
		$('.how-content a').click( function(){	toggleHowState(0); return false;});
		
		function toggleHowState(arg){
			
			$('.how-default').toggle();			
			$('.how-inner').toggle();
			
			//$('.how-default').slideToggle(250);
			//$('.how-inner').slideToggle(250);
			
			//$('.how-default').toggle(175);
			//$('.how-inner').slideToggle(175);
		}
	
	}
	
			
	
// Utils
	
	$(window).bind( 'orientationchange', function(e){
		
		//alert(window.innerHeight + ' / ' +window.innerWidth + ' / ' + window.orientation);
		
		
	});
	
	
	function trace(	arg	){
		console.log( arg );
	}
	
});
