
rootElement = ("onorientationchange" in window) ? $(document) : $(window);
//imgRatio, bgImg, bgWidth, bgHeight, bgOffset, bgCSS;
position = 1;
position_page = 1;
var $save_left;

var BE = {
	
	// Properties
	
	body : $('body,html'),
	main : $('#main'),
	header : $('#header'),
	content : $('#content'),
	sidebar : $('#sidebar'),
	sidebar_nav : $('#sidebar-nav'),
	panel : $('#panel'),
	footer : $('#footer'),
	background: null,
	isReady: false,
	isStarted: false,
	browser: null,
	tracker: _gaq
	
	
	// Getters
	
	,getBody : function() {
		return $('body');
	}
	
	,getHeader : function() {
		return $('#header');
	}
	
	,getContent : function() {
		return $('#content');
	}
	
	,getPanel : function() {
		return $('#panel');
	}
	
	,getScroller : function() {
		return $('#scroller');
	}
	
	,getSidebar : function() {
		return $('#sidebar');
	}
	
	,getSidebarNav : function() {
		return $('#sidebar-nav');
	}
	
	,getFooter : function() {
		return $('#footer');
	}
	
	,getLaunches : function() {
		return $('#launches');
	}
	
	,getGallery : function() {
		return $('#gallery-container');
	}
	
	,getPageGallery : function() {
		return $('#page-gallery-container');
	}
	
	,getLogo : function() {
		return $('#section_logo');
	}
	
	
	// Methods
	
	,init : function() {
		
		//if ( $.cookie('intro') == null ) $.cookie('intro', 0, { expires: null, path: '/', domain: '.borgoegnazia.com', secure: false });
		
		BE.getPage();
		
		if ( BE.getGallery().length > 0 ) BE.setUpGallery();
		
	}
	
	,getPage : function() {
		
		arrContexts = [];
		arrContexts['it'] = 'web';
		arrContexts['en'] = 'en';
		arrContexts['ru'] = 'ru';
		
        $.address.init()
        .externalChange(function(event) {
			
            context = event.pathNames[0];
			
			var pathNames = event.pathNames;
			
			if ( window.location.pathname == '/' &&  window.location.href.indexOf('#') == -1 ) {_gaq.push(['_trackPageview']); window.location = '/en/';}
			
			var language = pathNames.shift();
			var alias = pathNames.join('/');
			
			alias = ( alias != '' && alias.indexOf('.html') != '-1' ) ? alias : alias + '/';
			
			BE.startSiteLoading();
			
            $.ajax({
                url: '/ajax-backend/get-page-from-alias.php?alias=' + alias + '&context=' + arrContexts[context],
                dataType: 'json',
                success: function(data, textStatus, xhr) {
					
                    BE.setComponents(alias, data);
					
					if (BE.isReady) {
						// TO-DO
						
					}
                }
            });

        });
		
		$('#main-nav a:not(a.gallery,a.iframe), #service-nav > ul a:not(a.iframe, a.reserved), #footer ul a, #logo a').live('click',
	    function() {
	        var alias = $(this).attr('href');
			alias = alias.replace(/http:\/\/www.borgoegnazia.com/i, '');
			alias_to_send = alias.replace(/\/(it|en|ru)\//i, '');
			BE.viewMainPage(alias_to_send, arrContexts[language]);
	        $('#main-nav li, #service-nav > ul li, #footer-nav li').removeClass('active');
			$(this).parent().addClass('active');
	        $.address.value(alias);
	        return false;
	    });
	
		$('#second-nav ul > li > a:not(a.gallery,a.iframe), a#link-parent').live('click',
	    function(evt) {
	        var alias = $(this).attr('href');
			alias = alias.replace(/http:\/\/www.borgoegnazia.com/i, '');
			alias_to_send = alias.replace(/\/(it|en|ru)\//i, '');
			BE.viewSidebarPage(alias_to_send, arrContexts[language]);
	        //$('#sidebar-nav li').removeClass('active');
			//$(this).parent().addClass('active');
	        $.address.value(alias);
			evt.stopPropagation();
	        return false;
	    });
		
		/*$('#panel.offers a:not(a[href^="http"],a[href^="mailto"],a#switcher), #panel.news a:not(a[href^="http"],a[href^="mailto"])').live('click',
	    function() {
	        var alias = $(this).attr('href');
			alias = alias.replace(/http:\/\/www.borgoegnazia.com/i, '');
			alias_to_send = alias.replace(/\/(it|en|ru)\//i, '');
			BE.viewPageInsidePanel(alias_to_send, arrContexts[language]);
	        $.address.value(alias);
	        return false;
	    });*/
	
		$('#panel.offers .offer a, #panel.offers #back_to_archive').live('click',
	    function() {
	        var alias = $(this).attr('href');
			alias = alias.replace(/http:\/\/www.borgoegnazia.com/i, '');
			alias_to_send = alias.replace(/\/(it|en|ru)\//i, '');
			BE.viewPageInsidePanel(alias_to_send, arrContexts[language]);
	        $.address.value(alias);
	        return false;
	    });
		
		$('.paging a').die('click').live('click',
	    function(e) {
			e.preventDefault();
			var alias = $(this).attr('href').split('?')[0];
			alias = alias.replace(/http:\/\/www.borgoegnazia.com/i, '');
			alias_to_send = alias.replace(/\/(it|en|ru)\//i, '');
			BE.viewPageInsidePanel(alias_to_send, arrContexts[language]);
	        return false;
	    });
		
		$('#second-nav ul a.gallery').live('click',
	    function() {
			var alias = $(this).attr('href');
			alias = alias.replace(/http:\/\/www.borgoegnazia.com/i, '');
			alias_to_send = alias.replace(/\/(it|en|ru)\//i, '');
			BE.retrievePageGallery(alias_to_send, arrContexts[language]);
	        return false;
	    });
	
		$('a.iframe').live('mouseenter',
	    function() {
			var $item = $(this);
			$(this).fancybox({
				'width': 750,
				'height': '80%',
				'padding': 20,
				'titleShow' : false/*,
				'onComplete' : function(){
					var alias = $item.attr('href');
					alias = alias.replace(/http:\/\/www.borgoegnazia.com/i, '');
					alias_to_send = alias.replace(/\/(it|en|ru)\//i, '');
					BE.trackPage( '/' + arrContexts[language] + '/' + alias_to_send );
				}*/
			});
	    });
		
		$('#launches .launch-item').live('click',
	    function() {
	        var alias = $(this).find('.launch-title a').attr('href');
			alias = alias.replace(/http:\/\/www.borgoegnazia.com/i, '');
			alias_to_send = alias.replace(/\/(it|en|ru)\//i, '');
			BE.viewPageFromLaunch(alias_to_send, arrContexts[language]);
	        $('#main-nav li, #service-nav > ul li, #footer-nav li').removeClass('active');
	        $.address.value(alias);
	        return false;
	    });
		
		$('#panel a#switcher').live('click',
	    function() {
		
			if ( typeof $save_left == 'undefined' ) $save_left = $('#panel').css('left');
			var $button = $(this);
			var $img = $(this).find('img');
			if ( !$button.parent().hasClass('hidden') ) {
				$button.parent().animate({
					left: $(window).width() - 23
				}, 500, function(){
					$button.parent().addClass('hidden');
					$button.text('Show');
					$button.css({right: 'auto', left: 6})
				});
			} else {
				//$button.css('opacity', 0);
				//$button.find('img').css('opacity', 0);
				$button.css({right: 8, left: 'auto'})
				$(this).parent().animate({
					left: $save_left
				}, 500, function(){
					$button.parent().removeClass('hidden');
					$button.text('Hide');
				});
			}
	        return false;
	    });
		
		$(window).load(function(){
			
			var readytimer = setInterval(function(){

				if (BE.isReady) {
					
					clearInterval(readytimer);
					
					//if ( $.cookie('intro') == 0  ) {
						
						//BE.loadIntro(function() {
								
								$('#intro img').animate({
					                opacity: 0
					            },
					            1500,
					            function() {
									
									$('#intro').animate({
								        opacity: 0
								    }, 1000, function(){
										$(this).remove();
										//BE.showIntro(function(){

											BE.loadBackground(function(){
												
												BE.header.animate({
							                        top: 0
							                    },
							                    700, 'easeInOutQuad');
							                    BE.footer.animate({
							                        bottom: 0
							                    },
							                    700, 'easeInOutQuad');
												
												if ( !BE.getPanel().hasClass('map') ) {
													BE.showBackground(1000, function(){
														BE.stopSiteLoading();
													});
												} else {
													initialize();
													BE.stopSiteLoading();
												}

											});

										//});

									});
									
								});
								
								

						//});
						
					/*} else {
						
						BE.loadBackground(function(){
							
							$('#overlay').animate({
						        opacity: 0
						    }, 1000, function(){
								$(this).remove();
								BE.header.delay(1000).animate({
									top: 0
								}, 800, 'easeOutQuad');
								BE.sidebar.delay(1000).animate({
									left: 0
								}, 800, 'easeOutQuad');
								BE.footer.delay(1000).animate({
									bottom: 0
								}, 800, 'easeOutQuad');
								BE.showBackground(1000);
								BE.getLaunches().delay(1000).animate({
									opacity: 1
								}, 800);
								
							});

						});
						
					}*/
					
				}

			}, 10);
			
		});

	}
	
	,loadIntro : function(callback) {
		
		var counter = 0;
		
		//$.each(['/assets/i/intro-2.jpg', '/assets/i/intro-1.jpg'], function(index, img){						
		$.each(['/assets/i/intro-1.jpg'], function(index, img){						
			
			var image = $('<img/>').css({
		        position: "absolute",
		        top: 0,
		        left: 0,
		        opacity: 0
		    })
			.bind('load', function(e){
				counter = counter+1;
				var self = $(this);
		        imgRatio = $(e.target).width() / $(e.target).height();

		        BE._adjustBGIntro(function() {
					
		        });
				
				if ( counter == 1 ) {
					image.css({
						position: 'relative',
						opacity: 1
					});
					$('#backstretch img:first').remove();
					callback();
					return false;
				}
				
			})
			.appendTo($('#backstretch'));
			image.attr('src', img);
			
		});
		
	}
	
	,loopIntro : function(callback) {
		
		var cb = callback;
		
		if ( $('#backstretch img').size() > 1 ) {

			var $last = $('#backstretch img:last');

			$last.prev().css('opacity', 1);

			$last.animate({
				opacity: 0
			}, 1000, function(){
				$(this).remove();
				$('#backstretch img:last').css('position', 'relative');
			});
			
			setTimeout(function(){
				BE.loopIntro(cb);
			}, 1000);

		} else {
			clearTimeout( introtimeout );
			cb();
		}
		
	}
	
	,showIntro : function(callback) {
		var cb = callback;
		introtimeout = setTimeout(function(){
			BE.loopIntro(cb);
		}, 2000);
		
	}
	
	,viewMainPage : function(alias) {
		
		BE.removeLogo();
		
		BE.removeLaunches();
		
		BE.startPageLoading();
		
			$.ajax({
	            url: '/ajax-backend/get-main-page.php?alias=' + alias + '&context=' + arrContexts[context],
	            dataType: 'json',
	            success: function(data, textStatus, xhr) {
					
					$('#gmap').css('z-index', '-10000');
					
					// Set the panel to slide away
					
					BE.getPanel().addClass('to-hide');
					
					
					// Page title

					$.address.title(data.pagetitle);

					
					// Google Analytics tracking

					if ( alias == '/' ) {
						BE.trackPage( '/' + context + '/' );
					} else {
						BE.trackPage( '/' + context + '/' + alias );
					}
					
					
					// Background image

					BE.setBackground( data.background );
					
					
					// Launches
					
					if ( data.launches != '' ) BE.setLaunches( data.launches );
					
					
					// Section logo
					
					if ( data.logo != '' ) BE.setLogo( data.logo );
					
					
					// Create the new panel
					
					var $top = 140;
					
					$(data.panel)
					.hide()
					.appendTo( BE.getContent() )
					.addClass('to-show')
					.css({
						top: $(window).height() + $('.to-show').outerHeight()
					}).show();
					
					BE.loadBackground(function(){
						BE.getLaunches().animate({
							opacity: 1
						}, 1000	, function(){
							if (BE.browser.msie) {
								this.style.removeAttribute('filter');
							}
						});
						
						BE.getLogo().animate({
							opacity: 1
						}, 1000	, function(){
							if (BE.browser.msie) {
								this.style.removeAttribute('filter');
							}
						});
						
						BE.slidePanels();
						//BE.showBackground(0);
					});
						
	            }
	        });
		
	}
	
	,viewSidebarPage : function(alias) {
		
		BE.startPageLoading();
		
		//BE.removeLaunches();
		
		BE.getPanel().find('*').animate({
			opacity: 0
		}, 500);
		
		BE.getPanel().find('#scroller').animate({
			opacity: 0
		}, 500, function(){
			
			$.ajax({
	            url: '/ajax-backend/get-sidebar-page.php?alias=' + alias + '&context=' + arrContexts[context],
	            dataType: 'json',
	            success: function(data, textStatus, xhr) {
	                // Page title

					$.address.title(data.pagetitle);


					// Google Analytics tracking

					if ( alias == '/' ) {
						BE.trackPage( '/' + context + '/' );
					} else {
						BE.trackPage( '/' + context + '/' + alias );
					}


					// Background image

					BE.setBackground( data.background );


					// Panel content

					var panel = $(data.panel);
					var sidebar = $(data.panel).find('#second-nav').html();
					var content = $(data.panel).find('#scroller').html();

					BE.getPanel().html( '<div id="second-nav">' + sidebar + '</div><div id="scroller">' + content + '</div>' );
					$('<a id="switcher" title="Hide">Hide</a>').appendTo( BE.getPanel() );


					BE.resizeScroller();
					BE.loadBackground(function(){
						BE.getPanel().find('*').animate({
							opacity: 1
						}, 1000	, function(){
							if (BE.browser.msie) {
								this.style.removeAttribute('filter');
							}
						});
						BE.showBackground(0, function(){
							BE.stopPageLoading();
						});
					});
	            }

			});
			
		});
		
	}
	
	,viewFooterPage : function(alias) {
		
		BE.startPageLoading();
		
		BE.removeLaunches();
			
		if ( $('#collection-map').length > 0 ) {
			$('#collection-map').stop(true, true).animate({
				opacity: 0
			}, 500, function(){
				$(this).remove();
			});
		}
			
			$.ajax({
	            url: '/ajax-backend/get-footer-page.php?alias=' + alias + '&context=' + arrContexts[context],
	            dataType: 'json',
	            success: function(data, textStatus, xhr) {
	                // Page title

					$.address.title(data.pagetitle);
					
					
					// Google Analytics tracking

					if ( alias == '/' ) {
						BE.trackPage( '/' + context + '/' );
					} else {
						BE.trackPage( '/' + context + '/' + alias );
					}
					
					

					// Background image

					BE.setBackground( data.backgrounds );
					
					
					// Header

					BE.header.html( data.header );
					

					// Sidebar navigation
					$('#logo-sidebar').html( '<img alt="BE Collection" src="assets/i/logo-collection.png" id="corporate-logo" style="position: absolute; top: 0px; left: 0px;">' );
					BE.sidebar_nav.html('').css('opacity', 0);
					
					
					// Panel content
					
					BE.panel.html( data.panel ).css('width', 300).css('opacity', 1);
					
					
					// Footer

					BE.footer.html( data.footer );
					

					BE.resizeContent();
					BE.loadBackground(function(){
						BE.showBackground(0);
					});
	            }
	        });
		
	}
	
	,viewPageInsidePanel : function(alias) {
		
		BE.startPageLoading();
		
		//BE.removeLaunches();
		
		BE.getPanel().find('*').animate({
			opacity: 0
		}, 500);
		
		BE.getPanel().find('#scroller').animate({
			opacity: 0
		}, 500, function(){
			
			$.ajax({
	            url: '/ajax-backend/get-inside-panel-page.php?alias=' + alias + '&context=' + arrContexts[context],
	            dataType: 'json',
	            success: function(data, textStatus, xhr) {
					
	                // Page title

					$.address.title(data.pagetitle);
					
					
					// Google Analytics tracking

					if ( alias == '/' ) {
						BE.trackPage( '/' + context + '/' );
					} else {
						BE.trackPage( '/' + context + '/' + alias );
					}
					
					
					// Background image

					BE.setBackground( data.background );
					
					
					// Panel content
					
					var panel = $(data.panel);
					var content = $(data.panel).find('#scroller').html();

					BE.getPanel().html( '<div id="scroller">' + content + '</div>' );
					$('<a id="switcher" title="Hide">Hide</a>').appendTo( BE.getPanel() );
					
					if ( BE.getPanel().find('img').length > 0 ) {
						
						BE.getPanel().find('img').load(function(){
							BE.resizeScroller();
						});
						
					} else {
						BE.resizeScroller();
					}
					
					BE.loadBackground(function(){
						BE.getPanel().find('*').animate({
							opacity: 1
						}, 1000	, function(){
							if (BE.browser.msie) {
								this.style.removeAttribute('filter');
							}
						});
						BE.showBackground(0, function(){
							BE.stopPageLoading();
						});
					});
					
	            }
	        });
			
		});
		
	}
	
	,viewHotelPage : function(alias) {
		
		BE.startPageLoading();
		
		BE.removeLaunches();
		
		if ( $('#collection-map').length > 0 ) {
			$('#collection-map').stop(true, true).animate({
				opacity: 0
			}, 500, function(){
				$(this).remove();
			});
		}
		
		$('#panel').removeClass('medium');
		
		$.ajax({
            url: '/ajax-backend/get-hotel-page.php?alias=' + alias + '&context=' + arrContexts[context],
            dataType: 'json',
            success: function(data, textStatus, xhr) {

				// Page title

				$.address.title(data.pagetitle);
				
				
				// Google Analytics tracking
				
				if ( alias == '/' ) {
						BE.trackPage( '/' + context + '/' );
					} else {
						BE.trackPage( '/' + context + '/' + alias );
					}


				// Background image

				BE.setBackground( data.backgrounds );


				// Header

				BE.header.html( data.header );
				

				// Sidebar navigation
				
				BE.sidebar_nav.html('');
				
				
				// Panel content
				
				BE.panel.css('width', 300).html('');
				
				// Panel content
				
				BE.footer.html( data.footer );
				
				
				// Gallery
				if ( BE.getGallery().length > 0 ) BE.getGallery().remove();
				$(data.gallery).appendTo( $('body') );
				BE.setUpGallery();
				
				
				BE.setHotelSidebar( data.hotel_name, data.logo );
				
				BE.loadBackground(function(){
					BE.slideHotelIn();
					BE.showBackground(0);
				});
            }
        });
		
	}
	
	,viewHotelPageFromFlash : function(alias) {
		
		var alias = alias;
		alias = alias.replace(/http:\/\/www.borgoegnazia.com/i, '');
		alias_to_send = alias.replace(/\/(it|en|ru)\//i, '');
        $('#sidebar-nav li').removeClass('active');
		$(this).parent().addClass('active');
        $.address.value(alias);
		
		BE.startPageLoading();
		
		BE.removeLaunches();
		
		if ( $('#collection-map').length > 0 ) {
			$('#collection-map').stop(true, true).animate({
				opacity: 0
			}, 500, function(){
				$(this).remove();
			});
		}
		
		$('#panel').removeClass('medium');
		
		$.ajax({
            url: '/ajax-backend/get-hotel-page.php?alias=' + alias_to_send + '&context=' + arrContexts[context],
            dataType: 'json',
            success: function(data, textStatus, xhr) {

				// Page title

				$.address.title(data.pagetitle);
				
				
				// Google Analytics tracking
				
				if ( alias == '/' ) {
						BE.trackPage( '/' + context + '/' );
					} else {
						BE.trackPage( '/' + context + '/' + alias_to_send );
					}


				// Background image

				BE.setBackground( data.backgrounds );


				// Header

				BE.header.html( data.header );
				

				// Sidebar navigation
				
				BE.sidebar_nav.html('');
				
				
				// Panel content
				
				BE.panel.css('width', 300).html('');
				
				// Panel content
				
				BE.footer.html( data.footer );
				
				
				// Gallery
				if ( BE.getGallery().length > 0 ) BE.getGallery().remove();
				$(data.gallery).appendTo( $('body') );
				BE.setUpGallery();
				
				
				BE.setHotelSidebar( data.hotel_name, data.logo );
				
				BE.loadBackground(function(){
					BE.slideHotelIn();
					BE.showBackground(0);
				});
            }
        });
		
	}
	
	,viewCollectionPage : function(alias) {
		
		BE.startPageLoading();
		
		BE.removeLaunches();
		
		$.ajax({
            url: '/ajax-backend/get-collection-page-from-hotel.php?alias=' + alias + '&context=' + arrContexts[context],
            dataType: 'json',
            success: function(data, textStatus, xhr) {

				// Page title

				$.address.title(data.pagetitle);

				
				// Google Analytics tracking
				
				if ( alias == '/' ) {
						BE.trackPage( '/' + context + '/' );
					} else {
						BE.trackPage( '/' + context + '/' + alias );
					}
				
				
				// Background image

				BE.setBackground( data.backgrounds );


				// Header

				BE.header.html( data.header );
				

				// Sidebar navigation
				
				BE.sidebar_nav.html('');
				$('#panel').removeClass('medium');
				
				
				// Panel content
				
				BE.panel.animate({
					opacity: 0
				}, 1000, function(){
					BE.panel.css('width', 300).html('');
					if ( data.map != '' ) {
						$(data.map).appendTo( BE.content );
						if ( BE.browser.webkit || BE.browser.msie) {
							setTimeout(function(){
								var so = new SWFObject("/assets/i/collection-map.swf", "coll-map", "620", "900", "7", "");
								so.addParam("wmode", "transparent");
								so.write("coll-map");
								$(window).trigger( "resize" );
							}, 900);
						}

					}
				});
				
				
				// Footer

				BE.footer.html( data.footer );
				
				
				// Launches
				
				if ( data.launches != '' ) BE.setLaunches( data.launches );
				
				
				// Gallery
				if ( BE.getGallery().length > 0 ) BE.getGallery().remove();
				$(data.gallery).appendTo( $('body') );
				BE.setUpGallery();
				
				
				
				BE.setCorporateSidebar();
				
				BE.loadBackground(function(){
					BE.slideCorporateIn(data.sidebar);
					BE.showBackground(0);
					$('#collection-map').animate({
						opacity: 1
					}, 1000	, function(){
							if (BE.browser.msie) {
		                        this.style.removeAttribute('filter');
		                    }
						});
					BE.getLaunches().animate({
						opacity: 1
					}, 1000	, function(){
							if (BE.browser.msie) {
		                        this.style.removeAttribute('filter');
		                    }
						});
				});
            }
        });
		
	}
	
	,viewMainPageFromHotel : function(alias) {
		
		BE.startPageLoading();
		
		BE.removeLaunches();
		
		$.ajax({
            url: '/ajax-backend/get-main-page-from-hotel.php?alias=' + alias + '&context=' + arrContexts[context],
            dataType: 'json',
            success: function(data, textStatus, xhr) {
                
				// Page title

				$.address.title(data.pagetitle);

				
				// Google Analytics tracking
				
				if ( alias == '/' ) {
						BE.trackPage( '/' + context + '/' );
					} else {
						BE.trackPage( '/' + context + '/' + alias );
					}
				
				
				// Background image

				BE.setBackground( data.backgrounds );


				// Header

				BE.header.html( data.header );
				

				// Sidebar navigation
				
				BE.sidebar_nav.html( data.sidebar );
				
				
				// Panel content
				$('#panel').removeClass('medium');
				BE.panel.find('*').animate({
					opacity: 0
				}, 1000);
				BE.panel.animate({
					opacity: 0
				}, 1000, function(){
					BE.panel.css('width', 300);
				});
				
				
				// Footer

				BE.footer.html( data.footer );
				
				// Launches
				
				if ( data.launches != '' ) BE.setLaunches( data.launches );
				
				
				// Gallery
				if ( BE.getGallery().length > 0 ) BE.getGallery().remove();
				$(data.gallery).appendTo( $('body') );
				BE.setUpGallery();
				
				BE.panel.html( data.panel );
				BE.resizeContent();
				
				BE.setCorporateSidebar();
				
				BE.loadBackground(function(){
					BE.slideCorporateInService();
					if (data.sidebar != '') {
						BE.content.removeClass('no-bg');
						BE.sidebar_nav.animate({
							opacity: 1
						}, 1000, function(){
							if (BE.browser.msie) {
		                        this.style.removeAttribute('filter');
		                    }
						});
					} else {
						BE.panel.find('*').animate({
							opacity: 1
						}, 1000	, function(){
									if (BE.browser.msie) {
				                        this.style.removeAttribute('filter');
				                    }
								});
						BE.panel.animate({
							opacity: 1
						}, 1000, function(){
							if (BE.browser.msie) {
		                        this.style.removeAttribute('filter');
		                    }
						});
					}
					BE.showBackground(0);
					BE.getLaunches().animate({
						opacity: 1
					}, 1000);
				});
				
            }
        });
		
	}
	
	,viewPageFromLaunch : function(alias) {
		
		BE.removeLogo();
		
		BE.removeLaunches();
		
		BE.startPageLoading();
		
			$.ajax({
	            url: '/ajax-backend/get-page-from-launch.php?alias=' + alias + '&context=' + arrContexts[context],
	            dataType: 'json',
	            success: function(data, textStatus, xhr) {
					
					$('#gmap').css('z-index', '-10000');
					
					// Set the panel to slide away
					
					BE.getPanel().addClass('to-hide');
					
					
					// Page title

					$.address.title(data.pagetitle);

					
					// Google Analytics tracking

					if ( alias == '/' ) {
						BE.trackPage( '/' + context + '/' );
					} else {
						BE.trackPage( '/' + context + '/' + alias );
					}
					
					
					// Background image

					BE.setBackground( data.background );
					
					
					BE.getHeader().html( data.header );
					
					
					// Launches
					
					if ( data.launches != '' ) BE.setLaunches( data.launches );
					
					
					// Section logo
					
					if ( data.logo != '' ) BE.setLogo( data.logo );
					
					
					// Create the new panel
					
					var $top = 140;
					
					$(data.panel)
					.hide()
					.appendTo( BE.getContent() )
					.addClass('to-show')
					.css({
						top: $(window).height() + $('.to-show').outerHeight()
					}).show();
					
					BE.loadBackground(function(){
						BE.getLaunches().animate({
							opacity: 1
						}, 1000	, function(){
											if (BE.browser.msie) {
						                        this.style.removeAttribute('filter');
						                    }
										});
										
					BE.getLogo().animate({
						opacity: 1
					}, 1000	, function(){
						if (BE.browser.msie) {
							this.style.removeAttribute('filter');
						}
					});
										
						BE.slidePanels();
						//BE.showBackground(0);
					});
						
	            }
	        });
		
	}
	
	,startSiteLoading : function(){
		
		$('<div id="overlay-loading" style="position:absolute;top:0;left:0;width:100%;height:100%;z-index:100000;" />').css('opacity', 0).appendTo( BE.getBody() );
		
		/*$('<div id="loading-page" />').css({
	        position: 'absolute',
	        top: (($(window).height() - 120) / 2) - 20,
	        left: ($(window).width() / 2) - 20,
	        width: 41,
	        height: 40,
	        backgroundImage: "url('/assets/i/loading-page.png')",
	        zIndex: 2
	    })
	    .appendTo( BE.getBody() );*/

	    position_page = 1;
	    timer_page = setInterval('BE.loadingPage()', 100);
		
	}
	
	,stopSiteLoading : function(){
		
		$('#overlay-loading').remove();
		
	}
	
	,startPageLoading : function(){
		
		$('<div id="overlay-loading" style="position:absolute;top:0;left:0;width:100%;height:100%;z-index:100000;" />').css('opacity', 0).appendTo( BE.getBody() );
		
		$('<div id="loading-page" />').css({
	        position: 'absolute',
	        top: (($(window).height() - 120) / 2) - 20,
	        left: ($(window).width() / 2) - 20,
	        width: 41,
	        height: 40,
	        backgroundImage: "url('/assets/i/loading-page.png')",
	        zIndex: 2
	    })
	    .appendTo( BE.getBody() );

	    position_page = 1;
	    timer_page = setInterval('BE.loadingPage()', 100);
		
	}
	
	,stopPageLoading : function(){
		
		if ( $('#loading-page').length > 0 ) $('#loading-page').remove();
		if ( typeof timer_page != 'undefined' ) clearTimeout(timer_page);
		$('#overlay-loading').remove();
		
	}
	
	,setHotelSidebar : function(hotel_name, logo){
		
		hotel_logo = $('<img id="hotel-logo" src="' + logo + '" alt="' + hotel_name + '">')
		.css({
			position: 'absolute',
			top: 0,
			left: '-170px'
		}).appendTo( BE.content );
		
	}
	
	,slideHotelIn : function(){
		
		$('#logo-sidebar').animate({
			left: '-170px'
		}, 1000, 'easeOutQuad');
		
		BE.getSidebarNav().animate({
			left: '-170px'
		}, 1000, 'easeOutQuad');
		
		$('#hotel-logo').animate({
			left: 0
		}, 1000, 'easeOutQuad', function(){
			$('#logo-sidebar').css('left', 0).empty();
			hotel_logo.appendTo( $('#logo-sidebar') );
			BE.getSidebarNav().css({
				opacity: 0,
				left: 0
			}, 1000);
		});
		
	}
	
	,setCorporateSidebar : function(){
		
		corporate_logo = $('<img id="corporate-logo" src="/assets/i/logo-collection.png" alt="BE Collection">')
		.css({
			position: 'absolute',
			top: 0,
			left: '-170px'
		}).appendTo( BE.content );
		
	}
	
	,slideCorporateIn : function( sidebar ){
		
		$('#logo-sidebar').animate({
			left: '-170px'
		}, 1000, 'easeOutQuad');
		
		BE.getSidebarNav().animate({
			left: '-170px'
		}, 1000, 'easeOutQuad');
		
		$('#corporate-logo').animate({
			left: 0
		}, 1000, 'easeOutQuad', function(){
			$('#logo-sidebar').css('left', 0).empty();
			corporate_logo.appendTo( $('#logo-sidebar') );
			corporate_logo.attr('id', '');
			BE.content.removeClass('no-bg');
			BE.getSidebarNav()
			.html( sidebar )
			.css({
				opacity: 1,
				left: 0
			}, 1000);
		});
		
	}
	
	,slideCorporateInService : function(){
		
		$('#logo-sidebar').animate({
			left: '-170px'
		}, 1000, 'easeOutQuad');
		
		BE.getSidebarNav().animate({
			left: '-170px'
		}, 1000, 'easeOutQuad', function(){
			BE.getSidebarNav().css('opacity', 1);
		});
		
		$('#corporate-logo').animate({
			left: 0
		}, 1000, 'easeOutQuad', function(){
			$('#logo-sidebar').css('left', 0).empty();
			corporate_logo.appendTo( $('#logo-sidebar') );
		});
		
		BE.getSidebarNav().animate({
			left: 0
		}, 1000, 'easeOutQuad');
		
	}
	
	,setComponents : function(alias, data) {
		
		BE.removeGallery();
		BE.removeLaunches();
		
		// Page title
		
		$.address.title(data.pagetitle);
		
		// Google Analytics tracking

		if ( alias == '/' || alias == '' ) {
			BE.trackPage( '/' + context + '/' );
		} else {
			BE.trackPage( '/' + context + '/' + alias );
		}
		
		// Background image
		
		BE.setBackground( data.background );
		
		
		// Header
		
		BE.header.html( data.header );
		
		
		// Sidebar navigation
		
		//BE.sidebar_nav.html( data.sidebar ).css('opacity', 0);
		
		
		// Panel
		
		BE.getPanel().remove();
		$(data.panel).appendTo( BE.getContent() );
		
		
		// Launches
		
		if ( data.launches != '' ) BE.setLaunches( data.launches );
		BE.getLaunches().delay(1000).animate({
			opacity: 1
		}, 1000, function(){
						if (BE.browser.msie) {
	                        this.style.removeAttribute('filter');
	                    }
					});
		
		
		// Section logo
					
		if ( data.logo != '' ) BE.setLogo( data.logo );
		BE.getLogo().animate({
			opacity: 1
		}, 1000	, function(){
			if (BE.browser.msie) {
				this.style.removeAttribute('filter');
			}
		});
		
		
		// Gallery
		
		$(data.gallery).appendTo( BE.getBody() );
		BE.setUpGallery();
		
		
		// Footer
		
		BE.footer.html( data.footer );
		
		
		BE.resizeScroller();
		
		var $top = parseInt($(window).height()-BE.getPanel().outerHeight()-70);
		var $left = ($(window).width() / 2) + (($('#header-content').width()/2) - BE.getPanel().outerWidth());

		if ( BE.getPanel().hasClass('no-bg') || BE.getPanel().hasClass('form') ) {
			$left = $('#header-content').position().left+14;
		}

		if ( BE.getPanel().hasClass('offers') || BE.getPanel().hasClass('offer') || BE.getPanel().hasClass('press') || BE.getPanel().hasClass('news') ) {
			$left = ($(window).width() / 2)-14;
		}
		
		if ( BE.getPanel().hasClass('press') ) {
			$left = ($(window).width() / 2)-487;
		}

		BE.getPanel().css({
			top: $top,
			left: $left
		});
		
		var $panel = BE.getPanel();
		
		$(window).bind('resize', function(){
			
			BE.getLogo().css('left', ($(window).width()/2) - 480);
			
			if ( BE.getPanel().hasClass('no-bg') || BE.getPanel().hasClass('form') ) {
				BE.getPanel().css({
					top: parseInt($(window).height()-BE.getPanel().outerHeight()-70),
					left: ( $panel.is('.hidden') ) ? $(window).width() - 23 : $('#header-content').position().left+14
				});
				$save_left = $('#header-content').position().left+14;
			} else if ( BE.getPanel().hasClass('press') ) {
				BE.getPanel().css({
					top: parseInt($(window).height()-BE.getPanel().outerHeight()-70),
					left: ( $panel.is('.hidden') ) ? $(window).width() - 23 : $('#header-content').position().left
				});
				$save_left = $('#header-content').position().left;
			} else {
				BE.getPanel().css({
					top: parseInt($(window).height()-BE.getPanel().outerHeight()-70),
					left: ( $panel.is('.hidden') ) ? $(window).width() - 23 : ($(window).width() / 2) + (($('#header-content').width()/2) - BE.getPanel().outerWidth())
				});
				$save_left = ($(window).width() / 2) + (($('#header-content').width()/2) - BE.getPanel().outerWidth());
			}

		});
		
		if ( BE.isReady ) {
			BE.loadBackground(function(){
				BE.showBackground(1000, BE.stopSiteLoading());
			})
		}
		
		BE.isReady = true;
		
    }
	
	,slidePanels : function(toppx) {
		
		$('#second-nav').css('position', 'static');
		
		$('.to-hide').animate({
			top: -( $('.to-hide').outerHeight() + parseInt( $('.to-hide').css('margin-top') ) )
		}, 1000, 'easeInOutQuad', function(){
			$(this).remove();
			BE.resizeScroller();
			
			var $top = parseInt($(window).height()-($('.to-show').outerHeight()+70));
			var $left = ($(window).width() / 2) + (($('#header-content').width()/2) - BE.getPanel().outerWidth());
			
			var $to_show = $('.to-show');
			
			if ( $to_show.hasClass('no-bg') || $('.to-show').hasClass('form') ) {
				$left = $('#header-content').position().left+14;
			}
			
			if ( $to_show.hasClass('press') ) {
				$left = $('#header-content').position().left;
			}
			
			if ( $('.to-show').hasClass('offers') || $('.to-show').hasClass('offer') || $('.to-show').hasClass('news') ) {
				$left = ($(window).width() / 2)-14;
			}
			
			$('.to-show').css('left', $left);
			
			if ( !$('.to-show').hasClass('map') ) {
				BE.showBackground(0, function(){
					$('#gmap').remove();
				});
			} else {
				initialize();
				$('.bg-mask, #backstretch').remove();
			}
			$('.to-show').animate({
				top: $top
			}, 1000, 'easeInOutQuad', function(){
					$(this).removeClass('to-show');
					BE.stopPageLoading();
					var $panel = BE.getPanel();
					
					$(window).bind('resize', function(){
						
						BE.getLogo().css('left', ($(window).width()/2) - 480);
						
						if ( BE.getPanel().hasClass('no-bg') || BE.getPanel().hasClass('form') ) {
							BE.getPanel().css({
								top: parseInt($(window).height()-BE.getPanel().outerHeight()-70),
								left: ( $panel.is('.hidden') ) ? $(window).width() - 23 : $('#header-content').position().left+14
							});
							$save_left = $('#header-content').position().left+14;
						} else if ( BE.getPanel().hasClass('press') ) {
							BE.getPanel().css({
								top: parseInt($(window).height()-BE.getPanel().outerHeight()-70),
								left: ( $panel.is('.hidden') ) ? $(window).width() - 23 : $('#header-content').position().left
							});
							$save_left = $('#header-content').position().left;
						} else {
							BE.getPanel().css({
								top: parseInt($(window).height()-BE.getPanel().outerHeight()-70),
								left: ( $panel.is('.hidden') ) ? $(window).width() - 23 : ($(window).width() / 2) + (($('#header-content').width()/2) - BE.getPanel().outerWidth())
							});
							$save_left = ($(window).width() / 2) + (($('#header-content').width()/2) - BE.getPanel().outerWidth());
						}
						
					});
			});
			
		});
		
	}
	
	,updateGalleryCategory : function() {
		
		$('#gal-prev, #gal-next').remove();
		$('#gal-thumbs').css('top', 0);
		
	    if ($('#gal-thumbs li').size() > 3) {
			
	        $top = $('#gal-thumbs li').height() + 10;
			
	        $('#gal-thumbs').css('height', $('#gal-thumbs li').size() * ($('#gal-thumbs li').height() + 10));
	        $totalHeight = $('#gal-thumbs').height();
			
	        $btn_prev = $('<a id="gal-prev" title=""></a>')
	        .appendTo('#gal-thumbs-container')
	        .css({
	            visibility: 'hidden'
	        }).click(function(){
				BE.showPrev();
			});

	        $btn_next = $('<a id="gal-next" title=""></a>')
	        .appendTo('#gal-thumbs-container')
	        .css({
	            visibility: 'visible'
	        }).click(function(){
				BE.showNext();
			});
	    }
		
	}
	
	,updatePageGalleryCategory : function() {
		
		$('#page-gal-prev, #page-gal-next').remove();
		$('#page-gal-thumbs').css('top', 0);
		
	    if ($('#page-gal-thumbs li').size() > 3) {
			
	        $top_page = $('#page-gal-thumbs li').height() + 10;
			
	        $('#page-gal-thumbs').css('height', $('#page-gal-thumbs li').size() * ($('#page-gal-thumbs li').height() + 10));
	        $totalHeight = $('#page-gal-thumbs').height();
			
	        $btn_page_prev = $('<a id="page-gal-prev" title=""></a>')
	        .appendTo('#page-gal-thumbs-container')
	        .css({
	            visibility: 'hidden'
	        }).click(function(){
				BE.showPagePrev();
			});

	        $btn_page_next = $('<a id="page-gal-next" title=""></a>')
	        .appendTo('#page-gal-thumbs-container')
	        .css({
	            visibility: 'visible'
	        }).click(function(){
				BE.showPageNext();
			});
	    }
		
	}
	
	,setUpGallery : function() {
		
		if ( $('#gal-navigation ul li').size() > 0 && $('#gal-navigation ul li.active').length == 0 ) {
			$('#gal-navigation ul li:first').addClass('active');
		}
		
		BE.updateGalleryCategory();
		
		$('#main-nav a.gallery').live('click',
	    function() {
			BE.viewMainGallery();
	        return false;
	    });
	
		$('#gal-close').live('click',
	    function() {
			BE.closeGallery();
	        return false;
	    });
		
		$('#gal-navigation a').unbind('click').bind('click', function() {

			if (typeof slidetimer != 'undefined') clearInterval(slidetimer);
			
	        separator = $(this).attr('id').indexOf('-') + 1;
	        gal_id = $(this).attr('id').substring(separator);
	        clicked = $(this);

	        $.ajax({
	            url: '/ajax-backend/get-gallery.php?id=' + gal_id,
	            dataType: 'html',
	            success: function(data, textStatus, xhr) {
					$('#gallery').css('height', $('#gallery').height());
	                $('#gal-thumbs-container').fadeOut(500, function() {
	                    $('#gal-thumbs').html(data);
	                    BE.updateGalleryCategory();
	
						$('#gal-thumbs a').unbind('click').bind('click', function(evt) {
					        BE.loadImage( $(this) );
					        //evt.stopPropagation();
					        return false;
					    });
	
	                    $('#gal-thumbs img').load(function() {
	                        $('#gal-thumbs-container').fadeIn(500, function() {
	                            clicked.parent().siblings().removeClass('active');
	                            clicked.parent().addClass('active');
	                        });
	                    });
	                });
	            }
	        });
			
	        return false;

	    });

	    $('#gal-thumbs a').unbind('click').bind('click', function(evt) {
	        BE.loadImage( $(this) );
	        //evt.stopPropagation();
	        return false;
	    });
		
	}
	
	,setUpPageGallery : function() {
		
		if ( $('#page-gal-navigation ul li').size() > 0 && $('#page-gal-navigation ul li.active').length == 0 ) {
			$('#page-gal-navigation ul li:first').addClass('active');
		}
		
		BE.updatePageGalleryCategory();
	
		$('#page-gal-close').live('click',
	    function() {
			BE.closePageGallery();
	        return false;
	    });
		
		$('#page-gal-navigation a').unbind('click').bind('click', function() {

			if (typeof slidetimer != 'undefined') clearInterval(slidetimer);
			
	        separator = $(this).attr('id').indexOf('-') + 1;
	        gal_id = $(this).attr('id').substring(separator);
	        clicked = $(this);

	        $.ajax({
	            url: '/ajax-backend/get-gallery.php?id=' + gal_id,
	            dataType: 'html',
	            success: function(data, textStatus, xhr) {
					$('#page-gallery').css('height', $('#page-gallery').height());
	                $('#page-gal-thumbs-container').fadeOut(500, function() {
	                    $('#page-gal-thumbs').html(data);
	                    BE.updateGalleryCategory();
	
						$('#page-gal-thumbs a').unbind('click').bind('click', function(evt) {
					        BE.loadImage( $(this) );
					        //evt.stopPropagation();
					        return false;
					    });
	
	                    $('#page-gal-thumbs img').load(function() {
	                        $('#page-gal-thumbs-container').fadeIn(500, function() {
	                            clicked.parent().siblings().removeClass('active');
	                            clicked.parent().addClass('active');
	                        });
	                    });
	                });
	            }
	        });
			
	        return false;

	    });

	    $('#page-gal-thumbs a').unbind('click').bind('click', function(evt) {
	        BE.loadImage( $(this) );
	        //evt.stopPropagation();
	        return false;
	    });
		
	}
	
	,viewMainGallery : function() {
		
		BE.getGallery().animate({
            right: 20
        }, 1000, 'easeInOutQuad');
		
        BE.getHeader().animate({
            top: -(BE.getHeader().outerHeight())
        }, 1000, 'easeInOutQuad');
		
		BE.getPanel().fadeOut(1000);
		BE.getLaunches().fadeOut(1000);
		
		BE.getFooter().animate({
            bottom: -(BE.getFooter().outerHeight())
        }, 1000, 'easeInOutQuad');
		
	}
	
	,viewPageGallery : function() {
		
		BE.getPageGallery().animate({
            right: 20
        }, 1000, 'easeInOutQuad');
		
        BE.getHeader().animate({
            top: -(BE.getHeader().outerHeight())
        }, 1000, 'easeInOutQuad');
		
		BE.getPanel().fadeOut(1000);
		BE.getLaunches().fadeOut(1000);
		
		BE.getFooter().animate({
            bottom: -(BE.getFooter().outerHeight())
        }, 1000, 'easeInOutQuad', function(){
			BE.stopPageLoading();
		});
		
	}
	
	,retrievePageGallery : function(alias) {
		
		BE.startPageLoading();
		
		$.ajax({
            url: '/ajax-backend/get-page-gallery.php?alias=' + alias + '&context=' + arrContexts[context],
            dataType: 'html',
            success: function(data, textStatus, xhr) {
				
				if ( $('#page-gallery-container').length > 0 ) $('#page-gallery-container').remove();
				
				$(data).appendTo( BE.getBody() );
				BE.setUpPageGallery();
				BE.viewPageGallery();
				
            }
        });
		
	}
	
	,closeGallery : function() {
		
		BE.getGallery().animate({
            right: -140
        }, 1000, 'easeInOutQuad');
		
        BE.getHeader().animate({
            top: 0
        }, 1000, 'easeInOutQuad');
		
		BE.getPanel().fadeIn(1000);
		BE.getLaunches().fadeIn(1000);
		
		BE.getFooter().animate({
            bottom: 0
        }, 1000, 'easeInOutQuad');
		
	}
	
	,closePageGallery : function() {
		
		BE.getPageGallery().animate({
            right: -140
        }, 1000, 'easeInOutQuad');
		
        BE.getHeader().animate({
            top: 0
        }, 1000, 'easeInOutQuad');
		
		BE.getPanel().fadeIn(1000);
		BE.getLaunches().fadeIn(1000);
		
		BE.getFooter().animate({
            bottom: 0
        }, 1000, 'easeInOutQuad');
		
	}
	
	,showPrev : function() {
	    $galleryTop = $("#gal-thumbs").position().top;
	    $shift = $galleryTop + ($top * 3);
	    if ($galleryTop < 0) {
	        $("#gal-thumbs").animate({
	            top: $shift
	        },
	        300,
	        function() {
	            $btn_next.css('visibility', 'visible');
	            if ($shift == 0)
	            $btn_prev.css('visibility', 'hidden');
	        });
	    }
	}

	,showNext : function() {
	    $shift = $("#gal-thumbs").position().top - ($top * 3);
	    if (Math.abs($shift) < $totalHeight) {
	        $("#gal-thumbs").animate({
	            top: $shift
	        },
	        300,
	        function() {
	            $btn_prev.css('visibility', 'visible');
	            if (($totalHeight - Math.abs($shift)) <= ($top * 3))
	            $btn_next.css('visibility', 'hidden');
	        });
	    }
	}
	
	,showPagePrev : function() {
	    $galleryTop = $("#page-gal-thumbs").position().top;
	    $shift_page = $galleryTop + ($top_page * 3);
	    if ($galleryTop < 0) {
	        $("#page-gal-thumbs").animate({
	            top: $shift_page
	        },
	        300,
	        function() {
	            $btn_page_next.css('visibility', 'visible');
	            if ($shift_page == 0)
	            $btn_page_prev.css('visibility', 'hidden');
	        });
	    }
	}

	,showPageNext : function() {
	    $shift_page = $("#page-gal-thumbs").position().top - ($top_page * 3);
	    if (Math.abs($shift_page) < $totalHeight) {
	        $("#page-gal-thumbs").animate({
	            top: $shift_page
	        },
	        300,
	        function() {
	            $btn_page_prev.css('visibility', 'visible');
	            if (($totalHeight - Math.abs($shift_page)) <= ($top_page * 3))
	            $btn_page_next.css('visibility', 'hidden');
	        });
	    }
	}

	,loading : function() {
	    if (position == 5) {
	        $('#loading').css('background-position', '0 0');
	        position = 1;
	    } else {
	        $('#loading').css('background-position', '0 ' + -position * 40 + 'px');
	        position++;
	    }
	}
	
	,loadingPage : function() {
	    if (position_page == 5) {
	        $('#loading-page').css('background-position', '0 0');
	        position_page = 1;
	    } else {
	        $('#loading-page').css('background-position', '0 ' + -position_page * 40 + 'px');
	        position_page++;
	    }
	}

	,loadImage : function(obj) {

		if (typeof timer != 'undefined') clearInterval(timer);
		$('#loading').remove();

	    $to_remove = $('#backstretch img');

	    $('<div id="loading" />').css({
	        position: 'absolute',
	        top: (($(window).height() - 120) / 2) - 20,
	        left: ($(window).width() / 2) - 20,
	        width: 41,
	        height: 40,
	        backgroundImage: "url('/assets/i/loading-page.png')",
	        zIndex: 2
	    })
	    .appendTo('#backstretch');

	    position = 1;
	    timer = setInterval('BE.loading()', 100);

	    img = $('<img class="gal-current" />').css({
	        position: "absolute",
	        top: 0,
	        left: 0,
	        opacity: 0
	    })
	    .bind("load",
	    function(e) {

			$('#loading').css('z-index', '0').remove();
	        clearTimeout(timer);
	        var self = $(this);
	        imgRatio = $(e.target).width() / $(e.target).height();

			obj.parent().siblings().find('a').removeClass('active');
			obj.addClass('active');

	        BE._adjustBGGallery(function() {
	            self.animate({
	                opacity: 1
	            },
	            500,
	            function() {
	                self.css('position', 'relative');
	                self.removeClass('gal-current');
	                $to_remove.remove();
	                if (typeof callback == "function") callback();
	            });
	        });
	    })
	    .appendTo($('#backstretch'));
	    img.attr("src", obj.attr('href'));
	    // Hack for IE img onload event
	    $(window).resize(BE._adjustBGGallery);
	    // Adjust the background size when the window is resized or orientation has changed (iOS)
	}
	
	,_adjustBGIntro : function(fn) {
	    try {
	        bgCSS = {
	            left: 0,
	            top: 0
	        }
	        bgWidth = rootElement.width();
	        bgHeight = bgWidth / imgRatio;

	        // Make adjustments based on image ratio
	        // Note: Offset code provided by Peter Baker (http://ptrbkr.com/). Thanks, Peter!
	        if (bgHeight >= rootElement.height()) {
	            bgOffset = (bgHeight - rootElement.height()) / 2;
	            bgCSS = {top: "-" + bgOffset + "px"};
	        } else {
	            bgHeight = rootElement.height();
	            bgWidth = bgHeight * imgRatio;
	            bgOffset = (bgWidth - rootElement.width()) / 2;
	            bgCSS = {
	                left: "-" + bgOffset + "px"
	            };
	        }
	        if ($("#backstretch img").length) {
	            $("#backstretch img").width(bgWidth).height(bgHeight).css(bgCSS);
	        } else {
	            $("#backstretch img").width(bgWidth).height(bgHeight).css(bgCSS);
	        }
	    } catch(err) {
	        // IE7 seems to trigger _adjustBG before the image is loaded.
	        // This try/catch block is a hack to let it fail gracefully.
	        }

	    // Executed the passed in function, if necessary
	    if (typeof fn == "function") fn();
	}
	
	,_adjustBGGallery : function(fn) {
	    try {
	        bgCSS = {left: 0, top: 0}
			                bgWidth = rootElement.width();
			                bgHeight = bgWidth / imgRatio;

			                // Make adjustments based on image ratio
			                // Note: Offset code provided by Peter Baker (http://ptrbkr.com/). Thanks, Peter!
			                if(bgHeight >= rootElement.height()) {
			                    bgOffset = (bgHeight - rootElement.height()) /2;
			                    $.extend(bgCSS, {top: "-" + bgOffset + "px"});
			                } else {
			                    bgHeight = rootElement.height();
			                    bgWidth = bgHeight * imgRatio;
			                    bgOffset = (bgWidth - rootElement.width()) / 2;
			                    $.extend(bgCSS, {left: "-" + bgOffset + "px"});
			                }
	        if ($("#backstretch img.gal-current").length) {
	            $("#backstretch img.gal-current").width(bgWidth).height(bgHeight).css(bgCSS);
	        } else {
	            $("#backstretch img").width(bgWidth).height(bgHeight).css(bgCSS);
	        }
	    } catch(err) {
	        // IE7 seems to trigger _adjustBG before the image is loaded.
	        // This try/catch block is a hack to let it fail gracefully.
	        }

	    // Executed the passed in function, if necessary
	    if (typeof fn == "function") fn();
	}
	
	,resizeContent : function() {
		
		var content_height = BE.body.height() - (BE.header.outerHeight() + BE.footer.outerHeight());
		
		BE.content.css('height', content_height);
		
		$.each([BE.sidebar, BE.panel], function(index, el){
			$(el).css('height', content_height - (parseInt($(el).css('padding-top'))+parseInt($(el).css('padding-bottom'))));
		});
		
		if ( $('#launches').length > 0 ) $('#launches').css('bottom', BE.footer.outerHeight()-.5);
		if ( $('#collection-map').length > 0 ) $('#collection-map').css('height', content_height - (parseInt($('#collection-map').css('padding-top'))+parseInt($('#collection-map').css('padding-bottom'))));
		
		BE.sidebar_nav.css('height', BE.sidebar.height()-170);
		
		BE.getScroller().css('height', BE.panel.height());
		
		BE.resizeScroller();
		
	}
	
	,setBackground : function(img) {
		BE.background = img;
	}
	
	,showBackground : function(delay, callback) {
		
		mask.delay(delay).animate({
			width: bgWidth
		}, 1000, 'easeInOutQuad', function(){
			$(".bg-mask img")
			.css({
				position: 'relative'
			});
			mask.css({
				top: 0,
				left: 0,
				width: 'auto',
				height: 'auto',
				zIndex: '-9999',
				position: 'fixed'
			});
			$('#backstretch').remove();
			mask.attr('id', 'backstretch').removeClass('bg-mask');
			if (callback != null) callback();
		});
	}
	
	,loadBackground : function(callback) {
		
		var bg = BE.background;
		
		$to_remove = $('#backstretch > img');
		
		mask = $('<div class="bg-mask" />').css({
			width: 0,
			overflow: 'hidden',
			position: 'absolute',
			top: 0,
			left: 0,
			height: '100%'
	    }).prependTo( BE.getBody() );
		
	    var img = $('<img />').css({
	        position: 'absolute',
	        top: 0,
	        left: 0,
			display: 'block'
	    })
	    .bind("load",
	    function(e) {
			$('#loading-page').css('z-index', '0').remove();
	        if ( typeof timer_page != 'undefined' ) clearTimeout(timer_page);
	        var self = $(this);
	        imgRatio = $(e.target).width() / $(e.target).height();

	        BE._adjustBG(function() {
				callback();
	        });
	    })
	    .appendTo(mask);
	    img.attr("src", bg);
	    // Hack for IE img onload event
	    $(window).resize(BE._adjustBG);
	}
	
	,_adjustBG : function(fn) {
		
	    try {
	        bgCSS = {left: 0, top: 0}
			                bgWidth = rootElement.width();
			                bgHeight = bgWidth / imgRatio;

			                // Make adjustments based on image ratio
			                // Note: Offset code provided by Peter Baker (http://ptrbkr.com/). Thanks, Peter!
			                if(bgHeight >= rootElement.height()) {
			                    bgOffset = (bgHeight - rootElement.height()) /2;
			                    $.extend(bgCSS, {top: "-" + bgOffset + "px"});
			                } else {
			                    bgHeight = rootElement.height();
			                    bgWidth = bgHeight * imgRatio;
			                    bgOffset = (bgWidth - rootElement.width()) / 2;
			                    $.extend(bgCSS, {left: "-" + bgOffset + "px"});
			                }
	
	        if ( $(".bg-mask").length == 0 ) {
				$("#backstretch img").width( bgWidth ).height( bgHeight ).css(bgCSS);
			}
			
			if ( $(".bg-mask").length > 0 ) {
				$(".bg-mask img").width( bgWidth ).height( bgHeight ).css(bgCSS);
			}
			
	    } catch(err) {
	        // IE7 seems to trigger _adjustBG before the image is loaded.
	        // This try/catch block is a hack to let it fail gracefully.
	        }

	    // Executed the passed in function, if necessary
	    if (typeof fn == "function") fn();
	}
	
	,resizeScroller : function() {
		
		if ( BE.getScroller().length == 0 ) return;
		
        BE.getScroller().jScrollPane({
            verticalGutter: 12,
            animateScroll: true
        });

        /*var api = BE.getScroller().data('jsp');

        var throttleTimeout;
        $(window).bind(
        'resize',
        function() {
            if ($.browser.msie) {
                // IE fires multiple resize events while you are dragging the browser window which
                // causes it to crash if you try to update the scrollpane on every one. So we need
                // to throttle it to fire a maximum of once every 50 milliseconds...
                if (!throttleTimeout) {
                    throttleTimeout = setTimeout(
                    function()
                    {
                        api.reinitialise();
                        throttleTimeout = null;
                    },
                    50
                    );
                }
            } else {
                api.reinitialise();
            }
        });*/

    }
	
	,removeGallery : function() {
		if ( BE.getGallery().length > 0 ) {
			BE.getGallery().remove();
		}
	}
	
	,removeLaunches : function() {
		if ( BE.getLaunches().length > 0 ) {
			BE.getLaunches().animate({
				opacity: 0
			}, 500, function(){
				BE.getLaunches().remove();
			});
		}
	}
	
	,removeLogo : function() {
		if ( BE.getLogo().length > 0 ) {
			BE.getLogo().animate({
				opacity: 0
			}, 500, function(){
				BE.getLogo().remove();
			});
		}
	}
	
	,setLaunches : function(launches) {
		$(launches)
		.css('opacity', 0)
		.appendTo( BE.getBody() );
	}
	
	,setLogo : function(logo) {
		$('<img id="section_logo" src="' + logo + '" alt="">')
		.css({
			opacity: 0,
			position: 'absolute',
			left: ($(window).width()/2) - 480
		})
		.appendTo( BE.getBody() );
	}
	
	,trackPage : function(alias) {
		setTimeout(function(){
			_gaq.push(['_trackPageview', alias]);
		}, 500);
	}
	
	,readcookie : function(name) {
	    var re = new RegExp("(?:^| )" + name + "=([^;]*)", "i");
	    var matches = document.cookie.match(re);
	    return matches && matches.length == 2 ? matches[1] : null;
	}

	,savecookie : function(name, value, days) {
	    var date = new Date;
	    date.setTime(date.getTime() + (typeof days != "undefined" ? days : 3) * 24 * 60 * 60 * 1000);
	    var expires = "; expires=" + date.toGMTString();
	    document.cookie = name + ("=" + value + expires + "; path=/; domain=" + document.domain);
	}
	
}

$(document).ready(function(){
	
	BE.browser = $.browser;
	BE.init();
	/*BE.resizeContent();*/
	
});

