/**
 * JS for standard widgets.
 *
 * @author          Stephen Lewis
 * @package         experienceinternet.co.uk
 * @copyright       Experience Internet
 */

(function($){

    /*
     * ----------------------------------------------------
     * AFFILIATE LINKS
     * ----------------------------------------------------
     */
    
    function iniAffiliateLinks() {
        var eeAffiliateId       = 'experience';
        var eeRegExp            = /^(https?:\/\/(secure\.|www\.)?expressionengine\.com\/)([^#]*)(#(\w+))?$/i;
        var eeAffiliateRegExp   = new RegExp('^index\\.php\\?affiliate=' + eeAffiliateId);

        var amazonAffiliateId   = 'experienceinternet-21';
        var amazonUrlRegExp     = /^(https?:\/\/(www\.)?amazon\.(co\.uk|com))(.*)$/i;
        var amazonAsinRegExp    = /dp\/([a-z0-9]{10})\//i;

        $('a[href]').each(function() {
            href = this.href;

            // ExpressionEngine.
            eeMatch = href.match(eeRegExp);

            if (eeMatch && ! eeMatch[3].match(eeAffiliateRegExp)) {
                this.href = eeMatch[1] + 'index.php?affiliate=' + eeAffiliateId
                    + (eeMatch[3] ? '&page=/' + eeMatch[3] : '')
                    + (eeMatch[5] ? '&anchor=' + eeMatch[5] : '');

                return;
            }

            // Amazon.
            amazonMatch = href.match(amazonUrlRegExp);

            if (amazonMatch && (asinMatch = amazonMatch[4].match(amazonAsinRegExp))) {
                console.log(asinMatch);
                this.href = amazonMatch[1]
                    + '/dp/' + asinMatch[1]
                    + '/' + amazonAffiliateId;
            }
        });
    }
    
    
    /*
     * ----------------------------------------------------
     * FORM VALIDATION
     * ----------------------------------------------------
     */
    
    function iniContactForm() {
        $('#contact_form').validate({
            errorElement    : 'p',
            errorClass      : 'error',
            errorPlacement  : function(error, element) {
                error.insertAfter(element.parents('div.field').find('label')).slideDown();
            },
            messages: {
                message     : "There&rsquo;s no point sending a blank message.",
                email       : "Invalid email address (you can leave this blank if you’d prefer).",
                privacy     : "Tick the box to agree to our privacy policy."
            }
        });
    }
    
    
    function iniNewsletterForm() {
        $('#newsletter').validate({
            errorElement    : 'p',
            errorClass      : 'error',
            errorPlacement: function(error, element) {
                error.insertAfter(element.parents('div.field').find('label'));
            }
        });
    }
    
    
    
    /*
     * ----------------------------------------------------
     * TABBED NAVIGATION
     * ----------------------------------------------------
     */
    
    function enhanceTabs() {
        
        $('.enhanced_tabs li:first-child').each(function() {
            $targetTab = $($(this).find('a').attr('href'));     // Find the target content for the first tab.
            $targetTab.siblings('.tab_content').hide();         // Hide the tab content siblings.
            $(this).addClass('active');                         // 'Activate' the tab.
        });
        
        $('.enhanced_tabs a').click(function(e) {
            
            if ($(this).parent('li').hasClass('active')) {
                e.preventDefault();
                return;
            }
            
            targetTab   = e.target.hash;
            $targetTab  = $(targetTab);
            
            $targetTab.siblings('.tab_content').hide();     // Hide all the sibling tab content.
            $targetTab.fadeIn('fast');                      // Display the new tab content.
            $(this).parent('li').addClass('active')         // Set the active tab.
                .siblings('li').removeClass('active');      // 'De-activate' any other tabs.
            
            e.preventDefault();
        });
    }
    
    
    
    /*
     * ----------------------------------------------------
     * INITIALISATION
     * ----------------------------------------------------
     */
    
    enhanceTabs();
    iniAffiliateLinks();
    iniContactForm();
    iniNewsletterForm();
    
})(window.jQuery);


/* End of file          : plugins.js */
/* File location        : /assets/js/plugins.js */

