jQuery.noConflict();

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)


jQuery(function(){
    
    /**
    * CUFON
    */
    Cufon.replace('.cufon-replace');
    
    /**
    * BANNERS
    */
    var bannersControlWidth      = jQuery('.banners-control').width();
    
    var bannerControlParentWidth = jQuery('.banners-control').parent().width();
    
    var left = (bannerControlParentWidth - bannersControlWidth)/2 ;
    
    jQuery('.banners-control').css('left', left);
    
    jQuery('#sliderRGA').DDSlider({
        
        nextSlide: '.control-next',
        prevSlide: '.control-prev',
        selector: '.control-pages'
        
    });
    
    
    jQuery('#control-page-1').addClass('current');
    
    var firstColor = jQuery('.control-page:first').attr('cor');
    
    jQuery('#content-faixa').css({'background-color': firstColor});
    
    jQuery('#sliderRGA').live('mudou',function(t, k){
        
        var curr = k.attr('rel');
        
        var goColor = k.attr('cor');
        
        jQuery('#content-faixa').animate({'background-color': goColor}, 1500);
        
        /*if(curr == 1){
           jQuery('#content-faixa').animate({'background-color': '#50463a'}, 1500); 
        } else if(curr == 2){
           jQuery('#content-faixa').animate({'background-color': '#616b50'}, 1500); 
        } else if(curr == 3){
           jQuery('#content-faixa').animate({'background-color': '#4c3a3b'}, 1500); 
        }*/
        
    });
    
    
   
    
    function verifyColor(){
        if(curr != 1 && jQuery('.slider_1').hasClass('current')){
           curr = 1;
           jQuery('#content-faixa').animate({'backgroundColor': '#697257'}); 
        } else if(curr != 2 && jQuery('.slider_2').hasClass('current')){
           curr = 2;
           jQuery('#content-faixa').animate({'backgroundColor': '#575734'}); 
        } else if(curr != 3 && jQuery('.slider_3').hasClass('current')){
           curr = 3;
           jQuery('#content-faixa').animate({'background-color': '#000000'}); 
        }
    }
   
    
    
    
    
    
    /**
    * Portfolio
    */
        jQuery('.portfolio-box:not(.portfolio-filme)').livequery(function(){
            
            jQuery(this).hover(function(){
            
                jQuery(this).addClass('hover');
                var el = this;
                    
                if(jQuery(el).hasClass('hover')){

                    var old_top =  jQuery(el).height() - 30;
                    jQuery(el).children('.legend').css({'top': old_top});

                    var top = jQuery(el).height() - jQuery(el).children('.legend').height();
                    jQuery(el).children('.legend').animate({'top': top}, 200);
                    
                }
                 
            }, function(){

                jQuery(this).removeClass('hover');
                var el = this;
                    
                var top =  jQuery(el).height() - 30;

                jQuery(el).children('.legend').animate({'top': top}, 200);
                
            });
        });
    
    
    jQuery('[href$=.jpg]').livequery(function(){
        jQuery(this).fancybox({
            'centerOnScroll': true,
            'overlayOpacity': 0.5,
            'overlayColor': '#000',
            'titleShow': false,
            'mouseWheelNavigation': false
        });
        jQuery('#fancybox-outer').livequery(function(){
            jQuery(this).css('background', 'none');    
        });
    });

    
    /**
    * Box search ...
    */
    

    jQuery('.box-search').hide();    

    
    jQuery('#lay-search').hover(function(){
        jQuery(this).addClass('hover');
        
        var el = this;
        
        setTimeout(function(){
            if(jQuery(el).hasClass('hover'))
                jQuery('.box-search').fadeIn();    
        }, 300);
        
         
    }, function(){
        jQuery(this).removeClass('hover');
        jQuery('.box-search').fadeOut();
    });
    
    
    
     
    /**
    * MENU AND SCROLL
    */
    jQuery('.goto').click(function(){
        
        var goto = jQuery(this).attr('href');
        
        var ptop = jQuery(goto).position().top;
        
        var limit = jQuery('#lay-top').height();
        
        //ptop -= limit;
        
        jQuery('body, html').animate({
            'scrollTop': ptop
        }, 1500, 'easeInOutExpo');
        
        return false;
         
    });


    jQuery('.newwindow').live('click', function(){
        window.open(jQuery(this).attr('href'));
        return false;
    });
    
    jQuery('.fancyopen').livequery(function(){
        jQuery(this).fancybox(
        {   
            'padding': 0,
            'margin' : 20,
            'scrolling': 'no',
            'overlayOpacity': 0.9,
            'overlayColor': '#000'
        });
    });
    
    
    
    jQuery('.fancyload').fancybox(
        {   'padding': 0,
            'margin' : 20,
            'scrolling': 'no',
            'overlayOpacity': 0.9,
            'overlayColor': '#000',
            'onStart': fancyStarted,
            'onClosed' : fancyClosed,
            'mouseWheelNavigation': false
        }
    );
    jQuery('#fancybox-outer').livequery(function(){
        jQuery(this).css('background', 'none');    
    });

    jQuery('.portfolio-box:not(.portfolio-filme)').live('click', function(){
        
        var id = jQuery(this).attr('rel');
        
        var regex = /(rgacom)/ ;
        
        if(id.match(regex))
            window.location = id;
        else        
            jQuery('#link-project-'+id).click();
        
        return false;
        
    });

    var old_title;
    
    function fancyStarted(){
        old_title = jQuery('title').text();
    }

    function fancyClosed(){
        jQuery('title').html(old_title);
    }

    var regProject = /(show-)([0-9]+)/;

    var matches = window.location.href.match(regProject);

    if(matches){

        var projectid = matches[2];
        
        jQuery.fancybox({
            'type': 'ajax',
            'href': '/index.php/project/default/detail/?id='+projectid,
            'padding': 0,
            'margin' : 20,
            'scrolling': 'no',
            'overlayOpacity': 0.9,
            'overlayColor': '#000',
            'showNavArrows': true,
            'onStart': fancyStarted,
            'onClosed' : fancyClosed,
            'mouseWheelNavigation': false
        });
        
        jQuery('#fancybox-outer').livequery(function(){
            jQuery(this).css('background', 'none');    
        });
    }
    
    
    
    jQuery('.with-color').fadeTo(0, 0);

    jQuery('.item-cliente').hover(function(){
        jQuery(this).find('.with-color').fadeTo(200, 1);
    }, function(){
        jQuery(this).find('.with-color').fadeTo(200, 0);
    });
    
    jQuery('#show-more').live('click', function(){
        var href = jQuery(this).attr('href');
        jQuery('#loading-projects').fadeIn();
        var d = new Date();
        var newid = 'last' + d.getTime();
        
        jQuery.get(href, null, function(data){
           jQuery('#loading-projects').before('<div id="'+newid+'">' + data + '</div>'); 
           jQuery('#loading-projects').fadeOut(); 
        });
        
        jQuery(this).parent().remove();
        
        return false;
    });
    
    jQuery('.testimonial').hide();
    
    jQuery('.testimonial:first').show();
    
    var currentTestimonial = jQuery('.testimonial:first');
    
    
    
    var intervalTestimonial = setInterval(function(){
        jQuery(currentTestimonial).fadeOut(600);
        currentTestimonial = jQuery(currentTestimonial).next('.testimonial');
        if(!currentTestimonial.length){
            currentTestimonial = jQuery('.testimonial:first');
        }
        
        jQuery(currentTestimonial).fadeIn(600);
        
    }, 7000);
    
    
     jQuery.preLoadImages(
         '/_resources/media/img/default/menu_on.gif',
         '/_resources/media/img/default/ico-twitter-hover.gif',         
         '/_resources/media/img/default/ico-facebook-hover.gif',         
         '/_resources/media/img/default/ico-flickr-hover.gif',         
         '/_resources/media/img/default/ico-vimeo-hover.gif',         
         '/_resources/media/img/default/ico-blog-hover.gif',         
         '/_resources/media/img/default/bt_fancy_twitter-hover.png',         
         '/_resources/media/img/default/bt_fancy_facebook-hover.png'        
     );

    
    
});
