var planner_changed = 0;

$(document).ready(function() {
	
	
	$("button").mousedown( function () {
		$(this).addClass("down")
	})
	
	$("button").mouseup( function () {
		$(this).removeClass("down")
	})

	$("a.bt.project_delete").click(function () {
		if (confirm("Удалить план?")) {
			return true;
		}
		else {
			return false;
		}
	})
	
	$("li:last-child").addClass("last-child");
	$("li:first-child").addClass("first-child");

	$("td:last-child").addClass("last-child");
	$("td:first-child").addClass("first-child");

	$("tr:last-child").addClass("last-child");
	$("tr:first-child").addClass("first-child");

	$("input[type='text'], input[type='password']").addClass("text");
	
	// $(".link-login").overlay({
	// 	speed: 'fast',
	// 	zIndex: 2,
	// 	closeOnClick: false,
	// 	color: 'black',
	// 
	// 	// start exposing when overlay starts to load
	// 	onBeforeLoad: function() {
	// 		
	// 		// $("#box-reg-form").load( $(".link-reg").attr("source") );
	// 		
	// 		// this line does the magic
	// 		this.getBackgroundImage().expose({
	// 			color: '#30804e',
	// 			opacity: 0.4,
	// 			loadSpeed: 'slow',
	// 			zIndex: 1,
	// 			onClose: function () { $(".link-reg").overlay().close(); return true; }
	// 		});
	// 	}, 
	// 	
	// 	// when overlay is closed take the expose instance and close it as well
	// 	onClose: function() {
	// 		$.expose.close();
	// 	}
	// });
	$('a[rel*=facebox]').facebox({'closeImage': '/static/js/facebox/closelabel.png', 'loadingImage': '/static/js/facebox/loading.gif'})
	
	$("input").stylizeButton();
})

login_submit = function () {
    logindata = $(this).serializeArray()
    url = $(this).attr("action")
    ok2go = confirmUnsaved()
    if (ok2go) {
    	$.post(url, logindata,
    		function(data) {
    			if (data.status == 'error') {
    				$("input.username").val('')
    				$("input.username").focus()
    				$("input.password").val('')

    				$("div.box-login").naysay()

    				jQuery.noticeAdd({
    					text: data.message,
    					stay: false,
    					type: 'error'
    				});

    			}
    			else if (data.status = 'ok') {
    			    window.location = data.redirect
    		}
    	}, "json");
    }
    else {
        $("div#facebox a.close").click()
        return false
    }
	
	return false
}

function confirmUnsaved() {
    if (planner_changed) {
    	var answer = confirm(planner_close_message)
    	if (answer){
            return true
    	}
    	else{
    		return false
    	}
    }
    else {
        return true
    }
}

