(function($) { 
    
	$.fn.initHotelDeals = function(opts) {
	    
	    var defaults = {};
	    
	    var options = $.extend(true, {}, defaults, opts);
		
		return this.each(function() { 
            $(this)
                .bind('change', function() {
                    swap_out_rates_for_deal($(this).val());
                 })
                .attr('selectedIndex', 0);
 
		}); 
	}   
    
    function swap_out_rates_for_deal(evaltext) {
    	try {   
    		var data = $.parseJSON(evaltext),
    		    prefix = data[2],
    		    headerLink = $('#' + prefix + 'dealheader').find('a.deal-link'),
    		    urlRegex = /(_d)\d+(.html)/,
    		    roomID = null,
    		    e = null,
    		    roomLink = null; 
  
    	    headerLink
    	        .html(data[0])
    	        .attr('href', headerLink.attr('href').replace(urlRegex, '$1' + data[1] + '$2'));
    				
    		$('#' + prefix + 'hotelDealID').val(data[1]);
    		
    		roomID = data[2].split('-')[1];
    		roomLink = $('#rm-' + roomID); 

    		if(roomLink.size() > 0) {   
    			roomLink.attr('href', roomLink.attr('href').replace(urlRegex, '$1' + data[1] + '$2'));   
    	    }

    	    $("td[id^='" + prefix + "rate']").each(function(i, elm) {  
    	        $(elm).html(data[3][i]); 
    	    });
    	}
    	catch(error) {
    	    return false;
    	}
    }

})(jQuery);