function openFkbox(selector, noResize, height, largerFooter) {
	if(!noResize) {
		$(document.body).css("overflow", "hidden");
		$("html").css("overflow", "hidden"); 
	}

	var div = document.getElementById("fkbox_overlay");
	if(!div) {
		$(document.body).append("<div id='fkbox_overlay'></div>");
	}
	var window_height = $(document).height();

	$("#fkbox_overlay").css("height", (window_height + 70)  + "px");
	$("#fkbox_overlay").show();

	window_height = $(window).height();

	if(!noResize) { // if auto resize to 90%
		if(height)
			window_height = height;
		var fkbox_height = window_height - 60;
		$(selector).css("height", fkbox_height + "px");
		if(largerFooter)
			$(selector + " .content").css("height", (fkbox_height - 145) + "px");
		else
			$(selector + " .content").css("height", (fkbox_height - 125) + "px");

		if(height) {
			var y = (($(window).height() - height) / 2) + $(window).scrollTop();
			$(selector).css("top", y + "px");
		}
		else {
			var y = (($(window).height() - fkbox_height) / 2) + $(window).scrollTop();
			$(selector).css("top", y + "px");
		}
	}
	else {
		var width = $(selector).width();
		var x = ($(window).width() - width) / 2;
		$(selector).css("left", x +  "px");

		var height = $(selector).height();
		var y = ((window_height - height) / 2) + $(window).scrollTop();
		$(selector).css("top", y + "px");
	}
	$(selector).show();
}

function closeFkbox(selector) {
	$(document.body).css("overflow", "");
	$("html").css("overflow", "");
	$("#fkbox_overlay").hide();
	$(selector).hide();
}

var curTab = 0;
function tab(id, number) {
	var tab_lis = $("#" + id + " .tabs li");
	tab_lis.eq(curTab).removeClass("selected");
	tab_lis.eq(number).addClass("selected");

	$("#" + id + "_tab" + curTab).hide();
	$("#" + id + "_tab" + number).show();

	$("#" + id + "_footer" + curTab).hide();
	$("#" + id + "_footer" + number).show();
	curTab = number;

	//fixButtons();
}

var fkbox_errors;

// custom() returns true if there is an error
function fkbox_validate(selectorParentElement, custom) {
	var errors =  $(selectorParentElement + " .error");
	fkbox_errors = new Array();

	var has_error = false;
	var first_error = true;
	for(var i=0;i<errors.length;i++) {
		var error = errors.eq(i);
		if(error.attr("deactivated")=="true") continue;

		if(error.attr("required")=="true") {
			var sel = error.attr("selector");
			var val;
			if(sel) 
				val = $(sel).val();
			else 
				val = error.siblings("input[type='text'],input[type='hidden'],textarea,select").val();
			if(isEmpty(val)) {
				has_error = true;
				error.show();
				if(first_error) { scrollToError(error); first_error = false; }
			}
			else
				error.hide();
		}
		else if(error.attr("regex")!=null) {
			var reg = new RegExp(error.attr("regex"));
			var sel = error.attr("selector");
			var val;
			if(sel)
				val = $(sel).val();
			else
				val = error.siblings("input[type='text'],input[type='hidden'],textarea,select").eq(0).val();
			if(!isEmpty(val) && !val.match(reg)) {
				has_error = true;
				error.show();
				if(first_error) { scrollToError(error); first_error = false; }
			}
			else
				error.hide();
		}
		else if(error.attr("expr")!=null) { // return true on error
			var isError = eval(error.attr("expr"));
			if(isError) {
				has_error = true;
				error.show();
				if(first_error) { scrollToError(error); first_error = false; }
			}
			else
				error.hide();
		}
	}

	if(custom)
		has_error = custom() || has_error;

	return !has_error;
}

function scrollToError(elem) {
	elem.parents(".content").scrollTo(elem, 400);
}

function makeUpload(id, width, height, path, complete) {
	$(id).uploadify({
		'uploader'  	: '/media/image/uploadify/uploadify.swf',
		'script'    	: '/flash/upload', 
		'cancelImg' 	: '/media/image/uploadify/cancel.png',
		'auto'      	: true,
		'folder'    	: '',
		'fileDataName'	: 'photo',
		'onComplete'	: function(evt, queueID, fileObj, response, data) { complete(eval('(' + response + ')')); },
		'rollover'		: false,
		'method'		: 'POST',
		'scriptData'	: { width: width, height: height, path: path } 
	});
}

