var Front_door = null,
	fd = null, 
	UI = null;

var DEBUG = false, DEVELOPMENT = true, CLICK_EVENT = "click";

var Front_door = function(params){
	this.player = new HTML5Player($("#player"), true);
};	

fd = new Front_door(_data);

var Panel = function(container, next, prev){
	var panel = this, validators = {"next" : [], "prev" : []};

	container.find(".next").bind(CLICK_EVENT, function(){
		next();
	});
	container.find(".prev").bind(CLICK_EVENT, prev);

	this.show = function(){
		container.show();	
	};
	this.hide = function(){
		container.hide();
	};

	this.registerValidator = function(validator, direction){
		validators[direction].push(validator);
	};
}

var PanelGroup = function(container){
	var _this = this,
		cursor = 0;
		
	this.panels = []; 
		

	function next(){
		_this.panels[cursor].hide();
		cursor++;
		_this.panels[cursor].show();
	};

	function prev(){
		_this.panels[cursor].hide();
		cursor--;
		_this.panels[cursor].show();
	};

	container.children(".panel").each(function(i, element){
		var panel = new Panel($(element), next, prev);
		_this.panels.push(panel);

	});

	this.init = function(){
		this.panels[cursor].show();
	};

};


UI = {
	login : {
		form : $("#form-login").submit(function(e){
				e.stopPropagation();
				return false;
			}),
		username : $("#username").keyup(function(e){
			// Checking for Enter tap 
			if (e.keyCode == 13)
				Login();
			
		}),
		password : $("#password").keyup(function(e){
			// Checking for Enter tap 
			if (e.keyCode == 13)
				Login();
			
		}),
		submit : $("#submit").click(Login)
	},
	showMessage : function(message){
		UI.messageContainer.html(message);
		UI.messageContainer.fadeIn("fast").delay(5000).fadeOut("fast");
	},
	messageContainer : $("#message-container"),
	signup : new PanelGroup($("#signup")),
	countryPanel : $("#country-options"),
	countryInput : $("#country").bind(CLICK_EVENT, function(){
		UI.countryPanel.toggle();
	})
	.keydown(function(e){
		e.stopPropagation();
		return false;
	}),
	countryOptions : $("#country-options > div").bind(CLICK_EVENT, function(){
		var _this = $(this);
		_this.siblings().removeClass("selected");
		_this.addClass("selected");

		UI.countryInput.val(_this.text());

		setTimeout(function(){
			UI.countryPanel.fadeOut(300);
		}, 400);
	})
};



function Login (){
		var loginEmail = UI.login.username.val(), loginPassword = UI.login.password.val();
		jQuery.ajax({
			type: "POST",
			url: "/user/login",
			data: {loginEmail : loginEmail, loginPassword : loginPassword},
			success: function(data, status, request) {
				if(data['logged']) {
					
					var qs = document.location.search.substring(1);
					if (qs!=''){
						next = qs.slice(5, qs.length);

						document.location = (''+next);
					}else{
						document.location = '/';
					}
				}
				else {
					UI.showMessage(data["error"]);
				}
			},
			error: function(request, status, error) {
				UI.showMessage(MESSAGES.SIGNIN_BAD_ATTEMPT);
			}
		});
	}	

$(function(){
	UI.login.username.focus();
	UI.signup.init();

	window.flashReady = function () {
		var flex = $("#fd-player")[0];
		var uri = "rtmp://208.96.49.85/live~frontdoor"
		flex.playDirect(uri);
	};
});


