var CURRENT_PAGE_VIDEO = "";
var PATH = "";

window.addEvent ( "load", init );

function init () {
	
	/////setup_navigation ();
	
	$( "mailinglist-form" ).addEvent ( "submit", mailinglist_form_submit_handler );	
	
	if ( $( "contact-form" ) ) {
		$( "contact-form" ).addEvent ( "submit", contact_form_submit_handler );
	}
	
	//setup_autoplay_toggler ();
	
	if ( $$( "body.category" ).length > 0 ) {
		CURRENT_PAGE_VIDEO = $( "storage" ).get ( "html" );
	}
	
	load_video ( get_current_page_video () );
	
}

function mailinglist_form_submit_handler ( evt ) {
	evt.stop ();
	var email_address = $( "mailinglistemail" ).get ( "value" );
	var req = new Request ( {
		url: PATH + "/mailinglist.php",
		data: {
			email: email_address
		},
		onRequest: function () {
			$( "mailinglistemail" ).disabled = true;
		},
		onSuccess: function ( response ) {
			$( "mailinglistemail" ).disabled = false;

			if ( response.toInt () == 1 ) {
				alert ( "Thank You for subscribing to the mailing list with the address: " + email_address );
				$( "mailinglistemail" ).set ( "value", "" );
			} else {
				alert ( "There was an error adding your address " + email_address + ", please check the address and try again." );
			}
						
		}
	} );
	
	if ( validEmail ( $( "mailinglistemail" ).get ( "value" ) ) ) {
		req.send ();
	} else {
		alert ( "Please enter a valid email address." );
	}
	
}


function contact_form_submit_handler ( evt ) {
	evt.stop ();
	var contact_name = $( "contact-name" ).get ( "value" );
	var contact_email = $( "contact-email" ).get ( "value" );
	var contact_message = $( "contact-message" ).get ( "value" );
	var req = new Request ( {
		url: PATH + "/contact.php",
		data: {
			name: contact_name,
			email: contact_email,
			message: contact_message
		},
		onRequest: function () {
			// console.log ( data );
		},
		onSuccess: function ( response ) {
			// console.log ( "onSuccess..." + response );
			if ( response.toInt () == 1 ) {
				alert ( "Your email has been sent successfully." );
				$( "contact-name" ).set ( "value", "" );
				$( "contact-email" ).set ( "value", "" );
				$( "contact-message" ).set ( "value", "" );
			} else {
				alert ( "There was an error sending your email, please check the fields and try again." );
			}
		}
	} );
	
	if ( contact_name == "" || contact_email == "" || contact_message == "" ) {
		alert ( "Please enter your Name, Email & Message." );
	} else if ( !validEmail ( contact_email ) ) {
		alert ( "Please enter a valid email address." );
	} else if ( validEmail ( contact_email ) && contact_name != "" && contact_message != "" ) {
		req.send ();
	} 
	
	
}


function get_current_page_video () {
	return CURRENT_PAGE_VIDEO;
}

function setup_autoplay_toggler () {
	$( "autoplay" ).getElement ( "a" ).addEvent ( "click", autoplay_click_handler );
}

function setup_posts () {
	//console.log ( "POSTS!" );
}

function open_post () {
	//console.log ( "open post" );
}

function setup_navigation () {
	
	$( "nav" ).getElement ( "ul" ).getElements ( "li" ).each ( function ( el ) {
			
			// el.getElement( "a" ).addEvent ( "mouseover", nav_mouseover_handler );
			// el.getElement( "a" ).addEvent ( "mouseout", nav_mouseout_handler );
			
		el.getElement( "a" ).addEvents ( {
			"mouseover" : nav_mouseover_handler,
			"mouseout" : nav_mouseout_handler
		} );
			
	} );
	
	$( "autoplay-btn" ).addEvents ( {
		"mouseover" : nav_mouseover_handler,
		"mouseout" : nav_mouseout_handler
	} );
	
	$( "mailinglistsubmit" ).addEvents ( {
		"mouseover" : nav_mouseover_handler,
		"mouseout" : nav_mouseout_handler
	} );
	
}

function load_video ( video, ap ) {
	
	var auto_play;
	
	if ( ap ) {
		auto_play = Boolean ( ap );
	} else {
		auto_play = Boolean ( AUTOPLAY );
	}
	
	// alert ( "auto_play: " + auto_play + ", ap defined ? : " + ap );
	
	if ( CURRENT_PAGE_VIDEO ) {
		CURRENT_PAGE_VIDEO = video;	
	}
	
	if ( video ) {
		
		// if no flash... check for quicktime... (see about embedding youtube via quicktime)
		
		var swf = PATH + "/flash/toob.swf";
		var obj = new Swiff ( swf, {
		    id: "swf",
				container: "sub",
		    width: 400,
		    height: 300,
		    params: {
		        wmode: "transparent",
		        bgcolor: "#000"
		    },
				vars: {
					video: video,
					auto_play: auto_play
				}
		});
	} 
		
}

function validEmail ( email ) {
  var emailReg = "^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$";
  var regex = new RegExp ( emailReg );
  return regex.test ( email );
}

function nav_mouseover_handler ( evt ) {
	var target = evt.target;
	var parent = target.getParent();

	//var myEffect = new Fx.Morph(parent, {duration: 'short', transition: Fx.Transitions.Expo.easeOut});
	//myEffect.start({ 'margin-left' : '10px' });	
	//myEffect.start ( { 'opacity': 1 } );
	
	//parent.fade ( '1' );
	
	// parent.set ( 'opacity', '1' );
	// target.set ( 'opacity', '1' );
	/////target.addClass ( 'on' );

}

function nav_mouseout_handler ( evt ) {
	var target = evt.target;
	var parent = target.getParent();

	//var myEffect = new Fx.Morph(parent, {duration: 'short', transition: Fx.Transitions.Expo.easeOut});
	//myEffect.start({ 'margin-left' : '0px' });
	//myEffect.start ( { 'opacity': 0.5 } );
	
	//parent.fade ( '.5' );
	
	// parent.set ( 'opacity', '0.5' );
	// target.set ( 'opacity', '0.5' );
	/////target.removeClass ( 'on' );
}

function autoplay_click_handler ( evt ) {
	
	//evt.stop ();
	
	$( "autoplay-form" ).send ();
	
	/*
	$( "autoPlay" ).value = false;
	$( "autoplay-form" ).send ();
	*/

/*
	evt.stop ();
	var target = evt.target;
	target.blur ();
	
	var toggle;
	
	if ( AUTOPLAY ) {
		toggle = false;
		target.addClass ( "off" );
	} else {
		toggle = true;
		target.removeClass ( "off" );
	}
	
	
	var req = new Request ( {
		url: $( "storage" ).innerHTML,
		data: {
			autoPlay: toggle
		}
	} ).send ();
*/

}

