﻿/// <reference path="jquery.intellisense.js"/>

var site = {
    start: function() {
        //siteMenu.start();
        externalLinks.start();
        //imageSwitcher.start();
        tv.start();

        // start image rotator
        var bodyId = document.body.id;
        switch (bodyId) {
            case 'peoplepage':
                imageRotator.start(
                    new Array(
                        'images/people01.jpg',
                        'images/people02.jpg',
                        'images/people03.jpg',
                        'images/people04.jpg',
                        'images/people05.jpg',
                        'images/people06.jpg',
                        'images/people07.jpg',
                        'images/people08.jpg'
                    ), 4000
                );
                break;
            case 'facilitiespage':
                imageRotator.start(
                    new Array(
                        'images/facilities01.jpg',
                        'images/facilities02.jpg',
                        'images/facilities03.jpg',
                        'images/facilities04.jpg',
                        'images/facilities05.jpg',
                        'images/facilities06.jpg',
                        'images/facilities07.jpg',
                        'images/facilities08.jpg',
                        'images/facilities09.jpg'
                    ), 4000
                );
                break;
        }
    }
};

var tv = {
    start: function() {
        var div = $('#mainimageholder #flash');
        if (div.length > 0) {
            var params = {
                menu: "false",
                wmode: "transparent"
            };
            swfobject.embedSWF("/images/tv_clitheroe.swf", "flash", "633", "356", "9.0.0", "expressInstall.swf", null, params, null, null);
        }

        var div2 = $('#mainimageholder #flash2');
        if (div2.length > 0) {
            var params = {
                menu: "false",
                wmode: "transparent"
            };
            swfobject.embedSWF("/images/aypeTV_clitheroe480p.swf", "flash2", "853", "480", "9.0.0", "expressInstall.swf", null, params, null, null);
        }
    }
};

var externalLinks = {
    start: function() {
        if (!document.getElementsByTagName) return;
        var anchors = document.getElementsByTagName("a");
        for (var i=0; i<anchors.length; i++) {
            var anchor = anchors[i];
            if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
                anchor.target = "_blank";
        }
    }
};

var siteMenu = {
    timer: null,
    scroll: null,
    height: null,
    
    start: function() {
        // initial setup
        var isOpen = false;
        
        $('#nav').get(0).scrollTop = 0;
        $('#nav').addClass('closed');
        var li = $.grep($('#navmain li'), 
            function(el) {
                return($(el).css('display') != 'none');
            }
        );
        
        var currentPage = $(li).children('a');
        currentPage.attr('href', 'javascript:void(0)');
        
        currentPage.mouseover(function() {
            if(!isOpen)
                $('#navmain').addClass('over');
        });
        currentPage.mouseout(function() {
            $('#navmain').removeClass('over');
        });
        
        // handle show / hide
        $('#navmain').click(function(e) {
            e.stopPropagation();
        
            // pause image rotator if menu open
            imageRotator.paused = true;
            
            isOpen = true;
            $('#navmain').removeClass('over');
            $('#nav').addClass('open');
            $('#nav').removeClass('closed');
            
            // show menu items
            //$('#navmain li').css('display', 'block');
            
            // set menu height
            siteMenu.height = $.windowsize().height - 30;
            if($('#nav').height() > siteMenu.height)
                $('#nav').css('height', siteMenu.height + 'px');
            else
                siteMenu.height = $('#nav').height();
                
            // hide overflow and scroll to top    
            $('#nav').get(0).scrollTop = 0;
            $('#nav').css('overflow', 'hidden');
            
        });
        $('body').click(function(e) { 
            //e.stopPropagation();
            $('#nav').removeClass('open');
            $('#nav').addClass('closed');
            //$('#navmain li').css('display', 'none');
            //$(li).css('display', 'block');
            $('#nav').css('height', 'auto');
            $('#nav').get(0).scrollTop = 0;
            siteMenu.scroll = null;
            isOpen = false;
            
            // resume image rotator if paused
            imageRotator.paused = false;
        });
        
        // handle scrolling
        $('#nav').mousemove(function(e) {
            if(!isOpen)
                return;
            
            var y = e.clientY - $('#nav').position().top;
            if(y > siteMenu.height - 40) {
                siteMenu.scroll = 'down';
            } else if(y < 40) {
                siteMenu.scroll = 'up';
            }
            else siteMenu.scroll = null;
            
            if(siteMenu.scroll != null && siteMenu.timer == null) {
                siteMenu.timer = setTimeout(siteMenu.handleScroll, 100);
            }
        });
    },
    
    handleScroll: function() {
        // scroll down
        if(siteMenu.scroll == 'down') {
            $('#nav').get(0).scrollTop += 20;
            siteMenu.timer = setTimeout(siteMenu.handleScroll, 100);
        } 
        // scroll up
        else if(siteMenu.scroll == 'up') {
            $('#nav').get(0).scrollTop -= 20;
            if($('#nav').get(0).scrollTop > 0)
                siteMenu.timer = setTimeout(siteMenu.handleScroll, 100);
            else
                siteMenu.timer = null;
        } 
        // stop scrolling
        else {
            siteMenu.timer = null;
        }
    }
};

