(function ($) {
var COLLAPSABLE_REGION_FEATURE = {

	openSymbol:  		$('<span class="open-symbol">&nbsp;</span>'),
	colBText:			$(".regionAreaHeader .colB").html(),
	colCText:			$(".regionAreaHeader .colC").html(),
	colDText:			$(".regionAreaHeader .colD").html(),
	captions:			$(".regionAreaCaption").not(".topRegionClass"),
	
	memberInitialization: function() {
			this.openSymbol = $('<span class="open-symbol">&nbsp;</span>');
			this.colBText	= $(".regionAreaHeader .colB").html();
			this.colCText	= $(".regionAreaHeader .colC").html();
			this.colDText	= $(".regionAreaHeader .colD").html();
			this.captions	= $(".regionAreaCaption").not(".topRegionClass").add(".regionAreaCaption1");
		},
		
	initializeElements: function() {
			this.memberInitialization();

			this.openSymbol.prependTo( this.captions );
			
			this.captions.addClass("pointer-cursor");
			
			this.copyHeaderCells();
						
			$(".bestOffer").css("display", "inline");
			
			this.captions.next(".subRegionsContainer").hide();
			
		},
		
	bindEvents:	function() {
			this.captions.click( function (event) {
				
				$(this)
					.next( ".subRegionsContainer" )
					.stop(true, true)
					.slideToggle();
			
				$(this).children(".open-symbol").toggleClass("opened");
				
				if ( $(this).children(".open-symbol").hasClass("opened") ) {
					$(this).children(".colB, .colC, .colD")
						.stop( true, true )
						.fadeIn()
						.css("display", "inline");
						
					$(this).children(".bestOffer").children()
						.stop( true, true )
						.fadeOut();
				} else {
					$(this).children(".colB, .colC, .colD")
						.stop( true, true )
						.fadeOut()
						.css("display", "none");

					$(this).children(".bestOffer").children()
						.stop( true, true )
						.fadeIn()
						.css("display", "inline");
				}
			}); 
			
			
			$(".regionAreaCaption a").click( function(event) {
				event.stopPropagation();
			});	
		},
			
	copyHeaderCells: function () {
			$("<div class='colB'>" + this.colBText + "</div>")
				.appendTo( this.captions )
				.hide();
		
			$("<div class='colC'>" + this.colCText + "</div>")
				.appendTo( this.captions )
				.hide();
			
			$("<div class='colD'>" + this.colDText + "</div>")
				.appendTo( this.captions )
				.hide();
		}
}

    $(function() {
	
	if ( $(".topRegionClass").length > 0 ) {
		COLLAPSABLE_REGION_FEATURE.initializeElements();

		COLLAPSABLE_REGION_FEATURE.bindEvents();
	}
});

})(jQuery);
