// functions that only apply to fix IE6/IE7 problems
$(function(){
	var ua = navigator.userAgent;
	
	if(ua.match(/msie (\d+)/i)) {
	    $(".jpngfix").pngfix();
	
		$(".employees-list").each(function(i){
			// fixes heights on employees
			var items = $(this).children(".employee");
			var final_height = 0;

			items.each(function(i){
				if(i%2 == 0) {
					final_height = $(this).height();
				} else if(i%2 == 1) {
					if($(this).height() > final_height) {
						final_height = $(this).height();
						$(items[i-1]).css("height", final_height);
					} else {
						$(this).css("height", final_height);
					}
				}
			});
		});
		
		// fixes heights on glossary
		var glossary_items = $("#contents .glossary-box");
		var glossary_final_height = 0;

		glossary_items.each(function(i){
			if(i%2 == 0) {
				glossary_final_height = $(this).height();
			} else if(i%2 == 1) {
				if($(this).height() > glossary_final_height) {
					glossary_final_height = $(this).height();
					$(glossary_items[i-1]).css("height", glossary_final_height);
				} else {
					$(this).css("height", glossary_final_height);
				}
			}
		});
		
		
	}
});

$(function(){
	// fixes title heights on News
	var items = $("#news-list .news-element h2");
	var final_height = 0;

	items.each(function(i){
		if(i%3 == 0) {
			final_height = $(this).height();
		} else if(i%3 == 1) {
			if($(this).height() > final_height) {
				final_height = $(this).height();
			}
			if (i == items.length-1) {
				$(items[i]).css("height", final_height);
				$(items[i-1]).css("height", final_height);
			}
		} else {
			if($(this).height() > final_height) {
				final_height = $(this).height();
			}
			$(items[i]).css("height", final_height);
			$(items[i-1]).css("height", final_height);
			$(items[i-2]).css("height", final_height);
		}
	});
});

$(function(){
	// fixes heights on News
	var items = $("#news-list .news-element");
	var final_height = 0;

	items.each(function(i){
		if(i%3 == 0) {
			final_height = $(this).height();
		} else if(i%3 == 1) {
			if($(this).height() > final_height) {
				final_height = $(this).height();
			}
		} else {
			if($(this).height() > final_height) {
				final_height = $(this).height();
			}
			$(items[i]).css("height", final_height);
			$(items[i-1]).css("height", final_height);
			$(items[i-2]).css("height", final_height);
		}
	});
});

$(function(){
	// fixes heights on faq
	var faq_items = $("#faq-list .faq-element");
	var faq_final_height = 0;

	faq_items.each(function(i){
		if(i%2 == 0) {
			faq_final_height = $(this).height();
		} else if(i%2 == 1) {
			if($(this).height() > faq_final_height) {
				faq_final_height = $(this).height();
				$(faq_items[i-1]).css("height", faq_final_height);
			} else {
				$(this).css("height", faq_final_height);
			}
		}
	});
	
	var calculate_window = function() {
		var window_width = $(window).width()-40;
		var window_height = $(window).height()-40;
		
		$("#financial-report-fullscreen-close")
			.css("width", window_width);
			
		$("#financial-report-fullscreen-content")
			.css("width", window_width)
			.css("height", window_height-30);
			
		$("#financial-report-fullscreen")
			.css("width", window_width)
			.css("height", window_height);
			
		$("#financial-report-fullscreen-overlay")
			.css("width", $(window).width())
			.css("height", $(window).height());
		
		$("#fs-aux-wrapper")
			.css("width", $(window).width())
			.css("height", $(window).height())
			.css("overflow", "hidden");
	}
	
	var set_fullscreen = function() {
		$("#open-report-fullscreen").click(function(){
			var report = $("#financial-report").html();

			$("body").wrapInner("<div id='fs-aux-wrapper'></div>");

			$("body").append("<div id='financial-report-fullscreen' style='display: none;'><div id='financial-report-fullscreen-close'><a href='#' id='close-fullscreen'>fechar janela</a></div><div id='financial-report-fullscreen-content'></div></div><div id='financial-report-fullscreen-overlay' style='display: none;'></div>");

			calculate_window();

			$("#financial-report-fullscreen-close")
				.append("<h3>" + $("#financial-title").html() + "</h3>");

			$("#financial-report-fullscreen-content")
				.html(report);

			$("#financial-report-fullscreen")
				.fadeIn('700');

			$("#financial-report-fullscreen-overlay")
				.css("display", "block");
			
			$("select").addClass("hidden");
				
			$("#close-fullscreen").click(function(){
				$("#financial-report-fullscreen").add("#financial-report-fullscreen-overlay").remove();
				var document_content = $("#fs-aux-wrapper").html();
				$("body")
					.empty()
					.append(document_content);
					
				$("select").removeClass("hidden");

				set_fullscreen();

				return false;
			});
		});
	};
	
	set_fullscreen();
	
	var resizeTimer;
	
	$(window).bind('resize', function() {
		clearTimeout(resizeTimer);
		resizeTimer = setTimeout(calculate_window, 100);
	});
});