var imageSwitcher = {
    start: function() {
        var img = $('#mainimage');
        if (img == null)
            return;

        // hook up events
        var links = $('#imgnav a');
        links.each(function(i, link) {
            link.image = link.href;
            link.type = link.image.substring(link.image.length - 4, link.image.length);
            link.href = 'javascript:void(0)';

            // kick off flash movie if first item is flash
            if (i == 0 && link.type == '.swf') {
                // add div for flashmovie to render in
                var flashmovie = document.createElement('div');
                flashmovie.id = "flashmovie";
                $('#mainimageholder').append(flashmovie).css('height', 422);  //.css('backgroundColor', 'white').css('backgroundImage', 'url(/images/ajax-white.gif)');
                var params = {
                    menu: "false",
                    wmode: "transparent"
                };
                swfobject.embedSWF(link.image, "flashmovie", "750", "422", "9.0.0", "expressInstall.swf", null, params, null,
                    function() {
                        $('#mainimageholder').css('backgroundColor', 'white').css('backgroundImage', 'none')
                    });
            }

            $(link).click(function(e) {
                // set link state
                var links = $('#imgnav a');
                links.parents('li').removeClass('selected');
                $(this).parent('li').addClass('selected');

                // fix height before image / flash is removed
                $('#mainimageholder').height($('#mainimageholder').height()).css('backgroundColor', 'black').css('backgroundImage', 'url(/images/ajax.gif)');

                // hide flash
                $('#flashmovie').remove();

                // fade out image
                $('#mainimage').fadeOut(300, function() {
                    if (link.type == '.swf') {
                        // add div for flashmovie to render in
                        var flashmovie = document.createElement('div');
                        flashmovie.id = "flashmovie";
                        $('#mainimageholder').append(flashmovie).animate({ height: 422 }, 300, 'swing', function() {
                            var params = {
                                menu: "false",
                                wmode: "transparent"
                            };
                            swfobject.embedSWF(link.image, "flashmovie", "750", "422", "9.0.0", "expressInstall.swf", null, params, null,
                                function() {
                                    $('#mainimageholder').css('backgroundColor', 'white').css('backgroundImage', 'none')
                                });
                        });
                    } else {
                        // load in new image                 
                        $(new Image()).load(function() {
                            // set new image
                            $('#mainimage').attr('src', link.image);

                            // resize height of container
                            var holderheight = $('#mainimageholder').height();
                            var imageheight = $('#mainimage').height();
                            if (holderheight != imageheight) {
                                $('#mainimageholder').animate({ height: imageheight }, 300, 'swing', function() {
                                    // fade in image
                                    $('#mainimage').fadeIn(300);
                                });
                            } else {
                                $('#mainimage').fadeIn(300);
                            }
                        }).attr('src', link.image);
                    }
                });
            });
        });
    }
};

var imageRotator = {
    images: new Array(),
    delay: 3000,
    timer: null,
    paused: false,
    
    start: function(images, delay) {
        this.index = 0;
        this.images = images;
        if(delay != null)
            this.delay = delay;
        
        this.img = $("#mainimage");
        this.div = $("#mainimageholder");
        
        imageRotator.timer = setTimeout(this.nextImage, this.delay);
    },
    
    nextImage: function() {
        if(imageRotator.paused) {
            imageRotator.timer = setTimeout(imageRotator.nextImage, imageRotator.delay); 
            return;
        }
        
        nextIndex = imageRotator.index + 1;
        if(nextIndex >= imageRotator.images.length)
            nextIndex = 0;
        imageRotator.index = nextIndex;
        imageRotator.div.css("background", "url(" + imageRotator.images[nextIndex] + ")");
        $(new Image()).load(function() {
            // fade out image
            imageRotator.img.animate({opacity:0.01}, 900, 'swing', function() {
                // fade in image
                imageRotator.img.attr("src", imageRotator.images[nextIndex]);
                imageRotator.img.css('opacity', 1);
                imageRotator.timer = setTimeout(imageRotator.nextImage, imageRotator.delay); 
            });
        }).attr('src', imageRotator.images[nextIndex]);
    }
};

$(document).ready(function () {
    site.start();
});


jQuery.windowsize = function()
{
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}

jQuery.preloadImages = function()
{
    for(var i = 0; i<arguments.length; i++)
    {
        jQuery("<img>").attr("src", arguments[i]);
    }
}
