
// ------------------
// Cookwell namespace
// ------------------
var Cookwell = new function() {
	
	// --------------
	// Public Methods
	// --------------
	
	this.initRecipeSearch = function() {
		initRecipeSearchMouseovers();
		initRecipeSearchCheckboxFilters();
		
		$('#searchbar .flyoutfilter').each(function(){
			setRecipeSearchFilterText($(this));
		});
	};
	
	this.initFindAStorePage = function() {
		$('#selStates').change(function() {
			if ($('#selStates').val() != '') {
				document.location = "/Find-A-Store.aspx?state=" + $('#selStates').val();
			} 
		});

		var s;
		s = getParameterByName("state");
		if (s != "") {
			$('#selStates').val(s);
		}
	};

	// ---------------
	// Private Methods
	// ---------------
		
	// Recipe Search
	
	var initRecipeSearchMouseovers = function() {
		$('#searchbar .flyoutfilter').hover(function(){
			$(this).addClass('selected').find('.options').show();
		},function(){
			$(this).removeClass('selected').find('.options').hide();
		});
		
		$('#searchbar .flyoutfilter .options').hover(function(){
			$(this).show().parent('.flyoutfilter').addClass('selected');
		},function(){
			$(this).hide().parent('.flyoutfilter').removeClass('selected');
		});
	};
	
	var initRecipeSearchCheckboxFilters = function() {
		$('#searchbar .flyoutfilter input[type="checkbox"]').change(function(){
			var $filter = $(this).parents('.flyoutfilter').first();
			setRecipeSearchFilterText($filter);
		});
	};
	
	// takes a flyout filter div as input and sets the filter text
	// based on the selected options
	var setRecipeSearchFilterText = function($filter) {
		var str = '';
		$filter.find(':checked').each(function() {
			if (str != '') {
				str += ', ';
			}
			str += $(this).next().html();
		});
		
		if (str == '') {
			var defaultVal = $filter.find('.value').data('default');
			$filter.find('.value').html(defaultVal);
		} else {
			$filter.find('.value').html(str + '&nbsp;&raquo;');
		}
	};
	
	// find a store helper function
	var getParameterByName = function(name) {
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if( results == null )
			return "";
		else
			return decodeURIComponent(results[1].replace(/\+/g, " "));
	}

};

$(document).ready(function()
{
 
    //select the Submit buttons
    
    $('.CartForm [type="submit"]').click(function(e){
        e.preventDefault();
        //get the quantity
        var quantitySel = $(this).parents('.CartForm').first().find('select.quantity').val();
       
        //get the productID
        var ProductID = $(this).parents('.CartForm').first().find('[name="product_id"]').val();
        var URL = "https://cookwell.alice.com/cart/add/" +ProductID+"/"+quantitySel+"?_=1328733907927";

        $('#ProductIframe').attr('src', URL);
                
        //$("a#AAddingItem").trigger('click');        
        $("a#Success").trigger('click');
                
    });
    
    $('#Success').fancybox(); 
     
        $('#CloseBtn button').click(function(e){
            parent.$.fancybox.close();
        });

});

function GoShopping(){
    window.open ('http://cookwell.alice.com');
    parent.$.fancybox.close();
}

