/*global $, jQuery, DD_roundies, window, document: false, alert: false, confirm: false, setTimeout: false*/
"use strict";

// Global variables
var client;
var currentMenu;
var currentClip;

var mediaMenuOpen = false;
var modalSpeed = 500;

var clipPlaying;

jQuery.easing.def = "easeOutExpo";

// ++++++++++++++++++++++++++++++++++++++++++++++++++

// localpath
var path = 'php/';

// remotepath
// var path = 'http://www.houdinistree.com/php/'

// ++++++++++++++++++++++++++++++++++++++++++++++++++
// GLOBAL FUNCTIONS
// ++++++++++++++++++++++++++++++++++++++++++++++++++
String.prototype.capitalize = function () {
	return this.replace(/(^|\s)([a-z])/g, function (m, p1, p2) { return p1 + p2.toUpperCase(); });
};

function isEmail(string) {
	return (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++

function loadPage(thePage) {
	
	$('#innerContent').hide().load("pages/" + thePage, function () {
		$('#innerContent').fadeIn(1000);			
	});
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++
function onResize() {
	var theHeight;
	
	theHeight = $(document).height();
			
	$("#content").css({
		"min-height" : theHeight - 150
	});
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++
function getRadioValue(item) {
	for (var i = 0; i < document.productionForm[item].length; i++) {
		if (document.productionForm[item][i].checked) {
			return document.productionForm[item][i].value;
		}
	}
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++
// MEDIA BROWSER FUNCTIONS
// ++++++++++++++++++++++++++++++++++++++++++++++++++
function playSample(clip) {
	var clipURL, clipTitle, newPlayer, clipTemp;
	
	currentClip = clip;
	$("#currentlyPlaying").html($(clip).clone());
	clipURL = $(clip).attr('href');
	
	// $(clip).append('<div id="littleLoader" style="position :relative; left : -10px; top: -40px;"><img src="images/loader.gif"/></div>');
	
	clipTemp = $(clip).clone();
	$(clipTemp).children('img').detach();
	clipTitle = $(clipTemp).html();
	
	newPlayer = '<a href="#" id="openMediaMenu">^</a> <a href="#" id="stopSound" onclick="stopSound(); return false;">||</a>';
	// newPlayer += '<span class="nowPlaying">Now Playing : ' + clipTitle + ' </span>';
	newPlayer += '<embed id="movie" src="media/' + clipURL + '" width="200" height="20" bgcolor="000000" controller="false" hidden="true"></embed>';
		
	$('#mediaPlayer').html(newPlayer);
	
	clipPlaying = true;
}

function stopSound() {
	var newPlayer;
	
	newPlayer = '<a href="#" id="openMediaMenu">^</a> <a href="#" id="stopSound" onclick="playSample(currentClip); return false;">></a>';
	//newPlayer += '<span class="nowPlaying">Choose A Sample</span>';
		
	$('#mediaPlayer').html(newPlayer);
	
	clipPlaying = false;	
}

function closeMediaMenu() {
	mediaMenuOpen = false;
	$('#mediaMenu').fadeOut(250);
}

function showMediaMenu() {
	
	mediaMenuOpen = true;

	$('#mediaMenu').fadeOut(1000).css({
		"bottom" : "30px",
		"left" : "10px"
	}).animate({
		"bottom" : "50",
		"opacity": "show"
	}, 250);
	
	$('#mediaSamples a').bind('click', 
	function () {
		
		playSample(this);
					
		closeMediaMenu();
		return false;
	});
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++
// MODAL FUNCTIONS
// ++++++++++++++++++++++++++++++++++++++++++++++++++
function closeModal() {
	
	var screenHeight;
	
	$('#modalInner').fadeOut(modalSpeed, function () {
		
		$('#modalInner').html("");
		
		screenHeight = $(window).height() + 200;
				
		$('#modalContainer').animate({
			marginTop: -screenHeight
		}, modalSpeed, function () {
			
			$('#modal').animate({
				top: -screenHeight
			}, modalSpeed, function () {
			
			});
		});
	});
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++
// MODAL FUNCTIONS
// ++++++++++++++++++++++++++++++++++++++++++++++++++
function createModal(page, theItem) {
	
	var modalWidth, modalHeight, modalX, modalY, screenHeight;
	
	screenHeight = $(window).height() + 200;
	modalWidth = 800;
	modalHeight = 520;
	modalX = -modalWidth / 2;
	modalY = -modalHeight / 2;
	
	$('#modalContainer').css({
			'min-height' : modalHeight, 
			'width' : modalWidth,
			'margin-left' : modalX,
			'margin-top' : -screenHeight
			});
				
	$('#modal').animate({
			top: 0
	}, modalSpeed, function () {
		
		$('#modalInner').hide();
		
		$('#modalLoader').show().css({
			'margin-left' : (modalWidth / 2 - 16)
		});
		
		$('#modalContainer').animate({
			marginTop: modalY
		}, modalSpeed, function () {
		
			$('#modalInner').load(page, {item: theItem},
			function () {
				
				var uploadDest;
					
				// The user upload directory
				uploadDest = 'users/' + client + '/projects';
				createUploader(uploadDest);
				
				$('#modalLoader').fadeOut(modalSpeed);
				$('#modalInner').fadeIn(modalSpeed);
				
			});
		});
	});	
}


// ++++++++++++++++++++++++++++++++++++++++++++++++++
// CLIENT INFO
// ++++++++++++++++++++++++++++++++++++++++++++++++++
function getClientInfo() {
	$.post(path + 'clientInfo.php', 
	function (data) {
		client = data;		
	});
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++
// CONTACT FORM FUNCTIONS
// ++++++++++++++++++++++++++++++++++++++++++++++++++
function submitContactForm() {
	var theName, theEmail, theMessage;
	
	theName = document.contact.name.value;
	theEmail = document.contact.email.value;
	theMessage = document.contact.message.value;
	
	if (isEmail(theEmail)) {
		$.post(path + 'email.php', { myName: theName, myEmail: theEmail, myMessage: theMessage },
		function (data) {
		
			// alert(data);
			
			$('.message').fadeIn(1000, function () {
				$('#name').focus();
				setTimeout(function () {
					$('.message').fadeOut(1000);
				}, 5000);
			});
			
			document.contact.name.value = "";
			document.contact.email.value = "";
			document.contact.message.value = "";
		});
		
	} else {
		alert('You must enter a valid email');
	}	
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++
// CLIENT PROJECTS
// ++++++++++++++++++++++++++++++++++++++++++++++++++
function updateClientAccount() {
	
	var position, infoLeft, theInfoBubble;	
	
	theInfoBubble = $('#infoBubble');
	
	$('#clientProjects').load(path + 'getClientData.php',
	function () {
		$('.accountBigButton').fadeIn(500);
				
		$("#theProjects a").bind('mouseenter', function (e) {
			
			position = $(this).offset();
			infoLeft = ($(window).width() / 2) + 355;
								
			theInfoBubble.html('Click to Edit Options')
			.css('top', position.top)
			.show()
			
			theInfoBubble.css({
				left: infoLeft,
				opacity: 0
			}).animate({
				left: infoLeft + 13,
				opacity: 1
			});
			
		}).bind('mouseleave', function () {			
			theInfoBubble.hide();
		});
		
		// ++++++++++
		$("#theMixes a").bind('mouseenter', function (e) {			
			
			position = $(this).offset();
			infoLeft = ($(window).width() / 2) + 355;
			
			theInfoBubble.css('top', position.top)
			.html('Click to Download')
			.show();
			
			theInfoBubble.css({
				left: infoLeft,
				opacity: 0
			}).animate({
				left: infoLeft + 13,
				opacity: 1
			});
			
		}).bind('mouseleave', function () {			
			theInfoBubble.hide();
		});
	});
	
	$('#accountNav').load(path + 'clientInfo.php', {type: "nav"});
	$('#accountStatus').load(path + 'clientInfo.php', {type: "status"});
}

function showProjectForm (theItem) {
	createModal (path + 'productionForm.php', theItem);
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++
// CLIENT LOGIN / SIGNUP
// ++++++++++++++++++++++++++++++++++++++++++++++++++

function loadLogin() {
	$('#clientContainer').hide().load("pages/login.php", function () {
		$('#clientContainer').fadeIn(1000);	
	});
}

function loadClientHome() {
	$('#clientContainer').hide().load(path + "clientHome.php", function () {
		$('#clientContainer').fadeIn(1000);		
	});
}

function clientSignup() {
	var theName, theEmail, thePassword, passwordCheck;
	
	theName = document.signup.name.value;
	theEmail = document.signup.email.value;
	thePassword = document.signup.password.value;
	passwordCheck = document.signup.passwordCheck.value;
	
	// Check the passwords
	if (thePassword !== passwordCheck) {
		alert('your passwords do not match');
		document.signup.password.value = '';
		document.signup.passwordCheck.value = '';
	} else {
		// Create the client account
		$.post(path + 'clientSignup.php', { name: theName, email: theEmail, password: thePassword},
		function (data) {
			if (data == 'true') {
				loadClientHome();
			} else {
				alert(data);
			}
		});
	}	
}

function checkLogin() {
	$.post(path + 'clientInfo.php',
	function (data) {		
		if (data !== "") {
			loadClientHome();
		} else {
			loadLogin();	
		}
	});
}

function clientLogin() {
	var theEmail, thePassword;
	
	theEmail = document.login.email.value;
	thePassword = document.login.password.value;
		
	// Login the client account
	$.post(path + 'clientLogin.php', { email: theEmail, password: thePassword},
	function (data) {
				
		if (data) {
			loadClientHome();			
		} else {
			alert("We were not able to find your account.\nPlease check your account info and try again");
		}
	});
}

function clientLogout() {
	$.post(path + 'clientLogout.php', function () {
		loadLogin();
	});
}


// ++++++++++++++++++++++++++++++++++++++++++++++++++
// PASSWORD RESET FUNCTIONS
// ++++++++++++++++++++++++++++++++++++++++++++++++++
function showReset () {
	$('#passReset').fadeIn(500);
	$('#loginForm').fadeTo(500, 0.5);
	$('#signupForm').fadeTo(500, 0.5);
}

function hideReset () {
	$('#passReset').fadeOut(500);
	$('#loginForm').fadeTo(500, 1.0);
	$('#signupForm').fadeTo(500, 1.0);
}

function passwordReset () {
	
	thePass = $('#newPass').val();
		
	if (thePass != "") {
		$.post(path + 'passwordChange.php', {pass: thePass},
		function (e) {
			hideReset ();
		});
	} else {
		alert("You must enter a password");	
	}
}

function passwordRetrieve() {
	theName = $('#resetName').val();
	theEmail = $('#resetEmail').val();
		
	if (isEmail(theEmail)) {
		$.post(path + 'passwordSend.php', { name: theName, email: theEmail},
		function () {
			alert('check your email. Your info has been sent to ' + theEmail);	
			hideReset ();
		});
	} else {
		alert("Make sure to enter your email account");	
		
	}
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++
// DELETING FUNCTIONS
// ++++++++++++++++++++++++++++++++++++++++++++++++++
function deleteProject(projectID) {
	var answer = confirm("This will completely delete this project... are you sure?");
	
	if (answer) {
		$.post(path + 'deleteProject.php', {id: projectID},
		function (data) {
			updateClientAccount();
		});
	}
}

function deleteMix(mixID) {
	
	var answer = confirm("This will completely delete this mix... are you sure?");
	
	if (answer) {
		$.post(path + 'deleteMix.php', {id: mixID},
		function (data) {
			updateClientAccount();
		});
	}
}


function keyEvents() {
	// ++++++++++++++++++++++++++++++++++++++++++++++++++
	// KEYBOARD EVENTS
	// ++++++++++++++++++++++++++++++++++++++++++++++++++
	$(window).keydown(function(event){
		if (event.keyCode === 27) {
			closeModal();
	    }
		
		/*
		if (event.keyCode === 37) {
			if (theSection === "images") {
				showPrevImage();
			}
		}
		if (event.keyCode === 39) {
			if (theSection === "images") {
				showNextImage();
			}
		}
		*/
	});
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// ADDRESS DEEP LINKING EVENTS
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$.address.init(function (event) {

	var thePage = event.path.substring(1);
	
	if (thePage == ""){
		loadPage("about.php");	
	}
	loadPage(thePage + ".php");
	
	$.address.title("Houdinis Tree | Audio Production | " + thePage.capitalize());
	
}).change(function (event) {
	thePage = event.path.substring(1);
	
	loadPage(thePage + ".php");
	
	$(currentMenu).removeClass('selected');
	currentMenu = $('#mainNav li:contains(' + thePage.capitalize() + ')').addClass('selected');		
	
	$.address.title("Houdinis Tree | Audio Production | " + thePage.capitalize());
});

// ++++++++++++++++++++++++++++++++++++++++++++++++++
// SITE INIT
// ++++++++++++++++++++++++++++++++++++++++++++++++++
function siteInit() {
	
	keyEvents();
		
	$(window).resize(function () {
		onResize();	
	});
	
	onResize();

	currentClip = $("#mediaSamples a").eq(1);
	
	$('#openMediaMenu').live('click', function() {
		if (mediaMenuOpen) {
			closeMediaMenu();
			return false;
		} else {
			showMediaMenu();
			return false;
		}
	});	
	
	
	stopSound ();
	$('#mediaMenu').hide();
}
