
var $$ = $.fn;



$$.extend({
    //Split() takes that element's id name, splits it on the hyphen and returns whatever characters appear after the last hyphen.
    SplitID: function()
    {
        return this.attr('id').split('-').pop();
    },
    
    // A quick place to store a default slide delay
    SlideDelay: 5000,

    // Section 1 slideshow
    Slideshow: {

        //Ready() method sets everything up
        Ready: function()
        {
            $('.tmpSlideshowControl')
            .hover(
            function() {
                $(this).addClass('tmpSlideshowControlOn');
            },
            function() {
                $(this).removeClass('tmpSlideshowControlOn');
            }
            )
            .click(
            function() {

                $$.Slideshow.Interrupted = true;

                $('.tmpSlide').hide();

                $('.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');

                $('#tmpSlide-' + $(this).SplitID()).show()
			
                $(this).addClass('tmpSlideshowControlActive');
            }
            );

            this.Counter = 1;
            this.Interrupted = false;

            this.Transition();

        },
        // stop slideshow
        Stop: function()
        {
            $('#tmpSlide-1').hide();
            $('#tmpSlide-2').hide();
            $('#tmpSlide-3').hide();
            $('.tmpSlide').hide();
            this.Counter = 0;
            this.Interrupted = true;
            $('.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');
        },


        // slide transition
        Transition: function()
        {
            if (this.Interrupted) {
                return;
            }

            this.Last = this.Counter - 1;

            if (this.Last < 1) {
                this.Last = 3;
            }

            $('#tmpSlide-' + this.Last).fadeOut(
            'slow',
            function() {
                $('#tmpSlideshowControl-' + $$.Slideshow.Last).removeClass('tmpSlideshowControlActive');
                $('#tmpSlideshowControl-' + $$.Slideshow.Counter).addClass('tmpSlideshowControlActive');
                $('#tmpSlide-' + $$.Slideshow.Counter).fadeIn('slow');

                $('#section3').removeClass('slideshowLeftSection');

                //$('#section3').addClass('sectionInactive');

                //$('#section2').addClass('sectionInactive');

                //$('#section1').removeClass('sectionInactive');

                $('#section1').addClass('slideshowLeftSection');
                
                //$('#slideshowSectionArrow').show();

               // $('#slideshowSectionArrow2').hide();

               // $('#slideshowSectionArrow3').hide();

                $$.Slideshow.Counter++;

                if ($$.Slideshow.Counter > 4) {
                    $$.Slideshow.Counter = 1;
                    $('#section2').trigger('click');
                    $$.Slideshow.Stop();
                    $$.Slideshow2.Stop();
                    $$.Slideshow1.Ready();
                }

                interval = setTimeout('$$.Slideshow.Transition();', $$.SlideDelay);

            }
            );
        }
    },

    // Section 2 slideshow
    Slideshow1: {

        //Ready() method sets everything up
        Ready: function()
        {
            $('.tmpSlideshowControl')
            .hover(
            function() {
                $(this).addClass('tmpSlideshowControlOn');
            },
            function() {
                $(this).removeClass('tmpSlideshowControlOn');
            }
            )
            .click(
            function() {
                $$.Slideshow1.Interrupted = true;

                $('.tmpSlide').hide();
                $('.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');

                $('#tmpSlide-' + $(this).SplitID()).show();
                $(this).addClass('tmpSlideshowControlActive');

            }
            );

            this.Counter = 4;
            this.Interrupted = false;

            this.Transition();

        },
        // stop slideshow
        Stop: function()
        {
            $('#tmpSlide-4').hide();
            $('#tmpSlide-5').hide();
            $('#tmpSlide-6').hide();
            $('.tmpSlide').hide();

            this.Interrupted = true;
            this.Counter = 0;
            $('.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');
        },
        // slide transition
        Transition: function()
        {
            if (this.Interrupted) {
                return;
            }

            this.Last = this.Counter - 1;

            if (this.Last < 4) {
                this.Last = 6;

            }

            $('#tmpSlide-' + this.Last).fadeOut(
            'slow',
            function() {
                $('#tmpSlideshowControl-' + $$.Slideshow1.Last).removeClass('tmpSlideshowControlActive');
                $('#tmpSlideshowControl-' + $$.Slideshow1.Counter).addClass('tmpSlideshowControlActive');
                $('#tmpSlide-' + $$.Slideshow1.Counter).fadeIn('slow');


                $('#section1').removeClass('slideshowLeftSection');

                //$('#section1').addClass('sectionInactive');

                //$('#section2').removeClass('sectionInactive');

                $('#section2').addClass('slideshowLeftSection');

               // $('#section3').addClass('sectionInactive');

                //$('#slideshowSectionArrow').hide();

                //$('#slideshowSectionArrow2').show();

               // $('#slideshowSectionArrow3').hide();

                $$.Slideshow1.Counter++;

                if ($$.Slideshow1.Counter > 7) {
                    $$.Slideshow1.Counter = 4;
                    $('#section3').trigger('click');
                    $$.Slideshow.Stop();
                    $$.Slideshow1.Stop();
                    $$.Slideshow2.Ready();
                }

                interval1 = setTimeout('$$.Slideshow1.Transition();', $$.SlideDelay);
            }
            );
        }
    },

    //Section 3 slideshow
    Slideshow2: {

        //Ready() method sets everything up
        Ready: function()
        {
            $('.tmpSlideshowControl')
            .hover(
            function() {
                $(this).addClass('tmpSlideshowControlOn');
            },
            function() {
                $(this).removeClass('tmpSlideshowControlOn');
            }
            )
            .click(
            function() {
                $$.Slideshow2.Interrupted = true;

                $('.tmpSlide').hide();
                $('.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');

                $('#tmpSlide-' + $(this).SplitID()).show();
                $(this).addClass('tmpSlideshowControlActive');

            }
            );

            this.Counter = 7;
            this.Interrupted = false;

            this.Transition();

        },
        // stop slideshow
        Stop: function()
        {
            $('#tmpSlide-7').hide();
            $('#tmpSlide-8').hide();
            $('#tmpSlide-9').hide();
            $('.tmpSlide').hide();
            this.Counter = 0;
            this.Interrupted = true;
            $('.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');

        },

        // slide transition
        Transition: function()
        {
            if (this.Interrupted) {
                return;
            }

            this.Last = this.Counter - 1;

            if (this.Last < 7) {
                this.Last = 8;
            }

            $('#tmpSlide-' + this.Last).fadeOut(
            'slow',
            function() {
                $('#tmpSlideshowControl-' + $$.Slideshow2.Last).removeClass('tmpSlideshowControlActive');
                $('#tmpSlideshowControl-' + $$.Slideshow2.Counter).addClass('tmpSlideshowControlActive');
                $('#tmpSlide-' + $$.Slideshow2.Counter).fadeIn('slow');

               // $('#section1').addClass('sectionInactive');

                $('#section2').removeClass('slideshowLeftSection');

                //$('#section2').addClass('sectionInactive');

                //$('#section3').removeClass('sectionInactive');

                $('#section3').addClass('slideshowLeftSection');
                
               // $('#slideshowSectionArrow').hide();

                //$('#slideshowSectionArrow2').hide();

                //$('#slideshowSectionArrow3').show();

                $$.Slideshow2.Counter++;

                if ($$.Slideshow2.Counter > 10) {    // khe: if > 9, show 8 items only
                    $$.Slideshow2.Counter = 7;
                    $('#section1').trigger('click');
                    $$.Slideshow1.Stop();
                    $$.Slideshow2.Stop();
                    $$.Slideshow.Ready();
                }

                interval2 = setTimeout('$$.Slideshow2.Transition();', $$.SlideDelay);
            }
            );
        }
    },

    Pause: function()
    {
        $$.Slideshow.Interrupted = true;
        if(typeof interval !== "undefined") {
            clearTimeout(interval);
        }
        $$.Slideshow1.Interrupted = true;
        if(typeof interval1 !== "undefined") {
            clearTimeout(interval1);
        }
        $$.Slideshow2.Interrupted = true;
        if(typeof interval2 !== "undefined") {
            clearTimeout(interval2);
        }

        $('#playButton').show();
        $('#pauseButton').hide();

    },

    Play: function()
    {
        // Determine which section was active by selecting .slideshowLeftSection and 
        // deducing the section number and slideshow number. This is fairly ridiculous.
        // More ideal would be to do away with multiple Slideshow objects, and rewrite 
        // one Slideshow which handles subsections properly.
        var activeSlideshow = $$['Slideshow' + (($('.slideshowLeftSection').attr('id').substr(-1) - 1) || '')];
        activeSlideshow.Interrupted = false;
        activeSlideshow.Transition();

        $('#playButton').hide();
        $('#pauseButton').show();
    }

});



