/*global jQuery */

//  This is where the magic happens, like on Cribs, except nowhere near as vile.
(function ($, window, document) {

    //  Is any of this actually going to work?
    if (typeof jQuery === 'undefined') {
        return; //  Hot Christ! jQuery isn't available!
    }

    //  --  Instantiate(?) just ONE jQuery object, use this to find other
    //      elements rather than creating a new one for each selector
    $.document = new $.prototype.init(document);

    //	--	Make Rocket Go Now
    $.document.ready(function () {

        //  Cache <html> <head> and <body> elements
        $.HTML = $.document.find('html');

        $.Head = $.HTML.find('head');
        $.Body = $.HTML.find('body');

        //  Invoke plugins, yo -> $.Body.find('#element').functionName({ ... });
		$.Body.find('.main_content').contentAccordian();

        //  Other bindings and whatnot
        $.Body.find('.latest_list li:nth-child(odd)').addClass('new_row');
        $.Body.find('.gallery_list li:nth-child(4n+1)').addClass('new_row');
        
        $.Body.find('.gallery_list a').colorbox({
            opacity: 0.5
        });

	});



    //	--	Hey buddy I got your custom jQuery plugins right here...

	$.fn.contentAccordian = function () {

        this.each(function () {
            var accordian = $(this),
                panels    = accordian.find('li.content_block');

            $('<p class="indicator toggle">Open</p>').appendTo(panels);

            panels.find('.toggle').bind('click', function (event) {
                event.preventDefault();

                var toggle = $(this),
                    panel  = toggle.parent();

                if (panel.hasClass('open')) {
                    panel.removeClass('open')
                        .find('.inner').slideUp()
                        .next().text('Open');

                    return;
                }

                panels.filter('.open').removeClass('open')
                    .find('.inner').slideUp()
                    .next().text('Open');

                panel.addClass('open')
                    .find('.inner').slideDown().show()
                    .next().text('Close');
            }).first().trigger('click');

            panels.not(':first').removeClass('open')
                .find('.inner').slideUp()
                .next().text('Open');
        });

        return this;

	};

}(jQuery, this, this.document));
//  --  FIX UP; LOOK SHARP!
