//if (!FXM) var FXM = {};
FXM = function (){}

$(function () {
    fxm = new FXM();
    fxm.init();
});

FXM.prototype.init = function (){
	$(".scroll-pane").jScrollPane({scrollbarWidth: 10});
	$(".nav a").click(function (e) {
		$(".nav li").removeClass("selected");
		if($(this).attr("href") == "#"){
			e.preventDefault();
			$("#" + $(this).attr("rel")).modal();
			$(this).parent("li").addClass("selected");
		}
	});

	$(".close-modal").live("click", function(){
		$(".simplemodal-close").trigger("click");
		$(".nav li").removeClass("selected");
	});

	$("#show-dashboard ul[id!=\"originals-clips\"] li").click(function (e) {
		e.preventDefault();
		var clipsPerRow = 4;
		var title = $(this).find("label").text();
		var showId = $(this).data("showId");

		//$.get("clips.php?showId=" + showId, function(){
			var clipsCount = 9;	//Count of returned clips
			var showClips = "";
			for( var idx = 1; idx <= clipsCount; idx++ ){
				showClips += "<li><img src=\"/fxm/theme/img/pixel.gif\" /><label>American Horror Story</label></li>";
				//console.log("idx = " + idx);
				//console.log("clipsPerRow = " + clipsPerRow);
				//console.log("idx % clipsPerRow = " + (idx % clipsPerRow));

				if (idx % clipsPerRow === 0 && idx < clipsCount){
					showClips += "</ul><hr/><ul class=\"show-clips\">";
				}
			}

			var overflow = idx > clipsCount ? "overflow" : "";
			//$.modal("<div id=\"show-clips\"><h3>" + title + "</h3><hr class=\"shadow\" /><div id=\"show-clips-container\" class=" + overflow + "><ul class=\"show-clips\">" + showClips + "</ul></div>", {closeHTML: "<a href='#'>X</a>"});
		//});
	});

	$("#choose-provider a[rel='clear']").live("click", function(e){
		e.preventDefault();
		$("#choose-provider")[0].reset();
	});

	$("#register-for-updates form").submit(function(){
		$(this).attr("method", "post");
		$(".error-msg").each(function(){ $(this).remove()});
		return fxm.validateForm($(this), function(isValid, formObj){
			if(isValid){
				$.post("http://"+window.location.hostname+"/fxm/early_subscribe", formObj.serialize(), function (data){
					if (data){
						isValid = eval("(" + data + ")").error;
						var msg = eval("(" + data + ")").message;
						var field = eval("(" + data + ")").field;

						if(isValid == 0){
							$(formObj).replaceWith("<h4>" + msg + "</h4>");
						}else{
							if(field != ""){
								$("input[name=" + field + "]").after("<label class=\"error-msg\">" + msg + "</label>");
							}else{
								$(formObj).before("<label class=\"error-msg\">" + msg + "</label>");
							}
						}
					}
				});
			}else{
				$(".error").each(function(){
					$(this).after("<label class=\"error-msg\">" + $(this).data("error") + "</label>");
				});
			}
		});
	});
}

FXM.prototype.validateForm = function(formObj, callback){
	var isValid = true;
	var validFields = 0;
	$(".error").removeClass("error");
	$(formObj).find(".req").each(function(){
		if($(this).val() == ""){
			$(this).addClass("error");
			$(this).data("error", $(this).data("req-msg"));
			validFields++;
		}else{
			var re = "";
			if($(this).attr("name") == "email"){
				re = new RegExp(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/);
				if(!re.test($(this).val())){
					$(this).addClass("error");
					$(this).data("error", $(this).data("validity-msg"));
					validFields++;
				}
			}

			if($(this).attr("type") == "checkbox"){
				if(!$(this).is(":checked")){
					$(this).addClass("error");
					$(this).data("error", $(this).data("req-msg"));
					validFields++;
				}
			}
		}
	});

	isValid = (validFields > 0) ? false : true;

	if (callback && typeof (callback) === "function") { callback(isValid, formObj); }
	return false;
}
