﻿/// <reference path="jquery.intellisense.js"/>

var site = {
    start: function() {
        imageRotator.start();
        externalLinks.start();
        payment.start();
        lightBox.start();
        navigation.start();
        supporters.start();
    }
};

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

var imageRotator = {
    start: function() {
        $.preloadImages("Images/homeimage_01.jpg", "Images/homeimage_02.jpg", "Images/homeimage_03.jpg", "Images/homeimage_04.jpg", "Images/homeimage_05.jpg", "Images/homeimage_06.jpg");
        this.index = 0;
        this.images = new Array("Images/homeimage_01.jpg", "Images/homeimage_02.jpg", "Images/homeimage_03.jpg", "Images/homeimage_04.jpg", "Images/homeimage_05.jpg", "Images/homeimage_06.jpg");
        
        this.img = $("#homeimage img");
        this.div = $("#homeimage div");
        
        setTimeout(this.nextImage, 3000);
    },
    
    nextImage: function() {
        nextIndex = imageRotator.index + 1;
        if(nextIndex >= imageRotator.images.length)
            nextIndex = 0;
        imageRotator.index = nextIndex;
        imageRotator.div.css("background", "url(" + imageRotator.images[nextIndex] + ")");
        imageRotator.img.fadeOut(900, 
            function() { 
                imageRotator.img.attr("src", imageRotator.images[nextIndex]);
                imageRotator.img.fadeIn(1, null);
                setTimeout(imageRotator.nextImage, 3000); 
            }
        );
    }
};

var externalLinks = {
    start: function() {
        $('a[rel="external"]').attr('target', '_blank');
    }
};

