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

/** Added Prototypes **********/
/** -- ARRAYS */
// Map ( .map() ) - http://ow.ly/70cRA
if(!Array.prototype.map){Array.prototype.map=function(a,b){var c,d,e;if(this==null){throw new TypeError(" this is null or not defined")}var f=Object(this);var g=f.length>>>0;if({}.toString.call(a)!="[object Function]"){throw new TypeError(a+" is not a function")}if(b){c=b}d=new Array(g);e=0;while(e<g){var h,i;if(e in f){h=f[e];i=a.call(c,h,e,f);d[e]=i}e++}return d}}
// Filter ( .filter() ) - http://ow.ly/70cPJ
if(!Array.prototype.filter){Array.prototype.filter=function(a){"use strict";if(this===void 0||this===null)throw new TypeError;var b=Object(this);var c=b.length>>>0;if(typeof a!=="function")throw new TypeError;var d=[];var e=arguments[1];for(var f=0;f<c;f++){if(f in b){var g=b[f];if(a.call(e,g,f,b))d.push(g)}}return d}}
// Reduce ( .reduce() ) - http://ow.ly/70cSC
if(!Array.prototype.reduce){Array.prototype.reduce=function a(a){var b,c=this.length,d;if(typeof a!=="function")throw new TypeError("First argument is not callable");if((c==0||c===null)&&arguments.length<=1)throw new TypeError("Array length is 0 and no second argument");if(arguments.length<=1){d=this[0];b=1}else{d=arguments[1]}for(b=b||0;b<c;++b){if(b in this)d=a.call(undefined,d,this[b],b,this)}return d}}
// IndexOf ( .indexOf() ) - http://ow.ly/70cXD
if(!Array.prototype.indexOf){Array.prototype.indexOf=function(a){"use strict";if(this===void 0||this===null){throw new TypeError}var b=Object(this);var c=b.length>>>0;if(c===0){return-1}var d=0;if(arguments.length>0){d=Number(arguments[1]);if(d!==d){d=0}else if(d!==0&&d!==Infinity&&d!==-Infinity){d=(d>0||-1)*Math.floor(Math.abs(d))}}if(d>=c){return-1}var e=d>=0?d:Math.max(c-Math.abs(d),0);for(;e<c;e++){if(e in b&&b[e]===a){return e}}return-1}}
/** -- STRINGS */
// Capitalize ( .capitalize() )
if(!String.prototype.capitalize){String.prototype.capitalize=function(){return this.replace(/(^|\s)([a-z])/g,function(m,p1,p2){ return p1+p2.toUpperCase()})}}
// Trim ( .trim() ) - http://ow.ly/71aNY
if(!String.prototype.trim){String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}}
// JSON
if(!this.JSON){JSON=function(){function f(n){return n<10?"0"+n:n}Date.prototype.toJSON=function(){return this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z"};var m={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"};function stringify(value,whitelist){var a,i,k,l,r=/["\\\x00-\x1f\x7f-\x9f]/g,v;switch(typeof value){case"string":return r.test(value)?'"'+value.replace(r,function(a){var c=m[a];if(c){return c}c=a.charCodeAt();return"\\u00"+Math.floor(c/16).toString(16)+(c%16).toString(16)})+'"':'"'+value+'"';case"number":return isFinite(value)?String(value):"null";case"boolean":case"null":return String(value);case"object":if(!value){return"null"}if(typeof value.toJSON==="function"){return stringify(value.toJSON())}a=[];if(typeof value.length==="number"&&!(value.propertyIsEnumerable("length"))){l=value.length;for(i=0;i<l;i+=1){a.push(stringify(value[i],whitelist)||"null")}return"["+a.join(",")+"]"}if(whitelist){l=whitelist.length;for(i=0;i<l;i+=1){k=whitelist[i];if(typeof k==="string"){v=stringify(value[k],whitelist);if(v){a.push(stringify(k)+":"+v)}}}}else{for(k in value){if(typeof k==="string"){v=stringify(value[k],whitelist);if(v){a.push(stringify(k)+":"+v)}}}}return"{"+a.join(",")+"}"}}return{stringify:stringify,parse:function(text,filter){var j;function walk(k,v){var i,n;if(v&&typeof v==="object"){for(i in v){if(Object.prototype.hasOwnProperty.apply(v,[i])){n=walk(i,v[i]);if(n!==undefined){v[i]=n}else{delete v[i]}}}}return filter(k,v)}if(/^[\],:{}\s]*$/.test(text.replace(/\\./g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof filter==="function"?walk("",j):j}throw new SyntaxError("parseJSON")}}}()};

/** JQUERY FILTERS **********/
(function ($) {
    // Random ( :random ) - http://ow.ly/70cNY
    $.jQRandom = 0; $.extend($.expr[":"], { random: function(a, i, m, r) { if (i == 0) { $.jQRandom = Math.floor(Math.random() * r.length); }; return i == $.jQRandom; } });
}(jQuery));


/** FUNCTIONS **********/
(function ($) {

    /** ADD CLASS *****/
    // Format: $('.something').PINT_addClass('last');
    $.fn.PINT_addClass = function(addClass) {
        this.each(function() {
            $(this).addClass(addClass);
        });
    };
    
    /** ADD MODULUS **********/
    // Add 'last' class to set Modulus in floated set
    $.fn.PINT_addModulus = function(optionalUserDefClass) {
        var setClass = optionalUserDefClass || 'last'; // Set last class
        var countThis = this.length; // Get total num of targeted items
        var getModulus = this.parent().attr('data-columns');
        this.each(function(i) {
            // If modulus condition met, add Class (user defined or default 'last'
            if ( (i+1) % getModulus == 0 ) $(this).addClass(setClass);
        });
    };

    /** ANCHOR SLIDE *****/
    $.fn.PINT_anchorSlide = function(options) {
        var settings = $.extend({
            speed: 1100,
            excludeHash: '#top'
        }, options);	
        
        return this.each(function(){
            var eachThis = this;
            var pageURL = window.location.href;
            $(eachThis).click(function () {	
                var thisHref = $(eachThis).attr('href');
                var toTop = $(thisHref).offset().top;
                
                $("html,body").animate({ scrollTop:toTop }, settings.speed, function() {
                    if ( thisHref != settings.excludeHash ) {
                        window.location.hash = thisHref;
                    }
                    else {
                        window.location.hash = 'top';
                    }
                });
                return false;
            })
        })
    };
    
    /** CUSTOM 'SELECT-STYLE' DROPDOWN *****/
    $.fn.PINT_customDropdown = function(options) {
    
        var settings = $.extend({
            triggerEl: 'span',
            dropdownParent: 'ul',
            dropdownItems: 'a',
            activeClass: 'active'
        }, options);	
        
        return this.each(function() {
            var eachThis = $(this);
            var trigger = eachThis.find(settings.triggerEl);
            var triggerWidth = trigger.width();
            var dropdown = eachThis.find(settings.dropdownParent);
            var dropdownA = eachThis.find(settings.dropdownItems);
            var relatedTargetSelector = '.component-dropdown '+settings.dropdownParent+', .component-dropdown '+settings.dropdownParent+' *';
            var max = 0;
            
            /** GET MAX WIDTH ITEM *****/
            // FIND which dropdown A item is the widest and store width value
            eachThis
                .show()
                .find('a').each(function() {
                    var thisWidth = $(this).width();
                    if ( thisWidth > max ) max = thisWidth;
                })
            ;
            
            /** SET MAX WIDTH ON EACH ITEM *****/
            // IF width of TRIGGER is wider than widest DROPDOWN A tag, set width offset to width of TRIGGER
            if ( triggerWidth >= max ) max = triggerWidth;
            // SET the width of BOTH the TRIGGER and DROPDOWN A tags to this calculated width
            trigger.css({ width:max });  
            dropdownA.css({ width:max });
            
            /** TRIGGER 'MOUSEENTER/MOUSELEAVE' *****/
            trigger
                .next().hide()
                .end()
                .bind('click', function(e) {
                    if ( $(e.relatedTarget).is( relatedTargetSelector ) ) return false;
                    $(this).addClass(settings.activeClass).next().show();
                })
                .bind('mouseleave', function(e) {
                    if ( $(e.relatedTarget).is( relatedTargetSelector ) ) return false;
                    $(this).removeClass(settings.activeClass).next().hide();
                })
            ;
            
            $(dropdown).bind('mouseout', function(e) {
                if ( $(e.relatedTarget).is( settings.triggerEl+', '+relatedTargetSelector ) ) return false;
                $(this).hide().prev().removeClass(settings.activeClass);
            });
            
            
        });
    };
    
    /** CUSTOM SELECT BACKGROUNDS *****/
    // This code allows you to have a custom background image for <select> in a site. 
    // This doesn't work in ie6 or lower, so we remove the background image and do not show the <label> for those browsers.
    // Format: $('.custom-select').PINT_customSelect();
    $.fn.PINT_customSelect = function() {
        this.each(function() {
            if (($.browser.msie && $.browser.version.substr(0,1)<=7) || !$.browser.msie) {
                $(this)
                    .find('select')
                    .attr('selectedIndex',-1)
                    .change(function() {
                        var changeThis = $(this);
                        var optionText = changeThis.find('option:selected').text();
                        changeThis.siblings('label').text(optionText)
                    })
                ;
            }
        })
    };
    
    /** IMAGE FADE *****/
    $.fn.PINT_fade = function(options) {
        
        /** USER ADDED SETTINGS **/
        var settings = $.extend( {
            // Defaults
            fadeDuration:1300, // Set fade duration
            timeout:5000, // Set the 'pause' length
            autoplay:true // Set if auto-cycling set
        }, options);

        return this.each(function() {
            var controlsSelector = 'ul.banner-controls';
            var stop = false;
            var ul = $(this).children().first();
            var numBanners = ul.children().length;
        
            // Set Banner Position and hide except for the first banner
            ul.addClass('banner-items').children().css({ position:'absolute', top:0, left:0 }).first().addClass('active').end().not(':first').css({ display:'none' });
            // Set First banner to be visible
            ul.children().first().css('opacity', 1);
        
            /** SHOW NEXT BANNER (ON CLICK) FUNCTION **/
            var showNextBanner = function() {
                var current = ul.children(':visible');
                var index = current.index();
                // Go to the next banner ( unless at the end..go to the first image)
                showBanner(index+1 < numBanners ? index+1 : 0);
            }
        
            /** SHOW BANNER (AUTO FADE) FUNCTION **/
            var showBanner = function(slideIndex) {
            
                // Don't fade the currently active image/banner (ex: click on 'active' button, don't re-fade current item)
                if( slideIndex != ul.children('.active').index() ) {
            
                    // Remove any 'active' class from the control buttons, then add the class to the current button
                    $(controlsSelector).children().removeClass('active').eq(slideIndex).addClass('active');
                    ul.children().stop();
                    var li = ul.children().eq(slideIndex);
                
                    // Fade out all 'non-active' LIs (queue:false so animations run at same time)
                    li.siblings().removeClass('active').animate({ opacity: 0 }, {
                        duration: settings.fadeDuration, 
                        queue: false,
                        complete: function() { 
                            $(this).css('display', 'none'); 
                        }
                    });
                    
                    
                    // Show 'active' LI banner
                    li.addClass('active').css({ display:'block', opacity:0 }).animate({ opacity: 1 }, { 
                        duration: settings.fadeDuration,
                        queue: false,
                        complete: function() {
                            
                        }
                    });
                
                }

            };
        
            /** CREATE THE CONTROL BUTTONS **/
            var controls = '<ul class="banner-controls">';
            for (i=1; i<=numBanners; i++) {
                controls += '<li><a href="#">'+i+'</a></li>';
            }
            controls += '</ul>';        
        
            /** INITIAL BANNER SETUP **/
            $(this)
                // APPEND controls
                .append(controls)
                // APPEND 'shadow' element [ Add 'Shadow' element fix ( Skylight-specific addition ) ]
                .append('<span class="banner-shadow"></span>')
                // SET 'controls' configuration
                .find(controlsSelector)
                    .children(':first-child').addClass('active')
                    .end()
                    .children(':last-child').addClass('last')
            ;
        
            /** SET CLICK ACTION OF CONTROL BUTTONS **/
            $(controlsSelector)
                .children()
                    .click(function() { 
                        var clickThis = $(this);
                        var clickThisIndex = clickThis.index();
                        
                        // STOP the fade cycle
                        stop = true;
                        
                        // ADD/REMOVE 'active' class
                        clickThis
                            // Remove any 'active' class from the control buttons
                            .closest('ul').children().removeClass('active')
                            .end().end()
                            // Then add class 'active' to clicked item
                            .addClass('active')
                        ;
                        // RUN the banner fade function (passing in the clicked button's index)
                        showBanner(clickThis.index());
                        
                        // Don't run the native click action on the control button A tag
                        return false; 
                    })
            ;
            
            /** SET PAUSE/RESUME ON BANNER HOVER **/
            $(this)
                // MOUSEENTER the item (image)
                .mouseenter(function() {
                    // Pause the fade
                    stop = true;
                })
                // MOUSELEAVE the item (image)
                .mouseleave(function() {
                    // Start the fade
                    stop = false;
                })
            ;
            
            /** SET INTERVAL SO BANNERS FADE CONTINUOUSLY **/
            if ( settings.autoplay ) {
                window.setInterval(function(){
                    if( !stop ) {
                        showNextBanner();
                    }
                }, settings.timeout);
            }            
        });
    };
    
    /** FOCUS/BLUR *****/
    $.fn.PINT_focusBlur = function() {
        this.each(function() {
            var eachThis = $(this);
            var text = eachThis.text();
            var animDuration = 200;
        
            eachThis
                // Show LABEL on load (Hide with CSS for nonJS enabled browsers
                .children('label').css({ display:'block' })
                .end()
                // Get the INPUT
                .children('input')
                    .focus(function(){
                        var focusThis = $(this);
                        var focusLabel = focusThis.siblings('label');
                        // Add class to LABEL so we can change its color, etc.
                        focusLabel.addClass('active');
                        focusThis
                            // When you type in the input, hide the label
                            .bind('keydown', function () {
                                focusLabel.stop().animate({ opacity:0 }, animDuration, function() {})
                            })
                            // If you remove all your entered text, show the label again
                            .bind('keyup', function () {
                                if (focusThis.val() == '') {
                                    focusLabel.stop().animate({ opacity:1 }, animDuration, function() {});
                                }
                            })
                    })
                    .blur(function(){ 
                        var blurThis = $(this);
                        var blurLabel = blurThis.siblings('label');
                        // Remove LABEL class to return to default settings
                        blurThis.siblings('label').removeClass('active');
                        // Show the LABEL if you click out of the 'empty' input
                        if (blurThis.val() == '') {
                            blurLabel.stop().animate({ opacity:1 }, animDuration, function() {})		
                        }
                    })
            ;
        });
    };
    
    /** HOVER FADE *****/
    // Link 'hover' fade effect
    $.fn.PINT_hoverFade = function(options) {
        
        var settings = $.extend( {
            wrapEl:'span',
            wrapClass:'',
            fadeEl:'em',
            speed:'300'
        }, options);
    
        return this.each(function() {
            // If target doesn't have class="hover", add it
            if ( !$(this).hasClass('hover') ) $(this).addClass('hover');
            // Store link width. We'll set same width to '.bg' element
            // Since Chrome doesn't mask correctly on border-radius
            var parentWidth = $(this).width();
            // Get inner html
            var thisHtml = $(this).html();
            var wrapClass = ( settings.wrapClass != '' ) ? ' class="'+ settings.wrapClass +'"' : '';
            // 'this' should be an A tag
            $(this)
                .html('<'+ settings.wrapEl + wrapClass +'>'+ thisHtml +'</'+ settings.wrapEl +'>')
                // Add hover-fade 'code'
                .append('<'+ settings.fadeEl +' class="bg"></'+ settings.fadeEl +'>')
                // If link/this doesn't have 'active' class explicity set in the page on load...
                .not('.active')
                .bind('mouseenter', function() {
                    // 'Fade-In' the new 'hover-fade code'
                    $(this)
                        .children(':last-child')
                            .stop()
                            .animate({ opacity:1 }, settings.speed, function() {})
                    ;
                })
                .bind('mouseleave', function() {
                    // Fade it back out
                    $(this)
                        .children(':last-child')
                            .stop()
                            .css({ opacity:0 })
                    ;
                })
            ;
        });
    };
    
    /** LIST FLOW **********/
    // Flow a single UL into multiple UL 'column' instances
    $.fn.PINT_listFlow = function() {
        var rows = this.attr('data-rows');
        this.each(function() {
            var UL = $(this).children('ul');
            var totalItems = UL.children().size(); // Find the total # of LIs
            var perColumn = Math.ceil(totalItems / rows); // set # items per column
            var items = UL.children(); // Store the child LIs
            var parent = UL.parent(); // Store the parent of the UL so we can append back in UL rows
            UL.remove(); // Remove the starting UL
            
            // For each column
            for(var i = 0; i < rows; i++) {
                // append a new UL and populate it with the correct number of items
                // the Math.min adds LIs up to our column limit, and the totalItems parameter gets the remaining items
                parent.append($('<ul>').append(items.slice(i*perColumn, Math.min(i*perColumn + perColumn, totalItems))));
            }
            
            // Set classes for styling
            parent.find('li:last-child').addClass('last');
            
            /** HOVER FADE *****/
            $('a.btn2').PINT_hoverFade();
        });
    };
    
    /** PRIMARY NAV HOVER *****/
    // Makes the level 1 item hover state 'stick' when you traverse to its sub-items
    $.fn.PINT_menu = function() {
        this.each(function() {
            $(this)
                .bind('mouseover', function(e) {
                    if ( $(e.relatedTarget).is( $(this).next() ) ) return false;
                    $(this).addClass('nav-hover');
                })
                .bind('mouseout', function(e) {
                    if ( $(e.relatedTarget).is( $(this).next() ) ) return false;
                    $(this).removeClass('nav-hover');
                })
            ;
        });
    };
    
    
    /** TABLE ROW CLICK *****/
    // Click anywhere in table row activates interior A tag href ( Finds first A tag instance in row )
    $.fn.PINT_tableRowClick = function() {
        this.each(function() {
            var eachThis = $(this);
            var thisLink = eachThis.find('a').first();
            var thisHref = thisLink.attr('href');
            var hasTarget = thisLink.attr('target');

            // IF row has an A link ( .length since eachThis.find('a') returns empty array not null if not found ) AND its href value is NOT null (#)...
            if ( thisLink.length && thisHref != '#' ) {
                var target = ( hasTarget ) ? hasTarget : '_self';
                eachThis
                    .css({ cursor:'pointer' })
                    .click(function() {
                        window.open(thisHref,target);
                    })
                ;
            }
        });
    };
    
}(jQuery));


/** INITIALIZE/RUN FUNCTIONS **********/
$(document).ready(function() {

    /** JS ENABLED - Target any element differently (CSS) if JS enabled/disabled *****/
    $('body').PINT_addClass('js');
    $('#nav li:last-child, #h-callout > div:last-child').PINT_addClass('last');
    
    /** ANCHOR SLIDE *****/
    $('a[href^=#]').PINT_anchorSlide({ excludeHash:'#container' });
        
    /** CUSTOM 'SELECT-STYLE' DROPDOWN *****/
    $('div.component-dropdown').PINT_customDropdown();
    
    /** CUSTOM SELECT BACKGROUNDS *****/
    $('div.custom-select').PINT_customSelect();
        
    /** BANNER FADE *****/
    $('div.banner').PINT_fade();

    /** INPUT/TEXTAREA: 'FOCUS/BLUR' *****/
    $('div.input, div.textarea').PINT_focusBlur();
    
    /** HOVER FADE *****/
    $('a.btn1, a.btn2, #social a').PINT_hoverFade();
    
    /** LIST FLOW *****/
    $('div.list-items').PINT_listFlow();
    
    /** PRIMARY NAV HOVER *****/
    $('#nav > li').PINT_menu();
    
    /** TABLE ROW CLICK *****/
    $('table.tbl-list tr').PINT_tableRowClick();
    
    
    /** FANCYBOX OVERLAYS *****/
    if ( $('a.overlay-iframe').length || $('a.overlay-image').length ) {
        $('a.overlay-iframe').fancybox({ 'width':'60%','height':'60%','autoScale':false,'transitionIn':'fade','transitionOut':'fade','type':'iframe'});
        $('a.overlay-image').fancybox();
        $('#fancybox-tmp,#fancybox-loading,#fancybox-bg-n,#fancybox-bg-ne,#fancybox-bg-e,#fancybox-bg-se,#fancybox-bg-s,#fancybox-bg-sw,#fancybox-bg-w,#fancybox-bg-nw,#fancybox-left,#fancybox-right').remove();
    }
    
    
    /** JS CODE LOADING DEFERMENT TO AFTER JQUERY LOADS (DO NOT REMOVE) **********/

        /* 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 **********/
    
});
