// ##########  PAGE LOGIC  ############
var base_url = 'http://fukbox.com/';
var movie = 'http://fukbox.com/';
var player = 0;

function onYouTubePlayerReady(playerId) {
    player = document.getElementById(playerId);
    player.addEventListener("onStateChange", "onStateChange");
  
}

function load_vid(video_id) {
    var player_elem = document.getElementById('player');
    player_elem.loadVideoById(video_id);
}



function getCurrentTime() {
	var player_elem = document.getElementById('player');
  if (player_elem) {
    return player_elem.getCurrentTime();
  }
}

function getDuration() {
	var player_elem = document.getElementById('player');
  if (player_elem) {
    return player_elem.getDuration();
  }
}

function getPlayerState() {
	var player_elem = document.getElementById('player');
  if (player_elem) {
    return player_elem.getPlayerState();
  }
}

function play_vid(video_id) {
	var player_elem = document.getElementById('player');
	player_elem.loadVideoById(video_id);
}


function resumeVideo() {
	var player_elem = document.getElementById('player');
	if (player_elem) {
	  player_elem.playVideo();
	}
}

function stop_vid() {
	var player_elem = document.getElementById('player');
	player_elem.stopVideo();
}

function pause_vid() {
	var player_elem = document.getElementById('player');
	player_elem.pauseVideo();
}

function onStateChange(newState) {
	
   if(newState == 0 ){
		closePlayer();
	}

}

