var gs = {
	URL: '',
	curPage: '',
	msie: { 'is_browser' : false, 'version' : '0' },
	contentBGImg: { 'loaded': 0, 'num': 0, 'timeout': null },
	findHotelTimer: null,
	
	initialize: function(object){
		this.URL = '';
		
		for(var key in object){
			this[key] = object[key];
		}
		
		if($.browser.msie){
			this.msie.is_browser = true;
			this.msie.version = $.browser.version;
		}
				
		//Register Ajax Error Event
		$.ajaxSetup({
			error:function(x,e){
				Sexy.error('<h1 style="background:none;height:auto;font-weight:bold;font-size:14px;text-indent:0;">Hmmm....we got trouble here!</h1><p>Something wrong while requesting.</p><p>XHR Status : ' + x.status + '</p>');
			}
		});

		this.prepActions();	
	},
	prepActions: function(){
		/*---------------------------------------*/
		this.generalActions();
		
		/*---------------------------------------*/
		this.styling();
	},
	styling: function(){
		if( $('.content-wrapper').length <= 0 )
			return;
		
		$('.content-wrapper .bottom .overlay').css({ 'opacity': 0.75 });
	},
	contentBGLoad: function(){
		gs.contentBGImg.loaded++;
				
		if( gs.contentBGImg.loaded == gs.contentBGImg.num ){
			$('.content-background div:first').show();
			
			if( $('.content-background div').length > 1 ){
				$('.content-wp .img-pagination').show();
				gs.contentBGImg.timeout = setTimeout( gs.contentBGAnimation, 6000 );
			}
		}
	},
	contentBGAnimation: function(){
		clearTimeout( gs.contentBGImg.timeout );
		if( $('.content-wp .img-pagination').hasClass('animated') )
			return false;

		gs.contentBackgroundAnime( 'auto', true );
		
	},
	contentBackgroundAnime: function( action, timeout, callback ){
		var curImg = parseInt( $('.content-wp .img-pagination').attr('id').replace('i_', '') );
		switch( action )
		{
			case 'prev'	:
				if( curImg == 1 )
					return false;
				curImg -= 1;
				
				$('.content-wp .img-pagination').addClass('animated');
				var temp = $('.content-background #img_' + curImg ).clone().css({ 'opacity' : 0 }).attr('id', 'temp').show();
					$('.content-background').append(temp).find('.img:last').animate({ 'opacity' : 1 }, 'slow', function(){ 
						$('.content-background #img_' + curImg ).remove(); 
						$(this).attr('id', 'img_' + curImg ); 
						$('.content-wp .img-pagination').removeClass('animated');
						
						$('.content-wp .img-pagination').closest('.img-pagination').attr('id', 'i_' + curImg);
						
						clearTimeout( gs.contentBGImg.timeout );
						gs.contentBGImg.timeout = setTimeout( gs.contentBGAnimation, 6000 );
						
						if( typeof( callback ) != 'undefined' )
							callback;
					});
				break;
			case 'next'	:
				if( curImg == $('.content-background .img').length )
					return false;
				curImg += 1;
				
				$('.content-wp .img-pagination').addClass('animated');
				var temp = $('.content-background #img_' + curImg ).clone().css({ 'opacity' : 0 }).attr('id', 'temp').show();
					$('.content-background').append(temp).find('.img:last').animate({ 'opacity' : 1 }, 'slow', function(){
						$('.content-background #img_' + curImg ).remove(); 
						$(this).attr('id', 'img_' + curImg ); 
						$('.content-wp .img-pagination').removeClass('animated'); 
						
						$('.content-wp .img-pagination').closest('.img-pagination').attr('id', 'i_' + curImg);
						
						clearTimeout( gs.contentBGImg.timeout );
						gs.contentBGImg.timeout = setTimeout( gs.contentBGAnimation, 6000 );
						
						if( typeof( callback ) != 'undefined' )
							callback;
					});
				break;
			case 'auto':
				if( curImg == $('.content-background .img').length )
					curImg = 0;
				curImg += 1;
				
				$('.content-wp .img-pagination').addClass('animated');
				var temp = $('.content-background #img_' + curImg ).clone().css({ 'opacity' : 0 }).attr('id', 'temp').show();
					$('.content-background').append(temp).find('.img:last').animate({ 'opacity' : 1 }, 'slow', function(){
						$('.content-background #img_' + curImg ).remove(); 
						$(this).attr('id', 'img_' + curImg ); 
						$('.content-wp .img-pagination').removeClass('animated'); 
						
						$('.content-wp .img-pagination').closest('.img-pagination').attr('id', 'i_' + curImg);
						
						clearTimeout( gs.contentBGImg.timeout );
						gs.contentBGImg.timeout = setTimeout( gs.contentBGAnimation, 6000 );
						
						if( typeof( callback ) != 'undefined' )
							callback;
					});
				break;
			default:
				var toImg = parseInt( action );
				if( curImg == toImg )
					return;
				
				//$('.content-background .img:eq(' + ( toImg - 1 ) + ')').animate({ 'opacity' : 1 }, 'slow', function(){ $('.content-background .active').css({ 'opacity': 0 }); } );
				$('.content-wp .img-pagination').addClass('animated');
				var temp = $('.content-background #img_' + toImg ).clone().css({ 'opacity' : 0 }).attr('id', 'temp').show();
					$('.content-background').append(temp).find('.img:last').animate({ 'opacity' : 1 }, 'slow', function(){
						$('.content-background #img_' + toImg ).remove();
						$(this).attr('id', 'img_' + toImg ); 
						$('.content-wp .img-pagination').removeClass('animated'); 
						
						$('.content-wp .img-pagination').closest('.img-pagination').attr('id', 'i_' + toImg);
						
						clearTimeout( gs.contentBGImg.timeout );
						gs.contentBGImg.timeout = setTimeout( gs.contentBGAnimation, 6000 );
						
						if( typeof( callback ) != 'undefined' )
							callback;
					});
				break;
		}
	},
	generalActions: function(){
		//Content Background
		var bg = $('.content-background div');
		gs.contentBGImg.num = bg.length;
		bg.each(function(){
			var url = $(this).css('background-image');
			url = url.replace("url(\"", "").replace("url(", "").replace("\")", "").replace(")", "");
			
			var newImg = new Image();
			newImg.onLoad = gs.contentBGLoad();
			newImg.src = url;
		});
	
		$('.content-wp .img-pagination a span.overlay').css({ 'opacity': 0.75 });
		$('.content-wp .img-pagination').delegate('a', 'click', function(e){
			e.preventDefault();
			
			if( $('.content-wp .img-pagination').hasClass('animated') )
				return false;
			
			
			gs.contentBackgroundAnime( $(this).attr('rel') );			
		});
		
		/* Hotels */
		$('.find-a-hotel .pagi').delegate('a', 'click', function(e){
			e.preventDefault();
			var self = $(this);
			var container = self.parent('div').prev().find('div:first');
			
			if( parseInt( container.css('margin-left').replace('px','') ) < 0 )
				return;
				
			clearTimeout( gs.findHotelTimer );
			container.stop().animate({ 'margin-left': '-104px' });
			
			gs.findHotelTimer = setTimeout( gs.resetHotels, 3000 );
		});
	},
	resetHotels: function(){
		clearTimeout( gs.findHotelTimer );
		$('.find-a-hotel .hotels-overlay').stop().animate({ 'margin-left': '0px' });
	}
}
