var LiferayJedi = function () {
	var $ = jQuery;
	return {
		init: function() {
			var instance = this;

			instance.handleSearchForm();
			instance.dropDownMenu();
			instance.handleLastChild();
		},

		handleSearchForm: function() {
			var searchForm = $('#banner .search');

			var searchInput = searchForm.find('input[@type=image]');
			var searchLink = $('<a class="search-input-link" href="javascript:;"></a>');

			searchLink.click(
				function() {
					$(this).parents('form')[0].submit();
				}
			);

			searchInput.hide();
			searchInput.before(searchLink);
		},

		handleLastChild: function () {
			var instance = this;

			$('#footer ul li:last').addClass('last-child');
		},

		dropDownMenu: function() {
			$(".parent-nav-item").hoverIntent(
				{
					interval: 25,
					timeout: 0,
					over: function () {
						var instance = $(this);
						var child = $('.child-menu', this);

						instance.addClass("init");
						child.slideDown(100);
					},
					out: function () {
						var instance = $(this);
						var child = $('.child-menu', this);
						child.slideUp(50);
						instance.removeClass("init");
					}
				}
			);
		}
	};
}();

jQuery(document).ready(
	function() {
		LiferayJedi.init();
	}
);

function change_visibility(element_id)
{

  if(document.getElementById(element_id).style.display == "none")
  {
    document.getElementById(element_id).style.display= "";
  }
  else
  {
    document.getElementById(element_id).style.display= "none";
  }
}

