﻿(function(jq) {
    jq(document).ready(function() {

        function doBox() {

            var options = {
                preload: true,
                navigation: true,
                animate: true,
                close: true,
                caption: true
            }
            // override box methods;
            jq.fn.box.getFileSize = function(url) {
                return [576, ''];
            };
            jq.fn.box.getCaption = function(index, total) {
                var title = jq('.ajax h2').text();
                var html = '<div><strong>' + title + '</strong></div>';
                html += '<div>Flavour ' + index + ' of ' + total + '</div>';
                return html;
            };
            jq(".flavours").box(options);

        };

        function doBox2() {

            var options = {
                preload: true,
                navigation: false,
                animate: true,
                close: true,
                caption: false
            }
            jq("#tvAds").box(options);
            jq.fn.box.getFlashSize = function(elem) {
                return [640, 400];
            };
            
        };

        function stripe() {
            return jq('.box tbody tr').each(function() {
                if (!(arguments[0] % 2)) {
                    jq(this).addClass('stripe');
                };
            })
        };

        function doToggle() {
            jq(".toggle").append("<span class=\"control\"></span>").next().hide();
            jq(".toggle").click(
				function() {
				    if (jq(this).next().is(":visible")) {
				        jq(this).next().slideUp();
				        jq(this).css({ borderBottom: 'solid 1px #ccc' });
				        jq(this).children(".control").css({ backgroundPosition: 'top left' });
				    } else {
				        jq(".toggle").next().slideUp();
				        jq(".toggle").css({ borderBottom: 'solid 1px #ccc' });
				        jq(".toggle").children(".control").css({ backgroundPosition: 'top left' });
				        jq(this).next().slideDown();
				        jq(this).css({ borderBottom: 'none' });
				        jq(this).children(".control").css({ backgroundPosition: 'bottom left' });
				    }
				}
			);
        };

        function intialise() {

            // get lightbox js if required;
            if (!!(jq(".flavours").get(0))) {
                jq.ajaxSetup({ async: false });
                jq.getScript(danone.root + 'Includes/js/jquery.box.js', doBox);
                jq.ajaxSetup({ async: true });
            };

            // get lightbox js if required;
            if (!!(jq("#tvAds").get(0))) {
                jq.ajaxSetup({ async: false });
                doBox2();
                jq.ajaxSetup({ async: true });
            };

            if (!!(jq(".hasToggle").get(0))) {
                doToggle();
            };

            // listen for custom box event and stripe table;
            jq(document).bind('boxcomplete', stripe);

        }

        intialise();
    });
})(jQuery);