/**
*	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();
    //initNavigation();
    //initSubnavigation();
    initColorbox();
	 // Autostart Slideshow
    //setInterval(initSlideshow, 5000 );
});

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 initNavigation(){
    var _activeClass = 'active';

    $('ul#nav').each(function(){
            var _menu = $(this);
            var _items = _menu.find('>li:has(ul)');

            _items.each(function(){
                    var _item = $(this);
                    var _drop = _item.find('>ul');
                    var _opener = _item.find('>a');
/*
                    _opener.click(function(){
                            if(_item.hasClass(_activeClass)) {
                                    _item.removeClass(_activeClass);
                                    _drop.hide();
                                    //_menu.css({marginBottom:0})
                                    _menu.css({marginBottom:_drop.outerHeight(true)})
                            } else {
                                    _items.filter('.'+_activeClass).not(_item).removeClass(_activeClass).find('>ul').hide();
                                    _item.addClass(_activeClass);
                                    _drop.show();
                                    _menu.css({marginBottom:_drop.outerHeight(true)})
                            }
                            return false;
                    });*/
            });
            // set margin based on whether sub navigation exists or not
            _menu.css({marginBottom: _items.filter('.'+_activeClass).eq(0).find('>ul').outerHeight(true)});
    });
}

function initSubnavigation() {
    var _subpages = $('div.twocolumns');
    var _subnavitem = $("li.sub-level-1 a");

    // BEGIN CLICK EVENT
    _subnavitem.click(function(){

        // stop existing animations
        _subpages.stop(false,true);
        // make navigation show "selected" state
        $("li.sub-level-1").removeClass('active'),
        $(this).parent().addClass('active');

        // content variables
        var _identifier     = $('div.' + $(this).attr('rel'));
        var _currentactive  = $('div.subactive');
        var _summarytext    = $('.text-block .text-holder');
        var _summaryimg     = $('.text-block img.alignright');

            // hide landing page text and image
            _summarytext.fadeOut(300);
            _summaryimg.slideUp(1000, function(){
                // hide landing page text/image if neccesary, slide-hide any showing content and slide-show requested content
                if(_subpages.hasClass('subactive')) {
                    _currentactive.fadeOut(500,function(){
                        _subpages.removeClass('subactive');
                        _identifier.fadeIn(1000, function(){
                            _identifier.addClass('subactive');
                        }); // end slideDown
                    });
                } else {
                    _identifier.fadeIn(1000, function(){
                        _identifier.addClass('subactive');
                    }); // end _identifier.slideDown()
                }
            }); // end _summaryimg.slideUp()

        return false;
    }); // END CLICK EVENT

} // end initSubnavigation()

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(){
                //Examples of how to assign the ColorBox event to elements
                $(".colorbox").colorbox();
            
}
function initSlideshow() {


    var $active = $('#gallerycycle ul li.active');
 

    if ( $active.length == 0 ) $active = $('#gallerycycle ul li:last');
    
    var $next     = $active.next().length ? $active.next() : $('#gallerycycle ul li:first');
   

    $active.addClass('last-active');
   
	


    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 700, function() {
            $active.removeClass('active last-active');
            $active.animate({opacity: 0.0}, 700);
    })
    
        }