$(function() {

// Generic Input Default message reset
function inputMsgAutoReset(target,msg){	
	$('#'+target+'').die('click').live('click',function(){if($('#'+target+'').val() == msg)$('#'+target+'').val('');}).blur(function(){if($('#'+target+'').val() == msg || $('#'+target+'').val().length == 0)$('#'+target+'').val(msg);}).keydown(function(){if($('#'+target+'').val() == msg){$('#'+target+'').val('');}})
}


// Join Dialog

var inviteMsg = 'Enter email...';
var invMsg = 0;


// Simple Email validation
validateEmail = function(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   }else{
	return true;
	}
}

var emailAvailable = true;
var btn_html = '';
$('#email').focus();

// Check that email is available
checkEmailAvailability = function(){
	var email_val = $('#email').val();
	
	$.post(base_url+"wp-content/themes/flow/home/js/check-email-address.php", {email: email_val},
	function(d){
		if(d){
			
			if(d.ok.success == 1){
				$('#btn_area').html('<b>Redirecting to PayPal..</b>');
				// If available submit PayPal button
				submitPayPalButton('withemail');
			}
			
			if(d.ok.success == 0){
				alert(d.ok.msgDisplay)
				$('#email').select();
				$('#btn_area').html(btn_html);
			}

		}
	}, "json");
	return false;
}


// Submit PayPal button
submitPayPalButton = function(mode){

	// PayPal Button Variables
	var email_val = mode == 'noemail'? mode : $('#email').val();
	
	// Detect referral email
	var email_ref = $('#ref').val();
	var ref_url = typeof(email_ref) != "undefined"? '&ref='+email_ref : '';
	
	// Redirect to Redirecting Script
	window.location=base_url+'wp-content/themes/flow/home/js/submit-paypal-button.php?email='+email_val+ref_url;
	
}



// Process Buy Instructions Form
buyForm = function(){
	
	var error = new Array();
	
	var email_val = $('#email').val();
	
	// Validate Email
	if(!validateEmail(email_val)){
		error[1] = 'Enter a valid email address';
	}
	
	if(error.length == 0){
	
		// Make sure email is available
		checkEmailAvailability();
	
	}else{
		
		alert(error[1]);
		$('#btn_area').html(btn_html);
	}
	
}


$('#submit_btn').live('click',function(){
	//alert(123);
	btn_html = $('#btn_area').html();
	$('#btn_area').html('<b>Checking email..</b>');
	buyForm();
	return false;
})

$('#buy_now_btn').live('click',function(){
	//alert(123);
	$(this).attr('disabled','disabled').val('Wait..');
	submitPayPalButton('noemail');
})



function closeInvWindow(){	
//	$('.join-box').remove();
	$('#joinBtnbox').css('display','inline');
	$('#joinDialog').css('display','none');
}

requestInvite = function(){
	var error = new Array();
	var email = $('#req_invite_txt').val();
	var options = {ajax:"true",email:email};

	if(email.length == 0 || email == inviteMsg){
		error[0] = 'Please, enter email address.';
	}

	if(validateEmail(email) != true){
		error[0] = 'Please, enter a valid email address.';
	}

	if(error.length == 0){

	$.post("request_invite_ajax.php", options,
		function(data){
			if(data){

				if(data.ok.success == 1){
					showInvSuccessMsg();
				}

				if(data.ok.success == 0){			
					$("#invMsg").text(data.ok.msgDisplay).addClass('errorMsg');
					$("#req_invite_txt").val('').focus();
				}

			}
		}, "json");

	}else{
		invMsg = $("#invMsg").val();
		$("#invMsg").text(error[0]).addClass('errorMsg');
		$("#req_invite_txt").val('').focus();
	}
	return false;

}

function showInvSuccessMsg(){
var msg = 'Your email has been submitted!<br /><br />';
msg += "Soon you will be able to build your own sex machine and enjoy countless hours of mechanical love!<br />";
msg += "Follow our progress on Twitter at:<br />";
msg += '<a href="https://twitter.com/homesexmachine">https://twitter.com/homesexmachine</a>';
msg += "<br /><br />The FukBox Team";
	$("#req_invite_txt").css('display','none');
	$("#req_invite_btn").css('display','none');
	$("#invMsg").html(msg);
}



$('#joinBtn').click(function(){
	$('#joinBtnbox').css('display','none');
	$('#joinDialog').css('display','inline-block');
	$('#req_invite_txt').focus();
})

inputMsgAutoReset('req_invite_txt',inviteMsg);

$('#req_invite_txt').live('keyup', function(ev) {
    if(ev.keyCode == 13) {
		requestInvite();
    }
	return false;
});


$('#playvideo').live('click',function(){
	playPlayer();
})

function playPlayer(){
	$('#playvideo').css('display','none');
	$('#videoplayer').fadeIn('slow');	
	startVideo('61upXwSqq08'); // 30 Sec Timelapse: MymKUDROZfQ || 30 sec Sexy Add: 61upXwSqq08
}

function closePlayer(){
	$('#videoplayer').fadeOut('slow');
	$('#playvideo').css('display','inline');
}

$('#req_invite_btn').live('click',function(){requestInvite();})
$('#closePlayer').live('click',function(){closePlayer();return false;})

//launchVid = setTimeout(playPlayer, 1000);

// START VIDEOS
function startVideo(video_id){
		//clearTimeout(launchVid);
       var params = { allowScriptAccess: "always",allowFullScreen: "true"};
       var atts = { id: "player"};
       swfobject.embedSWF("http://www.youtube.com/v/" + video_id + "&enablejsapi=1&playerapiid=player&fs=1&color1=0x3a3a3a&color2=0x999999&border=1&rel=0&autoplay=0",
       "player", "520", "400", "8", null, null, params, atts); // add "&rel=0" to get rid of related videos
		
}


// Video Thumbs

$('.video-thumb').colorbox({iframe:true, width:650, height:440,scrolling:false,opacity:0.4});
$("a[rel='gallery01']").colorbox();



var disclaimer = '<p>\
	<h2>************  DISCLAIMER  ************</h2>\
	Build at your own risk!\
	The creator of FukBox instructions is not responsible for any injuries, deceased pets, blown fuses, angry mother in laws, lost jobs, and any possible bad stuff that may happen by following the instructions and building your own fucking machine.\
	These instructions should be used as a guideline only. Your design may and probably will be different based on the parts you have available and the requirements you wish for your fucking machine to have.\
	In case of doubt, always consult and expert electrician to ok your wiring before you use your machine.\
	Always use water based lubricant compatible with your choice of dildos. Use common sense when selecting dildo size, material, depth of penetration, and or speed.\
	</p><br /><br />\
	<p> By clicking the PayPal "Buy Now" button, you agree with the disclaimer and to be charged $19.99 to get unlimited access to the complete FukBox instructions. This payment is non-refundable. You understand that you are purchasing access to the instructions for yourself only and that you won\’t share or distribute my password or instructions to anyone else. If someone else wants access to the instructions you\’ll encourage them to purchase their own. </p>';

$('#disclaimer_btn').colorbox({html:disclaimer, width:450, height:400,scrolling:true,opacity:0.4});



});
