var main = {
	onLoad: function(){
		//:last-child SubMenu
		main.lastChild('#menu > li > .sub-menu > li > a', {'border-bottom' : '0px', 'padding' : '6px 0 10px 0'});
		main.lastChild('#menu > li > .sub-menu > li > .sub-sub-menu > li > a', {'border-bottom' : '0px', 'padding' : '6px 0 5px 0'});
		
		//Slide Start
		$('#home-slide').nivoSlider({
			effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
			animSpeed:1000,
			pauseTime:6000,
			startSlide:0, //Set starting Slide (0 index)
			directionNav:false, //Next & Prev
			directionNavHide:false, //Only show on hover
			controlNav:false, //1,2,3...
			keyboardNav:false, //Use left & right arrows
			pauseOnHover:true, //Stop animation while hovering
			captionOpacity:0.8
		});
		
		//:last-child Footer
		main.lastChild('.footer-nav-left > li > a', {'border-right' : '0px'});
		main.lastChild('.footer-nav-right > li', {'border-right' : '0px', 'margin' : '0px', 'padding' : '0px'});
		
		
			
		var sfEls = document.getElementById("menu").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}

		//$("#menu").find("li > ul").hide();
		
		/*//Menu - Sub Menu - Show / Hide
		$("#firstlevel-1").mouseover(function(){
			main.menu_show_hide('firstlevel-1', 'secondlevel-1', 'show');
		}).mouseout(function(){
			main.menu_show_hide('firstlevel-1', 'secondlevel-1', 'hide');
		});
		$("#secondlevel-1").hide().mouseover(function(){
			main.menu_show_hide('firstlevel-1', 'secondlevel-1', 'show');
		}).mouseout(function(){
			main.menu_show_hide('firstlevel-1', 'secondlevel-1', 'hide');
		});
		$("#thirdlevel-1").mouseover(function(){
			main.menu_show_hide('thirdlevel-1', 'thirdlevel-01', 'show');
		}).mouseout(function(){
			main.menu_show_hide('thirdlevel-1', 'thirdlevel-01', 'hide');
		});
		$("#thirdlevel-01").hide().mouseover(function(){
			main.menu_show_hide('thirdlevel-1', 'thirdlevel-01', 'show');
		}).mouseout(function(){
			main.menu_show_hide('thirdlevel-1', 'thirdlevel-01', 'hide');
		});*/
				
		//BOXES EFFECTS
		$(".firstbox, .secondbox, .thirdbox, .fourbox").mouseover(function(){
			$(this).stop();
			$(this).animate({marginTop: '-186px'}, {duration: 1000});
		}).mouseout(function(){
			$(this).stop();
			$(this).animate({marginTop: '-44px'}, {duration: 1000});
		});

                //NEWS
                if ( $(".news-list").length > 0 ) {
                    $(".news-list").hide().first().show();
                    setInterval( main.nextNews , 6000 );
                }
		
		//Fix height boxes
		var h1 = $("#sub-menu-b").height();
		var h2 = $(".section-content").height();
		var h3 = $(".section-widgets").height();



                var news = $("#news").height();
                h3 = (h3 - (news - 85));


		if ( h1 > h2 && h1 > h3 ) {
			$("#sub-menu-b, .section-content").height(h1);
			$(".section-widgets").height(h1-12);
		} else if ( h2 > h1 && h2 > h3 ) {
			$("#sub-menu-b, .section-content").height(h2);
			$(".section-widgets").height(h2-12);
		} else if ( h3 > h1 && h3 > h2 ) {
			$("#sub-menu-b, .section-content").height(h3+12);
		}
	},
        actualNews: 0,
        nextNews: function(){
            main.actualNews = main.actualNews+1;
            var total = $(".news-list").length;

            if ( main.actualNews > total ) {
                $(".news-list").hide().first().show();
                main.actualNews = 0;
            } else {
                $(".news-list").hide();
                $("#"+main.actualNews).show();
            }

            
        },
	lastChild: function(identificator, style){
		$(identificator).last().css(style)
	},
	menu_show_hide: function(el, sub_menu, action){
		if ( action == "show" ) {
			$("#"+el).addClass("on");
			$("#"+sub_menu).show();
		} else if ( action == "hide" ) {
			$("#"+el).removeClass("on");
			$("#"+sub_menu).hide();
		}
	},
	loadSubMenu:function() {
		var html = '<li><a href="#" class="galery-video">Galer&iacute;a de Videos</a></li><li><a href="#" class="documents">Documentos</a></li>';

                if($("#sub-menu-b li").length > 1){
                    $("#sub-menu-b").append(html);
                    $('#sub-menu-b .galery-video').css('margin-top','140px');
                    
                }else{
                    $("#sub-menu-b").append(html);
                }
	},
        addClassLast : function(){
            $menu = $('.sub-menu-c');
            if($menu.length == 1){
                $lis = $('.sub-menu-c li');
                $('.sub-menu-c li')[($lis.length -1)].className = 'last';
            }
        },
        fixLIDoubleLine : function(){
            $menu = $('#sub-menu-b li a');
            if($menu.length > 0){
                for($i = 0; $i < $menu.length; $i++){
                    text = $menu[$i].innerHTML;
                    if(text.length > 26){
                        if($menu[$i].className != ''){
                           $menu[$i].className = $menu[$i].className + ' double-line';
                        }else{
                            $menu[$i].className = 'double-line';
                        }
                    }
                }
            }
        },
        addFilesMenuFunctions : function(){
            
            var documents = $(".documents-container > ul.file-list > li").length;
            if ( documents > 4 ) {
                $('.documents-container .file-list').css('margin-top', '-110px');
            }

            if ( documents > 8 ) {
                $('.documents-container .file-list').css('margin-top', '-180px');
            }

            if ( documents > 12 ) {
                $('.documents-container .file-list').css('margin-top', '-280px');
            }
            

            if ( $('.gallery-container > ul.file-list > li').length == 0 ) {
                if ( $("ul#sub-menu-b > li").length <= 2 ) {
                    $('.documents-container').css('margin-top', '20px');
                } else {
                    $('.documents-container').css('margin-top', '140px');
                }  
            }

            var videos = $('.gallery-container > ul.file-list > li').length;

            if ( videos > 0 ) {
                if ( $("ul#sub-menu-b > li").length > 3 ) {
                    $('.gallery-container .file-list').css('margin-top', '140px');
                }
            }


            if ( videos > 4 ) {
                $('.gallery-container .file-list').css('margin-top', '-110px');
            }

            if ( videos > 8 ) {
                $('.gallery-container .file-list').css('margin-top', '-100px');
            }

            if ( videos > 12 ) {
                $('.gallery-container .file-list').css('margin-top', '-280px');
            }


            $('.documents').mouseover(function(){
                $('.documents-container .file-list').show();
                
            }).mouseout(function(){
                $('.documents-container .file-list').hide();
            });
            $('.documents-container .file-list').mouseover(function(){
                $('.documents-container .file-list').show()
            }).mouseout(function(){
                $('.documents-container .file-list').hide()
            });
            $('.galery-video').mouseover(function(){
                $('.gallery-container .file-list').show();

            }).mouseout(function(){
                $('.gallery-container .file-list').hide();
            });
            $('.gallery-container .file-list').mouseover(function(){
                $('.gallery-container .file-list').show()
            }).mouseout(function(){
                $('.gallery-container .file-list').hide();
            });

            $('.file-list').mouseout(function(){
                $('.file-list').hide();
            });
        }
}

$(document).ready(function(){
    //main.loadSubMenu();
    setTimeout(main.onLoad,100);
    main.addClassLast();
    main.fixLIDoubleLine();
    main.addFilesMenuFunctions();
});
