$(document).ready(function() {
	
	$.preloadCssImages();
	
	$("#galeria_instrucao").hover(function(){
		$(this).animate({top: '210px', opacity: 0}, 1500);									   
	});
		
});

jQuery.fn.sortElements = (function(){
 
    var sort = [].sort;
 
    return function(comparator, getSortable) {
 
        getSortable = getSortable || function(){return this;};
 
        var placements = this.map(function(){
 
            var sortElement = getSortable.call(this),
                parentNode = sortElement.parentNode,
 
                // Since the element itself will change position, we have
                // to have some way of storing its original position in
                // the DOM. The easiest way is to have a 'flag' node:
                nextSibling = parentNode.insertBefore(
                    document.createTextNode(''),
                    sortElement.nextSibling
                );
 
            return function() {
 
                if (parentNode === this) {
                    throw new Error(
                        "Erro ao orientar o elemento."
                    );
                }
 
                // Insert before flag:
                parentNode.insertBefore(this, nextSibling);
                // Remove flag:
                parentNode.removeChild(nextSibling);
 
            };
 
        });
 
        return sort.call(this, comparator).each(function(i){
            placements[i].call(getSortable.call(this));
        });
 
    };
 
})();

jQuery.fn.sort = function() {  
    return this.pushStack( [].sort.apply( this, arguments ), []);  
};  

 function sortBaseCidade(a,b){  
     if (a.title._content == b.title._content){
       return 0;
     }
     return a.title._conten> b.title._content ? 1 : -1;  
 };  
  function sortBaseCidadeDesc(a,b){  
     return sortBaseCidade(a,b) * -1;  
 };

function carregarGaleria(idGaleria){
$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=62c4907229b19bc6137aa4d58848083e&photoset_id="+idGaleria+"&extras=description%2C+url_s&per_page=50&format=json&jsoncallback=?", function(data){
            $.each(data.photoset.photo, function(i,item){
                var newthumb = $("ul.thumbs").children("li:first").clone();
                var baseimg = item.url_s;
                
                var thumbimg = baseimg.replace("_m.jpg", "_s.jpg");
                $(newthumb).find("img").attr("src", thumbimg);
                
                var disimg = baseimg.replace("_m.jpg", ".jpg");
                $(newthumb).find(".thumb").attr("href", disimg);
                              
                var title = item.title;
                var description = item.description._content;
                
            
                $(newthumb).find(".image-title").empty().html(title);
                $(newthumb).find(".image-desc").empty().html(description);
                
                $("ul.thumbs").append(newthumb);
            });    
            
            $("ul.thumbs").children("li:first").remove();
            
            // Initially set opacity on thumbs and add
            // additional styling for hover effect on thumbs
            var onMouseOutOpacity = 0.67;
            $('#thumbs ul.thumbs li').opacityrollover({
                mouseOutOpacity:   onMouseOutOpacity,
                mouseOverOpacity:  1.0,
                fadeSpeed:         'fast',
                exemptionSelector: '.selected'
            });
            
            // Initialize Advanced Galleriffic Gallery
            var gallery = $('#thumbs').galleriffic({
                delay:                     2500,
                numThumbs:                 18,
                preloadAhead:              40,
                enableTopPager:            false,
                enableBottomPager:         true,
                maxPagesToShow:            7,
                imageContainerSel:         '#slideshow',
                controlsContainerSel:      '#controls',
                captionContainerSel:       '#caption',
                loadingContainerSel:       '#loading',
                renderSSControls:          false,
                renderNavControls:         true,
                playLinkText:              '',
                pauseLinkText:             '',
                prevLinkText:              'anterior',
                nextLinkText:              'pr&oacute;xima',
                nextPageLinkText:          'pr&oacute;xima',
                prevPageLinkText:          'anterior',
                enableHistory:             false,
                autoStart:                 false,
                syncTransitions:           true,
                defaultTransitionDuration: 900,
                onSlideChange:             function(prevIndex, nextIndex) {
					$("#slideshow").find('.current').css('position', 'absolute');
                    // 'this' refers to the gallery, which is an extension of $('#thumbs')
                    this.find('ul.thumbs').children()
                        .eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
                        .eq(nextIndex).fadeTo('fast', 1.0);
                },
                onPageTransitionOut:       function(callback) {
					
                    this.fadeTo('fast', 0.0, callback);
                },
                onPageTransitionIn:        function() {
                    this.fadeTo('fast', 1.0);
                }
            });  
			
			$('div.navigation').css({'width' : '550px', 'float' : 'left', 'clear' : 'both', 'margin' : '0 0 0 35px'});
   			$('div.content').css('display', 'block');
    });
};

