﻿	function delayCutText(){ 
		$('#calendar').contents().find('td.description').each(function(){
			html = $(this).html().substr(0, 160);
			$(this).html(html + '(...)');
		});
		//alert('teste');
	}
	
	window.setTimeout("delayCutText()", 2000);

$(function () {
	
	
	
	
	/* Newsletter auto complete hidden name box */
    $('#AccessUserEmail_4').change(function(){
      $('#AccessUserName_4').val($(this).val());
    });
  
    if ( $.browser.webkit ) {
      $( '#PreviousMonth a, #NextMonth a' ).css( 'margin-top', '15px' );
    }
    $('.links:first').css("border", "0");
    $('.lowerdivs:first').css("border-left","0");

    /* Submenu Fix */ 
    var width = 0;
    var big = 0;
    $('.subpags').find('.level2').each(function(){
        $(this).css("display","block");
        $(this).find("a").each(function() {
            width = $(this).width();
            if (width > big) {
                big = width;
            }
        });
        $(this).width(big+20);
        big = 0;
        $(this).css("display","");
    });
    /* End fix */

    $('.rightColumnHome .ParagraphText img:first').css("padding-top", "2px");

    /* This code resolves the main titles of site ex: 01.(bold) News(regular font weight) 
    var inteiro;
    var string_news;

    $('#elements .h2').each(function() {
        integer = $(this).text().substring(0, 3);
        string_news = $(this).text().substring(3, 100);
        $(this).html("<strong>" + integer + "</strong>" + string_news);
    });
   End */

    /* Breadcrumb fix */
      $('#Breadcrumb .level1 li').css("border-left","none");
      $('#Breadcrumb li .firstitem').prepend(">");
      $('#Breadcrumb .level1 li a:first').css("margin-left","6px");
    /* End fix */

    /* this force a strong element on the first 3 string on h2
      $('.leftColumnHome .h2, .rightColumnHome .h2').each(function() {
        inteiro = $(this).text().substring(0, 3);
        string_noticia = $(this).text().substring(4, 100);
        $(this).html("<strong>" + inteiro + "</strong>" + string_noticia);
      });
*/

    /* form */
      $('form[name=postform1] td:last').css({"background" : "url('Files/Templates/Designs/cmchaves/images/main/dots.png')repeat-x center 4px", "padding-top" : "10px"});
      $('input[name=nome]').val("Nome");
      $('input[name=E-mail]').val("E-mail");
      $('textarea[name=observacoes]').val("Observações");
    /* End */

    $('.today_agenda li p:odd').css({"position":"relative","right":"9px"});
    $('.cultural_agenda img:first').css("margin-left", "14px");
    //$('.info li:last').css({"list-style-type" : "none", "margin-left" : "-52px", "background-image" : "none"});
    $(".project_table td:last").css("background-image", "none");
    $(".today_agenda li:even").addClass("today_list_title");
    $(".pdf table td:first").css({"border-right" : "1px solid #a0a0a0", "padding-right" : "3px"});
    $('.pageNumbers a:last').css("border-right", "none");

    /* End */


  /* This is to format "Projectos" in 2 columns */
  if ($("#projectos").size() !=0) {
    var  i = 0;
    $("#projectos").prepend("<div class='projectLeft'></div><div class='projectRight'></div>");
    $("#projectos").find("li").each( function() {
      if (i == 0) {
        $(this).appendTo(".projectLeft");
        i=1;
      }
      else {
        $(this).appendTo(".projectRight");
        i=0;
      }
    });
  }

  /* This is to format "Turismo" in 3 columns */
  if ($("#turismo_map").size() !=0) {
    var  i = 1;
    $("#turismo_map").prepend("<div class='turismo1'></div><div class='turismo2'></div><div class='turismo3'></div>");
    $("#turismo_map").find("li").each( function() {
      $(this).appendTo(".turismo" + i);
      if (i == 3) i = 0;
      i++;
    });
  }
  /* End */
  
  
  // Weather forecast
  getWeatherForecast();


  /* tooltips Overall Menus */

  if ($("#MainMenu .level2 a[title], #LeftMenu .level1 a[title]").length) {
    $("#MainMenu .level2 a[title], #LeftMenu .level1 a[title]").tooltip({
      position: "center right",
      offset: [2, 10],
      effect: "fade"
    });
  }
});

function inquerito() {
  $('.alertPoll').fadeOut("fast");
  return true;
}