var lightBox = {
    inProgess: false,

    start: function() {
        var links = $('a[rel="lightbox"]');
        links.each(function(i) {
            this.link = this.href;
            this.href = 'javascript:void(0)';
        }).click(function(e) {
            lightBox.show(this);
        });
    },
    
    show: function(link) {
        // create background
        
        var bg = $($.create('div', { 'id': 'lightbox_bg' }));
        bg.css({
            'opacity': 0,
            'height': $(document).height()
        });
        $('html body').append(bg);
        bg.click(function(e) {
            lightBox.hide();
        });
        
        // create lightbox 
        var lightbox = $($.create('div', { 'id' : 'lightbox' }));
        lightbox.click(function(e) {
            lightBox.hide();
        });
        lightbox.css({
            'margin-top': $(window).scrollTop(),
            'opacity' : 0,
            'display' : 'none'
        });
        $('html body').append(lightbox);
        
        // create lightbox wrapper
        var lightboxwrapper = $($.create('div', { 'id' : 'lightboxwrapper' }));
        lightbox.append(lightboxwrapper);
        
        // work out previous / next links
        var links = $('a[rel="lightbox"]');
        var index = -1;
        links.each(function(i) {
            if(this == link) {
                index = i;
                return false;   
            }
        });
        lightBox.nextlk = index == links.length - 1 ? links[0] : links[index + 1];
        lightBox.prevlk = index == 0 ? links[links.length - 1] : links[index - 1];
        
        // create previous link
        var previous = $($.create('a', { 'id' : 'previous', 'href' : 'javascript:void(0)' }));
        previous.html('previous');
        lightboxwrapper.append(previous);
        
        // prevent lightbox closing when clicking previous link
        previous.click(function(e) {
            e.stopPropagation();
            lightBox.switchImage(lightBox.prevlk);
        });
        
        // create imgholder 
        var imgholder = $($.create('div', { 'id' : 'imgholder' }));
        lightboxwrapper.append(imgholder);
        
        // prevent lightbox closing when clicking in imgholder 
        imgholder.click(function(e) {
            e.stopPropagation();
        });
        
        // create close link
        var close = $($.create('a', { 'id' : 'close', 'href' : 'javascript:void(0)' }));
        close.html('close');
        imgholder.append(close);
        close.click(function(e) {
            lightBox.hide();
        });
        
        // create lightboxcontent 
        var lightboxcontent = $($.create('div', { 'id' : 'lightboxcontent' }));
        imgholder.append(lightboxcontent);
        
        // create next link
        var next = $($.create('a', { 'id' : 'next', 'href' : 'javascript:void(0)' }));
        next.html('next');
        lightboxwrapper.append(next);
        
        // prevent lightbox closing when clicking next link
        next.click(function(e) {
            e.stopPropagation();
            lightBox.switchImage(lightBox.nextlk);
        });
        
        lightBox.inProgess = true;
        // display lightbox
        bg.animate({ opacity: '0.75' }, 150, 'linear', function() {
            //lightbox.animate({opacity: '1'}, 150, 'linear', null);
            lightbox.css('opacity', 1);
            lightbox.css('display', 'block');
            bg.css({
                'height': $(document).height()
            });
            lightBox.loadImage(link);
        });
    },
    
    hide: function() {
        $('#lightbox_bg').remove();
        $('#lightbox').remove();
    },
    
    resize: function(height, width, callback) {
        var oldwidth = $('#imgholder').width();
        $('#imgholder').animate( { 'height' : height, 'width' : width }, { queue : false, duration : 200 }, 'linear', null);
        var top = (height + 116) / 2;
        $('#lightbox #previous, #lightbox #next').animate( { 'top' : top }, {queue: false, duration: 200 }, 'linear', null);
        var left = (524 - width) / 2;
        $('#lightbox #previous').animate( { 'marginLeft' : left }, {queue: false, duration: 200, complete: function() { if(callback) callback(); } }, 'linear', null);
    },
    
    loadImage: function(link) {
        var lightboxcontent = $('#lightboxcontent');
        
        if(link.link.indexOf('youtube') > 0) {
            var youtube = $($.create('div', { 'id' : 'youtube' }));
            lightboxcontent.append(youtube);
            lightBox.resize(452, 559, function() {
                youtube.html('<object type="application/x-shockwave-flash" style="width:559px; height:452px;" data="' + link.link + '"><param name="movie" value="' + link.link + '" /></object>');
                lightBox.inProgess = false;
            });
        } else {
            // create image
            var img = $($.create('img', { 'id' : 'lightboximage' }));
            img.css('opacity', 0);
            lightboxcontent.append(img);
            
            // load image
            img.load(function() {
                // resize
                lightBox.resize(img.height(), img.width(), function() { 
                    // fade in
                    img.animate({ 'opacity' : 1 }, 300, 'linear', function() {
                        lightBox.inProgess = false;
                    });
                });
            }).attr('src', link.link);
        }
    },
    
    switchImage: function(link) {
        // prevent multiple clicks
        if(lightBox.inProgess)
            return;
        lightBox.inProgess = true;
        
        // work out previous / next links
        var links = $('a[rel="lightbox"]');
        var index = -1;
        links.each(function(i) {
            if(this == link) {
                index = i;
                return false;   
            }
        });
        lightBox.nextlk = index == links.length - 1 ? links[0] : links[index + 1];
        lightBox.prevlk = index == 0 ? links[links.length - 1] : links[index - 1];
        
        $('#youtube').remove();
        
        var img = $('#lightboximage');
        if(img.length > 0) {
            // fade out old image
            img.animate({ 'opacity': 0 }, 300, 'linear', function() {
                $('#lightboximage').remove();
                lightBox.loadImage(link);
            });
        } else {
            lightBox.loadImage(link);
        }
    }
};

