var MenuOpen = false;

    $(document).ready(function(){
        
      	$('#slideshow').cycle({
            fx: 'fade',
            timeout: 8000,
            speed: 3000,
            /*before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
            
               
               $('.RightSideCol').fadeOut('slow', function () {
                  
                  $('.RightSideCol').html(formatTitle($(nextSlideElement).attr('title')));
                  $('.RightSideCol').fadeIn('slow');
               });
               
               $('.BottomBox').hide();
            },*/
            after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
               
               $('.Slide2').animate({
                      "height": $('#slideshow img:visible').height()+30
                  }, "1500");
            }
         });
        
        $('#slideshow').mouseenter(function(){
            $('#slideshow').cycle('pause');
        });
        
        $('#slideshow').mouseleave(function(){
            if ( $('#fancybox-content:empty').length > 0 )
            {
              $('#slideshow').cycle('resume');
            }
        }); 
         
        $("a.gallery").fancybox({
      		'transitionIn'	:	'elastic',
      		'transitionOut'	:	'elastic',
      		'speedIn'		:	600, 
      		'speedOut'		:	200, 
      		'hideOnOverlayClick'	:	true,
      		'overlayOpacity' : 0.3,
      		'overlayColor' : '#CCCCCC',
      		'padding' : 0,
      		'titleFormat' : function (title, currentArray, currentIndex, currentOpts) { return formatTitle(title); },
      		'showCloseButton'	: false,
      		'titlePosition' 		: 'over',
      		'onComplete' : function () {
               $('#slideshow').cycle('pause');
               if ($('#slideshow').css('display') != 'none') {
                  $('#slideshow').fadeOut('slow');
               }
            },
            'onCleanup' : function () {
               $('#slideshow').show();
               $('#slideshow').cycle('resume');
            },
            'onCancel' : function () {
               $('#slideshow').show();
               $('#slideshow').cycle('resume');
            }
      	});
      	
        $('#fancybox-right').click(function() {
            $('#slideshow').cycle('next');
        });
      	
        $('#fancybox-left').click(function() {
            $('#slideshow').cycle('prev');
        });
        
        $(".LeftCol").mouseenter(function () {

            if ($(".NavBlock").css('display') == 'none' && !MenuOpen)
            {
               MenuOpen = true;
                        
               $(".LandingLogo").animate({
                      "opacity": "toggle"
                  }, "2000", function () { 
                     $(".NavBlock").animate({"opacity": "toggle"},
                        "2000"); 
                  });
            }
        });
        
        
        // Open Active nav items
        $('.SiteNav ul').each(function(index, element) {
        
            if($(this).prev('.Active').length > 0 || $(this).find('.Active').length > 0) {
               
               if($(this).prev('.Active').length > 0 && $(this).css('display') == 'none') {
                  $(this).animate({
                      "height": "toggle", 
                      "opacity": "toggle"
                  }, "1500");
               }
               $(this).prev('h4').addClass('Active');
               $(this).addClass('open');
            }
        });
        
        $(".SiteNav h4").click(
            function () {
                
                if($(this).attr('onclick') == null || $(this).attr('onclick') == '')
                {
                  $(".SiteNav h4.Active").removeClass('Active');
                  toggleMenu($(".SiteNav ul").index($(this).next("ul")));
                  $(this).addClass('Active');
                }
            }
        );
        
        $(".SiteNav li li a").click(
            function () {
                if($(this).attr('href') == null || $(this).attr('href') == '#' || $(this).attr('href') == 'javascript:void(0);')
                {
                  $(".SiteNav  li li a.Active").next("ul").hide('slow');
                  $(".SiteNav  li li a.Active").removeClass('Active');
                  $(this).next("ul").show('slow');
                  $(this).addClass('Active');
                }
            }
        );
        
        $(".SiteNav h4").hover(function() {
            
            $(this).toggleClass('Hover');
        });
                
    });
    
    function formatTitle (title) {
	    var titleArr = title.split("|");
	    var output = '';
	    if (titleArr.length > 0) {
         output = output + '<div class="titleWrap"><h1>' + titleArr[0] + '</h1>';
         
         if(titleArr.length > 1) {
            
            output = output + '<h5>' + titleArr[1] + '</h5>';
         
            if(titleArr.length > 2) {
               
               output = output + titleArr[2];
            }
         }
         
         output = output + '</div>';
       }
       
       return output;
   }
    
    function toggleMenu(clickedItemIndex)
    {
        var currentOpenIndex = $(".SiteNav ul").index($(".SiteNav ul.open"));
        
        if(currentOpenIndex >= 0)
        {
            var currentOpenItem = $(".SiteNav ul")[currentOpenIndex];
            
            $(currentOpenItem).animate({
                "height": "toggle", 
                "opacity": "toggle"
            }, "1500").andSelf().removeClass("open");
        
        }
        
        if(currentOpenIndex != clickedItemIndex && clickedItemIndex >= 0)
        {
            var clickedItem = $(".SiteNav ul")[clickedItemIndex];
            $(clickedItem).animate({
                "height": "toggle", 
                "opacity": "toggle"
            }, "1500");
            
            if($(clickedItem).hasClass("open"))
                $(clickedItem).removeClass("open");
            else
                $(clickedItem).addClass("open");
        }
    }
