/**
*	Author: Jarrett M. Barnett
*	E-mail: jarrett@mc2design.com
*	Company: MC2 Design Group, Inc.
*	Copyright (c) 2009
*	Last Modified: 2010-01-27
*/

/** NOTES:
 *      initCustomFuctions() initiates custom functions that extend jQuery's library.
 *      initSearch() makes search input field clear onfocus, only when search hasn't been performed.
 *      initExtlinks() initiates external linking rules, such as rel="external", and class="noclick".
 *      initAjax() is a basic AJAX function that can be reused. It's built to really only handle one specific class usage,
 *               as most sites only need just that. Additional AJAX functions can be added here as required. Simply create
 *               the function and add it to the "jquery ready" function just below these notes.
 *
 *   BUILT FOR JQUERY v1.3.2
 */


// jquery ready
$(function () {
    //initCustomFunctions();
    //initSearch();
    //initExtlinks();
    //initAjax();
    initColorbox();
    initHomeCarousel();
    initProductCarousel();
});

function initCustomFunctions(){
    $.fn.delay = function(duration) {
        $(this).animate({ dummy: 1 }, duration);
        return this;
    };
}

function initSearch(){
    var _search = $('input#search');
    var _defaultvalue = "enter keywords";

    // On focus of search field, clear it unless user keywords exist
    _search.focus(function(){
        _searchvalue = $(this).val('');
        // only erase if user hasnt already performed a search
        if(_searchvalue == 'enter keywords') $(this).val('');
    });
}

function initExtlinks(){
    var _extlinkclass   =   $('a:external');
    var _extlink        =   $('a[rel="external"]');
    var _noclick        =   $('a.noclick');

    // Creating custom :external selector
    $.expr[':'].external = function(obj){
        return !obj.href.match(/^mailto\:/)
                && (obj.hostname != location.hostname);
    },
        // Add 'external' CSS class to all external links
        _extlinkclass.attr('rel', 'external'),


    // Open External Links In New Window
    _extlink.click(function(){
        $(this).attr('target','_blank');
    }),

    // Links that have class of noclick not direct to target href
    _noclick.click(function() {
        return false;
    });
}

function initAjax(){
    var _eventid        =   $('a.ajax'); // what element activates the AJAX
    var _dataurl        =   ''; // URL of page to have AJAX fetch
    var _dataattr       =   'href'; // attribute with URL
    var _type           =   'GET'; // GET or POST
    var _contentwrapper =   $('#ajax_content'); // content area that is populated by AJAX
    var _loadingwrapper =   $('#loading'); // loading div identifier for user friendly "progress bars"
    var _hidecontent    =   true; // true or false, true will enable the fancier "effects" for the show/hide of AJAX content
    var _datatype       =   'html';

    _eventid.click(function () {
        // stop existing animations
        _contentwrapper.stop(false,true);

        //hide the content and show the progress bar
        if (_loadingwrapper) _loadingwrapper.show();

        var _data = $(this).attr(_dataattr); // data needed to be passed

        // ajax function
        $.ajax({
            url: _dataurl + _data,
            type: _type,
            dataType: _datatype,
            cache: false,
            success: function (html) {

                //hide the progress bar
                if (_loadingwrapper) _loadingwrapper.hide();

                // add retrieved content into _contentwrapper
                if(_hidecontent == true) {
                    _contentwrapper.animate({opacity: 0, bgColor:'#fff'}, 0, null, function(){
                        _contentwrapper.html(html);
                    });
                } else {
                    _contentwrapper.html(_content);
                }
                //display the content (available effects here: http://docs.jquery.com/Effects )
                _contentwrapper.animate({opacity: 1,bgColor:'#000'}, 2000); // show(1500), fadeIn(1500), slideDown(1500), animate({width: "100%", opacity: 0.4}, 1500)
            }
        });// end ajax

        return false;

    });// end click function
}


