
var head,
    body,
    Des = Des || {};

(function ($, window, document, undefined) {

    if (typeof $ === 'undefined') {
        //  jQuery isn't available - abort!
        return;
    }

    //	--	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);

    //	--	Make Rocket Go Now
    $.root.ready(function () {

        //	Cache <head> and <body> elements
        head = $.root.find('head');
        body = $.root.find('body');

        //	Add "js" class to <body> for styling
        body.addClass('js');

        if (body.find('.boxy a')[0]) {
            body.find('.boxy')
                .css('cursor', 'pointer')
                .bind({
                    click: function () {
                        window.location = $(this).find('a').attr('href');
                    },
                    mouseenter: function () {
                        $(this).addClass('hover');
                    },
                    mouseleave: function () {
                        $(this).removeClass('hover');
                    }
                });
        }

        Des.booking_form();
        Des.courses();

        body.find('.video_embed').videoEmbed();
        body.find('#content .boxy').equalHeights();
        body.find('.home_course_slides').cycle({timeout: 7000});

    });



    $.prototype.equalHeights = function () {
        var tallest_item = 0;

        return this.each(function () {
            var current_item = $(this),
                item_height  = current_item.height();

            if (item_height > tallest_item) {
                tallest_item = item_height;
            }
        }).css('height', tallest_item);
    }



    //	General Video embedder thinger
    $.prototype.videoEmbed = function () {
        return this.each(function () {

            var video  = $(this),
                config = {};

            config.params = {
                allowfullscreen: true,
                allowscriptaccess: 'always',
                wmode: 'transparent'
            };

            config.height = video.dataAttr('height');
            config.width  = video.dataAttr('width');
            config.wmode  = 'transparent';

            if (video.dataAttr('type') === 'youtube') {
                config.swf = video.dataAttr('url');
                config.flashvars = {
                    fs: 1,
                    hl: 'en',
                    rel: 0
                };
            }
            else {
                config.swf = 'http://vimeo.com/moogaloop.swf';
                config.flashvars = {
                    clip_id: video.dataAttr('id'),
                    server: 'vimeo.com',
                    show_title: 1,
                    show_byline: 1,
                    show_portrait: 0,
                    fullscreen: 1
                };
            }

            video.empty().flash(config);

        });
    };



    Des.booking_form = function () {
        var discount,
            del_count,
            price_per_del,
            vat_rate,
            sub_total,
            discount_total,
            vat_total,
            final_total;

        //	check if courses table exists
        if (body.find('form.booking_form')[0]) {
            body.find('#delegates').change(function () {
                discount  = false;
                del_count = $(this).val();

                if (body.find('.discount_row')[0]) {
                    body.find('.discount_row').remove();
                }

                if (del_count > 1) {
                    discount = true;
                }

                price_per_del = body.find('span.price').text();
                vat_rate      = body.find('span.vat_rate').text();

                sub_total = price_per_del * del_count;

                if (discount) {
                    discount_total = (sub_total / 100) * 5;
                    sub_total      = sub_total - discount_total;

                    $('<tr class="discount_row"><th>Discount:</th><td>&pound;' + discount_total.toFixed(2) + '</span></td></tr>').insertBefore('.sub_total_row');
                }

                vat_total   = (sub_total / 100) * vat_rate;
                final_total = sub_total + vat_total;

                body.find('.del_count').text(del_count);
                body.find('.sub_total').text(sub_total.toFixed(2));
                body.find('.vat_total').text(vat_total.toFixed(2));
                body.find('.final_total').text(final_total.toFixed(2));
            }).change();
        }
    };



    Des.courses = function () {
        var courses,
            display_show,
            cities;

        //	define actions for showing and hiding courses
        function pagination_init() {
            var show_city = body.find('form.city_select').find('select').val();

            if (show_city !== 'All Cities') {
                body.find('.side_courses_wrap').css('height', 'auto');

                body.find('table.side_courses tbody tr').removeClass('odd');

                body.find('.courses_pagination').empty().css('display', 'none');

                body.find('table.side_courses tbody tr td.city').each(function () {
                    var display_val = $(this).text() === show_city ? display_show : 'none';

                    $(this).parent().css('display', display_val);
                });

                body.find('table.side_courses tbody tr:visible:even').addClass('odd');
            }
            else {
                body.find('.side_courses_wrap').css('height', 202);

                body.find('.courses_pagination').pagination(courses.length, {
                    callback: function (page_index) {
                        body.find('table.side_courses tbody tr').removeClass('odd');

                        var items_per_page = 5,
                            max_elem       = Math.min((page_index + 1) * items_per_page, courses.length),
                            i = page_index * items_per_page;

                        body.find('table.side_courses tbody tr').css({ display : 'none' });

                        for (; i < max_elem;) {
                            body.find('table.side_courses tbody tr:eq(' + i + ')').css('display', display_show);
                            i += 1;
                        }

                        body.find('table.side_courses tbody tr:visible:even').addClass('odd');

                        return false;
                    },
                    items_per_page: 5,
                    num_display_entries: 5
                }).css('display', 'block');
            }

            body.find('.showing_courses').find('span').text(show_city);
        }



        //	check if courses table exists
        if (body.find('table.side_courses')[0]) {
            body.find('table.side_courses').wrap('<div class="side_courses_wrap"></div>');

            courses      = body.find('table.side_courses tbody tr');
            display_show = !+"\v1" ? 'block' : 'table-row';

            if (courses.length > 5) {
                cities = [];

                //	count number of unique cities
                body.find('table.side_courses tbody tr').each(function () {
                    var city = $(this).find('td.city').text();

                    if (!in_array(city, cities)) {
                        cities.push(city);
                    }
                });

                sort(cities);

                if (cities.length > 1) {
                    $('<form class="clearfix city_select"><fieldset><label for="city">Showing dates in:</label> <select id="city"><option>All Cities</option></select></label></fieldset></form>').insertBefore('.side_courses_wrap');

                    $(cities).each(function (index, value) {
                        $('<option>' + value + '</option>').appendTo('.city_select select');
                    });

                    body.find('form.city_select select').change(function () {
                        body.find('form.city_select').submit();
                    });

                    body.find('form.city_select').submit(function () {
                        pagination_init();

                        return false;
                    });

                }

                $('<div class="courses_pagination"></div>').insertAfter('.side_courses_wrap');

                pagination_init();
            }
        }
    };

}(jQuery, this, this.document));
//	--	Good.. Can you say it faster?

