﻿/**
 * Shows promos as a carousel over the top of the main page
 * Loads content from stand alone pages via ajax request
 * @author ted littledale
 * @requires jQuery 1.3.*
 * @requires jQuery.fn.supersleight
 */

(function(){ 
    
    /**
     * @constructor
     */
    
    var HomePromo = window.HomePromo = function(){
        var overlayHTML,
	        overlay,
	        overlayContent,
		    currentItem = 1,
		    totalItems,
		    that = this;
			/**
			 * Creates the shell for the promo and adds it to the dome
			 */
			
            var ititHomePromo = function(){
                overlayHTML = '<div id="homePromoOverlay">' +
                                    '<div id="homePromoInner">'+
		                                '<div class="homePromoOverlayBody">'+
		                                    '<div id="homePromoArea">'+
		                                        '<p class="homePromoClose"><a href="#">' + translations.lightbox.close + '</a></p>'+
		                                        '<div id="homePromoContentMask">'+
    		                                        
		                                        '</div>'+
		                                    '</div>'+
		                                '</div>' +
		                            '</div>' +
			                  '</div>';
                jQuery('body>form').append(overlayHTML);
                that.overlay = jQuery("#homePromoOverlay").css('z-index' , 10);
                that.overlay.css('opacity', 0);
                that.overlay.find('.homePromoClose').bind('click', close);
            };
			/**
			 * Add the content to the promo once it has been returned by the server
			 * @param {String|Object} data whatever type is returned from server
			 * @param {String} textStatus Status returned by server
			 * @returns nothing
			 */
			
            this.contentLoaded = function(data, textStatus){
	            that.overlay.find('#homePromoContentMask').empty();
	            that.overlay.find('#homePromoContentMask').append(jQuery(data).find('#homePromoContentMask .homePromoItem'));

	            that.overlay.find('#homePromoContentMask').prepend(jQuery(data).find('#homePromoContentMask #homePromoNav'));
                that.overlay.find('ul a').bind('click', changeItem);
                //fix ie6 bug - content showing through under overlay
                that.overlay.css('opacity', 1).find('#homePromoNav, #homePromoContentMask, .slant *, .homePromoItem').css('opacity', 1);
                that.overlay.find('.slant').supersleight();
                that.overlay.find('.slant').supersleight({shim: '/assets/images/trans.gif'});
	            
		        totalItems = that.overlay.find('#homePromoNav  li').size() - 2;//take away 2 for the left and right nav
		        if(totalItems == 1){
		            totalItems = that.overlay.find('#homePromoNav').hide();
		        }
		        that.overlay.find('#homePromoNav li:eq(1)').addClass('current');//li:eq(0) is the left nav button
	            return;
	        };
			/**
			 * Add the next promo content after it has been returned by server and fades the opacity back to 1
			 * @param {String|Object} data whatever type is returned from server
			 * @param {String} textStatus Status returned by server
			 * @returns nothing
			 */
	        this.nextContentLoaded = function(data, text){
	            that.overlay.find('#homePromoContentMask .homePromoItem').remove();
	            that.overlay.find('#homePromoContentMask').append(jQuery(data).find('#homePromoContentMask .homePromoItem').css('opacity', 0));
                that.overlay.css('opacity', 1).find('#homePromoNav, #homePromoContentMask, .slant *').css('opacity', 1);
	            that.overlay.find('.slant').supersleight();
                that.overlay.find('.slant').supersleight({shim: '/assets/images/trans.gif'});
	            that.overlay.find('.homePromoItem').animate({
                            'opacity' : 1
                        }, 500);  
	        };
			/**
			 * Fades the content out and then make the ajax request to get the next content
			 * @param {Object} e Mouse event
			 * @returns nothing
			 */
			
            var changeItem = function(e){
                var link = $(this),
                    parentLi = link.parent(),
                    nextItem,
                    url;
                
                if(parentLi.hasClass('hpPrev')){
                    nextItem = currentItem - 1;
                }
                else if(parentLi.hasClass('hpNext')){
                    nextItem = currentItem + 1;
                }
                else{
                    nextItem = parentLi.parents().children().index(parentLi);
                }
                if(nextItem == currentItem){
                    return false;
                }
                else{
                    parentLi.parents().find('li:eq('+currentItem+')').removeClass('current');
                    parentLi.parents().find('li:eq('+nextItem+')').addClass('current');
                    currentItem = nextItem;
                }
                url = that.overlay.find('li:eq('+nextItem+') a')[0].href;
                that.overlay.find('.homePromoItem').animate({
                    'opacity' : 0
                },
                {
                    'duration' : 500,
                    'complete' : function(){
                        jQuery.get(url, that.nextContentLoaded);
                        
                    }
                }
                );
                
		        return false;
	        };
			/**
			 * Fades and hides the promo
			 * @returns nothing
			 */
			
            var close = function(){
                that.overlay.animate({
                        opacity: 0
                    }, 
                    { 
                        duration: 1000,
                        complete: function(){   
                            that.overlay.hide();
                        }
                    }
                );
                return false;
            };
        ititHomePromo();
        return;
    };
    
    
    HomePromo.prototype = {
		/**
		 * Shows and fades in the promo and makes an ajax request to get the content
		 * @param {String} url location of the promo content
		 */
		
        show : function(url){
            var that = this;
            jQuery.get(url, that.contentLoaded);
            that.overlay.show();
            that.overlay.animate({
                    opacity: 1
                }, 
                { 
                    duration: 1000
                }
            );
            return false;
        }
    };
    
    // ... 
})();

