var $tabs = null;
var shakespearesApp = function () {

    var footerOriginal = 0;
    var hpSlideShow;
    var officeArr = new Array();

    // [ToDo] Find a way to generate Javascript location list dynamically.

    officeArr.push(["Birmingham", "52.480179", "-1.898971", "/contact/detail/2"]);
    officeArr.push(["Nottingham", "52.951294", "-1.153022", "/contact/detail/7"]);
    officeArr.push(["Stratford upon Avon", "52.193978", "-1.700402", "/contact/detail/5"]);
    officeArr.push(["Shipston on Stour", "52.063886", "-1.624034", "/contact/detail/11"]);
    officeArr.push(["Moreton in Marsh", "51.990694", "-1.702781", "/contact/detail/9"]);
    officeArr.push(["Leicester", "52.628500", "-1.124400", "/contact/detail/15"]);
    officeArr.push(["Solihull", "52.4138", "-1.7850", "/contact/detail/16"]);

    return {
        init: function () {

            shakespearesApp.cufon();
            shakespearesApp.navHighlight();
            shakespearesApp.loginPanel();
            shakespearesApp.globalSearch();

            if ($.browser.msie) {
                $("#nav a:last-child").css("margin-right", "0px");
            };

            switch ($("body").attr('id')) {
                case "home":
                    shakespearesApp.startSlideshow();
                    //shakespearesApp.footerNews();
                    break;
                case "contact":
                    shakespearesApp.contactMap();
                    //shakespearesApp.footerNews();
                    break;
                case "contact-detailed":
                    shakespearesApp.contactDetailMap();
                    //shakespearesApp.footerNews();
                    break;
                case "services":
                    shakespearesApp.createTabs_services();
                    shakespearesApp.slideshow();
                    //shakespearesApp.serviceList();
                    //shakespearesApp.footerNews();
                    break;
                case "sectors":
                    shakespearesApp.createTabs_sectors();
                    shakespearesApp.slideshow();
                    //shakespearesApp.footerNews();
                    break;
                case "people":
                    shakespearesApp.people();
                    //shakespearesApp.footerNews();
                default:
                    break;
            }

            shakespearesApp.footerNews();
        },
        globalSearch: function () {
            //            $('#search').click(function () {
            //                if ($('#search-panel').height() == 24) {
            //                    toggle = 80
            //                    $('#search-options').stop().fadeIn('fast');
            //                } else {
            //                    toggle = 24
            //                    $('#search-options').stop().fadeOut('fast');
            //                }
            //                $('#search-panel').stop().animate({
            //                    height: toggle
            //                }, 400, function () { });
            //            });

            $('#search-all').css('cursor', 'pointer');
            $('#search-people').css('cursor', 'pointer');

            $('#search-all').click(function () {
                $('#search-type').val('all');
                $('#search-all').addClass("active");
                $('#search-people').removeClass("active");
            });
            $('#search-people').click(function () {
                $('#search-type').val('people');
                $('#search-people').addClass("active");
                $('#search-all').removeClass("active");
            });
        },
        people: function () {
            $('a.jt').cluetip({
                width: 240,
                cluezIndex: 50000,
                positionBy: 'relative',
                topOffset: -99,
                leftOffset: -78,
                local: true,
                showTitle: false,
                dropShadow: false,
                fx: {
                    open: 'show'
                },
                mouseOutClose: true,
                sticky: true,
                closeText: '',
                tracking: false
            });

            //$('.live').show();

            $('#page-content #search-content-az li').find("a").click(function () {
                $('#page-content #search-content-az li a').removeClass('selected');
                $(this).addClass('selected');
                Cufon.refresh();
                if (this.attributes.title.nodeValue.toLowerCase() == "all") {
                    $('.letters').addClass('active');

                } else {
                    $('.letters').removeClass('active');
                    $('.letters.letter-' + this.attributes.title.nodeValue.toLowerCase()).addClass('active');
                }
                $(document).trigger('hideCluetip');
            });
        },
        slideshow: function () {
            $('#profile-previews').cycle({
                fx: 'fade',
                speed: 'slow',
                timeout: 5000,
                next: '.arrow-preview-right',
                prev: '.arrow-preview-left',
                before: slideshowText
            });

            $('#profile-previews').show();

            var totalCaptions = 0;
            var current = 1;

            $('.preview-info').find('.profile').each(function (i) {
                totalCaptions++;
            });

            $('.profile:first').addClass('active');

            function slideshowText(obj) {

                if (obj) {
                    if (current < totalCaptions) {
                        current++;
                        $('.preview-info').find('.profile.active').next().addClass('active').prev().removeClass('active');
                    } else {
                        $('.profile').removeClass('active');
                        $('.profile:first').addClass('active');
                        current = 1;
                    }
                } else {
                    if (current == 1) {
                        $('.profile').removeClass('active');
                        $('.profile:last').addClass('active');
                        current = totalCaptions;
                    } else {
                        current--;
                        $('.preview-info').find('.profile.active').prev().addClass('active').next().removeClass('active');
                    }
                }
            }
        },
        serviceList: function () {
            $('.services-list:first').css('display', 'block');
            $('ul.a-z li').find("a").hover(function () {
                $('ul.a-z a').removeClass('selected');
                $(this).addClass('selected');
                Cufon.refresh();
                $('.services-list').css('display', 'none');
                $('.services-list.letter-' + this.attributes.title.nodeValue.toLowerCase()).css('display', 'block');
            });
        },
        cufon: function () {
            Cufon.replace('.login-btn', { fontFamily: 'FoundryFormSerif' });
            Cufon.replace('#centeredmenu ul li a.top', { hover: true, fontFamily: 'FoundryFormSerif' });
            Cufon.replace('.slideshow-content h2', { fontFamily: 'FoundryFormSerif-ExtraBold' });
            Cufon.replace('#sub-panel h3', { fontFamily: 'FoundryFormSerif' });
            Cufon.replace('#sub-panel h4', { fontFamily: 'FoundryFormSerif' });
            Cufon.replace('#contact-info h2', { fontFamily: 'FoundryFormSerif' });
            Cufon.replace('.ui-state-active a, .ui-tabs .ui-tabs-nav li a', { hover: true, fontFamily: 'FoundryFormSerif-ExtraBold' });
            Cufon.replace('ul.a-z a, ul.a-z', { hover: true, fontFamily: 'FoundryFormSerif' });
            Cufon.replace('#page-content h2', { hover: true, fontFamily: 'FoundryFormSerif' });
            Cufon.replace('#right-col h3', { hover: true, fontFamily: 'FoundryFormSerif' });
            Cufon.replace('#news-detail-sidebar h3', { hover: true, fontFamily: 'FoundryFormSerif' });
            Cufon.replace('.news-detail-header p', { hover: true, fontFamily: 'FoundryFormSerif' });
            Cufon.replace('.news-detail-subheader h2.fr', { hover: true, fontFamily: 'FoundryFormSerif' });
        },
        loginPanel: function () {
            // buttons
            //            $('.login-btn').css('cursor', 'pointer');
            //            $('#login-form-panel .btn-close').css('cursor', 'pointer');
            //            $('#login-form-panel .btn-close').click(function () {
            //                $('.login-btn').click();
            //            });

            //            $('.login-btn').toggle(function () {
            //                if ($.browser.msie) {
            //                    $("#login-form-panel").css('display', 'block');
            //                }
            //                toggleSlideShow();
            //                $('.login-btn').addClass('active');
            //                $("#user-login-panel").slideToggle(1000, function () {
            //                    $("#login-form-panel").stop();
            //                    if (!$.browser.msie) {
            //                        $("#login-form-panel").fadeIn("slow");
            //                    }
            //                    $('#login-username').focus();
            //                    $('#login-username').val('');
            //                    $('#login-password').val('');
            //                    $('#login-arrow').attr("src", "/assets/images/arrow-up-login.png");
            //                });
            //            }, function () {
            //                closeLogin();
            //            });

            $('#news-bar').tabSlideOut({
                tabHandle: '#news-title-bar p a',
                tabLocation: 'bottom',
                speed: 300,
                action: 'click',
                fixedPosition: false
            });

            function closeLogin() {
                $('.login-btn').removeClass('active');
                $("#login-form-panel").stop();
                $("#login-form-panel").fadeOut("fast", function () {
                    $("#user-login-panel").slideToggle(500, function () {
                        toggleSlideShow();
                        $('#login-arrow').attr("src", "/assets/images/arrow-down-login.png");
                    });
                });
            }

            function toggleSlideShow() {
                if ($('body').attr('id') == "home") {
                    $('.slideshow').cycle('toggle');
                }
            }
        },
        navHighlight: function () {
            if (topNav != "Home") {
                $("#nav" + topNav + " a").addClass("selected");
                if (subNav.length != 0) {
                    $("#subNav" + subNav + " a").addClass("selected");
                }
            }
            $('#centeredmenu ul li.sub').mouseenter(function () {
                $(this).addClass('hover');
            }).mouseleave(function () {
                $(this).removeClass('hover');
            });
        },
        startSlideshow: function () {
            hpSlideShow = $('.slideshow').cycle({
                fx: 'scrollHorz',
                timeout: 16000,
                pause: 1,
                next: '#slideshow-container .arrow-right',
                prev: '#slideshow-container .arrow-left'
            });

            $('#slideshow-container .arrow-left').css('cursor', 'pointer');
            $('#slideshow-container .arrow-right').css('cursor', 'pointer');
        },
        createTabs_services: function () {
            $tabs = $("#tabs").tabs({
                event: "mouseover"
            });

            $(".ui-tabs .ui-tabs-nav li a").mouseout(function () { Cufon.refresh() });

            $("#tabs").show();
        },

        createTabs_sectors: function () {
            //            $tabs = $("#tabs").tabs();
            // USE SELECTED TAB


            // $(".ui-tabs .ui-tabs-nav li a").mouseout(function () { Cufon.refresh() });
        },
        contactMap: function () {

            var latlng = new google.maps.LatLng(officeArr[0][1], officeArr[0][2]);

            // Creating an object literal containing the properties we want to pass to the map  
            var options = {
                zoom: 7,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            // Calling the constructor, thereby initializing the map  
            var map = new google.maps.Map(document.getElementById('google-map'), options);

            for (var x = 0; x < officeArr.length; x++) {
                marker = new google.maps.Marker({
                    position: new google.maps.LatLng(officeArr[x][1], officeArr[x][2]),
                    map: map,
                    title: officeArr[x][0],
                    clickable: true,
                    icon: '/assets/images/google-maps/marker.png',
                    linkURL: officeArr[x][3]
                });

                google.maps.event.addListener(marker, 'click', function () {
                    document.location.href = this.linkURL;
                });
            }
        },
        contactDetailMap: function () {

            var location_id = $('#location_order').val();

            var latlng = new google.maps.LatLng(officeArr[location_id][1], officeArr[location_id][2]);

            // Creating an object literal containing the properties we want to pass to the map  
            var options = {
                zoom: 15,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            // Calling the constructor, thereby initializing the map  
            var map = new google.maps.Map(document.getElementById('google-map'), options);

            var markerBirmingham = new google.maps.Marker({
                position: new google.maps.LatLng(officeArr[location_id][1], officeArr[location_id][2]),
                map: map,
                title: officeArr[location_id][0],
                clickable: true,
                icon: '/assets/images/google-maps/marker.png'
            });

            var boxText = document.createElement("div");
            boxText.innerHTML = $('.googlemaps-directions-panel').html();

            var myOptions = {
                content: boxText
				, disableAutoPan: false
				, maxWidth: 0
				, pixelOffset: new google.maps.Size(14, -144)
				, zIndex: null
				, boxStyle: {
				    background: "url('tipbox.gif') no-repeat"
				, opacity: 1
				, width: "280px"
				}
				, closeBoxMargin: ""
				, closeBoxURL: ""
				, infoBoxClearance: new google.maps.Size(1, 1)
				, isHidden: false
				, pane: "floatPane"
				, enableEventPropagation: false
            };

            google.maps.event.addListener(markerBirmingham, "click", function (e) {
                ib.open(map, this);
            });

            var ib = new InfoBox(myOptions);
            ib.open(map, markerBirmingham);
        },
        footerNews: function () {

            $('.news-slides').cycle({
                fx: 'scrollHorz',
                timeout: 100000,
                pause: 1,
                next: '#news-nav .news-arrow-right',
                prev: '#news-nav .news-arrow-left'
            });
            $('#news-nav .news-arrow-right').css('cursor', 'pointer');
            $('#news-nav .news-arrow-left').css('cursor', 'pointer');

            $('.latest-news-slider')
				.cycle({
				    fx: 'scrollLeft',
				    speed: 'fast',
				    timeout: 0,
				    pager: '#nav',
				    next: '.arrow.right',
				    prev: '.arrow.left',
				    width: 960
				});

        }
    }
} ();
$(document).ready(shakespearesApp.init);

(function($){
    $.fn.tabSlideOut = function(callerSettings) {
        var settings = $.extend({
            tabHandle: '.handle',
            speed: 300, 
            action: 'click',
            tabLocation: 'left',
            topPos: '200px',
            fixedPosition: false,
            positioning: 'absolute',
            pathToTabImage: null,
            imageHeight: null,
            imageWidth: null,
            onLoadSlideOut: false                       
        }, callerSettings||{});


        settings.tabHandle = $(settings.tabHandle);
        var obj = this;
        if (settings.fixedPosition === true) {
            settings.positioning = 'fixed';
        } else {
            settings.positioning = 'absolute';
        }
        
        //ie6 doesn't do well with the fixed option
        if (document.all && !window.opera && !window.XMLHttpRequest) {
            settings.positioning = 'absolute';
        }
        
        obj.css({
            'line-height' : '1',
            'position' : settings.positioning
        });
		
        var properties = {
        	containerWidth: parseInt(obj.outerWidth(), 10),
       		containerHeight: parseInt(obj.outerHeight(), 10)
        };

        if(settings.tabLocation === 'bottom') {
            obj.css({'bottom' : '-' + (properties.containerHeight-112)+'px', 'position' : 'fixed'});
            
        }
        
        settings.tabHandle.click(function(event){
            event.preventDefault();
            
        
        });
        
        var slideIn = function() {
            obj.animate({bottom: '-' + (properties.containerHeight-112)+'px'}, settings.speed).removeClass('open');
           $('#popout_text').text('');
        };
        
        var slideOut = function() {
            obj.animate({bottom:'10px'},  settings.speed).addClass('open');
           $('#popout_text').text(' - X');
        };

        var clickScreenToClose = function() {
            obj.click(function(event){
                event.stopPropagation();
            });
            
            $(document).click(function(){
                slideIn();
            });
        };
        
        var clickAction = function(){
            settings.tabHandle.click(function(event){
                if (obj.hasClass('open')) {
                    slideIn();
                } else {
                    slideOut();
                }
            });
            
            clickScreenToClose();
        };
        
        
        var slideOutOnLoad = function(){
            slideIn();
            setTimeout(slideOut, 500);
        };
        
        //choose which type of action to bind
        if (settings.action === 'click') {
            clickAction();
        }
        if (settings.onLoadSlideOut) {
            slideOutOnLoad();
        };
        
    };
})(jQuery);

