var selectedIngredients = new Array(); // this needs to be pre-populated for cases where we already have something selected

$(document).ready(function() {
   setEventValues();
});


function setEventValues() {    
    setOnloadValues();
    
    $("input[id='ctl00_MainPlaceHolder_ucMailSubscriptions_chkSendNewRecipes']").click(function (){
        for (var oCurrent in selectedIngredients) {
	        if (oCurrent != '') {
	            if(selectedIngredients[oCurrent] == 1) {
	                selectedIngredients[oCurrent] = 0;
	            }
	        }
        }
        
        $("div.ChoosenProductsItem").each(function() {
            $(this).remove();     
        })
    })
    
   $("input[id='ctl00_MainPlaceHolder_ucMailSubscriptions_btnSubmit']").click(function() {
        formSubmit();        
   })
   
   $("input[id='ctl00_MainPlaceHolder_ucMailSubscriptions_btnAdd']").click(function() {
        formSubmit();        
   })
   
   $("input[id='ctl00_MainPlaceHolder_ucMailSubscriptions_btnMyExclusions']").click(function(){
        formSubmit();
   })
   
   $('#ctl00_MainPlaceHolder_ucMailSubscriptions_txtAdd').keydown(function(event) {        
        if(navigator.userAgent.indexOf("MSIE")!=-1){
            if (event.keyCode == 13){
                $('#ctl00_MainPlaceHolder_ucMailSubscriptions_btnAdd').click();
            }
        }
   })

   $("div.ChoosenProductsItem").each(function() {
     selectedIngredients[$(this).find('span').innerHTML] = 1;
   })
 
   $('a.tag').click(function() {
     $("input[id='ctl00_MainPlaceHolder_ucMailSubscriptions_chkSendNewRecipes']").attr('checked', 0);
     var curIngredient = $(this).attr('data');
     if (! selectedIngredients[curIngredient] ) {
       selectedIngredients[curIngredient] = 1;
       
       $("div.ChosenContainer").append( '<div class="ChoosenProductsItem" data="' + $(this).attr('data') + '"><span>' + $(this).attr('data') + '</span><a href="#" class="clearIngredient" data="' + $(this).attr('data') + '"><img src="/Images/icoDelete.png" /></a></div>' );
       setIngrEvents();
     }
     return false;
  })
  setIngrEvents();
}

function formSubmit() {
    var txtHiddenfield = $("input[id='ctl00_MainPlaceHolder_ucMailSubscriptions_txtSelectedProducts']");
        var sText = '';

        for (var oCurrent in selectedIngredients) {            
	        if (oCurrent != 'undefined' && oCurrent != '') {
	            if(selectedIngredients[oCurrent] == 1) {
	                sText += oCurrent + ',';
	            }
	        }
        }

        txtHiddenfield.attr('value', sText);
}

function setIngrEvents() {
    $("a.clearIngredient").unbind('click');
    $("a.clearIngredient").click(function() {   
        if(selectedIngredients[$(this).attr('data')]) {
            selectedIngredients[$(this).attr('data')] = 0;            
            $("div[data='" + $(this).attr('data') + "'][class='ChoosenProductsItem']").remove();
        }
        
    });
}

function setOnloadValues()
{
    var txtHiddenfield = $("input[id='ctl00_MainPlaceHolder_ucMailSubscriptions_txtSelectedProducts']");
    var sText = txtHiddenfield.attr('value');
    if (sText && sText.length > 0)
    {
        var products = sText.split(",");
        for (var oCurrent in products) {
            if(products[oCurrent] && products[oCurrent] != '') {
                selectedIngredients[products[oCurrent]] = 1;
                $("div.ChosenContainer").append( '<div class="ChoosenProductsItem" data="' + products[oCurrent] + '"><span>' + products[oCurrent] + '</span><a href="#" class="clearIngredient" data="' + products[oCurrent] + '"><img src="/Images/icoDelete.png" /></a></div>' );
            }
        }
    }
}



