// JavaScript Document - Tilley Associates bespoke functions - by Paolo Marchetti - Winona eSolutions


jQuery(function($){ // on page ready


        // This condition prevents the fields value being covered by the label when the page is reloaded after an error in the submit.
	   $(":text, textarea").filter(function(index) {
                  return $(this).val() == "";
	   }).labelify();

        jQuery('.subnav').accordion({

            alwaysOpen: false,
            active: false,
            autoHeight: false,
            navigation: true,
            event: 'click',
            fillSpace: false,
            animated: false

        });
            
           
        $('.link a').click(function(){
            $( ".subnav" ).accordion('disable');
            window.open($(this).attr('href'), "_self");

            setTimeout ( function() {
              $(".subnav").accordion('enable');
            }, 250 );
        });

        $('#gallery img:first').css("display", "none").fadeIn(1000, function() {
             $('#gallery').cycle({
            fx:      'scrollHorz',
            prev:    '#prev',
            next:    '#next',
            pager:   '#nav',
	        speed:    500, 
	        timeout:  3000,
            pagerAnchorBuilder: pagerFactory,
	        nowrap: 1, 
	        after:   onAfter, 
            cleartype:  1, // enable cleartype corrections 
            end:      function() {  
				        if ( $("p.trigger").next().css("display") == "none" ){
                       $("p.trigger").toggleClass("active").next().slideDown("slow"); 
			           }
            } 
           });
        });


        function onAfter(curr, next, opts) {
            var index = opts.currSlide;
            $('#prev')[index == 0 ? 'hide' : 'show']();
            $('#next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
        }
         
        	
        	
        function pagerFactory(idx, slide) {
                var s = idx > 15 ? ' style="display:none"' : '';
                return '<li'+s+'><a href="#">'+(idx+1)+'</a></li>';
        };


        //Hide (Collapse) the toggle containers on load
        //$(".toggle_container").hide(); 
        $("a.thumb h3").hide();  //hide thumbnails caption
        $(".nav-arrows").hide(); //hide gallery arrows
        $("p.trigger").toggleClass("active").next().css("display", "none"); //lower down the content div on start (gallery only)


        //Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
        $("p.trigger").click(function(){
        $(this).toggleClass("active").next().slideToggle("slow");
        return false; //Prevent the browser jump to the link anchor
        });
		
		
		//Show thumnails caption on mouse hover
         $("a.thumb").mouseenter(function(){
        $($(this).find('h3')).fadeIn("fast");

        });
		//then hide them again
        $("a.thumb").mouseleave(function(){
        $($(this).find('h3')).fadeOut("fast");

        });

		//Show gallery arrows on mouse hover
        $(".content").mouseenter(function(){
        $($(this).find('.nav-arrows')).css("display", "block"); 

        });
		//then hide them again
        $(".content").mouseleave(function(){
        $($(this).find('.nav-arrows')).css("display", "none"); 

        });


        function markActiveLink() {

            //Look through all the links in the sidebar
            $(".subnav div a").filter(function() {

                //Take the current URL and split it into chunks at each slash
                var currentURL = window.location.toString().split("/");

                //return true if the bit after the last slash is the current page name
                return $(this).attr("href") == currentURL[currentURL.length - 1];

                //when the filter function is done, you're left with the links that match.
            }).addClass("active");

            //Afterwards, look back through the links. If none of them were marked,
            //mark your default one.
           
        }

        markActiveLink();	
     
}); // end on ready