$(function(){
	if($('#flash'))
		setTimeout(function(){$('#flash').fade(800)},4000); 
});

$(function(){
	$("#faq-list").find(".faq-element:odd").addClass("odd");
	$("#contents .employees-list").find(".employee:even").addClass("even");
	$("#contents .employees-list").find(".employee:odd").addClass("odd");
	$("#news-list")
		.find(".news-element:nth-child(3n)")
			.addClass("last")
			.end()
		.find(".news-element:nth-child(3n-2)")
			.addClass("first");
	$("#documents-list").find("h3:first").addClass("first");
});

$(function() {
	title_element = $("#contents h1");

		title = $(title_element).text();
		
		$("#contents h1")
			.css("margin-top", 0)
			.css("margin-bottom", 0)
			.css("position", "absolute");
		
		$(title_element)
			.wrap("<div id='page-title-wrapper'></div>")
			.after("<span id='page-title-shadow'>" + title + "</span>");
});

$(function(){
	$("li.register a", "#access")
		.mouseover(function(){
			$("#access").css("z-index", 1001);
			$(this).parents("li").addClass("over");
		})
		.mouseout(function(){
			$("#access").css("z-index", 50);
			$(this).parents("li").removeClass("over");
		});
});

$(function(){    
	$('#menu-top li:last').addClass("last");
	$('#sections li:first').addClass("first");
	$('#sections li:last').addClass("last");
	$('#sections > ul > li')
		.mouseover(function(){          
			$(this).addClass("over");
		})
		.mouseout(function(){
			$(this).removeClass("over");
		});
	$("#sections > ul > li").each(function(){
		if($(this).find("li").length == 0) {
			$(this).find("ul").css("display", "none");
		}
	});
});

$(function(){
  $(".glossary-box:even").addClass("even");
  $(".glossary-box li:last").addClass("last");
});

$(function(){
	$("input[type='text']").add("textarea").add("input[type='password']")
		.focus(function(){
			$(this).addClass("focus");
		})
		.blur(function(){
			$(this).removeClass("focus");
		});
});

$(function(){
	$('#char')
		.click(function(){          
			$(this).attr("value","");
		})
		.blur(function(){
			if($(this).val() == ""){
				$(this).attr("value", "buscar no site");
			}
		});
	$("#text")
		.click(function(){          
			$(this).attr("value","");
		})
		.blur(function(){
			if($(this).val() == ""){
				$(this).attr("value", "buscar no glossário");
			}
		});
});

$(function(){
  $('#filter_hotel')
    .change(function(){
        var selected = $("#filter_hotel option:selected");		
       if(selected.val() != 0) 
       {
         $("#ajax-loader").fadeIn();
         $.ajax({
            type: "GET",
            url: "/user_apartments/get_unit?ap=" + selected.val(),
            dataType: "script"
          });     
       }
    })
});