/**
 * A carousel with a scrolling thumb navigation that gets content dynamically from the server via ajax requests
 * @author ted littledale
 * @requires jQuery 1.3.
 * @requires jQuery.fn.supersleight (for ie6 png fix)
 */

(function(){ 
    
    /**
     * @constructor
	 * @param {Object} opts options for carousel (see options below)
	 * @option {String} container jQuery selector for the element the carousel will be inserted into
	 * @option {Boolean} sideControlls should the carousel have navigation side panels (the ones that appear when you mouse over them)
	 * @option {String} carId unique id (with be on the outermost div)
	 * @option {Array} dimensions width and height of the carousel
	 * @option {Array} thumbDimensions width and height of the thumbs in the navigation scroller
	 * @option {String} stuffToHide jQuery selector for content that needs to be hidden when carousel opens
     */

    var PdtCarousel = window.PdtCarousel = function(opts){
        var carouselHTML,
	        carousel,
	        carouselSideNav,
	        carouselScrollNavLeft,
	        carouselScrollNavRight,
		    carItem,
		    currentItem = 0,
		    scrollPos = 0,
		    totalItems,
		    scrolling = false,
		    switching = false,
		    disableLeft = false,
		    disableRight = false,
		    disableScrollLeft = false,
		    disableScrollRight = false,
		    visibleThumbs,
		    isEnglish,
		    THAT = this;
		    

        this.opts = opts;
        this.isOpen = false;
        this.carCache = {};
        /**
         * Insert the shell of the carousel into the dom
         */
        
        var ititPdtCarousel = function(){
            carouselSideNav = '<div class="carPanelNavLeft"><a href="#">&lt;</a></div>'+
			                  '<div class="carPanelNavRight"><a href="#">&gt;</a></div>';
		    carouselScrollNavLeft = '<div class="carNavPrev"><a href="#">&lt;</a></div>';
		    carouselScrollNavRight = '<div class="carNavNext"><a href="#">&gt;</a></div>';
            carouselHTML =  '<div class="pdtCarousel" id="'+THAT.opts.carId+'">'+
		                        '<div class="inner">'+
		                        '</div>'+
	                        '</div>';

            THAT.carousel = jQuery(carouselHTML).width(THAT.opts.dimensions[0]).height(THAT.opts.dimensions[1]).addClass('carJs').hide();
            jQuery(THAT.opts.container).append(THAT.carousel);

            THAT.carousel.css('opacity', 0);
            var locString = window.location+'';
    		isEnglish = (locString.indexOf('/en/') > -1);
		    return;
	    };
		/**
		 * Add the content to the promo once it has been returned by the server
		 * @param {String|Object} data      whatever type is returned from server
		 * @param {String} textStatus       Status returned by server
		 * @param {Number} startNo      The item that the carousel opens on
		 * @param {Boolean} page2       Should the item show page2 first (for inspirations when loading from email link to sign up)
		 * @param {String} url      the url that has just loaded
		 * @returns nothing
		 */
	    this.contentLoaded = function(data, text, startNo, page2, url){
	        THAT.carCache[url] = data;
	        THAT.carousel.removeClass('firstLoading');
	        currentItem = (startNo) ? startNo : 0;
		    scrollPos = 0;
	        THAT.carousel.find('.inner').empty();
	        if(THAT.opts.sideControlls){
	            THAT.carousel.find('.inner').prepend(carouselSideNav);
	        }   
	        THAT.carousel.find('.inner').append(jQuery(data).find('.pdtCarousel .inner').html());
	        carItem = THAT.carousel.find('.carItem');
	        THAT.carousel.find('.carNav').prepend(carouselScrollNavLeft).append(carouselScrollNavRight);
	        THAT.carousel.find('.carNav ul').css('left', '30px');
	        //need to trigger backgroud
	        
	        THAT.carousel.css('background-repeat', 'no-repeat');
		    totalItems = THAT.carousel.find('.carNav ul > li').size();
		    visibleThumbs = Math.floor(parseInt(THAT.carousel.find('.carNav').css('width'), 10) / THAT.opts.thumbDimensions[0]);
		    visibleThumbs = (totalItems < visibleThumbs) ? totalItems : visibleThumbs;
		    //fix ie6 bug - content showing through under overlay
            
            if(jQuery.browser.msie){
				THAT.carousel.find('.backToPos, .backToStory').css('opacity', 1);
				THAT.carousel.find('.page2 input').css('opacity', '0');
				if(jQuery.browser.version < 7){
					THAT.carousel.find('.slant *, .carItem, .page2, .page2 *, .carPanelNavLeft, .carPanelNavRight').css('opacity', 1);
				}
			}
		    THAT.carousel.find('.slant').supersleight();
            THAT.carousel.find('.slant').supersleight({shim: '/assets/images/trans.gif'});
            THAT.carousel.find('.page2').supersleight();
            THAT.carousel.find('.page2').supersleight({shim: '/assets/images/trans.gif'});
            THAT.carousel.find('.carNav span.mask').supersleight();
            THAT.carousel.find('.carNav span.mask').supersleight({shim: '/assets/images/trans.gif'});
            if(THAT.carousel.find('.carNav h5').size() > 0){
                trimNavText();
            }
            if(page2){
                THAT.carousel.find('.page1').hide();
                THAT.carousel.find('.page2').css('opacity', 1).show();
            }
	        bindNavigation();
            addMasks();
            monitorControls();
	    };
		/**
		 * Add the next carousel item content after it has been returned by server and fade the opacity back to 1
		 * @param {String|Object} data whatever type is returned from server
		 * @param {String} textStatus Status returned by server
		 * @param {String} url      the url that has just loaded
		 */
	    this.nextContentLoaded = function(data, text, url){
	        THAT.carCache[url] = data;
	        THAT.carousel.removeClass('loading');
	        THAT.carousel.find('.carItem').replaceWith(jQuery(data).find('.pdtCarousel .carItem').css('opacity', 0));
	        carItem = THAT.carousel.find('.carItem');
	        THAT.carousel.find('.slant').supersleight();
            THAT.carousel.find('.slant').supersleight({shim: '/assets/images/trans.gif'});
            if(jQuery.browser.msie){
				THAT.carousel.find('.backToPos, .backToStory').css('opacity', 1);
				THAT.carousel.find('.page2 input').css('opacity', '0');
				if(jQuery.browser.version < 7){
					THAT.carousel.find('.slant *, .carItem, .page2, .page2 *').css('opacity', 1);
				}
			}
	        //need to trigger backgroud
	        THAT.carousel.css('background-repeat', 'no-repeat');
	        carItem.animate({
			    'opacity' : 1
		    }, 
		    {
		        'duration':500,
		        'complete' : function(){
		            switching = false;
                    monitorControls();
		        }   
		    });
		    if(THAT.carousel.find('.openPage2').size() > 0){
		        THAT.carousel.find('.openPage2').css('background-repeat', 'repeat');
                bindExtras();
            }
	    };
	    /*
	     * Trim text that doesn't fit within the carousel nav thumb sections
	     */
	    var trimNavText = function(){
	        THAT.carousel.find('.carNav h5').each(function(j){
	            var em = jQuery(this).find('em'),
	                words = [],
	                lineCharLimit = 18,
	                currentLine = 0,
	                charCount = 0;
                words = em.text().split(" ");
                
                for(var i = 0, l = words.length; i < l; i++){
                    
                    if(charCount != 1){
                        charCount++;
                    }
                    charCount += words[i].length;
                    if(charCount > lineCharLimit){
                        currentLine++;
                        charCount = words[i].length;
                    }
                    if(currentLine > 1){
                        words.splice(i, words.length - i, '...');
                        break;
                    }
                    
                };
                em.text(words.join(" "));

			});
	    };
		/**
		 * bind the links for the navigation
		 */
		
	    var bindNavigation = function(){
	        THAT.carousel.find('.carNavPrev, .carNavNext').bind('click', scrollNav);
            THAT.carousel.find('.carPanelNavLeft').bind('click', function(){
                switchToItem(currentItem - 1);
                jQuery(this).find('a').blur();
                return false;
            });
            THAT.carousel.find('.carPanelNavRight').bind('click', function(){
                switchToItem(currentItem + 1);
                jQuery(this).find('a').blur();
                return false;
            });
            THAT.carousel.find('.carNav ul a').bind('click', function(){
                var link = $(this),
                    parentLi = link.parent(),
                    itemNo = parentLi.parents().children().index(parentLi);
                if(itemNo == currentItem){
                    return false;
                }
                else{
                    switchToItem(itemNo);
                }
                this.blur();
                return false;
            });
            THAT.carousel.find('.carNav li').bind('mouseenter', function(e) {   
                jQuery(this).addClass('active');
            });
            THAT.carousel.find('.carNav li').bind('mouseleave', function(e) {
                jQuery(this).removeClass('active');
            });
            if(THAT.carousel.find('.openPage2').size() > 0){
                THAT.carousel.find('.openPage2').css('background-repeat', 'repeat');
                bindExtras();
            }

	    };
		/**
		 * Bind the extra links for carousels with 2 page carousel items
		 */
		
	    var bindExtras = function(){
	        THAT.carousel.find('.openPage2').bind('click', function(){
                THAT.carousel.find('.page1').animate({
                    'opacity' : 0
                }, {
                    'duration' : 500,
                    'complete' : function(){
                        THAT.carousel.find('.page1').hide();
                        THAT.carousel.find('.page2').pause(500).css('opacity', 0).show().animate({
                            'opacity' : 1
                        },{
                            'duration' : 500,
                            'complete' : function(){
                                if(jQuery.browser.msie){
                                    THAT.carousel.find('.page2').css('opacity', '');
                                    THAT.carousel.find('.page2 input').css('opacity', '1');
                                }
                            }
                        });
                    }   
                });
                return false;
            });
	        THAT.carousel.find('.backToPos').bind('click', function(){
                THAT.hide();
                return false;
            });
            
	        THAT.carousel.find('.backToStory').bind('click', function(){
	        	if(jQuery.browser.msie){
	        		THAT.carousel.find('.page2 input').css('opacity', '0');
	        	}
                THAT.carousel.find('.page2').animate({
                    'opacity' : 0
                }, {
                    'duration' : 500,
                    'complete' : function(){
                        THAT.carousel.find('.page2').hide();
                        THAT.carousel.find('.page1').pause(500).css('opacity', 0).show().animate({
                            'opacity' : 1
                        },{
                            'duration' : 500,
                            'complete' : function(){
                                if(jQuery.browser.msie){
                                    THAT.carousel.find('.page1').css('opacity', '');
                                }
                            }
                        });
                    }   
                });
                return false;
            });
            THAT.carousel.find(".page2 input[type='submit']").bind('click', function(){
                var value = THAT.carousel.find(".page2 input[type='text']")[0].value;
                var optinvalue = THAT.carousel.find(".page2 input[type='checkbox']")[0].checked;
                var url = THAT.carousel.find(".openPage2")[0].href;
                var isEmail_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
                if(value.search (isEmail_re) != -1){
                    $.get(url, { 'email': value, 'optin' : optinvalue });
                    
                    THAT.carousel.find('.page2 .carContact:not(.hidden) h4').replaceWith(THAT.carousel.find('.page2 div.hidden h4'));
                    
                    THAT.carousel.find('.page2 .carContact:not(.hidden) p:first').replaceWith(THAT.carousel.find('.page2 div.hidden p'));
                    THAT.carousel.find('input, .disclaimer, p:last').hide();
                }
                else{
                    THAT.carousel.find('.pdtEmlErr').remove();
                    THAT.carousel.find(".page2 input[type='text']").css('background-color' , '#ff8181');
                    jQuery(this).after('<p class="pdtEmlErr" style="color:red; width:280px; padding-bottom:0px">'+translations.validation.pageError+'</p>');
                }
                return false;
            });
            if(isEnglish){
            	THAT.carousel.find(".page2 input[type='text']").css('color', '#777777')[0].value = 'Enter email';
            }
        	else{
        		THAT.carousel.find(".page2 input[type='text']").css('color', '#777777')[0].value = 'Entrez votre email';
        	}
        	//fix for weird ie bug
        	THAT.carousel.find(".page2 input[type='checkbox']").css('background', '#00ABB1');
        	
        	THAT.carousel.find(".page2 input[type='text']").bind('focus', function(){
            	THAT.carousel.find(".page2 input[type='text']").css('color', '#000000')[0].value = '';
            });
            THAT.carousel.find(".page2 input[type='text']").bind('keyup', function(){
            	THAT.carousel.find(".page2 input[type='text']").css('background-color' , '');
                THAT.carousel.find('.pdtEmlErr').remove();
            });
            THAT.carousel.find('.lightboxLink').bind('click', lightboxes.lightboxLinkClicked);
	    };
		/**
		 * for each image in the thumb navigation a div with partial opacity is added to partially mask all but the selected image
		 */
		
	    var addMasks = function(){
	        THAT.carousel.find('.carNav li').each(function(){
	            var li = jQuery(this);
	            li.css('position', 'relative');
	            li.find('a:first').css({
				    'width' : THAT.opts.thumbDimensions[0],
	                'height' : THAT.opts.thumbDimensions[1],
				    'display' : 'block'
			    });
	            li.find('img')
	                .css({
    	                
	                    'zoom' : 1,
	                    'width' : THAT.opts.thumbDimensions[0],
	                    'height' : THAT.opts.thumbDimensions[1],
	                    'opacity' : 1
	                })
	                .before(jQuery('<span class="thumbMask"></span>').css({
	                    'position' : 'absolute',
	                    'background' : '#000',
	                    'opacity' : 0,
	                    'width' : THAT.opts.thumbDimensions[0],
	                    'height' : THAT.opts.thumbDimensions[1],
	                    'top' : 0,
	                    'left' : 0,
	                    'zoom' : 1,
	                    'cursor' : 'pointer',
	                    'display' : 'block'
	            }));
	        });
	        THAT.carousel.find('li:eq('+currentItem+') .thumbMask').css('opacity', 0.65);
	    };
		/**
		 * This is called at the end of every transition to check if any of the controls need to be disables
		 */
	    var monitorControls = function(itemNo){
	        if(scrollPos == 0){
	            disableScrollLeft = true;
	            THAT.carousel.find('.carNavPrev').addClass('disabled');
	        }
	        else{
	            disableScrollLeft = false;
	            THAT.carousel.find('.carNavPrev').removeClass('disabled');
	        }
	        if(scrollPos + visibleThumbs >= totalItems){
	            disableScrollRight = true;
	            THAT.carousel.find('.carNavNext').addClass('disabled');
	        }
	        else{
	            disableScrollRight = false;
	            THAT.carousel.find('.carNavNext').removeClass('disabled');
	        }
	        if(currentItem == 0){
	            disableLeft = true;
	            THAT.carousel.find('.carPanelNavLeft').addClass('disabled');
	        }
	        else{
	            disableLeft = false;
	            THAT.carousel.find('.carPanelNavLeft').removeClass('disabled');
	        }
	        if(currentItem == (totalItems - 1)){
	            disableRight = true;
	            THAT.carousel.find('.carPanelNavRight').addClass('disabled');
	        }
	        else{
	            disableRight = false;
	            THAT.carousel.find('.carPanelNavRight').removeClass('disabled');
	        }
	    };
	    
	    
		/**
		 * Switch to the a different item in the carousel, 
		 * First it fades out the current item and thumb, then it makes an ajax call to get the next content
		 * @itemNumber {Number} itemNo the next item to open (0 indexed)
		 */		
	    var switchToItem = function(itemNo){
	        if(switching || (itemNo < 0) || (itemNo > (totalItems - 1))){
	            return false;
	        }
	        THAT.calculateNav(itemNo);

            
	        var url = THAT.carousel.find('li:eq('+itemNo+') a')[0].href;

	        THAT.carousel.find('li:eq('+currentItem+') .thumbMask').animate({'opacity': 0}, {'duration':500});
	        THAT.carousel.find('li:eq('+itemNo+') .thumbMask').pause(500).animate({'opacity': 0.65}, {'duration':500});
	        if(jQuery.browser.msie){
				THAT.carousel.find('.page2 input').css('opacity', '0');
			}
		    carItem.animate({
				    'opacity' : 0
			    },
			    {
				    'duration' : 500,
				    'complete' : function(){
                        THAT.carousel.addClass('loading');
                        if(THAT.carCache[url] != null){
                        	THAT.nextContentLoaded(THAT.carCache[url], 'success', url);
                        }
                        else{
                        	jQuery.get(url, function(data, text){
                            	THAT.nextContentLoaded(data, text, url);
                        	});
                        }
					    return;
				    }
			    });
		    switching = true;
		    currentItem = itemNo;
	    };
	    /**
		 * Works out if and how far the nav needs to scroll when an item is selected
		 * @itemNumber {Number} itemNo the item that has been selected (0 indexed)
		 */		
	    this.calculateNav = function(itemNo){
            var distance;
            if(itemNo < scrollPos){
                distance = -(scrollPos - (itemNo));
                scrollPos += distance;
                scrollNav(null, distance);
            }
            if((itemNo) > (scrollPos + (visibleThumbs - 1))){
                distance = itemNo - (scrollPos + (visibleThumbs - 1));
                scrollPos += distance;
                scrollNav(null, distance);
            }
            
        };
		/**
		 * Moves the thumbs in the navigation left or right
		 * @param {Object} e mouse event
		 * @param {Number} distance how many left or right the nav needs to move
		 */
		
        var scrollNav = function(e, distance){
            if(scrolling){
                return false;
            }
		    var navUl = THAT.carousel.find('.carNav ul');
		    var navUlLeft = parseInt(navUl.css('left'), 10);
		    
		    if(distance){
                navUl.animate({
			        'left' : (navUlLeft - (distance * 172))+'px'
		        }, { duration: 500, queue: false, complete : function(){
		            scrolling = false;
		            monitorControls();
		            } });
		        return false;
		    }
		    if(this.className.indexOf('Next') > -1){
		        if(!disableScrollRight){
			        navUl.animate({
				        'left' : (navUlLeft - 172)+'px'
			        }, { duration: 500, queue: false, complete : function(){
			            scrolling = false;
			            monitorControls();
			            } });
			        scrolling = true;
			        scrollPos++;
			    }
		    }
		    else{
		        if(!disableScrollLeft){
			        navUl.animate({
				        'left' : (navUlLeft + 172)+'px'
			        }, { duration: 500, queue: false, complete : function(){
			        scrolling = false;
			        monitorControls();
			        } });
			        scrolling = true;
			        scrollPos--;
			    }
		    }
    		
		    return false;
	    };
        
        ititPdtCarousel();
        
        return;
    };
    
   
    
    
    
    PdtCarousel.prototype = {
		/**
		 * Hides the page content and Shows and fades in the carousel then makes an ajax request to get the content
		 * @param {String} url location of the promo content
		 * @param {Number} itemNumber which item to start on
		 * @param {Boolean} page2 should it open on page 2 (for the link from the email to the sign up page
		 */
		
        show : function(url, itemNumber, page2){
            var THAT = this;
            
            if(THAT.isOpen){
                THAT.carousel.before(jQuery('<div class="pdtCarousel carJs" id="tempLoader"></div>').css({
                    'background' : 'transparent url(/assets/images/Loading_EN.gif) no-repeat scroll center',
                    'display' : 'block',
                    'opacity' : 1,
                    'width' : THAT.opts.dimensions[0],
                    'height' : THAT.opts.dimensions[1]
                }));
                THAT.hide(true);
                THAT.isOpen = false;
                setTimeout(function() {
                    THAT.show(url, itemNumber);
                }, 600);
                return false;
            }
            
            
            THAT.isOpen = true;
            jQuery.get(url, function(data, text){
            	if(THAT.carCache[url] != null){
                	THAT.contentLoaded(THAT.carCache[url], 'success', itemNumber, page2, url);
                }
                else{
                	jQuery.get(url, function(data, text){
                    	THAT.contentLoaded(data, text, itemNumber, page2, url);
                	});
                }
            });
            THAT.carousel.show();
            THAT.carousel.addClass('firstLoading');
	        
            jQuery(this.opts.stuffToHide).animate({
                    opacity: 0
                }, 
                { 
                    duration: 500,
                    complete : function(){
                        jQuery(THAT.opts.container).css('position', 'relative');
                        jQuery(THAT.opts.stuffToHide).hide();
                    }
                }
            );
            THAT.carousel.pause(500).animate({
                    opacity: 1
                }, 
                { 
                    duration: 500,
                    complete : function(){
                        THAT.calculateNav(itemNumber);
                        jQuery('#tempLoader').remove();
                    }
                }
            );
            
            return false;
        },
		/**
		 * Fades out and hides the carousel
		 */
        hide : function(carouselOnly){
            var THAT = this;
            if(!THAT.isOpen){
                return;
            }
            THAT.carousel.animate({
                    opacity: 0
                }, 
                { 
                    duration: 500,
                    complete:function(){
                        THAT.carousel.hide();
                        THAT.isOpen = false;
                        if(!carouselOnly){
                            jQuery(THAT.opts.stuffToHide).show().animate({
                                    opacity: 1
                                }, 
                                { 
                                    duration: 500,
                                    complete: function(){
                                    	//intro dissapearing in safari, have to hide and show it to make it stay
                                    	if(jQuery.browser.safari){
                                    		jQuery('.hubIntro').hide();
                                    		jQuery('.hubIntro').show();
                                		}
                                    }
                                }
                            );
                        }
                    }
                }
            );
            
            
            return false;
        }
    };
    
    // ... 
})();

