$(document).ready(function(){
		
    $('#primeNav').addClass("homeNav");
    
	// Nav Slider
		$('a.setTitle').slideNav();
		
	// Nav Highlight
		$('.page a').selectNav();

	
	//Dump Lazyload		
		$(".dump img").lazyload({ 
		    placeholder : "/includes/style/images/grey.gif",
		    effect : "fadeIn" 
		});
		
	//////////////////////////////////////////
	//  Paged Navigation
	/////////////////////////////////////////
	$('#primeNav>li').each(function(){
		$(this).find('ul.subCats').children('li').each(function(){
			var id = $(this).attr('id');
			var count = $('#'+id+' ol.pages').children('li.page');
			if (count.length > 1) {
				$(this).find('ol.pages').after('<div class="controls" />');
				$('#'+id+' div.controls').html(
					'<h6><span class="curPage">1</span> of <span class="totalPages">'+ count.length +'</span></h6>'+
					'<ul class="setNav"><li><a class="next" href="#">next</a></li><li><a class="prev" href="#">prev</a></li></ul>'
				);
				$('#'+id+' ol.pages').cycle({
					fx: 'scrollHorz',
					speed:  'fast', 
					timeout: 0,
					prevNextClick: updatePage,
					prev: '#'+id+' .controls ul a.prev',
					next: '#'+id+' .controls ul a.next'
				});
			}
			function updatePage(idx, slide) {
				$('#'+id+' span.curPage').text(slide + 1);
			}
		});
	});
	

	//////////////////////////////////////////
	//  Cache $$ Loader
	/////////////////////////////////////////

    $('.loader').live("click", function(){		
      
      $('.loader').css("color", "#ff0000");
      $('.loader').hover(
       function() {
        $(this).addClass('hover');
       },
       function() {
        $(this).removeClass('hover');
       }
      );

      $(this).css("color", "#000");      
      $('#primeNav').removeClass("homeNav");
  		$('#loader').show();
  		var $url = $(this).attr('href');
  		window.location.hash = jQuery.url.setUrl($(this).attr('href')).attr("path");
  						
  		$('#page').fadeOut('slow', fetchContent);															
  		// Loads Content				
  				
  		function fetchContent(){
  			$.ajax({
  				type: 'get',
  				url: $url,	
  				dataType: 'html',		
  				success: function(html){
  					var $html = jQuery('<div>'+html+'</div>').find('#page');
  					$('#page').fadeOut('slow').html($html.html()).fadeIn('slow');
  					$('#page').attr('class', $html.attr('class'));
  			        $('#loader').hide();
  			        init();
  			    }
  			})						
  		}								
  		return false;		
  	});
	
	if( window.location.hash != '' ) {				
		var w = window.location.hash.substr(1);
		$('#loader').show();
				
		$.ajax({
			type: 'get',
			url: w,
			success: function(html) {
				var $html = $('<div>'+html+'</div>').find('#page');
				$('#page').fadeOut('slow').html($html.html()).fadeIn('slow');
				$('#page').attr('class', $html.attr('class'));
				$('#loader').hide();
				$('#primeNav').removeClass("homeNav");
				
				init();
			}
		});	
		return false;	
	}
	
	
	////////////////////////////////////////
	// INIT
	////////////////////////////////////////

	function init() {

		// Open external links in new window
		$('a[rel*="external"]').click(function(){
			window.open(this.href);
			return false;
		});
		
		// Gridview Alignment and Effects
		$('ul.grid li a img').absoluteCenter().subtleFade();
		
		// Slideshow Image Alignment
		$('.slideShow img').hzCenter();
		//$('.dump img').absoluteCenter();
		
		
		//Dump Lazyload		
		$(".dump img").lazyload({ 
		    placeholder : "/includes/style/images/grey.gif",
		    effect : "fadeIn" 
		});
		
		// Contact Form ajax submit
        $('#cForm').ajaxForm(function() { 
            $('#cForm').hide();
            $('#thankyou').fadeIn('slow'); 
        }); 
		
	
		////////////////////////////////////////
		// FOR DEMO PURPOSES ONLY!!!!!!!!!!!!
		////////////////////////////////////////
		//$('#folioPage .subCats>li:first-child').addClass('open').css({'height':295}).find('ol.pages').css({'opacity':1});
		////////////////////////////////////////
		////////////////////////////////////////
	

	
		//////////////////////////////////////////
		//  Slideshows
		/////////////////////////////////////////


		// Homepage Slideshow
		$('.home .slideShow')
//		.css({'opacity':0})
//		.delay(500).animate({'opacity':1}, 800)
		.cycle({
			fx: 'fade',
			timout: 9000,
			speed: 3000
		});


		// Folio Slideshow
		$('.folio .slideShow')
//		.css({'opacity':0})
//		.delay(500).animate({'opacity':1}, 800)
		.cycle({
			fx: 'fade',
			timeout: 0,
			pager:  'ul.grid',
			pagerClick: hideGrid,
			prev: '.folio ul.galCntrl a.prev',
			next: '.folio ul.galCntrl a.next,  .slideShow img',
			after:   onAfter,
			pagerAnchorBuilder: function(idx, slide) { 
				return 'ul.grid li:eq(' + idx + ') a'; 
			}
			// prevNextClick: updateCount
		});
		var $caption = $('.slideShow img:first').attr("alt");
		$('.slideShow').after(
			'<div class="info">'+
			'<p>'+ $caption +'</p>'+
			'</div>'
		);
		function onAfter() { 
			$('.info p').text(this.alt);
			if($(this).attr('rel')>1){$('.instructions').hide()};			
		}
		function hideGrid() {
			$('div.slideShow').delay(600).fadeIn();
			$('#pageHead h6.counter, #pageHead h6:not(.counter), div.slideShow, a.next.slide, a.prev.slide, div#thumbGrid, a.next.page, a.prev.page, div.info').toggle();
			$('div#thumbGrid').fadeOut('fast');
		}

		// 	var $totalSlides = $('div.slideShow img').length;
		// 	$('.slide ul.galCntrl').after(
		// 		'<h6 class="counter">Image: <span class="item">1</span> of <span class="count">'+ $totalSlides +'</span></h6>'
		// 	);
		// Thumb Pager
		var $totalThumbSets = $('#thumbGrid>ul').length;
		$('#pageHead ul.galCntrl').after(
			'<h6 class="counter">Page: <span class="item">1</span> of <span class="count">'+  $totalThumbSets +'</span></h6>'
		);
		$('div#thumbGrid, a.next.page, a.prev.page, h6.counter').hide();
		$('.thmbGrd').click(function(){
			$('#pageHead h6.counter, #pageHead h6:not(.counter), div.slideShow, a.next.slide, a.prev.slide, div#thumbGrid, a.next.page, a.prev.page, div.info').toggle();
		});

		$('#thumbGrid').cycle({
			fx: 'fade',
			timeout: 0,
			speed: 500,
			prev: 'a.prev.page',
			next: 'a.next.page',
			prevNextClick: updateCount
		});
		function updateCount(idx, slide) {
			$('span.item').text(slide + 1);
		}
	
		//////////////////////////////////////////
		//  Forms
		/////////////////////////////////////////
		
		//check for the plugin
		if(jQuery.fn.validate) {
			$("#cForm").validate();
		}
	
	}

	init();
});