function addBookmark(title,url) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url,"");
        return true;
    } else if( document.all ) {
        window.external.AddFavorite( url, title);
        return true;
    }
    else {
        return false;
    }
}
function checkIfEmpty( input, defaultValue ) {
    if( ( $('#'+input).val() == "" ) || $('#'+input).val().toLowerCase() == defaultValue.toLowerCase() )
        $('#'+input).val( defaultValue );
}

function clearField( input, defaultValue ) {
    if( ( $('#'+input).val() == "" ) || ( $('#'+input).val().toLowerCase() == defaultValue.toLowerCase() ) )
        $('#'+input).val("");
}

function ValidateSearch( errorMessage, defaultValue ) {
  if( ( $('#motorPesquisa').val() == "") || ( $('#motorPesquisa').val().toLowerCase() == defaultValue.toLowerCase() ) ) {
        return generalAlert(errorMessage);
    }
    else  {
        return true;
    }
}

function generalAlert(errorMessag) {
  $("#DwMain").append("<div class='alertSearch'><a href='#' onclick='return closeSearchAlert();'>X</a><span>" + errorMessag + "</span></div>");
  $(".alertSearch").overlay({
    top: 260,
    mask: {
      color: '#fff',
      loadSpeed: 200,
      opacity: 0.5
    },
    closeOnClick: false,
    load: true
  });
  return false;
}

function closeSearchAlert() {
  $('.alertSearch').fadeOut("slow", function() { $(this).remove(); });
  return true;
}

function validatePoll() {
  var defined = $('#query_home form input:radio:checked').val();
  if(defined > 0) {
      return true;
  }
  else {
      $(".alertPoll").fadeIn("fast");
    return false;
  }
}

function updateImage(imageURL, title) {
  var copy = $("#updateImage").text($("#updateImage").html());
  if ($("#updateImage img").size() != 0) {
    $("#updateImage img").attr("src", "/admin/public/getimage.aspx?Width=373&Height=216&Crop=0&Image=" + imageURL);
    $("#updateImage p").html("<p>" + title + "</p>");
  }
  else {
    $("#updateImage").html("<img src='/admin/public/getimage.aspx?Width=373&Height=216&Crop=0&Image=" + imageURL + "' alt='" + title + "' height='216' width='373' /><p>" + title + "</p>");
  }
}
function displayEscuta() {
  $("#showThisDiv").fadeIn("slow");
 /* $("#showThisDiv").find("img").text($("#listen .h2").text().substr(0, 12));*/
  $("#listen").next().next().css("z-index" , "-10");
  $("#listen img.paragraphimage").css({"position":"absolute", "top": "0", "left": "128px" });
  $("#showThisDiv").height($("form[name=postform3]").parent().parent().outerHeight(true)+28);
}
function hideEscuta() {
  $("#showThisDiv").fadeOut("slow");
  $("#listen").next().next().css("z-index", "1");
}
 
function getWeatherForecast() {
  $.ajax({ url: location.protocol + "//" + location.hostname + "/public/readxml.aspx?PageUrl=http://weather.yahooapis.com/forecastrss?w=739513&u=c",
    success: function(data) {
      var xml,
          xmlElement = "yweather\\:forecast",
          date = "#Today",
          weatherCode = "",
          wheatherCondition = "";
      if ($.browser.msie && typeof data == "string") {
        xml = new ActiveXObject("Microsoft.XMLDOM");
        xml.async = false;
        xml.loadXML(data);
      }
      else {
        xml = data;
      }
      
      $(xml).find(xmlElement).each(function(i) {
        if (i == 1) date = "#Tomorrow";
        weatherCode = $(this).attr("code");
        //weatherCondition = getWeatherTrans(weatherCode);
        $(date + " img").attr("src", "Files/Templates/Designs/cmchaves/images/weather/" + weatherCode + ".gif");
        //$(date + " img").attr("alt", weatherCondition);
        //$(date + " img").attr("title", weatherCondition);
        $(date + " .minTemp").text($(this).attr("low") + "ºC");
        $(date + " .maxTemp").text($(this).attr("high") + "ºC");
      });
    }
  });
}
 
jQuery.fn.wrapInChunks = function(html, chunkSize) {
  chunkSize = chunkSize || 1;
  var items = this.get(),
    rows = [],
    cur = rows[0] = $(html);
  while (items[0]) {
    if (rows[rows.length - 1].children().length === chunkSize) {
      cur = rows[rows.length] = $(html);
    }
    cur.append( items.shift() );
  }
  return this.pushStack(rows);
};


