/*!
 * Script deals with adding and removing items from personal lists
 * Date: Aug 12
 */
      
     function updateCounter(action, id)
     {
        var personalListCtrl = $("#personalList");
       var counterCtrl = $("#counter"); 
       var counterLink = $("#counterLink");
       var items = personalListCtrl.val(); 
       if (items == null)
            items = "";
       //   
        var personalListSize = 0;
       //
        if (action == "add")
       {
            items = items + "," + id;
       } 
       else //remove
       {
            items = items.replace(id, "");
       }
       //
       var itemsArray = $.unique(items.split(',').sort());
       //
       $.each(itemsArray, function (index, value) {
           if (value != null && value != "") {
               if (personalListSize <10)
                   personalListSize++; //only 10 last personal events are shown
           }
       });
       //
      personalListCtrl.val(itemsArray); 
      counterCtrl.val(personalListSize);
        //
       if (counterLink != null)  
       {
            counterLink.text("You Have (" + personalListSize + ") Events in Your List"); 
            if (personalListSize < 1)
                counterLink.attr("href", "#");
            else 
                counterLink.attr("href", "PersonalEventList.aspx" + $.query);
        }
     }
     
     function enforceTimeoutAndSubmit()
     {
        $.doTimeout( 1000, function(){
            window.location = "PersonalEventList.aspx" + $.query;
        });
     }

     $(document).ready(function () {

         var keyword = $.query.get("kwd");
         //alert(keyword);
         if (keyword != null && keyword != "") {
             $(".highlighter").highlight(keyword);
         }
         else {
             $(".highlighter").removeHighlight();
         }

         // replace plural with single for personal event count 
         $.doTimeout(200, function () {
             var counterLink = $("#counterLink");
             if (counterLink.text().indexOf("(1) Events") > 0)
                 counterLink.text("You Have (1) Event in Your List");

             return true;

         });

         $(".action").click(function () {

             var ctrl = $(this);
             var id = ctrl.attr("id");
             var actionType = ctrl.text();

             if (actionType.indexOf("Add") > -1) {
                 //
                 ctrl.text("- Remove from List");
                 // 
                 $.get("Event.aspx?action=add&eventID=" + id, function (data) {
                     updateCounter("add", id);
                 });
             }
             else {
                 //
                 ctrl.text("+ Add to List");
                 // 
                 $.get("Event.aspx?action=remove&eventID=" + id, function (data) {
                     updateCounter("remove", id);
                 });
             }

             if (window.location.href.indexOf("PersonalEventList.aspx") > -1)
                 enforceTimeoutAndSubmit();

             return false;

         });

     }); 