$$.extend({
    Menu: {
        initialize: function(menuSelector) {
            $$.Menu.menuSelector = menuSelector || "#menu";

            // Hide submenus
            $($$.Menu.menuSelector + ' ul').hide();
			
			$('#slideshowSectionArrow1').show();
			$('#section2,#section3').addClass('sectionInactive');
			
            // Show the first submenu
            $($$.Menu.menuSelector + ' ul:first').slideDown('fast');
            // set a click handler to trigger opening
            $($$.Menu.menuSelector + ' li span').click(function() {
			
                $$.Menu.openSection(this);
            });
        },
        
        openSection: function(sectionTrigger) {
            var nextSibling = $(sectionTrigger).next();
            if (nextSibling.is('ul') && nextSibling.is(':not(:visible)')) {
                $($$.Menu.menuSelector + ' ul:visible').slideUp('slow');
                nextSibling.slideDown('slow');
				var parentSlideNumber = sectionTrigger.id;
				parentSlideNumber = parentSlideNumber.substring(parentSlideNumber.length - 1,parentSlideNumber.length);
				var parentSlideIndex = 	parentSlideNumber-1;
				$('div[id^="slideshowSectionArrow"]:gt('+parentSlideIndex+'),div[id^="slideshowSectionArrow"]:lt('+parentSlideIndex+')').hide();
				$('div[id^="slideshowSectionArrow"]:eq('+parentSlideIndex+')').show();
				
				$('span[id^="section"]').addClass('sectionInactive');
				$('#section'+parentSlideNumber).removeClass('sectionInactive');
						   
				//$("div#slideshowSectionArrow:eq("+index+")").addClass("active");
                //$('#slideshowSectionArrow2').hide();
                //$('#slideshowSectionArrow3').hide();
            }    
        }
    }
});

jQuery(function($) {
    $$.Menu.initialize();
    
    // Start first section
    $$.Slideshow.Stop();
    $$.Slideshow1.Stop();
    $$.Slideshow2.Stop();
    $$.Slideshow.Ready();
    
    //play	
    $('#play').click(function() {
        $$.Play();

    });
    //pause	
    $('#pause').click(function() {
        $$.Pause();

    });
    $('.tmpSlideshowControl').click(function() {
        $$.Pause();
    });

});
