
/*global jQuery */

(function ($, window, document) {

    /*  This one's for you, Crockford...
     */

    "use strict";



    /*  Is any of this actually going to work?
     */

    if (typeof jQuery === 'undefined') {
        return null; // Hot Christ! jQuery isn't available! ABORT!
    }



    /*  Global damn variables - alphabetical order so no one gets jealous.
     */

    var body,           //  <body> element

        head,           //  <head> element

        html,           //  <html> element

        root;           //  The DOM document wrapped in a jQuery object



    /*  Instantiate just ONE jQuery object, use this to find other elements
        rather than creating a new jQuery object for each selector.
     */

    root = new $.prototype.init(document);



    /*  Any of them variables up there need setting?
     */



    /*  Plain old JavaScript functions should go here, or else JSLint will
        kick OFF and throw errors about like ninja stars or whatever.
     */



    /*	Are you using any small "utility" style jQuery methods?
     */

    $.prototype.homeLogoStateSwitcher = function () {

        this.each(function () {

            var nav  = $(this),
                logo = body.find('#home_logo');

            nav.find('li a').bind({
                mouseenter: function () {
                    var state_class = $(this).parent().attr('class');

                    logo.removeClass().addClass(state_class);
                },
                mouseleave: function () {
                    logo.removeClass();
                }
            });

        });

        return this;

    };



    $.prototype.mainSlideshow = function () {

        this.each(function () {

            var container      = $(this),
                list           = container.find('ul'),
                slides         = list.find('li'),
                slides_counter = parseInt(0, 10),
                prev           = null,
                next           = null,
                pager_html     = '',
                pager          = null;

            if (slides.length === 0) {
                return;
            }

            prev = $('<a/>', {
                'class': 'slides_control',
                'href':  '#',
                'id':    'prev',
                'html':  '&larr;'
            }).prependTo(container);

            next = $('<a/>', {
                'class': 'slides_control',
                'href':  '#',
                'id':    'next',
                'html':  '&rarr;'
            }).appendTo(container);
/*
            pager_html+= '<ul id="pager">';
            for (slides_counter; slides_counter < slides.length; slides_counter += 1) {
                pager_html+= '<li><a href="#">' + slides_counter + '</a></li>';
            }
            pager_html+= '</ul>';
*/
            pager = $('<ul id="pager"/>').appendTo(container);

            list.cycle({
                activePagerClass: 'active',
                next: next,
                pager: pager,
                pagerAnchorBuilder: function (idx, slide) {
                    return '<li><a href="#">' + (idx + 1) + '</a></li>';
                },
                prev: prev,
                timeout: 7000
            });

            pager.css('margin-top', -(slides.length * 15 + 5) / 2);

        });

        return this;

    };



    $.prototype.productSlideshow = function () {

        this.each(function () {

            var container = $(this),
                figure    = container.find('figure'),
                master    = figure.find('img'),
                thumbs    = container.find('li');

            thumbs.first().addClass('active');

            thumbs.find('a').bind('click', function (event) {
                event.preventDefault();

                var link      = $(this),
                    parent_li = link.parent(),
                    image_src = this.href;

                if (parent_li.hasClass('active')) {
                    return;
                }

                parent_li.addClass('active').siblings().removeClass();

                master.animate({opacity: 0}, 250, 'swing', function () {
                    master.attr('src', image_src).bind('load', function () {
                        master.animate({opacity: 1}, 250);
                    });
                });
            });

        });

        return this;

    };



    /*	Make Rocket Go Now!
     */

    root.ready(function () {

        /*  Cache the godforsaken <html>, <head> & <body> elements, if you're
            in to that kind of thing...
         */

        html = root.find('html');
        head = html.find('head');
        body = html.find('body');

        //  Set up image switch states on #home_logo
        body.find('#home_nav').homeLogoStateSwitcher();

        body.find('#navigation ul li').has('ul').bind({
            mouseenter: function () {
                $(this).addClass('hover');
            },
            mouseleave: function () {
                $(this).removeClass('hover');
            }
        });
		
		$("a.colorbox").colorbox({iframe:true, innerWidth:640, innerHeight:400, opacity:0.7});
		
        body.filter('.slideshow').find('#content').mainSlideshow();
        body.find('#multi_images').productSlideshow();

		$('form.default').submit( function() {
			$(this).find('button').html('Please Wait');
			return true;
		});

    });


    /*  Function's gotta return SOMETHING, right? WRONG hahahahaha just return
        something anyway though it won't matter...
     */

    return null;

}(jQuery, this, this.document));