$(function(){
  $('#filter_kind')
    .change(function(){
        var selected = $("#filter_kind option:selected");		
       if(selected.val() != 0)
         window.location = "/secao/3-noticias/" + selected.val();
    })
});

$(function(){
  $('#submit_search')
    .click(function(){
       var value = $('#char').attr("value");
       if(value == 'buscar no site' || value == 'digite o texto')
         return false;
       if(value == '')
       {
         $('#char').attr("value","digite o texto");
         return false;
       }       
    })
});

$(function(){
    $('#table-list tr')
      .mouseover(function(){
          $(this).addClass('over');
      })
      .mouseout(function(){
          $(this).removeClass('over');
      });
});

$(function(){
  $('.menu-account ul li .parent')
    .click(function()
    {
      $('.menu-account ul li').children('ul').hide();
      $(this).parent('ul li').children('ul').toggle();
    })
});

function loading(message) 
{ 
	var contents = "<div id='flash_message_contents'>" + message + "</div>";
	$("#flash_message")
		.append(contents)
		.css("display", "block");
	//	.slideDown("normal");
}

function closeLoading(message)
{
  $('#loading').html('<a href="#" onclick="hideLoading()">' + message + '  Clique aqui e tente novamente.</a>');
  hideLoading(7000);
}

function closeMessage() {
	$("#flash_message").fadeOut("normal");
}

function hideLoading(time)
{
	var timeout;
	timeout = setTimeout(closeMessage, time);
	$("#flash_message")
		.mouseover(function(){
			clearTimeout(timeout);
		})
		.mouseout(function(){
			timeout = setTimeout(closeMessage, time);
		});
}

function redirectToPage(page) {
  document.location.href = page;
}

$(function(){
  $('#category_id')
    .change(function(){
        var selected = $("#category_id option:selected");		
       if(selected.val() != 0)
         window.location = "/relatorios/documentos?category=" + selected.val();
    })
});

$(function(){
  $('#study_id')
    .change(function(){
        var selected = $("#study_id option:selected");		
       if(selected.val() != 0)
         window.location = "/relatorios/estudos-de-mercado?s=" + selected.val();
    })
});

$(function(){
  $('#market_value_id')
    .change(function(){
        var selected = $("#market_value_id option:selected");		
       if(selected.val() != 0)
         window.location = "/relatorios/valores-de-mercado?v=" + selected.val();
    })
});

$(function(){
    $("#checkAll").click(function(){
        checkboxs_mark(true)
    });
});

$(function(){
    $("#uncheckAll").click(function(){
        checkboxs_mark(false)
    });
});

function checkboxs_mark(value)
{
  $("#checkboxs").find("input[@type$='checkbox']").each(function(){
          this.checked = value;
  });
}

$(function(){
  $('#evolution_hotel_id')
    .change(function(){
       var selected = $("#evolution_hotel_id option:selected");		
       if(selected.val() != 0)
         window.location = "/evolucao-da-concorrencia-por/" + selected.val();
    })
});

$(function(){
  $('#filter_archives')
    .click(function(){
        var month = $("#date_month_2i option:selected");		
        var year = $("#date_year option:selected");
        var url = window.location.toString();
        var items = url.split('/');
        url = "/relatorios/" + items[4]
        window.location = url + "/" + month.val() + "/" + year.val();
    })
});

$(function(){
  $('#item_position')
    .change(function(){
      check_page_picture()
    })
});

function check_page_picture()
{
   var selected = $("#item_position option:selected");		
   if(selected.val() == 3)
    $('#page_picture').show()
   else
     $('#page_picture').hide()
}

$(function(){
  $('.color')
    .click(function(){
      var color = $(this).attr("bgcolor")
      $("#item_color").attr("value", color);
      $("#block_color").css("background-color", color)
    })
});


$(function(){
  $('#item_admin')
    .change(function(){
     moveAdmin()
    })
});

function moveAdmin()
{
   var selected = $("#item_admin option:selected");     
  if(selected.val() == "true")
    $("#admin_fields").show();
  else
    $("#admin_fields").hide();
}