if(typeof this.PINT==="undefined"){var PINT={}; }

/* FUNCTIONS *******************/

(function ($) {

    /** ADD CLASS **********/
    // Format: $('.something').PINT_addClass('last');
    $.fn.PINT_addClass = function(addClass) {
        this.each(function() {
            var eachThis = $(this);
            eachThis.addClass(addClass);
        });
    };

    /* ACCORDION **********/
    $.fn.PINT_accordion = function(type) {
        this.each(function() {
            var eachThis = $(this);
            type = type || false;
            // Hide 'content boxes' on load
            eachThis.children(':odd').hide();
            
            // IF 'toggle' passed in by the user, set the 'toggle' behavior
            if ( type == 'toggle' ) {
                eachThis
                    .children(':even')
                        .bind('click', function() {
                            var clickThis = $(this);
                            if ( !clickThis.next().is(':visible') ) {
                                clickThis.toggleClass('open').next().slideDown();
                            }
                            else {
                                clickThis.toggleClass('open').next().slideUp();
                            }
                        })
                ;
            }
            // ELSE set the default 'swap' behavior
            else {
                eachThis
                    // ':odd' set so only 'trigger' click recognized since 'triggers' 
                    // are the odd children (content boxes are 'even')
                    .children(':even')
                        .bind('click', function() { 
                            var clickThis = $(this);
                    
                            // For all non-visible content boxes
                            if ( !clickThis.next().is(':visible') ) {
                                clickThis.parent().children(':even').removeClass('open').end().children(':odd').not( clickThis.next() ).slideUp();  // Slide them up on click
                                clickThis
                                    .addClass('open')
                                    .next().slideDown(); // Then slide down the 'active' content box
                            }
                        })
                ;
            }
        });
    };
    
    /** EMAIL PAGE **********/
    $.fn.PINT_email = function() {
        this.each(function() {
            var eachThis = $(this);
            eachThis
                .bind('click', function() {
                    var link =  'mailto:?subject= Information about (' + document.title + ')';
                        link += '&body= ' + document.title;
                        link += '... at: ' + location.href;
                    location.href = link;
                    return false;
                })
            ;
    
        });
    };
    
    /** FADER GALLERY **********/
    $.fn.PINT_faderControls = function(title) {
        this.each(function() {
            var eachThis = $(this);
            var bannerControls = '<div class="banner-cont"><div class="banner-controls clearfix"><div class="clearfix"></div></div><div class="banner-title"></div></div>';
            function toggleTitleText() { 
                // Callback after each image swap
                if ( title ) {
                    var currTitle = eachThis.children('.slideshow-img').find('a:visible').attr('title');
                    //eachThis.children('.banner-cont').children('.banner-title').text(currTitle);
                    
                    eachThis.children('.banner-cont').children('.banner-title').text(currTitle + "... ").append($('<a href="#" class="slideLink">(more)</a>').click(function(e){
                        e.preventDefault();
                        $('.slideshow .pintbox:visible').first().click();
                    }));

                }
            }
            eachThis
                .append(bannerControls)
                .children('div:first-child')
                    .cycle({
                        fx: 'scrollLeft',
                        speed: 2000,
                        timeout: 8000,
                        pager: '.banner-controls div',
                        pause: true,
                        after: toggleTitleText,
                        easing: 'easeOutExpo'
                    })
            ;
        })
    };
    
    /** FANCYBOX (OVERLAY) **********/
    // Format: $('.pintbox').PINT_overlay();
    $.fn.PINT_overlay = function(text) {
        $(this).each(function() {
            // Find the href="#target". Nicely doubles as ID selector!
            var thisID = $(this).attr('href');
            // Hide page content on page load
            if ($(thisID).length) $(thisID).hide();
            // When you click 'trigger' first show the content briefly, then add to the overlay so the overlay isn't blank.
            $(this).bind('click', function() { $(thisID).show() }).fancybox({ 'titleShow':false });
            // Finally, when you click the 'close' button, hide all inline content divs
            $('#fancybox-close').bind('click', function() { $('.overlay').hide() })
        })
    };
    
    /** FOCUS/BLUR **********/
    $.fn.PINT_focusBlur = function() {
        
        this.each(function() {
            if( $(this).attr('data-emptyText') ) {
                
                var emptyText = $(this).attr('data-emptyText');
                
                $(this).focus(function(){

                    if( $(this).val() == emptyText ) {
                        $(this).val('');
                    }
                
                }).blur(function(){

                    if( !$(this).val().length ) {
                        $(this).val(emptyText);
                    }
                
                });
                
                //initialize
                if( !$(this).val().length ) {
                    $(this).val(emptyText);
                }
                
                //$(this.form).submit(function(field){ return function(){
                    //if( $(field).val() == emptyText ) $(field).val('');
                //}; }(this));
                
                $(this.form).submit($.proxy(function(){
                    if( $(this).val() == emptyText ) $(this).val('');
                }, this));
                
                
            }
        });    
    };
    
    /*
    $.fn.PINT_focusBlur = function() {
        this.each(function() {
            var eachThis = $(this);
            var text = eachThis.attr('value') || eachThis.attr('rel');
        
            eachThis
                .focus(function(){
                    var focusThis = $(this);
                    if (focusThis.val() == text) {
                        focusThis.val('')
                        if ( focusThis.hasClass('input-password') ) this.type = 'password';
                    }
                    focusThis.parents('#search').addClass('focus');
                }).blur(function(){ 
                    var blurThis = $(this);
                    if (blurThis.val() == '') {
                        if ( blurThis.hasClass('input-password') ) this.type = 'text';
                        blurThis.val(text);				
                    }
                    blurThis.parents('#search').removeClass('focus');
                })
            ;
        });
    };
    */

    /** ELEMENT MATCH HEIGHT **********/
    // Sets the target's height to match that of another element
    // copy = The element who you are copying the height from
    // offset [optional] = If tgt has other factors affecting height, use offset to compensate
    // Format: $('#sidebar').PINT_matchHeight('#main',17);
    $.fn.PINT_matchHeight = function(copy,offset) {
        this.each(function() {
            var eachThis = $(this);
            var tgtHeight = eachThis.height();
            var copyHeight = $(copy).height();
            offset = (offset) ? offset : 0; // If offset set use it, else set offset to zero
            var newHeight = copyHeight-offset;
            
            if ( (eachThis.length) && (copyHeight > tgtHeight) ) eachThis.css('min-height',newHeight);
        });
    };
    
    /** FORM VALIDATION **********/
    $.fn.PINT_validate = function() {
    
        this.each(function() {
        
            $(this).bind('reset', function(){
                setTimeout( $.proxy(function() {
                    $(this).find('textarea, input[type="text"]').each(function(){
                        $(this).blur();
                    })}, this), 100);
            });
        
            var eachThis = $(this);
            eachThis.validate({
            
                invalidHandler: function() {
                
                    $(this).find('textarea, input[type="text"]').each(function(){
                        $(this).blur();
                    });
                
                }
            
            });
        });
    };

}(jQuery));