function initPreloader(){


    jQuery.preloadImages = function()
    {
      for(var i = 0; i<arguments.length; i++)
      {
        jQuery("<img>").attr("src", arguments[i]);
      }
    }

    $(document).ready(function(){
            $.preloadImages(
                    // design elements
                    "/themes/nevinandwitt/assets/graphics/sb-menu-active.gif",
                    "/themes/nevinandwitt/assets/graphics/more-active.png"
            );
    }); // end jQuery


}

function initColorbox(){
                $(".lightbox").colorbox();
                $("a.documentation").colorbox({inline:true, href:"#product_documentation"});
}

function initHomeCarousel() {

    var settings = {};

//    carousel unique name
    settings['name']                    =   'homecarousel';

//    elements
    settings['selector']                =   '#carousel ul li';
    settings['slide_selector_class']    =   ''; // only apply to selectors with the provided class
    settings['desc_selector']           =   '';

//    default settings
    settings['autoplay']                =   true;
    settings['slide_timing']            =   5000; // slide display timer in milliseconds
    settings['enable_controls']         =   false;

//    button controls
    settings['prev_btn']                =   'a.prev';
    settings['next_btn']                =   'a.next';
    settings['play_btn']                =   'a.play';
    settings['pause_btn']               =   'a.pause';

//    transition controls
    settings['effect_transition']       =   'fade'; // transition effect
    settings['effect_timing']           =   1200; // fade transition in milliseconds

//    active / last-active classes
    settings['active_class']            =   'active'; // don't forget to set the first element to opacity 0 by default
    settings['lastactive_class']        =   'last-active';

    homeCarousel = new mc2Carousel(settings);
}

function initProductCarousel() {

    var settings = {};

//    carousel unique name
    settings['name']                    =   'prodcarousel';

//    elements
    settings['selector']                =   '#carousel_products ul li';
    settings['slide_selector_class']    =   ''; // only apply to selectors with the provided class
    settings['desc_selector']           =   '';

//    default settings
    settings['autoplay']                =   true;
    settings['slide_timing']            =   5000; // slide display timer in milliseconds
    settings['enable_controls']         =   false;

//    button controls
    settings['prev_btn']                =   'a.prev';
    settings['next_btn']                =   'a.next';
    settings['play_btn']                =   'a.play';
    settings['pause_btn']               =   'a.pause';

//    transition controls
    settings['effect_transition']       =   'fade'; // transition effect
    settings['effect_timing']           =   1200; // fade transition in milliseconds

//    active / last-active classes
    settings['active_class']            =   'active'; // don't forget to set the first element to opacity 0 by default
    settings['lastactive_class']        =   'last-active';

    homeCarousel = new mc2Carousel(settings);
}


function mc2Carousel(settings) {

    $("#splashcarousel").show();


    var settings2 = settings;

    function processCarousel() {

        var _selector           = settings.selector;

        //var _selector_class     = settings['slide_selector_class'];
        var _activeclass        = settings['active_class'];
        var _lastactiveclass    = settings['lastactive_class'];
        var _effect_timing      = settings['effect_timing'];

        var $active = $(_selector+'.'+_activeclass);

        if ( $active.length == 0 ) $active = $(_selector+':last');
        var $next     = $active.next().length ? $active.next() : $(_selector+':first');

        $active.addClass(_lastactiveclass);

        $next.css({opacity: 0.0})
            .addClass(_activeclass)
            .animate({opacity: 1.0}, _effect_timing),

        $active.removeClass(_activeclass+' '+_lastactiveclass),
        $active.animate({opacity: 0.0}, _effect_timing);

        if(settings.enable_controls) {
            settings.prev_btn.removeClass(_activeclass),
            settings.pause_btn.addClass(_activeclass);
        }
    } // end process Carousel

    if ($(settings.selector).length > 1) // run Carousel only if there is more than 1 element to carousel through
    {
        window[settings['name']] = setInterval(processCarousel, 5000, settings2);
    }

}