function getHistoryEvents(year) {
        var feed = new google.feeds.Feed("http://www.google.com/calendar/feeds/multicert.eventos%40gmail.com/public/full?ctz=Europe/Lisbon&start-min="+year+"-01-00T00:00:00&start-max="+(year+1)+"-01-01T00:00:00&orderby=starttime&sortorder=ascending");
        feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
        feed.load(function(result) {
            var container = document.getElementById("feed");
            container.innerHTML = "";
            var h2 = document.createElement("h2");
            h2.appendChild(document.createTextNode("Eventos "+year+":"));
            container.appendChild(h2);
            var ul = document.createElement("ul");
            
            if (!result.error) {
                var entries = google.feeds.getElementsByTagNameNS(result.xmlDocument, "http://www.w3.org/2005/Atom", "entry");
                for (var i = 0; i < entries.length; i++) {
                    var titleEventElement = google.feeds.getElementsByTagNameNS(entries[i], "http://www.w3.org/2005/Atom", "title")[0];
                    var eventDescriptionElement = google.feeds.getElementsByTagNameNS(entries[i], "http://www.w3.org/2005/Atom", "content")[0];
                    var eventDateElement = google.feeds.getElementsByTagNameNS(entries[i], "http://schemas.google.com/g/2005", "when")[0];
                    var eventLocationElement = google.feeds.getElementsByTagNameNS(entries[i], "http://schemas.google.com/g/2005", "where")[0];
                    
                    var eventLocationElementAttributes = eventLocationElement.attributes;
                    var eventDateElementAttributes = eventDateElement.attributes;
                    
                    var title             = titleEventElement.firstChild.nodeValue;
                    var description       = eventDescriptionElement.firstChild.nodeValue;
                    var startDate         = eventDateElementAttributes[0].firstChild.nodeValue.split('T')[0];
                    var endDate           = eventDateElementAttributes[1].firstChild.nodeValue.split('T')[0];
                    var eventLocation     = eventLocationElementAttributes[0].nodeValue;

                    var li = document.createElement("li");
                    var strong = document.createElement("strong");

                    var rootElement = li; 
                    if(title == 'Alfresco Iberia Roadshow')
                    { 
                        var a = document.createElement("a");
                        var href = document.createAttribute("href");
                        href.nodeValue = "http://www.alfresco.com/es/about/events/2008/10/lisboaroadshow/";
                        a.setAttributeNode(href);
                        li.appendChild(a);
                        rootElement = a; 
                    }
                    if(startDate == endDate || startDate == eventDateElementAttributes[0].firstChild.nodeValue || startDate == null)
                    {
                        strong.appendChild(document.createTextNode(startDate+' - '+title));
                        rootElement.appendChild(strong);
                        rootElement.appendChild(document.createTextNode(' - '+eventLocation));
                    }
                    else
                    {
                        strong.appendChild(document.createTextNode('De '+startDate+' a '+endDate+' - '+title));
                        rootElement.appendChild(strong);
                        rootElement.appendChild(document.createTextNode(' - '+eventLocation));
                    }
//                    if(title == 'Alfresco Iberia Roadshow')
//                    { 
//                        
//                        var a = document.createElement("a");
//                        var href = document.createAttribute("href");
//                        var p = document.createElement("p");
//
//                        href.nodeValue = "http://www.alfresco.com/es/media/coverage/2008/10/alfrescoiberiaroadshow3/";
//                        a.setAttributeNode(href);
//                        a.appendChild(document.createTextNode(description));
//                        p.appendChild(a);
//                        li.appendChild(p);
//                    }
//                    else
//                    {
                        var p = document.createElement("p");
                        p.appendChild(document.createTextNode(description));
                        li.appendChild(p);
//                    }
                    ul.appendChild(li);
                    container.appendChild(ul);
                }
                var a = document.createElement("a");
                var href = document.createAttribute("href");
                var p = document.createElement("p");
				href.nodeValue = "eventos";
                a.setAttributeNode(href);
                a.appendChild(document.createTextNode("\u00ab Ir para lista de eventos"));
                p.appendChild(a);
                container.appendChild(p);
            }
        });
    }


    function getEventsbyContent(eventType, feed) {
        var eventsContainer  = document.getElementById(eventType);
        var title_event      = document.getElementById("title_"+eventType);
        feed.setResultFormat(google.feeds.Feed.XML_FORMAT);
        feed.load(function(result) {
            if (!result.error) {
                var entries = google.feeds.getElementsByTagNameNS(result.xmlDocument, "http://www.w3.org/2005/Atom", "entry");
		if(entries.length == 0)
		{
			removeElement("title_"+eventType);
		}
		else
		{
                for (var i = 0; i < entries.length; i++) {
                    var titleEventElement 		= google.feeds.getElementsByTagNameNS(entries[i], "http://www.w3.org/2005/Atom", "title")[0];
                    var eventDescriptionElement 	= google.feeds.getElementsByTagNameNS(entries[i], "http://www.w3.org/2005/Atom", "content")[0];
                    var eventDateElement 		= google.feeds.getElementsByTagNameNS(entries[i], "http://schemas.google.com/g/2005", "when")[0];
                    var eventLocationElement 		= google.feeds.getElementsByTagNameNS(entries[i], "http://schemas.google.com/g/2005", "where")[0];
                    
		    if(titleEventElement != null &&  eventDateElement != null && eventLocationElement)
		    {	
                    	var eventLocationElementAttributes 	= eventLocationElement.attributes;
                    	var eventDateElementAttributes 		= eventDateElement.attributes;
                    	if(titleEventElement.firstChild != null)
			{
                    		var title          		= titleEventElement.firstChild.nodeValue;
				var description			= null;
				if(eventDescriptionElement != null && eventDescriptionElement.firstChild != null)
				{
                    			description    		= eventDescriptionElement.firstChild.nodeValue;
				}	

				if(	eventDateElementAttributes[0] != null && eventDateElementAttributes[0].firstChild != null &&
					eventDateElementAttributes[1] != null && eventDateElementAttributes[1].firstChild != null &&
					eventLocationElementAttributes[0] != null)
				{
                    			var startDate		= eventDateElementAttributes[0].firstChild.nodeValue.split('T')[0];
                    			var endDate     	= eventDateElementAttributes[1].firstChild.nodeValue.split('T')[0];
                    			var eventLocation     	= eventLocationElementAttributes[0].nodeValue;
			
			
                        		var li = document.createElement("li");
                        		var strong = document.createElement("strong");
                        		var rootElement = li; 
                        		if(title == 'Alfresco Iberia Roadshow')
                        		{ 
                            			var a = document.createElement("a");
                            			var href = document.createAttribute("href");
                            			href.nodeValue = "http://www.alfresco.com/es/about/events/2008/10/lisboaroadshow/";
                            			a.setAttributeNode(href);
                            			li.appendChild(a);
                            			rootElement = a; 
                        		}

                        		if(startDate == endDate || startDate == eventDateElementAttributes[0].firstChild.nodeValue || startDate == null)
                        		{
                            			strong.appendChild(document.createTextNode(startDate+' - '+title));
                            			rootElement.appendChild(strong);
                            			rootElement.appendChild(document.createTextNode(' - '+eventLocation));
                        		}
                    			else
                    			{
                        			strong.appendChild(document.createTextNode('De '+startDate+' a '+endDate+' - '+title));
                        			rootElement.appendChild(strong);
                        			rootElement.appendChild(document.createTextNode(' - '+eventLocation));
                    			}
					if(description != null)
                    			{
						if(title == 'Alfresco Iberia Roadshow')
                    				{	 
                        
                        				var a = document.createElement("a");
                        				var href = document.createAttribute("href");
                        				var p = document.createElement("p");

                        				href.nodeValue = "http://www.alfresco.com/es/media/coverage/2008/10/alfrescoiberiaroadshow3/";
                        				a.setAttributeNode(href);
                        				a.appendChild(document.createTextNode(description));
                        				p.appendChild(a);
                        				li.appendChild(p);
                    				}
                    				else
                    				{
                        				var p = document.createElement("p");
                        				p.appendChild(document.createTextNode(description));
                        				li.appendChild(p);
                    				}
					}
                    			eventsContainer.appendChild(li);
				}
			}
                    }
		}
		}
            }
        });
         
    }
    
    function getStartMaxDate()
    {
        var date  = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() +1;
        if(month < 10)
            month = "0"+month;
        var day   = date.getDate();
        if(day < 10)
            day = "0"+day;
        
        return year+"-"+month+"-"+day;
    }
    
    function removeElement(elemId) {
  		var child_ = document.getElementById(elemId);
  		var parent_ = child_.parentNode;
  		parent_.removeChild(child_);
	}
        function insertUrl(url, elemId)
        {
                var elem = document.getElementById(elemId);
                elem.value = elem.value+"\n"+url;
        }