function carregarPrimeiraGaleria(){
$.getJSON("http://api.flickr.com/services/rest/?method=flickr.people.getPublicPhotos&api_key=62c4907229b19bc6137aa4d58848083e&user_id=48846270%40N07&extras=description%2Curl_s&per_page=36&format=json&jsoncallback=?", function(data){
            $.each(data.photos.photo, function(i,item){
                var newthumb = $("ul.thumbs").children("li:first").clone();
                var baseimg = item.url_s;
                
                var thumbimg = baseimg.replace("_m.jpg", "_s.jpg");
                $(newthumb).find("img").attr("src", thumbimg);
                
                var disimg = baseimg.replace("_m.jpg", ".jpg");
                $(newthumb).find(".thumb").attr("href", disimg);
                              
                var title = item.title;
                var description = item.description._content;
                
            
                $(newthumb).find(".image-title").empty().html(title);
                $(newthumb).find(".image-desc").empty().html(description);
                
                $("ul.thumbs").append(newthumb);
            });    
            
            $("ul.thumbs").children("li:first").remove();
            
            // Initially set opacity on thumbs and add
            // additional styling for hover effect on thumbs
            var onMouseOutOpacity = 0.67;
            $('#thumbs ul.thumbs li').opacityrollover({
                mouseOutOpacity:   onMouseOutOpacity,
                mouseOverOpacity:  1.0,
                fadeSpeed:         'fast',
                exemptionSelector: '.selected'
            });
            
            // Initialize Advanced Galleriffic Gallery
            var gallery = $('#thumbs').galleriffic({
                delay:                     2500,
                numThumbs:                 18,
                preloadAhead:              40,
                enableTopPager:            false,
                enableBottomPager:         true,
                maxPagesToShow:            7,
                imageContainerSel:         '#slideshow',
                controlsContainerSel:      '#controls',
                captionContainerSel:       '#caption',
                loadingContainerSel:       '#loading',
                renderSSControls:          false,
                renderNavControls:         true,
                playLinkText:              '',
                pauseLinkText:             '',
                prevLinkText:              'anterior',
                nextLinkText:              'pr&oacute;xima',
                nextPageLinkText:          'pr&oacute;xima',
                prevPageLinkText:          'anterior',
                enableHistory:             false,
                autoStart:                 false,
                syncTransitions:           true,
                defaultTransitionDuration: 900,
                onSlideChange:             function(prevIndex, nextIndex) {
					$("#slideshow").find('.current').css('position', 'absolute');
                    // 'this' refers to the gallery, which is an extension of $('#thumbs')
                    this.find('ul.thumbs').children()
                        .eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
                        .eq(nextIndex).fadeTo('fast', 1.0);
                },
                onPageTransitionOut:       function(callback) {
					
                    this.fadeTo('fast', 0.0, callback);
                },
                onPageTransitionIn:        function() {
                    this.fadeTo('fast', 1.0);
                }
            });  
			
			$('div.navigation').css({'width' : '550px', 'float' : 'left', 'clear' : 'both', 'margin' : '0 0 0 35px'});
   			$('div.content').css('display', 'block');
    });
};

$(document).ready(function() {
		
	//hoverintent do menu
	function menuHoverOver(){
		if($(this).hasClass('nivel1')){
			$(this).children("a").stop().addClass('menu_selecionado').css('opacity', 0).animate({opacity: 0.4}, 'fast').show();
			
			if($(this).children("div").length != 0){
				$("#sub_menu_multimidia").show();
			}
		}
		else {
			$(this).children("a").stop().addClass('menu_selecionado').css('opacity', 0).animate({opacity: 0.4}, 'fast').show();
		}
	};
	
	function menuHoverOut(){
		if($(this).hasClass('nivel1')){
			$(this).children("a").stop().animate({opacity: 0}, 'fast', function() {
				$(this).removeClass('menu_selecionado'); 
			});
			
			if($(this).children("div").length != 0){
				$("#sub_menu_multimidia").hide();
			}
		}
		else {
			$(this).children("a").stop().animate({opacity: 0}, 'fast', function() {
				$(this).removeClass('menu_selecionado'); 
			});
		}
	};
	
	var menuHover = {    
		 sensitivity: 10, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 50, // number = milliseconds for onMouseOver polling interval    
		 over: menuHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 300, // number = milliseconds delay before onMouseOut    
		 out: menuHoverOut // function = onMouseOut callback (REQUIRED)    
	};
	
	$("#menu_superior li").hoverIntent(menuHover);
	
});
