/**
 * This script is supposed to hold all necessary helper functions for the 
 * Compare product section of the Columbia Storefront.
 */ 
var mycompareproduct = (function(jQuery)
{
	return { // <== this curly bracket has
		// dw scope public
		// note, that this code relies on single dialog modals (multi dialog, e.g. modal in modal is not supported)
	
		compare_enabled: function( root ) {
			//alert('compare-enabled');
			jQuery('#flag-for-compare-summary').click();
		},
		
		// app initializations called from jQuery(document).ready at the end of the file
		init: function( root ) {
			//alert("init");
			/* 
			here go all the inits.  One part of it
			is attaching the onClick functions to the buttons and other elements
			*/
			if ( root == null )
			{
				root = 'body ';
			}
			else
			{
				root += ' ';
			}
			
			
			
			//####################################################
			//
			//####################################################
			
			jQuery( root + 'a.compare-product-flag').click(function(){ 
					var flagForCompareSummary = $('#flag-for-compare-now');
					var numProductsFlaggedForComparison=1;
					var maxProductsFlaggableForComparison=3;
				//	debugger;
				
					var options = {
							triggeredObject: this, 
							hookItHereDivId: 'CompareLink'+this.id, 
							callBack: function( options ) { 
									mycompareproduct.init( '#' + options.hookItHereDivId + " " );		
									$('#' + options.hookItHereDivId + " .compare-product-flag").toggle();															
									flagForCompareSummary.load(window.DisplayCompareCountURL);		
									flagForCompareSummary	
					                .css({
					                    'background-position': (numProductsFlaggedForComparison > 1 ? '0 -21px' : '0 0'),
					                    'background-color': (numProductsFlaggedForComparison > 1 ? '#0093D1' : '#8C8C8C')
					                }); 
									jQuery('.compare-product-flag').show();
									jQuery('.compare-product-flag-toggled').show(); 
							} };
					app.ajaxSubmitAndShow( options ); 
					return false; 
			});

			jQuery( root + 'a.compare-product-flag-toggled').click(function(){ 
					var flagForCompareSummary = $('#flag-for-compare-now');
					var numProductsFlaggedForComparison=1;
					var maxProductsFlaggableForComparison=3;
				//    	debugger;
					var options = {
							triggeredObject: this, 
							hookItHereDivId: 'CompareLink'+this.id, 
							callBack: function( options ) { 
									$('#' + options.hookItHereDivId + " .compare-product-flag").toggle();
									mycompareproduct.init( '#' + options.hookItHereDivId + " " );
									flagForCompareSummary.load(window.DisplayCompareCountURL);		
									flagForCompareSummary					                
					                .css({
					                    'background-position': (numProductsFlaggedForComparison > 1 ? '0 -21px' : '0 0'),
					                    'background-color': (numProductsFlaggedForComparison > 1 ? '#0093D1' : '#8C8C8C')
					                }); 
							} };
					app.ajaxSubmitAndShow( options ); 
					return false; 
			});
			

			jQuery( root + '#flag-for-compare-summary').click(function(){ 
						var options = {
								triggeredObject: this, 
								hookItHereDivId: 'enableCompareDiv', 
								callBack: function( options ) { 
										mycompareproduct.init( '#' + options.hookItHereDivId + " " );
										jQuery('.compare-product-flag').show();
										jQuery('.compare-product-flag-toggled').show();  
								} };
						app.ajaxSubmitAndShow( options ); 
						return false; 
			});
			
			jQuery( root + '#flag-for-compare-hide').click(function(){ 
						var options = {
								triggeredObject: this, 
								hookItHereDivId: 'enableCompareDiv', 
								callBack: function( options ) { 
										mycompareproduct.init( '#' + options.hookItHereDivId + " " );	
										jQuery('.compare-product-flag').hide();
										jQuery('.compare-product-flag-toggled').hide();  
								} };
						app.ajaxSubmitAndShow( options ); 
						return false; 
			});
			
			
		}
	}
}
)(jQuery);

jQuery(document).ready(function(){
	mycompareproduct.init();
});