var payment = {
    count: 1,
    
    start: function() {
        var button = $('#paymentproceed');
        if(button.length != 1)
            return;
        button.click(function(e) {
            var drops = new Array('adult4', 'adult5', 'adult6', 'adult7', 'child7', 'senior7', 'family7', 'gallery1');
            
            for(var i=0; i<drops.length; i++) {
                var id = drops[i];
                var drop = $('#' + id);
                if(drop.val() > 0) {
                    switch(id) {
                        case 'adult4':
                            payment.insertItem('4th June Adult Ticket', '23.50', drop.val());
                            break;
                        case 'adult5':
                            payment.insertItem('5th June Adult Ticket', '23.50', drop.val());
                            break;
                        case 'adult6':
                            payment.insertItem('6th June Adult Ticket', '23.50', drop.val());
                            break;
                        case 'adult7':
                            payment.insertItem('7th June Adult Ticket', '23.50', drop.val());
                            break;
                        case 'child7':
                            payment.insertItem('7th June Child Ticket', '10.00', drop.val());
                            break;
                         case 'senior7':
                            payment.insertItem('7th June Senior Citizen Ticket', '10.00', drop.val());
                            break;
                         case 'family7':
                            payment.insertItem('7th June Family Ticket', '40.00', drop.val());
                            break;
                         case 'gallery1':
                            payment.insertItem('5th June Gallery Day Reservation', '5.00', drop.val());
                            break;
                    }
                    
                }
            }
            if(payment.count == 1)
                alert('please select your tickets before proceeding');
            else
                document.getElementById('submitbutton').click();
        });
    },
    
    insertItem: function(title, amount, quantity) {
        var div = $('#paypalfields');
        var name = $($.create('input', { 'type' : 'hidden', 'name' : 'item_name_' + payment.count, 'value' : title}));
        div.append(name);
        var amount = $($.create('input', { 'type' : 'hidden', 'name' : 'amount_' + payment.count, 'value' : amount}));
        div.append(amount);
        var quantity = $($.create('input', { 'type' : 'hidden', 'name' : 'quantity_' + payment.count, 'value' : quantity}));
        div.append(quantity);
        payment.count++;
    }
};

var navigation = {
    display: null,
    marginLeft: null,
    timer: null,
    
    start: function() {
        var nav = $('#navigation');
        if(nav.length > 0) {
            $('#navigation a').mouseover(function() {
                if(navigation.timer)
                    clearTimeout(navigation.timer);
                    
                var pointer = $('#navpointer');
                if(navigation.display == null) {
                    navigation.display = pointer.css('display');
                    navigation.marginLeft = pointer.css('marginLeft');
                }
                
                pointer.css('display', 'block');
                var left = $(this).offset().left - $($('#navigation a')[0]).offset().left + 6;
                
                //if(navigation.display != 'none') 
                //    pointer.animate({'marginLeft': left }, 100, 'swing', null);
                //else 
                    pointer.css('marginLeft', left);
                
            }).mouseout(function() {
                navigation.timer = setTimeout(navigation.reset, 20);
            });
        }
    },
    
    reset: function() {
        var pointer = $('#navpointer');
        pointer.css('display', navigation.display);
        if(navigation.display != 'none')
            pointer.css('marginLeft', navigation.marginLeft); //pointer.animate({'marginLeft' : navigation.marginLeft}, 100, 'swing', null);
    }
};

var supporters = {
    start: function() {
        var el = $('.supporters');
        if(el.length == 0)
            return;
        
        var lis = $('.supporters li');
        lis.each(function(i) {
            if(i > 0)
                $(this).css('display', 'none');
        });
        
        setTimeout(supporters.showNext, 3000);
    },
    
    showNext: function() {
        var lis = $('.supporters li');
        var visible = lis.filter(function(i) {
            if($(this).css('display') != 'none')
                return true;
        });
        
        var index = lis.index(visible) + 1;
        if(index >= lis.length)
            index = 0;

        var next = $(lis[index]);
        // fade out previous
        visible.css('display', 'none');
        next.css('display', 'block');
        setTimeout(supporters.showNext, 3000);
        
        //alert(index);
    }
};

$(document).ready(function () {
    site.start();
});
