var md = md ? md : {};

md.menu = {};

jQuery(function()
{
    var containerStyle = '';
    var currentStyle = '';
    var optionsStyle = 'background-color: #fff; border: 1px solid #ccc; margin-top: -2px; padding: 5px; list-style: none;';
    var optionStyle = 'padding: 2px; display: block';
    var optionAnchorStyle = 'text-decoration: none;';
    var ExtraAndHalfhalfStyle = 'color:#666666;float:left;font-size:10px;';
    var chargeText = '';
    if(jQuery('#halfhalfChargeValue').length != 0)
    {
    	chargeText = '   + $' + jQuery('#halfhalfChargeValue')[0].value;
    }
    
    md.menu.replaceSlots = function()
	{
		jQuery('label.dropdown').each(function()
	    {
	    	var label = jQuery(this).remove();
	        var select = jQuery('#' + label.attr('for')).hide();
	        var idSelect = select.attr('id');
	        
	        var replacement = jQuery('<div id="' + idSelect + 'Container" style="float: left;' + containerStyle + '" class="slot"><div id="' + idSelect + 'Current" style="' + currentStyle + '" class="current">' + jQuery(':selected', select).text() + '</div><ul id="' + idSelect + 'Options" style="display: none; position: absolute;' + optionsStyle + '"></ul></div>').hover(function(event)
	        {
	            jQuery('#' + idSelect + 'Options').show();
	            jQuery(this).addClass('active');
	        },
	        function(event)
	        {
	            jQuery('#' + idSelect + 'Options').hide();
	            jQuery(this).removeClass('active');
	        });
	        
	        select.after(replacement);
	        var options = jQuery('#' + idSelect + 'Options');
	        
	        jQuery('option', select).each(function()
	        {
	            var option = jQuery(this);
	            var value = option.val();
	            var text = option.text();
	        
	            var optHtml = jQuery('<li id="' + idSelect + 'Value' + value + '" style="' + optionStyle + '"><a href="" title="select ' + text + '" style="' + optionAnchorStyle + '" class="selectProduct">' + text + '</a><span class="value" style="display: none">' + value + '</span></li>').click(function(event)
	            {
	               select.val(jQuery('span', this).html()).change();
	               jQuery('#' + idSelect + 'Current').text(jQuery('a', this).text());
	               
	               jQuery(this).parent().hide();
	               
	               return false;
	            });
	        
	            options.append(optHtml);
	        });
	    });
    }
    
    md.menu.replaceHalfhalf = function()
	{
		jQuery('label.hhdropdown').each(function()
	    {
	    	var label = jQuery(this).remove();
	        var select = jQuery('#' + label.attr('for')).hide();
	        var idSelect = select.attr('id');
	        
	        currentClass = 'current';
	        currentProductClass='class3 prodName';
	        var chargeV ='';
	        if(select[0].value != 0){
	        	ExtraAndHalfhalfStyle = '';
	        	currentClass=currentProductClass;
	        	chargeV = chargeText;
	        }
	        
	        if(browserFullName == 'IE 7' || browserFullName == 'IE 6'){
	        	optionsStyle += 'margin-left:-0px;';
	        }
	        	
	        var replacement = jQuery('<div id="' + idSelect + 'Container" style="float: left;'+ExtraAndHalfhalfStyle+ + containerStyle + '" class="slot"><div id="' + idSelect + 'Current" style="' + currentStyle + '" class="'+currentClass+'">' + jQuery(':selected', select).text() + chargeV + '</div><ul id="' + idSelect + 'Options" style="display: none; position: absolute;' + optionsStyle + '"></ul></div>').hover(function(event)
	        {
	            jQuery('#' + idSelect + 'Options').show();
	            jQuery(this).addClass('active');
	        },
	        function(event)
	        {
	            jQuery('#' + idSelect + 'Options').hide();
	            jQuery(this).removeClass('active');
	            
	        });
	        
	        select.after(replacement);
	        var options = jQuery('#' + idSelect + 'Options');
	        
	        jQuery('option', select).each(function()
	        {
	            var option = jQuery(this);
	            var value = option.val();
	            if(option.text().toLowerCase().indexOf('?') != -1){
	            	var text = 'no half/half';
	            }else{
	            	var text = option.text();
	            }
	        
	            var optHtml = jQuery('<li id="' + idSelect + 'Value' + value + '" style="' + optionStyle + '"><a href="" title="select ' + text + '" style="' + optionAnchorStyle + '" class="selectProduct">'+ text + '</a><span class="value" style="display: none">' + value + '</span></li>').click(function(event)
	            {
	               select.val(jQuery('span', this).html()).change();
	               jQuery('#' + idSelect + 'Current').text(jQuery('a', this).text());
	               
	               if(select[0].value != 0){
	            	   jQuery('#' + idSelect + 'Current').text(jQuery('a', this).text()+chargeText);
	            	   jQuery('#' + idSelect + 'Current').attr('class',currentProductClass);
	            	   jQuery('#' + idSelect + 'Container').attr('style','float: left;'+containerStyle);
	               }else{
	            	   jQuery('#' + idSelect + 'Current').text('Half/Half?');
	            	   jQuery('#' + idSelect + 'Current').attr('class',currentClass);
	            	   jQuery('#' + idSelect + 'Container').attr('style','float: left;'+ExtraAndHalfhalfStyle+containerStyle);
	               }
	               
	               jQuery(this).parent().hide();
	               
	               return false;
	            });
	        
	            options.append(optHtml);
	        });
	        
	    });
    }
    
    
    md.menu.replaceExtra = function()
	{
		jQuery('div.extraTopping').each(function()
	    {
			var ind = this.id.replace('tprod_','');
	    	jQuery(this).click(function(event){
	    		
	    		var contId = 'topping_'+ind+'Options';
	    		hs.htmlExpand(this, {captionId: contId+'Cap', contentId: contId, dimmingOpacity: 0.75, width: 230, height: 180});
	    		
	    	});
	    });
		
		jQuery('.addToppings').each(function(){
			var thisInd = this.id.replace('addToppings_','');
			jQuery(this).click(function(){
	    		var selectedToppings = new Array();
	    		var selectedToppingsNames = new Array();
	    		
	    		jQuery(':input.tprodOptions_'+thisInd).each(function(){
	    			if(this.checked){
	    				selectedToppings.push(this.value);
	    				selectedToppingsNames.push(this.title);
	    			}
	    		});
	    		
	    		
	    		if(selectedToppings.length == 0){
	    			jQuery('#prodTopp_'+thisInd).attr('value','');
	    		}else{
	    			jQuery('#prodTopp_'+thisInd).attr('value',selectedToppings.join(','));
	    		}
	    		
	    		if(selectedToppingsNames.length == 0){
	    			jQuery('#tprod_'+thisInd).attr('innerHTML','<span style="text-decoration: underline;" onmouseover="this.style.textDecoration=\'none\';" onmouseout="this.style.textDecoration=\'underline\';">Extra Toppings?</span>');
	    		}else{
	    			jQuery('#tprod_'+thisInd).attr('innerHTML','<span style="text-decoration: underline;" onmouseover="this.style.textDecoration=\'none\';" onmouseout="this.style.textDecoration=\'underline\';">Extra Toppings:</span> '+selectedToppingsNames.join(', '));
	    		}
	    		
	    		jQuery('#decoy_tprod_'+thisInd).change();
	    		jQuery('#closeToppings_'+thisInd).click();
	    	});
		});
    }
    
    md.checkEmptySlots = function()
    {
	    jQuery('[name=theSlotDiv]','#freeBasketDeals').each(function(){
	    	var hasHtmlSlot = jQuery('[name="theSlotLi"]',jQuery(this)).children().length;
	    	var hasHtmlExtra = jQuery('[name="theExtraLi"]',jQuery(this)).children().length;
	    	
	    	if(!hasHtmlSlot && !hasHtmlExtra){jQuery(this).remove();}
	    });
    }
    
    md.menu.replaceSlots();
    md.menu.replaceHalfhalf();
    md.menu.replaceExtra();
    md.checkEmptySlots();
});