var DOMUtilities = {
	targetBlank: function(locality){
		// XHTML 1.0 Strict work around for external links
		$(locality+' a[rel*="external"]').attr("target","_blank");
	},
	inputAutoClear: function(locality){
		$(locality+' input.clearField').focus(function(){
			if(this.defaultValue == this.value){
				this.value='';
				$(this).addClass('entered');
			}
		}).blur(function(){
			if(this.value == ''){
				this.value = this.defaultValue;
				$(this).removeClass('entered');
			}
		});
	},
	imgRollover: function(locality){
		// Image roll-over setup
		$(locality+' img.rollOver, '+locality+' input[type="image"].rollOver')
			.mouseenter(function(){
				if (this.src.indexOf("_i.") != -1) {
					this.src = this.src.replace("_i.", "_o.");
				}
			}).mouseleave(function(){
				if (this.src.indexOf("_o.") != -1) {
					this.src = this.src.replace("_o.", "_i.");
				}
				if(this.src.indexOf("_a.")) {
					this.src = this.src.replace("_a.","_i.");
				}
			}).filter("input").mousedown(function(){
				this.src = this.src.replace("_o.","_a.");
			}).mouseup(function(){
				this.src = this.src.replace("_a.","_i.");
			});
	},
	initialize: function(locality){
		if(locality == null) {
			locality = "body";
		}
		this.targetBlank(locality);
		this.inputAutoClear(locality);
		this.imgRollover(locality);
	}
};


var HotelPackages = {
    hash: "#key=data[PackagesHome]&val=1&deal=0",
    
    initTandC: function(){
        $( '#link_terms_and_conditions' ).fancybox( {
            width: 500,
            height: 460,
            autoDimensions: false,
            padding: 20
        } );
    },
    
    choosePackage: function(key, val, deal){
        if(typeof(deal) == 'undefined'){
            deal = 0;
        }

        document.location.hash = "key=" + key + "&val=" + val + "&deal=" + deal;
        this.hash = document.location.hash;

        $.ajax({
            url: document.location.pathname,
            type: 'post',
            data: key + "=" + val + "&data[Packages]=" + deal,
            beforeSend: function(){
                $('#promos').append('<div id="promos_mask"></div>');
                $('#promos_mask').css({
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    width: $('#promos').outerWidth(),
                    height: $('#promos').height(),
                    background: "url(/img/hotels/home.dt/loading_large.gif) center center no-repeat #fff",
                    opacity: 0.5
                });
            },
            complete: function(data){
                $('#promos').html(data.responseText);
                DOMUtilities.initialize('#promos');
                Cufon.refresh('.promoItem .content .cufon');
                Cufon.refresh('#joie_ride_bonus .cufon');
                Cufon.refresh('#results_title h2');
                $('#main .dropdowns select, #action_bar .actionBox .content select, #summer_packages .chooser select').each(function(ind,e){
                    for(i=0; i<e.options.length; i++){
                        e.options[i].selected = (i == 0);
                    }
                });
                HotelPackages.initTandC();
            }
        });
    },
    
    checkHash: function(){
        if(document.location.hash != this.hash){
            this.hash = document.location.hash;
            
            var hash_pieces = document.location.hash.replace('#',"").split("&");
            var paramsObj = {};
            var valid = true;
            
            for(var i=0; i<hash_pieces.length; i++){
                var keyVal = hash_pieces[i].split("=");
                paramsObj[keyVal[0]] = keyVal[1];
            }
            
            for(var k in paramsObj){
                if($.inArray(k, ['key','val','deal']) == -1){
                    valid = false;
                }
            }
            
            if(valid == true){
                this.choosePackage(paramsObj.key, paramsObj.val, paramsObj.deal);
            }
        }
    },
    
    initialize: function(){
        var self = this;
        
        $('#main .dropdowns select, #action_bar .actionBox .content select, #summer_packages .chooser select').live('change', function(event){
            var el = this;
            if(this.value != ""){
                var deal = '0';
                if($(this).is('#summer_packages .chooser select')){
                    deal = '1';
                }
                $.scrollTo('#promos', 500, {
            		easing: 'swing',
            		offset: {
            			top: -30
            		},
                    onAfter: function(){
                        self.choosePackage( el.name, el.value, deal );
                    }
            	});
            }
        });
        
        $('#btn_jrb-more-details').live('click', function(event){
            event.preventDefault();
            $('#' + this.href.split('#')[1]).slideDown(500);
            $(this).fadeOut(500);
        });
        
        $('a.backPackages').live('click', function(event){
            event.preventDefault();
            self.choosePackage( 'data[PackagesHome]', '1' );
        });
        
        if(document.location.hash == ""){
            document.location.hash = this.hash;
        }
        this.timer = setInterval('HotelPackages.checkHash()',100);
        
        this.initTandC();
    }
};


var ImageRotator = {
    images: [
        '/img/promos/roadtrippin/header_1.jpg',
        '/img/promos/roadtrippin/header_2.jpg',
        '/img/promos/roadtrippin/header_3.jpg',
        '/img/promos/roadtrippin/header_4.jpg',
        '/img/promos/roadtrippin/header_5.jpg',
        '/img/promos/roadtrippin/header_6.jpg',
        '/img/promos/roadtrippin/header_7.jpg'
    ],
    current: 0,
    speed: 1000,
    delay: 3500,
    
    next: function(){
        var self = this;
        
        this.current = this.current + 1;
        if(this.current == this.images.length){
            this.current = 0;
        }
        
        var img = new Image();
            img.src = this.images[this.current];
            
        img.onload = function(){
            ImageRotator.container.append('<img src="' + img.src + '" alt="" style="position:absolute;top:0;left:0;z-index:1;" />');
            ImageRotator.container.find('img:first').fadeOut(ImageRotator.speed, function(){
                $(this).remove();
                ImageRotator.container.find('img').css({
                    zIndex: 100
                });
                ImageRotator.timer = setTimeout('ImageRotator.next()', ImageRotator.delay);
            });
        }
    },
    
    initialize: function(el){
        var self = this;
        
        this.container = $(el);
        
        this.container.css({
            width: this.container.width(),
            height: this.container.height()
        }).find('img').css({
            position: 'absolute',
            top: 0,
            left: 0,
            zIndex: 100
        });
        
        this.timer = setTimeout('ImageRotator.next()', this.delay);
    }
};


$(document).ready(function(){
	DOMUtilities.initialize();
    HotelPackages.initialize();
    ImageRotator.initialize('#main_images');
    
    $('.actionBox.action2 .btnVideo').fancybox({
        type: 'swf',
        swf: {
            allowFullScreen: 'true',
            allowscriptaccess: 'always'
        },
        width: 640,
        height: 385
    });
});

$(window).load(function(){
    $('#action_bar .twtr-widget .twtr-reference-tweet').remove();
});

