var processamento=0;
$(document).ready(function() 
{
	$("#menu_area a").hover(function(){
	
		if($(this).hasClass("menusel")){ return false;}
		$(this).addClass("sel");
	
	}, function(){
		if($(this).hasClass("menusel")){ return false;}
		$(this).removeClass("sel");
	});

	
	$("#left_menu").treeview({
		animated: "fast",
		collapsed: true,
		unique: true,
		persist: "cookie"
		// toggle: function() {
			
		// }
	});
	
	$("#left_menu").disableSelection();
	
	$.ajax({
	url: "http://"+$website_patch+"/processa.php",
	data: "w=events&id=1",
	dataType: "json",
	success: function(calendarEvents){ 
        $(".calendarwidget").datepicker({

	   //// [rows, columns] if you want to display multiple calendars.  
	   
			numberOfMonths: [1, 1],
			showCurrentAtPos: 0,
			beforeShowDay: function (date){
				var event_title;
				for (i = 0; i < calendarEvents.length; i++){
				
					if (date.getMonth() == calendarEvents[i][0] - 1 && date.getDate() == calendarEvents[i][1] && date.getFullYear() == calendarEvents[i][2]) {
					  
						if(event_title){event_title+= "<p>&bull; "+calendarEvents[i][3];}
						else{event_title = "&bull; "+calendarEvents[i][3];}
					}
					
					if(i==(calendarEvents.length)-1)
					{ if(event_title){return [true,"calendar_evento",event_title];} }
				}
				return [true, ""];//enable all other days
			},
			onSelect: function(dateText, inst) { $('.tipsy').remove(); $.facebox(dateText);  setTimeout("$('.calendarwidget .calendar_evento').tipsy({gravity: $.fn.tipsy.autoNS, html: true}); $('.ui-datepicker-today a').attr('title', 'Hoje'); ", '250');       },
			onChangeMonthYear: function(year, month, inst) {
                            setTimeout("$('.calendarwidget .calendar_evento').tipsy({gravity: $.fn.tipsy.autoNS, html: true}); $('.ui-datepicker-today a').attr('title', 'Hoje');", '250');                              
                        }

			
		});
	   
		
		$('.calendarwidget .calendar_evento').tipsy({gravity: $.fn.tipsy.autoNS, html: true});
		$('.ui-datepicker-today a').attr("title", "Hoje");
	  
	   }
	});
	 
	$.datepicker.regional['pt-pt'] = {
	closeText: 'Fechar',
	prevText: '&#x3c;Anterior',
	nextText: 'Pr&oacute;ximo&#x3e;',
	currentText: 'Hoje',
	monthNames: ['Janeiro','Fevereiro','Mar&ccedil;o','Abril','Maio','Junho',
	'Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
	monthNamesShort: ['Jan','Fev','Mar','Abr','Mai','Jun',
	'Jul','Ago','Set','Out','Nov','Dez'],
	dayNames: ['Domingo','Segunda-feira','Ter&ccedil;a-feira','Quarta-feira','Quinta-feira','Sexta-feira','Sabado'],
	dayNamesShort: ['Dom','Seg','Ter','Qua','Qui','Sex','Sab'],
	dayNamesMin: ['Dom','Seg','Ter','Qua','Qui','Sex','Sab'],
	weekHeader: 'Sm',
	dateFormat: 'dd/mm/yy',
	firstDay: 0,
	isRTL: false,
	showMonthAfterYear: false,
	yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['pt-pt']);
	
	$("#area_top_menu .par").click(function(){
	
		if($(".sub_menu").is(":visible"))
		{		
		
			if(!$(this).children("div").is(":visible"))
			{
				$(".sub_menu").slideUp(50);
				$(".par").removeClass("dir_open").addClass("dir");
					
				$(this).removeClass("dir").addClass("dir_open");
				$(this).children("div").slideDown(200);
			}
			else
			{
				$(".sub_menu").slideUp(200).delay(200).queue(function(next){
				$(".par").removeClass("dir_open").addClass("dir");
				next();
				});
			}

		}
		else
		{
			$(this).removeClass("dir").addClass("dir_open");
			$(this).children("div").slideDown(200);
		}
	});
	

	$("#bt_exp_submenu").click(function()
	{
		if($("#submenu").is(":visible"))
		{
			$("#left_top, #right_top").animate(
				{height:178}, {duration:350}
			);
			$("#submenu").slideUp(350);
			$(this).css({'background-position':'0 -10px'});
			$(this).css({'margin-top':'2px'});
		}
		else
		{
			$("#left_top, #right_top").animate(
				{height:312}, {duration:350}
			);
			$("#submenu").slideDown(350);
			$(this).css({'background-position':'0 0'});
			$(this).css({'margin-top':'-9px'});
		}
	
	});
	
	$(".submenu_item").hover(
	
		function() {
			$(this).css({backgroundPosition: "0px 140px"});
		},
		function() {
			$(this).css({backgroundPosition: "0px 0px"});
		}

	);

	$("#combo_form").change(function(){
	
		if($(this).val() == 1){ $("#box_login_aluno").fadeOut(function(){ $("#box_login_prof").fadeIn(); });}	
		else if($(this).val() == 2){ $("#box_login_prof").fadeOut(function(){ $("#box_login_aluno").fadeIn(); });}	
	
	});
		
	$("#bt_login").click(function(){
		
		if($("#combo_form").val() == 1){
			$.post("http://"+$website_patch+"/LponSite/docentes/pagValidaBD.php",
			{
				username:$("#login_username").val(),
				passwd:$("#login_password").val()
			},
			function(data)
			{
				var resposta = $(data).find('span.style4').text();
				resposta =  resposta.replace(/[^a-zA-Z 0-9]+/g,'');
				if($.trim(resposta)=="Invlido")
				{  $.facebox("Login Inv&aacute;lido !"); }
				else
				{ location.href = "http://"+$website_patch+"/LponSite/docentes/Main.php"; }
			});
		}
		else if($("#combo_form").val() == 2){
			$.post("http://"+$website_patch+"/LponSite/Lpon/pagValidaBI.php",
			{
				passwd:$("#login_bi").val()
			},
			function(data)
			{
				var resposta = $(data).find('span.style4').text();
				resposta =  resposta.replace(/[^a-zA-Z 0-9]+/g,'');
				if($.trim(resposta)=="Login Inexistente em Base de Dados")
				{ $.facebox("Login Inv&aacute;lido !"); }
				else
				{ location.href = "http://"+$website_patch+"/LponSite/Lpon/Main.php"; }
			});
		}
		
	
	});

	
	$("#images_slider img").parent().fadeIn(500);
	setInterval( "slideSwitch()", 4000 );
});

var $image_active = 1;

function slideSwitch() 
{
	$image_active = $image_active+1;
	if($("#image_slide-"+$image_active).length == 0){$image_active=1;}
	
	var new_image = $("#image_slide-"+$image_active).val();
	
	$("#images_slider").css({filter:'0'});
	$("#images_slider").fadeOut(400, function(){
		$("#images_slider img").attr("src", new_image).parent().fadeIn(500);			
	});
}


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

$.fn.disableSelection = function() {
    $(this).attr('unselectable', 'on')
           .css('-moz-user-select', 'none')
           .each(function() { 
               this.onselectstart = function() { return false; };
            });
};