(function(){ 
    
	    
    
    var ImageTabs = window.ImageTabs = function(opts){
        var tabContainer,
        tabBlocks,
        tabTabs,
        that;
        that = this;
        this.opts = opts;
        var initImageTabs = function(){
            tabContainer = jQuery(that.opts.containerId);
            if(tabContainer.size() < 1){
                return;
            }
            tabBlocks = tabContainer.find(that.opts.blockClass);
            tabTabs = tabContainer.find('h3');
            bindTabLinks();
        };
        var bindTabLinks = function(){
            tabTabs.find('a').bind('click', switchTab);
        };
        var switchTab = function(e){
            var link = jQuery(this),
                h3 = link.parent(),
                classString = h3.attr('class'),
                tabClass = classString.substring(classString.indexOf('tab'), classString.indexOf('tab')+4);
                tabTabs.removeClass('current').filter('.'+tabClass).addClass('current');
                tabBlocks.addClass('hiddenTab').filter('.'+tabClass).removeClass('hiddenTab');
            return false;
        };
        initImageTabs();
    };
    
    

    /*ImageTabs.prototype = {
        someFunction : function(){
            
        }
    };*/
    
    // ... 
})();
jQuery(document).ready(function() {
    jQuery('#collageArea li').bind('mouseenter', function(e) {
        jQuery(this).addClass('active').parents('#collageArea').addClass('hoverState');
    });
    jQuery('#collageArea li').bind('mouseleave', function(e) {
        jQuery(this).removeClass('active').parents('#collageArea').removeClass('hoverState');
    });
    var homePromo = new HomePromo();
    var carousel = new PdtCarousel({ 'container': '#the-space .content_container',
        'sideControlls': true,
        'carId': 'artCar',
        'dimensions': [778, 390],
        'thumbDimensions': [162, 92],
        'stuffToHide': '#the-space .hub_content'
    });

    var inspirationsCarousel = new PdtCarousel({ 'container': '#inspirations .content_container',
        'carId': 'posCar',
        'dimensions': [911, 544],
        'thumbDimensions': [162, 92],
        'stuffToHide': '#inspirations .hub_content'
    });
    jQuery('#collageArea li').bind('click', function(e) {
        var link = $(this),
            parentUl = link.parent(),
            itemNo = parentUl.children().index(link);
        inspirationsCarousel.show(jQuery(this).find('a')[0].href, itemNo);
        return false;
    });
    jQuery('#collageArea li a').bind('click', function(e) {
        e.preventDefault();
    });
    var locString = window.location+'';
    var showSignup = locString.substr(locString.indexOf('show-signup=')+12, 1);
    if(locString.indexOf('show-signup=') > -1){
        inspirationsCarousel.show(jQuery('#collageArea li a:eq('+showSignup+')')[0].href, showSignup, true);
    }
    var artHomeTabs = new ImageTabs({ 'containerId': '#the-space .gridTabContainer',
        'blockClass': '.imageTab'
    });
    var lunchTabs = new ImageTabs({ 'containerId': '#lunch .gridTabContainer',
        'blockClass': '.imageTab'
    });
    var toursTabs = new ImageTabs({ 'containerId': '#tours .gridTabContainer',
        'blockClass': '.imageTab'
    });
    var dinnerTabs = new ImageTabs({ 'containerId': '#dinner .gridTabContainer',
        'blockClass': '.imageTab'
    });
    jQuery('#the-space .carouselLaunch').bind('click', function() {
        jQuery(this).parents('#the-space').find('ul.subnav li').removeClass('selected');
        jQuery(this).parents('#the-space').find('ul.subnav a[href="'+this.href+'"]').parent().addClass('selected');
        carousel.show(this.href, 0);
        return false;
    });
    jQuery('#the-space li.title em a, #the-space a.slurp, .hubNav > ul > li > a').bind('click', function() {
        carousel.hide();
        return false;
    });
    jQuery('.hubNav > ul > li > a, .hubNav .title > em > a').bind('click', function() {
        inspirationsCarousel.hide();
        return false;
    });
    jQuery('.homePromoLink').bind('click', function() {
        homePromo.show(this.href);
        return false;
    });
    if (jQuery.cookie('pdtHp') != 'set') {
        if(locString.indexOf('show-signup=') == -1){
            var promoTimeout = setTimeout(function() {
            	if(jQuery('.homePromoLink').size() > 0){
                	homePromo.show(jQuery('.homePromoLink')[0].href);
            	}
            }, 2000);
            jQuery.cookie('pdtHp', 'set');
        }
    }
});

jQuery.fn.supersleight = function(settings) {
	settings = jQuery.extend({
		imgs: true,
		backgrounds: true,
		shim: 'x.gif',
		apply_positioning: true
	}, settings);
	
	return this.each(function(){
		if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
			jQuery(this).find('*').andSelf().each(function(i,obj) {
				var self = jQuery(obj);
				// background pngs
				if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
					var bg = self.css('background-image');
					var src = bg.substring(5,bg.length-2);
					var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
					var styles = {
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
						'background-image': 'url('+settings.shim+')'
					};
					self.css(styles);
				};
				// image elements
				if (settings.imgs && self.is('img[src$=png]')){
					var styles = {
						'width': self.width() + 'px',
						'height': self.height() + 'px',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
					};
					self.css(styles).attr('src', settings.shim);
				};
				// apply position to 'active' elements
				if (settings.apply_positioning && self.is('a, input') && (self.css('position') === '' || self.css('position') == 'static')){
					self.css('position', 'relative');
				};
			});
		};
	});
};
/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

$.fn.pause = function(duration) {
    $(this).animate({ dummy: 1 }, duration);
    return this;
};