(function($){

	$.fn.layout = function(method) {
	    if ( methods[method] ) {
	      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
	    } else if (!method) {
	      return methods.set_main_layout.apply( this, arguments );
	    } else {
	      $.error( 'Method ' +  method + ' does not exist on $mwrc.layout' );
	    } 		
	}
	
	var sortNumber = function(a,b){return b - a;};
	
	var methods = {
			set_main_layout: function (options) {
		
				layout_set = false;

				var category_tree = $('#mwrc_featured_brand');
				var main_content_width = $('#mwrc_main_content').width();
				
				if(category_tree.length)
				{
					var second_content_area = $('#mwrc_second_content_area');
					var category_tree_width = category_tree.outerWidth();
					
					// this variable contains any possible margin or padding width.
					// this value needs to be subtracted from the main_content_width along with the product_category_tree width
					var second_content_area_extra = (second_content_area.outerWidth(true) - second_content_area.width());
					var main_content = (main_content_width - category_tree_width - second_content_area_extra);
					
					second_content_area.width(main_content);
				}
				else
				{
					$('#mwrc_second_content_area_full_width').width(main_content_width);
				}

				layout_set = true;
			},
			set_cat_prod_list: function () {
				if(!layout_set) methods.set_main_layout();

				setItemListDefaults(this);

				$(this).each(function(idx, div_cont) {
					
					if(typeof(custom_category_list_layout) != "undefined" && ((custom_category_list_layout == true) && $(div_cont).attr('id') == "mwrc_category_listing")) return;
					if(typeof(custom_product_list_layout) != "undefined" && ((custom_product_list_layout == true) && $(div_cont).attr('id') == "mwrc_product_listing")) return;
					if(typeof(custom_product_search_list_layout) != "undefined" && ((custom_product_search_list_layout == true) && $(div_cont).attr('id') == "mwrc_product_search_listing")) return;

					methods.standardize_boxes.apply($('li.mwrc_listing', div_cont));
				});
				
			},
			set_interested_list: function () {
				if(!layout_set) methods.set_main_layout();
				
				setItemListDefaults(this);
				
				methods.standardize_boxes.apply($('li.mwrc_listing', this));
				
			},
			standardize_boxes: function() {
				
				var maxHeight = 0;
				var maxWidth = 0;
				
				$(this).css('height', 'auto').css('width', 'auto')
				.each(function() {
					if($(this).height() > maxHeight) maxHeight = $(this).height();
					if($(this).width() > maxWidth) maxWidth = $(this).width();
				}).width(maxWidth).height(maxHeight);
			},
			set_product_detail: function () {
				
				if(!layout_set) methods.set_main_layout();
				
				if(typeof(custom_product_layout) != "undefined" && custom_product_layout == true) return;
				
				var main_content_width = (($('#mwrc_second_content_area').length)?$('#mwrc_second_content_area').outerWidth():$('#mwrc_second_content_area_full_width').outerWidth());

				var product_data = $('#mwrc_product_data');
				var product_image_div = $('#mwrc_product_image');
				var product_image = $('#mwrc_product_image img');
				
				product_image_div.width(product_image.outerWidth() + 15);
				
				var product_data_width = (main_content_width - 
					parseInt(product_data.css('borderLeftWidth')) - 
					parseInt(product_data.css('borderRightWidth')) - 
					parseInt(product_data.css('paddingRight')) - 
					parseInt(product_data.css('paddingLeft')));
				
				product_data.width(product_data_width);
				
				var secondary_detail = $('#mwrc_secondary_detail');
				if(secondary_detail.length) ///retailer display
				{		
					var second_content_width = (product_data_width - 
						parseInt(secondary_detail.css('paddingLeft')) - 
						parseInt(secondary_detail.css('paddingRight')));

					secondary_detail.width(second_content_width);
					$('#mwrc_product_details_with_retailers').width(second_content_width - 20);
				}
				
				var product_details = $('#mwrc_product_details'); 
				var product_retailers = ($('#mwrc_product_retailers').length?$('#mwrc_product_retailers'):$('#mwrc_product_details_with_retailers'));
				
				var detail_width = parseInt((product_data.width() - 
					product_image_div.outerWidth() - 
					(product_details.outerWidth(true) - product_details.width()) -
					(product_retailers.outerWidth(true) - product_retailers.width())
					));

				product_details.width(detail_width);
				product_retailers.width(detail_width);
				
				product_details.children('.mwrc_product').width(detail_width - 20);
				product_retailers.children('.mwrc_product').width(detail_width - 20);
				
			},
			set_login_layout: function() {
				if(!layout_set) methods.set_main_layout();
				
				if(typeof(custom_login_layout) != "undefined" && custom_login_layout == true) return;
				
				var main_content_width = (($('#mwrc_second_content_area').length)?$('#mwrc_second_content_area').outerWidth():$('#mwrc_second_content_area_full_width').outerWidth());

				if($('.mwrc_create_account').length)
				{
					$('.mwrc_login, .mwrc_create_account').width(Math.round((main_content_width-35)/2));
				}
			}
	};
	
	var setItemListDefaults = function(div_cont)
	{
		$(div_cont).each(function(i, listing) {
			var maxHeight=0;
			var maxWidth=0;
			
			$('li.mwrc_listing a img, li.mwrc_listing object, li.mwrc_listing .mwrc_no_product_image', listing)
			.each(function(i, obj){
				if($(obj).outerHeight() > maxHeight) maxHeight = $(obj).outerHeight();
				if($(obj).outerWidth() > maxWidth) maxWidth = $(obj).outerWidth();
			});
			
			if(maxWidth < 150 || maxWidth == 0) maxWidth = 150;
			
			$('li.mwrc_listing div.mwrc_listing_product_name, li.mwrc_listing div.mwrc_listing_product_image', listing).each(function(i, div) {	
				$(div).width(maxWidth);
				if($(div).hasClass('mwrc_listing_product_image')) $(div).height(maxHeight);
				if(!$.browser.msie) $(div).css({marginLeft: 'auto', marginRight: 'auto' });
			});
			
			setItemListNamePriceHeight(listing);	
		});
	};

	var setItemListNamePriceHeight = function (listing)
	{
		var maxHeight=0;
		$('.mwrc_name_price_list', listing).css('height', 'auto')
		.each(function(i,obj) {
			if($(obj).outerHeight() > maxHeight) maxHeight = $(obj).outerHeight();
		}).css('height', maxHeight);
	};
	
})($mwrc);