/* INITIALIZE FUNCTIONS *******************/
PINT.init = function() {

    /** JS ENABLED - Target any element differently (CSS) if JS enabled/disabled **********/
    $('body').PINT_addClass('js-enabled');
    $('#sitemap li:last-child').PINT_addClass('last');
    

    /** FANCYBOX (OVERLAY) **********/
    $('.pintbox-iframe').each(function() {
        var eachThis = $(this);
        eachThis.fancybox({ 
            type        : 'iframe',
            'width'     : '80%',
            'height'    : '80%',
            'autoScale' : false
        });
    });
    
    $('a.pintbox').each(function() {
        var eachThis = $(this);
        eachThis.fancybox({
            'transitionIn'  : 'none',
            'transitionOut' : 'none',
            'titlePosition' : 'over',
            'cyclic'        : 'true',
            'titleFormat'   : function(title, currentArray, currentIndex, currentOpts) {
                return '<span id="fancybox-title-over">' + (currentArray[currentIndex].children[0].alt || title) + '</span>';
            }
        })
    });

    
    /** EMAIL PAGE **********/
    $('.icon-email').PINT_email();

    /** INPUT: 'FOCUS/BLUR' **********/
    $('.input').PINT_focusBlur();

    /** ELEMENT MATCH HEIGHT **********/
    // Sets the target element's height to match that of another element (optional offset)
    $('#col1').not('#layout-homepage #col1').PINT_matchHeight('#col2',176);



    /** JS CODE LOADING DEFERMENT TO AFTER JQUERY LOADS (DO NOT REMOVE) **********/
    
        /* Required Files */
        // 'Setup' code added to 'Page Begin'. Find 'PINT.requireJsFiles'
        // Code added via PHP block in page to 'external support script' gets loaded here
        // Example: PINT.requireJs('/scripts/jquery.cycle.all.js');
        if ( PINT.requireJsFiles ) { 
            var loaded = [];
            $.each(PINT.requireJsFiles, function(index, path) {
                if( $.inArray(path, loaded) == -1 ) {
                    loaded.push(path); path = PINT.themeRootDirectory + path;
                    jQuery.ajax({ async:false, url:path, dataType:'script' });
                }
            
            });
        }
        
        /* Code Execution/Functions */
        // 'Setup' code added to 'Page Begin'. Find 'PINT.onStartFns'
        // Code added via CONTENTBLOCK to be run inside common.js, so it is run after jQuery loaded
        if( PINT.onStartFns ) { $.each(PINT.onStartFns, function(index, fn) { fn(); }); }
    
    /** ENDS JS CODE LOADING **********/

}

/* RUN FUNCTIONS *******************/
$(document).ready(function() {
  PINT.init();
});
