// JavaScript Document
$(document).ready(function() {
    // home page build
    $(".home #header").hide().slideDown('slow, easeIn');
    $(".home .bulb, .home .footercopy, .home #menu li, .home .homebox, .home .triptych").hide();
    $('.home #menu li').each(function(i) {
        $(this).delay(i*30).fadeIn('fast');
    }); // end each function
    $(".home .bulb").hide().delay(2000).fadeIn('slow');
    $(".home .footercopy").delay(2400).fadeIn('slow');
    $(".home .homebox, .home .triptych").delay(3200).fadeIn('medium');

    // menu effects 
    $('#menu li ul').css({display: "none", left: "auto"});
    $('#menu li').hoverIntent(function() {
      var position = jQuery(this).position();
      $(this)
        .find('ul')
	    .css("left", position.left-=20)
	.stop(true, true)
        .slideDown(600,'easeOutBounce');
    }, function() {
      $(this)
        .find('ul')
        .stop(true,true)
        .fadeOut('fast');
    });
    $('#menu li.last').hoverIntent(function() {
      var position = jQuery(this).position();
      $(this)
        .find('ul')
	    .css("right", 0)
	.stop(true, true)
        .slideDown(600,'easeOutBounce');
    }, function() {
      $(this)
        .find('ul')
        .stop(true,true)
        .fadeOut('fast');
    });
    
    jQuery("#menu li").mouseenter (function() {
	    jQuery(this).find('a').addClass('hold');
	}).mouseleave(function() {
	    jQuery(this).find('a').removeClass('hold');
    }); 

    // pager effects
    var pager = $(".prev, .next, .trip-prev, .trip-next, .logo-prev, .logo-next, .dub-prev, .dub-next, .trip-prev2, .trip-next2,");
    $(pager).css("opacity", "0.3");
    $(pager).hover(function() {
	$(this).stop(true, true).animate({opacity: 1.0}, 200);
	}, function () {
	$(this).stop(true, true).animate({opacity: 0.3}, 400);
	});

// clients build animation
    $('.clients .block2, .clients .testimonial').hide();
    $('.clients .block2').delay(1000).each(function(i) {
        $(this).delay(i*120).fadeIn('slow');
    $('.clients .testimonial').delay(3000).fadeIn('fast');	
    }); // end each function

    
//ajax load
	
    $('.ajaxy').click(function() {						  
        var toLoad = $(this).attr('href')+' #content';
        $('#content').fadeOut('fast',loadContent);
        $('#load').remove();
        $('.wrap960').append('<span id="load">LOADING...</span>');
        $('#load').fadeIn('normal');
        window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);
        function loadContent() {
                $('#content').load(toLoad,'',showNewContent())
        }
        function showNewContent() {
                $('#content').fadeIn('normal',hideLoader());
        }
        function hideLoader() {
                $('#load').fadeOut('normal');
        }
        return false;
    });
	
// find caption heights
	var captionHeight = $(".slide").find('img').height()-30;	
	$("#pager").css('top', captionHeight+'px');

// init carousel
$(function() {
    $(".tripwrap").jCarouselLite({
        btnNext: ".trip-next",
        btnPrev: ".trip-prev",
        auto:6000,
        speed:1000,
        easing:"easeOutBounce"
    });
});

// init carousel
$(function() {
    $(".tripwrap2").jCarouselLite({
        btnNext: ".trip-next2",
        btnPrev: ".trip-prev2",
        auto:6000,
        speed:1000,
        easing:"easeOutBounce"
    });
});

// init carousel
$(function() {
    $(".dubwrap").jCarouselLite({
        btnNext: ".dub-next",
        btnPrev: ".dub-prev",
        auto:6000,
        speed:1000,
        easing:"easeOutBounce",
	visible:2
    });
});

// nivo slider
$('#slider').nivoSlider();

// fire "the latest" animation
animLatest(); // call the function

$('.logo-sample').hide();
//$('.block-logos').children(':last').addClass('active');
animLogos(); // call the function
    
// colorbox init
$('a.clrbox').colorbox();
$('a.gallery').colorbox({rel:'gal'});

//$('body.test .wrap260').after('<a href="store.mkjmarketing.com" class="storebutton">Store Link</a>');

$('.carousel').carousel({frontWidth:350, frontHeight:225, carouselWidth:640, carouselHeight:275, shadow:true, buttonNav:'bullets', directionNav:true});



}); // end document ready function

// list animation
function animLatest() {
    var current = $('.latestlist .active');
    var next = current.next().length ? current.next() : current.parent().children(':first');

    current.switchClass('active', 'inactive', 500);
    next.switchClass('inactive', 'active', 200);

    setTimeout(animLatest, 3000);
}

// logos animation
function animLogos() {

var logos = $('.block-logos > .logo-sample');
for(var i = 0; i < logos.length; i+=3) {
logos.slice(i, i+3).wrapAll('<div class="logo-wrap"></div>');
$('.block-logos').children(':last').addClass('active');
}
    var current = $('.block-logos .active');
    var next = current.next().length ? current.next() : current.parent().children(':first');

    current.hide().switchClass('active', 'inactive', 500);

    next.show().switchClass('inactive', 'active', 500);    
    next.find('.logo-sample').hide();
    next.find('.logo-sample').each(function(i) {
        $(this).delay(i*200).show('drop',{ direction: 'down' },300);
    }); // end each function

    next.delay(4200).fadeOut(500);
    
    setTimeout(animLogos, 4500);
}

//ajax load details
var hash = window.location.hash.substr(1);
var href = $('a.more, .block a').each(function() {
        var href = $(this).attr('href');
        if(hash==href.substr(0,href.length-5)) {
            var toLoad = hash+'.html #content';
            $('#content').load(toLoad)
        }											
});
