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

    /* 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() {
                    var eachThis = $(this);
                    if (($.browser.msie && $.browser.version.substr(0,1)<=9) || !$.browser.msie) {
	
                            eachThis
                                    .find('select')
                                    //.attr('selectedIndex',-1)
                                    .change(function() {
                                            var changeThis = $(this);
                                            var optionText = changeThis.find('option:selected').text();
                                            changeThis.siblings('label').text(optionText)
                                    }).change()
                            ;
                    }
            })
    };
        
    /** FOCUS/BLUR **********/
    $.fn.PINT_focusBlur = function() {
        this.each(function() {
            var eachThis = $(this);
            var text = eachThis.attr('value');
        
            eachThis
                .focus(function(){
                    var focusThis = $(this);
                    if (focusThis.val() == text) {
                        focusThis.val('')
                        if ( focusThis.hasClass('input-password') ) this.type = 'password';
                    }
                }).blur(function(){ 
                    var blurThis = $(this);
                    if (blurThis.val() == '') {
                        if ( blurThis.hasClass('input-password') ) this.type = 'text';
                        blurThis.val(text);                
                    }
                })
            ;
        });
    };

    /** 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('height',newHeight);
        });
    };
    
    /** MENU (TRIGGER+'DROP' BOX) **********/
    // JS Controlled Main-Nav Dropdown (Overides CSS-Only dropdown) or show/hide something based on trigger
    $.fn.PINT_menu = function() { 
        this.each(function() {
            var eachThis = $(this);
            eachThis
                .next('.menu-box').hide()
                .end()
                .bind('mouseover', function(e) {
                    if ( $(e.relatedTarget).is('.menu-box, .menu-box *') ) return false;
                    $(this).addClass('hover').next().show();
                })
                .bind('mouseout', function(e) {
                    if ( $(e.relatedTarget).is('.menu-box, .menu-box *') ) return false;
                    $(this).removeClass('hover').next().hide();
                })
            ;
            
            $('.menu-box').bind('mouseout', function(e) {
                if ( $(e.relatedTarget).is('.menu, .menu-box, .menu-box *') ) return false;
                $(this).hide().prev().removeClass('hover');
            });
        });
    };

    /** NAV STICK **********/
    $.fn.PINT_navStick = function() {
        this.each(function() {
            var eachThis = $(this);
            eachThis
                .bind('mouseover', function(e) {
                    $('#nav li').removeClass('hover');
                    $(this).addClass('hover');
                })
                .bind('mouseout', function(e) {
                 //   if ( $(e.relatedTarget).is('ul, ul *') ) return false;
                    $(this).removeClass('hover');
                })
            ;
            
            $('.menu-box').bind('mouseout', function(e) {
             //   if ( $(e.relatedTarget).is('.drop, ul, ul *') ) return false;
                $(this).removeClass('hover');
            });
        });
    };
    

    /** BANNER ROTATE w/ CONTROLS **********/
    $.fn.PINT_rotateControls = function() {
        this.each(function() {
            var eachThis = $(this);
            var galleryControls = '<div class="gallery-controls clearfix"><span id="prev"></span><span id="next"></span></div>';
            eachThis
                .append(galleryControls)
                .children('div:first-child')
                    .cycle({
                        fx: 'fade',
                        speed: 1600,
                        timeout: 8000,
                        next: '#next', 
                        prev: '#prev',
                        pause: true
                    })
            ;
        })
    };
    
    /** ROTATE (FADER) w/o CONTROLS **********/
    $.fn.PINT_fader = function() {
        this.each(function() {
            var eachThis = $(this);
            eachThis.cycle({
                fx: 'fade',
                speed: 1600,
                timeout: 4000
            });
        });
    };
    
    
    /** ACCORDION ADDITIOINS **********/
    /*
    $.fn.PINT_accord = function() {
        this.each(function() {
            $(this).next().show();
            if(jQuery(this).find('a').hasClass('expand')){
               jQuery(this).find('a').removeClass('expand');
               jQuery(this).find('a').addClass('close');
            }
        });
    };
    */
    
    /** LINKED BOXES **********/
    $.fn.PINT_linked_box = function() {
        this.each(function() {    
           jQuery(this).click(function(){
                
                // jQuery(this).children('a').attr('href');
                var link = jQuery(this).find('a').attr('href');
                //console.debug(link);

                window.location = link;

            });
        });
    };    
    
    /** HOME HOVER LINKED BOXES ***********/
    $.fn.PINT_hover_linked_box = function() {
        this.each(function() {    
           jQuery(this).mouseover(function(){
                jQuery(this).attr('id', 'on'); 
                jQuery(this).find('a').addClass('hover');
            });
           jQuery(this).mouseout(function(){
                jQuery(this).attr('id', '');   
                jQuery(this).find('a').removeClass('hover');
            });
        });
    };
    
    /** RIGHT SIDE LINKED BOXES **********/
    $.fn.PINT_right_linked_box = function() { 
        this.each(function() {  
           jQuery(this).css('cursor','pointer');
           
           jQuery(this).click(function(){ 
                var link = jQuery(this).find('a').attr('href');
                window.location = link;

            });
        });
    };   

    $.fn.PINT_accord2 = function() {
        this.each(function() {    
           jQuery(this).click(function(){

               if(jQuery(this).find('a').hasClass('close')){
                  jQuery(this).find('a').removeClass('close');
                  jQuery(this).find('a').addClass('expand');
                  $(this).next().hide();
               }else{
                 jQuery(this).find('a').removeClass('expand');
                 jQuery(this).find('a').addClass('close');
                 $(this).next().show();
               }

               $(this).siblings('h2').find('a').removeClass('close').addClass('expand');

            });
        });
    };
    
    
}(jQuery));


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

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

    /** FADER **********/
    $('.component-rotate ul').PINT_fader();
    
    /** INPUT: 'FOCUS/BLUR' **********/
    $('.input').PINT_focusBlur();

    /* ELEMENT MATCH HEIGHT **********/
    // Sets the target element's height to match that of another element
    $('#layout-2col #col1').PINT_matchHeight('#sidebar',50); // Add offset as 2nd parameter if desired (number)
    
    /** JS DROPDOWN (SEE DROPDOWN IN HEADER) **********/
    $('.menu').PINT_menu();
    $('#nav li').PINT_navStick();

    /** BANNER ROTATE **********/
    $('.component-rotate-controls').PINT_rotateControls();
    
    /* CUSTOM SELECT BACKGROUNDS **********/
    $('.custom-select').PINT_customSelect();
        
    
    /** ACCORDION **********/
    $(".accordion").accordion({
        autoHeight: false
    });
    
    if ( $(".accordion").length ){
        $(".accordion h2.ui-state-active").next().show();
        $(".accordion h2.ui-state-active").find('a').removeClass('expand').addClass('close');
        $(".accordion h2").PINT_accord2();
    }    
    
    /** LINKED BOXES *****/
    $('#linked_boxes li').PINT_linked_box(); 
    
    /** HOME HOVER LINKED BOXES *****/
    $('.home #linked_boxes li').PINT_hover_linked_box(); 
    
    /** RIGHT SIDE LINKED BOXES *****/
    $('#sidebar .box').PINT_right_linked_box(); 
    
    /** EXTERNAL LINKS *****/
    $('a[href^=http://]').not('a[href^=http://pwp.kyocerasolar.l3.pint.com]').not('.internal').addClass('external')
    
    /** FORM VALIDATION *****/
    $('.form-generic').validate();
