var arr_agenda = new Array();

/*

var arr_sorttable = new Array();
*/
/* push is done from progress party_agenda obj
arr_agenda.push( 'member.party.iditem' );
arr_agenda.push( 'main.party.iditem' );
arr_agenda.push( 'bigone.party.iditem' );
*/

var lSortingEnabled = false;
timeDiff.setStartTime() ;




function callmemberagenda( sender, action, iditem )
{
	
	
   for (x in arr_agenda)
       { el = arr_agenda[x] + iditem ;
     	   img = document.getElementById(el);
     	   if (img == null ) continue;
         $( img ).attr('src', cStaticPathSite + '/image/formulier/loading.gif' );
       }
	
   $.ajax({ type: "GET" ,
	        url: cDomain + "/ajax/ajaxpartyagendamember.p", 
	        data: {action: action , iditem: iditem , language: cLanguage },
	        dataType: "xml" ,
          success: function(data){
           
           $(data).find('result').each(function()
            { 
           
            	var cAgenda = $("memberagenda", this).text();
            	var $ui = $(this).find('ui');
              $( '#AgendaMember' ).html( cAgenda );
              
              var $mess = $(this).find('message');
              
              if ( $( $mess ).length  > 0 )
              { var cMessage = $($mess).text();
              	var cType    = $($mess).attr('type' );
              	var cTitle   = $($mess).attr('title' );
              	
              	if (cType == 'alert') 
              	{
             
              		  $("#dialogcontainer").dialog( 'option', 'position', 'center' );
              		  $('#dialogcontainer').dialog( 'option', 'width', 500);	

                  	if ( cTitle == '' )  cTitle = (cLanguage=='en') ? 'Warning' : 'Waarschuwing';
                  	
                  	$('#dialogcontainer').dialog('option', 'title', cTitle  );		
                    $("#dialogcontainer").html( cMessage );
                    $('#dialogcontainer').dialog('option', 'buttons', { "Ok": function() { $(this).dialog("close"); } });
            			  $("#dialogcontainer").dialog( "open" );
            	
              	}	
              	else if ( cMessage != '' ) showmessage( sender , cType, cMessage, true ); 
              }
              
         
              if ( $( $ui ).length  > 0 )
              {
              	
              var qtyvisitors = $($ui).attr('qtyvisitors' );
              el2 = document.getElementById( 'qtyvisitor.id' + iditem ) ;
              if (el2) $( el2 ).text( qtyvisitors );
              	
              var menuname    = $($ui).attr('ag_menuname' );
              var icontitle   = $($ui).attr('ag_icontitle' );
              var iconalt     = $($ui).attr('ag_iconalt' );
              var iconsrc     = cStaticPathSite + $($ui).attr('ag_iconsrc' );

              var iconclass   = $($ui).attr('ag_iconclass' );
              for (x in arr_agenda)
              {
              	   el = arr_agenda[x] + iditem ;
              	   img = document.getElementById(el);
              	   if (img == null ) continue;
           
                   $( img ).attr('src', iconsrc );
                   $( img ).attr('alt', iconalt );
                   $( img ).attr('class', iconclass );                     
                   $( img ).attr('title', icontitle );                                          
                  
                   if ( arr_agenda[x] == 'menu_party_iditem' )
                     $( img ).parents("a:first").find("span.textinlink").text( menuname );
              }

              reportericon( $ui, iditem ); 

             }              
           	}); // each(function)

           // $('#AgendaMember input[type=checkbox]').click( function(e) { UpdateTicketsInPocket( this ); e.stopPropagation();  } ); 
           
           $("#personalagenda tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
           
           agendaOpen();
         	
          } , // succes
          error: function(request,error){ alert( 'Oeps ' + request.statusText  ); }
          });
	
}	

function memberpartyagenda( sender ) 
{
	 var x;
	 var $this = $(sender);
	 
	 i = $this.attr('id').indexOf('party_iditem');
	 
	 if (i == -1) 
	 {// check if it is the anchor in a menu, and handle this as if the icon is clicked
	 	if ($this.get(0).tagName.toLowerCase() == 'a')
	 	{ $this = $this.find("img");
	 		var i = $this.attr('id').indexOf('party_iditem');
	 		if (i == -1) return; 
	 		
	 	}
	 	else return;

	 }
	 
	 action = 'add';
	 
	 if ( $this.hasClass( 'user_green' ) || $this.hasClass( 'bin_closed' ) )
   {action   = 'del';	
   	larchive = false;
   }
   else    	
   if ($this.hasClass( 'date_delete' ) )
   {action   = 'del';	
   	larchive = true;
   }

	 iditem = $this.attr('id').substring( i + 12) ;
   callmemberagenda( sender, action, iditem );
}


function UpdateTicketsInPocket( sender )
{

  $this = $(sender);
  cAction = ( $this.attr("checked") ) ? "sure" : "unsure" ;
  // name TicketIdItem60031
  IdItem = $this.attr("name").substr(12);

  // alert( 'updtick:' + cAction );
  
  $.ajax({ type: "GET" ,
	        url: cDomain + "/ajax/ajaxpartyagendamember.p", 
	        data: { iditem: IdItem, action: cAction  },
	        dataType: "xml" ,
          success: function(data){
          	
           $(data).find('result').each(function()
            { var cChecked = ( $(this).attr('sure') == 'yes' ) ? "sure" : "unsure" ;
              /*
              if (cChecked != cAction)  
              { cNameParty = $this.parents("tr:first").find("a.party").text();
              	alert( 'Huh... database response is not equal to checkbox for ' + cNameParty + ' db=' + cChecked + '/check=' + cAction ); 
              }
              else if ( $(this).attr('sure') == "?" ) alert( 'Huh... response database was unknown' );
              */

              var $mess = $(this).find('message');
              if ( $( $mess ).length  > 0 )
              { var cMessage = $($mess).text();
              	var cType    = $($mess).attr('type' );
            	  showmessage( sender , cType, cMessage, true ); 
              }

           	}); // each(function)
         	
          } , // succes
          //error: function(request,error){ alert( 'Oeps ' + request.statusText  ); alert( request.responsText  ); }
          
          error:function (xhr, ajaxOptions, thrownError){    alert(xhr.status);    alert(xhr.statusText);    alert(thrownError); }
          
          });

} 

function enableSortTable( cPageLayout ) 
{
	
	 if (lSortingEnabled)
	 { 
	 	if (cLanguage=='en') alert( 'Sorting is already enabled' );
	 	else alert( 'Sorteren is al aangezet' );
	 		
	 	return;
	 }	

	 // timeDiff.setStartTime() ;
	
	 var x;
   for (x in arr_sorttable)
   {

     // Table is sorted allready, only set the right classes, don't sort via jscript
     $("#" + arr_sorttable[x] ).find('thead th').each(function(i)
     {
    	if ( (cPageLayout == 'PartyAgendaByDate') && ( i == 3 || i == 5 )  ||
    	     (cPageLayout == 'PartyAgendaByCity') && ( i == 1 ) )	
    	     $(this).addClass('header headerSortDown');
     });   

   	  
   	  $("#" + arr_sorttable[x] ).tablesorter( 
       {
/* faked sorting above
       	sortList: [[4,0], [3,0]] ,
  */     	
	      headers: {  
	      	0:  { sorter: false }, 
	      	2:  { sorter: false },
	      	4:  { sorter: false },
	      	6:  { sorter: false },
	      	7:  { sorter: false },
	      	8:  { sorter: false }
	      	
	      	       } 
        });
   }
 
   // alert( timeDiff.getDiff().toString() );
   lSortingEnabled = true; 
}

function agendaOpen()
{
  el = document.getElementById( 'personalagenda' );
  if ( el == null ) return;
  document.getElementById( 'personalagenda' ).style.display = 'block';
 	document.getElementById( 'textpersonalagenda' ).style.display = 'none';
 	
}

function agendaClose()
{
  el = document.getElementById( 'personalagenda' );
  if ( el == null ) return;
  document.getElementById( 'personalagenda' ).style.display = 'none';
 	document.getElementById( 'textpersonalagenda' ).style.display = 'block';
}

function togglePersonalAgendaMenu()
{
  el = document.getElementById( 'personalagenda' );
  if ( el )
  { if (el.style.display == 'none' ) agendaOpen();
    else agendaClose();
  }	
  	
  /* onderstaande code werkt niet in ie7 , wel in 8 en ff
	if ($('#personalagenda').is(':visible') ) 
	{ $('#personalagenda').hide(); 
		$('#textpersonalagenda').show();
  }	
	else 
	{	$('#personalagenda').show();
		$('#textpersonalagenda').hide();
  }	
  **/
  
  return false;
}


function reportericon( $ui , cIdItem ) {

   var menuname    = $($ui).attr('rep_menuname' );
   var icontitle   = $($ui).attr('rep_icontitle' );
   var iconalt     = $($ui).attr('rep_iconalt' );
   var iconsrc     = cStaticPathSite + $($ui).attr('rep_iconsrc' );
   var iconclass   = $($ui).attr('rep_iconclass' );

   $( '#detail_reporter_' + cIdItem ).attr('title', icontitle );
   $( '#detail_reporter_' + cIdItem ).attr('src', iconsrc );
   $( '#detail_reporter_' + cIdItem ).attr('alt', iconalt );
   $( '#detail_reporter_' + cIdItem ).attr('class', iconclass );
   $( '#menu_reporter_' + cIdItem ).attr('title', icontitle );
   $( '#menu_reporter_' + cIdItem ).attr('src', iconsrc );
   $( '#menu_reporter_' + cIdItem ).attr('alt', iconalt );
   $( '#menu_reporter_' + cIdItem ).attr('class', iconclass );
   $( '#menu_reporter_' + cIdItem ).parents("a:first").find("span.textinlink").text( menuname );
}   



function ReporterRequest( sender, cIdItem )
{
	var cAction = '';
  
  // el = document.getElementById( ('detail_reporter_' + cIdItem)  );
	$icon  = $( sender );


  // find image in anchor if clicked in text
 	if ($(sender).get(0).tagName.toLowerCase() == 'a')
	 	{ $icon = $(sender).find("img"); 
	 		}

  
  if ( $icon.hasClass( 'user_go' ) ) cAction = 'take' ;
  else if ( $icon.hasClass( 'user_red' ) ) cAction = 'taken' ;
  else if ( $icon.hasClass( 'user_green' ) || $icon.hasClass( 'bin_closed' ) ) cAction = 'remove' ;
  	
  showmessage( sender , 'succes', ( (cLanguage=='en') ? 'One moment please...' : 'Een ogenblik geduld ajb' ) , false ); 

	$.ajax({ type: "GET" ,
	        url: cDomain + "/ajax/ajaxreporterrequest.p", 
	        data: {iditem: cIdItem , action: cAction},
	        dataType: "xml" ,
          success: function(data){

         $(data).find('result').each(function()
          {          
            var $mess = $(this).find('message');
            if ( $( $mess ).length  > 0 )
            { cMessage = $($mess).text();
            	cType    = $($mess).attr('type' );
          	  showmessage( sender , cType, cMessage, true ); 
            }
         
            var $ui = $(this).find('ui');
            if ( $( $ui ).length  > 0 )
            {
              // reportericon( $ui , cIdItem );              	
              // refresh agenda , ververst ook reportericon indien avail
              callmemberagenda( sender, 'refresh', cIdItem );
          	}  

         	}); // each(function)
      

         } , // succes
         error: function(request,error){ alert( 'Oeps ' + request.statusText  ); }
         });
}


function RefreshRepRequest( sender )
{
	var cAction = 'Refresh';
  showmessage( sender , 'succes', ( (cLanguage=='en') ? 'One moment please...' : 'Een ogenblik geduld ajb' ) , false ); 

   $.ajax({ type: "GET" ,
	        url: cDomain + "/ajax/ajaxreporterrequest.p", 
	        data: {action: cAction},
	        dataType: "xml" ,
          success: function(data){
           $(data).find('result').each(function()
            { 
            	var cRepReq = $("reporterrequest", this).text();
              $( '#containerreportrequest' ).html( cRepReq );
           	}); // each(function)

          } , // succes
          error: function(request,error){ alert( 'Oeps ' + request.statusText  ); }
          });
}




function UpdateReportType( e, sender, IdItem ) {
  var current = sender.selectedIndex;
	$.ajax({ type: "GET" ,
	        url: cDomain + "/ajax/ajaxreporterrequest.p", 
	        data: {iditem: IdItem , action: 'reporttype' , typereport: current },
	        dataType: "xml" ,
          success: function(data){
         $(data).find('result').each(function()
          {        
            var $mess = $(this).find('message');
            if ( $( $mess ).length  > 0 )
            { cMessage = $($mess).text();
            	cType    = $($mess).attr('type' );
          	  showmessage( sender , cType, cMessage, true ); 
            }
         	}); // each(function)

         } , // succes
         error: function(request,error){ alert( 'Oeps ' + request.statusText  ); }
         });
}

function DeleteCompetition( sender, cIdItem,  iIdUserprofile )
{	callcompetition( sender, 'remove;refresh', cIdItem, '',  iIdUserprofile ); 
	
	}

function ReturnGuestList( sender, cIdItem, cTitle, iIdUserprofile )
{
	 // cMessage = (cLanguage=='en') ? 'By clicking OK the guestlist wil be returned to DJGuide and given away to someone else.' : 'Door op OK te klikken geef je de gewonnen gastenlijstplekken terug aan DJGuide, die er iemand anders blij mee zal maken.';
	 
	 $.ajax({
   type: "GET",
   url: "/ajax/ajaxgethelp.p",
   data: "helptag=returnguestlist",
   success: function(msg){
     
     $("#dialogcontainer").dialog( 'option', 'position', 'center' );
	   $('#dialogcontainer').dialog( 'option', 'width', 500);	
     $('#dialogcontainer').dialog('option', 'title', cTitle  );		
     $("#dialogcontainer").html( msg );
     $('#dialogcontainer').dialog('option', 'buttons', { "Ok": function() { callcompetition( sender, 'cancel;refresh', cIdItem, '',  iIdUserprofile ); $(this).dialog("close"); } , "cancel": function() { $(this).dialog("close");} });
     $("#dialogcontainer").dialog( "open" );

     
    } , // succes
         error: function(request,error){ alert( 'Oeps, ' + request.statusText  ); }

   });

	 

}



function Competition( sender, cIdItem,  iIdUserprofile )
{
	var cAction = '';
  var cClass  = '';
  
  // el = document.getElementById( ('detail_reporter_' + cIdItem)  );
	$icon  = $( sender );

  // find image in anchor if clicked in text
 	if ($(sender).get(0).tagName.toLowerCase() == 'a')
	 	{ $icon = $(sender).find("img"); 
	 		}
  
  if ( $icon.hasClass( 'win_add' ) ) 
  { cAction = 'add' ;
    cClass  = 'win_add'; }
  
  else if ( $icon.hasClass( 'win_del' ) ) 
  { cAction = 'remove' ;
  	cClass  = 'win_del'; }	
  
  $win = $( '.' + cClass );
  $win.attr({ "src" : cStaticPathSite + '/image/formulier/loading.gif' });
  	
  showmessage( sender , 'succes', ( (cLanguage=='en') ? 'One moment please...' : 'Een ogenblik geduld ajb' ) , true ); 

  callcompetition( sender, cAction, cIdItem , cClass,  iIdUserprofile );    
  
}


function callcompetition( sender, cAction, cIdItem, cClass,  iIdUserprofile )
{
	  	
	  var cMemberCompetitions = '';
	
		$.ajax({ type: "GET" ,
	        url: cDomain + "/ajax/ajaxcompetition.p", 
	        data: {iditem: cIdItem , action: cAction, iduserprofile: iIdUserprofile },
	        dataType: "xml" ,
          success: function(data){

         $(data).find('result').each(function()
          {          
            var $mess = $(this).find('message');
            
            if ( $( $mess ).length  > 0 )
            { var cMessage = $($mess).text();
            	var cType    = $($mess).attr('type' );
            	var cTitle   = $($mess).attr('title' );
            	
            	if (cType == 'alert') 
            	{
           
            		  $("#dialogcontainer").dialog( 'option', 'position', 'center' );
            		  $('#dialogcontainer').dialog( 'option', 'width', 700);	
                	
                	
                	if ( cTitle == '' )  cTitle = (cLanguage=='en') ? 'Competitions' : 'Winacties';
                	
                	$('#dialogcontainer').dialog('option', 'title', cTitle  );		
                  $("#dialogcontainer").html( cMessage );
                  $('#dialogcontainer').dialog('option', 'buttons', { "Ok": function() { $(this).dialog("close"); } });
          			  $("#dialogcontainer").dialog( "open" );
          	
            	}	
            	else if ( cMessage != '' ) showmessage( sender , cType, cMessage, true ); 
            }

            
            var $ui = $(this).find('ui');
            
            if ( ( $( $ui ).length  > 0 ) && ( cClass != '' ) )
            {

              var menuname    = $($ui).attr('win_menuname' );
              var icontitle   = $($ui).attr('win_icontitle' );
              var iconalt     = $($ui).attr('win_iconalt' );
              var iconsrc     = cStaticPathSite + $($ui).attr('win_iconsrc' );
              var iconclass   = $($ui).attr('win_iconclass' );

              
              $win = $( '.' + cClass );
             
              $win.attr({
                "src" : iconsrc,
                "title" : icontitle,
                "alt" : iconalt,
                "class" : iconclass  });
             
                
              $( '#winstar' ).attr('src', cStaticPathSite + '/image/formulier/win.png' );
          	}  

            cMemberCompetitions = $("membercompetitions", this).text();
            if (cMemberCompetitions != '' ) 
            {
                $( '#containermembercompetitions' ).html( cMemberCompetitions );
                membercompetitions();
                
            }   
          	
          	callmemberagenda( sender, 'refresh', cIdItem ); // refresh personal agenda
            
         	}); // each(function)
      

         } , // succes
         error: function(request,error){ alert( 'Oeps ' + request.statusText  ); }
         });

}


function membercompetitions()

{
	//  competitions member
$('#membcomp_active tbody tr').mouseover(function() {   
                     $(this).addClass('over');   
                    }).mouseout(function() {   
                     $(this).removeClass('over');   
                   }).click(function() {   
                      // alert($(this).attr( 'onclick' ) );   
                      eval($(this).attr( 'onclick' ) );   
                    });   
//  competitions member
$('#membcomp_wonhistory tbody tr').mouseover(function() {   
                     $(this).addClass('over');   
                    }).mouseout(function() {   
                     $(this).removeClass('over');   
                   }).click(function() {   
                    // alert($(this).attr( 'onclick' ) ); 
                    eval($(this).attr( 'onclick' ) );     
                    });                       
	
	}
$(document).ready(function() {

$("#debuginfo").append(" document.ready (memberagenda):" + timeDiff.getDiff().toString() );								 	
timeDiff.setStartTime() ;


/* moved to progress, to slow for party_agenda 
$(".hoverMe tbody tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
*/


/* event delegation of click */
$('#containerupdpartyagenda').click(function(e) {   
    var $this = $(e.target);   
    
   if( $this.is("td") ) 
   {
    
    /* zoek anchor met class party om naar toe te gaan bij click */
    urlToOpen = $this.parents("tr:first").find("a.party").attr("href");
    if (urlToOpen != undefined) window.location.href = urlToOpen;  
   }
   else if( $this.is("img") ) 
   {
      if ( $this.hasClass( 'bin_closed' ) || $this.hasClass( 'addbookmark' ) || $this.hasClass( 'user_green' ) ) memberpartyagenda( e.target );
   }
   
   else if( $this.is("div") ) 
   {
      if ( $this.hasClass( 'detailinfo' ) ) 
      {
      	urlToOpen = $this.parents("tr:first").find("a.party").attr("href");
        if (urlToOpen != undefined) window.location.href = urlToOpen; 
      }
   }
}); 


$("#personalagenda tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});

/* event delegation of click in container member agenda */
$('#AgendaMember').click(function(e) {   
    var $this = $(e.target);   

   if( $this.is("td") ) 
   {
 
    /* zoek anchor met class party om naar toe te gaan bij click */
    urlToOpen = $this.parents("tr:first").find("a.party").attr("href");
    if (urlToOpen != undefined) window.location.href = urlToOpen;  
   }
   else if( $this.is("img") ) 
   {

      if ( $this.hasClass( 'bin_closed' ) || $this.hasClass( 'addbookmark' ) || $this.hasClass( 'user_green' ) ) memberpartyagenda( e.target );
      else
      if ($this.attr( 'onclick' ) != null )
   	   {  eval($this.attr( 'onclick' ));
   	   	  return false;
       }	 
       
   }

   else if( $this.is("input") ) 
   {
   	  // ( $this.attr("type" ) == 'checkbox' )  	  
   	  
      UpdateTicketsInPocket( e.target );
   }
   else if( $this.is("a") ) 
   { 
   	   
   	   if ($this.attr( 'onclick' ) != null )
   	   {  eval($this.attr( 'onclick' ));
   	   	  return false;
       }	 
   }
   
}); 




membercompetitions();

togglePersonalAgendaMenu(); 
$("#debuginfo").append(" Memberagenda.js onready parsetime:" + timeDiff.getDiff().toString() );								 



});




