//------------------------------------
//	HOME.JS
//	Author: 	Engage Interactive
//	Requires:	jquery 1.4.2
//				estream.js
//------------------------------------

$(function() {
    //BEGIN jQuery

    $('#panels .mini_panel:first').addClass('left_panel');

    function findUs() {

        // Settings
        var total = $('#find_us li').size() - 1;
        var current = 0;
        var mooseBan = false;

        // On load
        $('#find_us li:first').show().addClass('active');

        // NEXT BUTTON
        $('#find_next').click(function() {
            if (mooseBan == false) {
                mooseBan = true;
                current++;
                if (current > total) {
                    current = 0;
                    $('#find_us li.active').fadeOut(200, function() {
                        $(this).removeClass('active').siblings('li:first').addClass('active').fadeIn(200, function() {
                            mooseBan = false;
                        });
                    });

                } else {
                    $('#find_us li.active').fadeOut(200, function() {
                        $(this).removeClass('active').next('li').addClass('active').fadeIn(200, function() {
                            mooseBan = false;
                        });
                    });
                }
            }
            return false;
        });


        // PREVIOUS BUTTON
        $('#find_prev').click(function() {
            if (mooseBan == false) {
                mooseBan = true;
                current--;
                if (current < 0) {
                    current = total;
                    $('#find_us li.active').fadeOut(200, function() {
                        $(this).removeClass('active').siblings('li:last').addClass('active').fadeIn(200, function() {
                            mooseBan = false;
                        });
                    });

                } else {
                    $('#find_us li.active').fadeOut(200, function() {
                        $(this).removeClass('active').prev('li').addClass('active').fadeIn(200, function() {
                            mooseBan = false;
                        });
                    });
                }
            }
            return false;

        });

        // Create the slideshow timer
        var findusTimer = {};

        // Remove the timer on mouseover, and re-start it on mouseout. Also show and hide the hourglass
        $('#find_us').hover(function() {
            $.clearTimer(findusTimer);
        }, function() {
            findusShow();
        });


        $('#find_us').click(function() {
        
            var strHTML=(($(this).find('li')[current]).innerHTML);

            if (strHTML.indexOf("/2.gif") > -1) {

                window.open("http://www.wkc.ac.uk");

            } else if (strHTML.indexOf("/10.gif") > -1) {

                window.open("http://www.dur.ac.uk/");
                
            } 
            
        });

        $('#find_us').mouseover(function() {

            var strHTML = (($(this).find('li')[current]).innerHTML);

            if ((strHTML.indexOf("/2.gif") > -1)|(strHTML.indexOf("/10.gif") > -1)) {

                $('#find_us').css({ cursor: "hand" });

            } else {

                $('#find_us').css({ cursor: "auto" });
                
            }

        });

        // Slideshow timer function
        function findusShow() {

            // Start the slide timer function
            findusTimer = $.timer(2000, function() {

                // Clear the timer so that it doesn't keep firing
                $.clearTimer(findusTimer);

                // Click the next arrow 
                $('#find_next').click();

                // Fire this function again!
                findusShow();
            });
        }




        // Initial start
        findusShow();

    }

    // Initial start
    findUs();

    function slideshow() {

        // SETTINGS FOR SLIDESHOW
        var w = 614; // Slide width
        var s = 800; // Duration of animation
        var e = 'easeInOutExpo'; // Easing
        var t = $('#slideshow .s_content').size() - 1; // Total number of slides (reduced by 1 so that it is indexed from 0 like c)
        var c = 0; // Current slide
        var spause = 11; // Slideshow pause in seconds

        // FOR THUMBS
        var thumbw = 143; // Thumb width
        var thumbt = $('#slideshow_thumbs li').size() - 1; // Total number of thumbs
        var tpp = 6; // Number of Thumbs Per Page
        var pages = Math.ceil(thumbt / tpp) - 1; // Number of pages
        var cpage = 0; // Current page

        var mooseBan = false; // Global mouseban varible
        var emooseBan = false; // The thumb scrolling needs to be independent

        // ON LOAD
        $('#s_nav li.prev a, #slideshow_thumbs .thumbs_scroll:first').addClass('disabled').animate({ opacity: 0.4 }, 200);
        $('#slideshow_thumbs li:first a').addClass('active');

        // CLICK THE ARROWS
        $('#s_nav li a').click(function() {

            // Only continue if mouse is enabled
            if (mooseBan == true || $(this).hasClass('disabled')) return false;

            // Find the direction
            dir = $(this).parent('li').attr('class');

            // Increase or decreace current
            if (dir == 'next') {
                c++;
            } else if (dir == 'prev') {
                c--;
            }

            // Make sure that we are on the active thumbs page
            checkPage();

            // Fire the slideTo function
            slideTo(c);

            // Stop the links acting like a link
            return false;
        });

        // CLICK THE THUMBS
        $('#slideshow_thumbs li a').click(function() {

            // Only continue if mouse is enabled
            if (mooseBan == true || $(this).hasClass('active')) return false;

            // Find the index of the clicked thumb and set current
            c = $(this).parent().index();

            // Fire the slideTo function
            slideTo(c);

            // Stop the links acting like a link
            return false;
        });

        // THUMB NAV
        $('#slideshow_thumbs a.thumbs_scroll').live('click', function() {

            // Only continue if mouse is enabled
            if (emooseBan == true || $(this).hasClass('disabled')) return false;

            // Work out the direction
            dir = $(this).attr('id');

            // Increase or decreace current
            if (dir == 'thumbs_left') {
                cpage--;
            } else if (dir == 'thumbs_right') {
                cpage++;
            }

            thumbSlide(cpage);

        });

        blarg = 1;

        // SLIDE FUNCTION
        function slideTo(c, callback) {

            // BAN the moose! Bad moose!
            mooseBan = true;

            // Sort out the left and right arrows
            updateNav();

            // Work out new x based on current (we need to invert the number, so: 10 - (10 * 2) = -10
            x = (c * w) - ((c * w) * 2);

            // Do the slide
            $('#s_slider').stop([]).animate({ left: x }, s, e, function() {

                // If we have a callback function, do that now!
                if (callback) callback.call();

                // Un ban the moose! Good moose!
                mooseBan = false;

            });
        }

        function thumbSlide(cpage, callback) {

            // BAN the moose! Bad moose!
            emooseBan = true

            // Sort out the left and right arrows
            updateNav();

            // Work out new x based on current page and thumb width
            x = (cpage * (thumbw * tpp)) - ((cpage * (thumbw * tpp)) * 2);

            // Do the slide
            $('#slideshow_thumbs ul').stop([]).animate({ left: x }, s, e, function() {

                // Un ban the moose! Good moose!
                emooseBan = false;

                // If we have a callback function, do that now!
                if (callback) callback.call();
            });
        }

        function updateNav() {
            // Check that we are not on the first or last
            if (c == t) {
                $('#s_nav li.next a').addClass('disabled').animate({ opacity: 0.4 }, 200);
                $('#s_nav li.prev a').removeClass('disabled').animate({ opacity: 1 }, 200);
            } else if (c == 0) {
                $('#s_nav li.prev a').addClass('disabled').animate({ opacity: 0.4 }, 200);
                $('#s_nav li.next a').removeClass('disabled').animate({ opacity: 1 }, 200);
            } else {
                $('#s_nav li a.disabled').removeClass('disabled').animate({ opacity: 1 }, 200);
            }

            // Set active thumb
            $('#slideshow_thumbs li:eq(' + c + ') a').addClass('active').parent('li').siblings('li').children('a').removeClass('active');

            // If there is only one page
            if (pages == 0) {

                // Disable both arrows
                $('#thumbs_right').addClass('disabled').animate({ opacity: 0.4 }, 200);
                $('#thumbs_left').addClass('disabled').animate({ opacity: 0.4 }, 200);
            } else {

                // Check that we are not on the first or last page of thumbs
                if (cpage == pages) {
                    $('#thumbs_right').addClass('disabled').animate({ opacity: 0.4 }, 200);
                    $('#thumbs_left').removeClass('disabled').animate({ opacity: 1 }, 200);
                } else if (cpage == 0) {
                    $('#thumbs_left').addClass('disabled').animate({ opacity: 0.4 }, 200);
                    $('#thumbs_right').removeClass('disabled').animate({ opacity: 1 }, 200);
                } else {
                    $('.thumbs_scroll').removeClass('disabled').animate({ opacity: 1 }, 200);
                }
            }

        }

        // Fire on load
        updateNav();

        // Check which page the active thumb is on and scroll it into view
        function checkPage() {
            if (Math.ceil((c + 1) / tpp) > cpage + 1) {
                $('#thumbs_right').click();
            } else if (Math.ceil((c + 1) / tpp) < cpage + 1) {
                $('#thumbs_left').click();
            }
        }

        // Create the slideshow timer
        var slideTimer = {};

        // Remove the timer on mouseover, and re-start it on mouseout. Also show and hide the hourglass
        $('#slideshow_thumbs, #slideshow').hover(function() {
            $.clearTimer(slideTimer);
            $('#s_timer').stop().animate({ bottom: -4 }, 200, 'easeInOutExpo');
        }, function() {
            autoShow();
            $('#s_timer').stop().animate({ bottom: 14 }, 200, 'easeInOutExpo');
        });

        // Slideshow timer function
        function autoShow() {

            // Work out the delay - the set delay * 1000 (js works in milliseconds), plus the animation duration.
            p = (spause * 1000) + s;

            // Hourglass animation
            $('#s_timer span').removeAttr('style').stop([]).delay(s).animate({ width: 80 }, (spause * 1000));

            // Start the slide timer function
            slideTimer = $.timer(p, function() {

                // Clear the timer so that it doesn't keep firing
                $.clearTimer(slideTimer);

                // Click the next arrow unless we are at the end, in that case click the first thumbnail
                if (c == t) {
                    $('#slideshow_thumbs li:first a').click();
                    checkPage();
                } else {
                    $('#s_nav .next a').click();
                }

                // Fire this function again!
                autoShow();
            });
        }

        // Initial start
        autoShow();

    }

    slideshow();

    //END jQuery

});


