if(typeof this.PINT==="undefined"){var PINT={};}

/* FUNCTIONS *******************/

(function ($) {

    /** ADD CLASS *****/
    // Format: $('.something').PINT_addClass('last');
    $.fn.PINT_addClass = function(addClass) {
        this.each(function() {
            $(this).addClass(addClass);
        });
    };
        
    /** BLOCKQUOTE *****/
    // Format: $('.something').PINT_blockquote();
    $.fn.PINT_blockquote = function(addClass) {
        this.each(function() {
            var thisWrappedHtml = $(this).html().replace(/"/gi, '<span>"</span>'); // Replace all " characters with wrapper code
            
            $(this)
                // Add 'fixed' text
                .html( thisWrappedHtml )
                // Add 'first' class to first child
                .children(':first-child')
                    .addClass('first')
                .end()
                // Add 'last' class to last child
                .children(':last-child')
                    .addClass('last');
        });
    };

    /** FADER/ROTATE *****/
    $.fn.PINT_fader = function() {
        this.each(function() {
            $(this).cycle({
                fx: 'fade',
                speed: 1400,
                timeout: 6000
            });
        });
    };
    
    /** FOCUS/BLUR *****/
    $.fn.PINT_focusBlur = function() {
        this.each(function() {
            var text = $(this).attr('value');
        
            $(this)
                .focus(function(){
                    if ($(this).val() == text) {
                        $(this).val('')
                        if ( $(this).hasClass('input-password') ) this.type = 'password';
                    }
                }).blur(function(){ 
                    if ($(this).val() == '') {
                        if ( $(this).hasClass('input-password') ) this.type = 'text';
                        $(this).val(text);				
                    }
                })
            ;
        });
    };

    /** COMPONENT-LINK BORDER FIX *****/
    $.fn.PINT_linkBorderFix = function() {
        this.each(function() {
            $(this).append( '<div class="border">&nbsp;</div>' );
        });
    };
    
    /* 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 tgtHeight = $(this).height();
            var copyHeight = $(copy).height();
            offset = (offset) ? offset : 0; // If offset set use it, else set offset to zero
            var newHeight = copyHeight-offset;
            
            if ( ($(this).length) && (copyHeight > tgtHeight) ) $(this).css('height',newHeight);
        });
    };

    /** TABS *****/
    // Write out the tabs in JS so that w/ JS disabled we don't show buttons that don't work!
    $.fn.PINT_tabs = function() {
        // Find each TAB instance on the page
        this.each(function(tabsIndex, tabsElement){
            // Add a class only if JS enabled. Allows for tabbox top-margin
            $(this).addClass('tabs-jsenabled').find('.tab-title').hide();
        
            var tabs = $(this).find('div.tabbox'); // Get tabboxs
            var ul = $('<ul class="tab-links"></ul>'); // Create UL links only when JS enabled
            
            // For each tabbox we're going to create corresponding <li>
            tabs.each(function(index, element){
                var thisID = $(this).attr('id'); // Get tabbox ID
                var thisChildren = $(this).find('.tab-title'); // Get tabbox HTML
                var selected = (index == 0) ? ' class="ui-tabs-selected"' : '';  // Sets which tab starts 'on'
                
                // Add <li>s to the UL per tabbox
                ul.append('<li'+selected+'><a href="#'+thisID+'"><span>'+thisChildren.html()+'</span></a></li>');
            });
            
            // Add the <ul> to the TAB instance
            $(this).prepend(ul);
        });
        if ( $('div.tabs').length ) {
            this.tabs();
        }
    };


}(jQuery));


/* INITIALIZE FUNCTIONS *******************/
PINT.init = function() {

    /** JS ENABLED - Target any element differently (CSS) if JS enabled/disabled *****/
    $('body').PINT_addClass('js-enabled');

    /** BLOCKQUOTE *****/
    $('blockquote').PINT_blockquote();
    
    /** INPUT: 'FOCUS/BLUR' *****/
    $('input.input').PINT_focusBlur();
    
    /** TESTIMONIAL (FADER/ROTATOR) **********/
    $('div.component-testimonial ul').PINT_fader();

    /** COMPONENT-LINK BORDER FIX *****/
    $('div.component-links').PINT_linkBorderFix();
    
    
    /***** HOMEPAGE *****/
    /* Element Match Height */
    $('#h-main1').PINT_matchHeight('#h-main2');
    $('#h-main2').PINT_matchHeight('#h-main1');

    
    /***** PRODUCT DETAIL PAGE *****/
    /* Tabs */
    $('div.tabs').PINT_tabs();
	
	/***** FANCYBOX OVERLAY *****/
	if( $('.pintbox').length || $('.pintbox-iframe').length || $("a[rel^=gallery]").length ){
		$('.pintbox').fancybox({ 'titleShow':false });
		$('.pintbox-iframe').fancybox({ 'width':'75%','height':'75%','autoScale':false,'transitionIn':'fade','transitionOut':'fade','type':'iframe'});
		// Gallery
		$("a[rel^=gallery]").fancybox({
			'padding'			: 20,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'titlePosition' 	: 'over',
			'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' of ' + currentArray.length + ' ' + '</span><span id="fprev" onclick="$.fancybox.prev(); return false;"><a href="#">Prev</a></span><span id="fnext" onclick="$.fancybox.next(); return false;"><a href="#">Next</a></span>';
				// <span id="fprev"><a href="#">Prev</a></span><span id="fnext"><a href="#">Next</a></span>
			}
		});	
	};
	
    
};

/* RUN FUNCTIONS *******************/
$(document).ready(function() {
  PINT.init();
});