//    $('.form-generic').each(function() {
 //       $(this).validate();
  //  });
  
  
    /** 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 **********/      

    
    
    /*
        ul.pagination { 
            list-style: none; 
            overflow: hidden; 
            margin: 5px 0; 
            padding: 0px; 
        }
        
        ul.pagination li { 
            float: left; 
            margin: 3px; 
            cursor: pointer; 
            padding: 2px 6px; 
            color: #E31B23; 
            border: 1px solid #E31B23; 
        }
        
        ul.pagination li.on { 
            font-weight: bold; 
            cursor: default; 
            color: #000000; 
            border: none; 
        }
    */

    
    $('body').append('<style type="text/css">ul.pagination { list-style: none; overflow: hidden; margin: 5px 0; padding: 0px; } ul.pagination li { float: left; margin: 3px; cursor: pointer; padding: 2px 6px; color: #E31B23; border: 1px solid #E31B23; } ul.pagination li.on { font-weight: bold; cursor: default; color: #000000; border: none; }</style>');
    
    (function($){
        $.fn.tpaginate = function(config) {

            var defaults = {
                perPage: 15,
                itemSelector: '',
                wrapperTag: '<ul class="pagination" />',
                pageTag: '<li />',
                activePageClass: 'on'
            };
     
            var config = $.extend(defaults, config);

            return $(this).each(function(){

                var items = $(this).children(config.itemSelector);
                var pages = Math.ceil(items.length/config.perPage);
                if( pages <= 1 ) return;
                
                items.slice(config.perPage).hide();
                
                var pagination = $(config.wrapperTag);
                
                for( var i = 0; i < pages; i++ ) {
                    pagination.append($(config.pageTag).text(i+1));
                }
                
                pagination.children().first().addClass(config.activePageClass);

                pagination.children().click(function(){
                    $(this).addClass(config.activePageClass).siblings().removeClass(config.activePageClass);
                    
                    var paginate = $($(this).parent().data('paginate'));
                    var items = paginate.children();
                    var index = $(this).index()*config.perPage;
                    
                    items.hide().slice(index, index+config.perPage).show();
                });

                pagination.insertBefore($(this));

                pagination.data('paginate', this);

            });

        };
    })(jQuery);
    $('.paginate').tpaginate();

};

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