jQuery(document).ready(function() {    
    // Add even class for alternating colors
    jQuery("table.codes tr:even, table.draws-list tr:even").addClass("even");
    
    // Cancel click behavior in "i" icon
    jQuery('a.comment_anchor').click(function() {return false;})
    // Hover detection on "i" icon
    jQuery("a.comment_anchor").hover(over, out);
    
    //Click detection on codes checkboxes
    jQuery('table.codes td.code input').change(codeClick)
    
    //Submit detection of form to fill hidden fields
    jQuery('#randomDrawsForm').submit(randowDrawFormSubmit);
    
   checkPreviouslySelecteds();
});

function checkPreviouslySelecteds() {
    if(jQuery('#inscriptions').length > 0) {
	if(jQuery('#inscriptions').val() == "") {
		jQuery('table.codes td.code input:checked').each(function(){
			jQuery(this).removeAttr('checked');
		})
	}
	else {
		var codes = (jQuery('#inscriptions').val()).split(',');
		jQuery.each(codes,function(){
			if (this.length > 0) {
				jQuery('#'+this).attr("checked", "checked");
				jQuery('#'+this).trigger('click');
				jQuery('#'+this).attr("checked", "checked");
			}
		})
	}
   }
}

function randowDrawFormSubmit() {
	var inscriptionsHidden = jQuery('#inscriptions');
	inscriptionsHidden.val('');
	var curVal = (inscriptionsHidden.val()).toString();
	
	jQuery.each(checkedCodesList,function(i,val){
		if(curVal == '') {
			inscriptionsHidden.val(val)
		}
		else {
			inscriptionsHidden.val(curVal+','+val)
		}
		
		curVal = inscriptionsHidden.val();
	})
}


function codeClick(event){
	jQuery('table.codes td.code label span').remove();
	var sub = 'er';
	var curPos = jQuery.inArray(jQuery(this).attr('id'), checkedCodesList);
	if(curPos === -1)
	{
		checkedCodesList.push(jQuery(this).attr('id'));
	}
	else {
		checkedCodesList.splice(curPos,1);
	}
	jQuery.each(checkedCodesList,function(i){
		if(i>0)
		{
			sub = 'e';
		}
		jQuery('#'+this+':checked').parents('td').find('label').append('<span style="color:#e27827">&nbsp;('+parseInt(i+1)+'<sub>'+sub+'</sub>)</span>')
	})
}

function over(event){
    
  var code = $(this).attr('href');
  var messages = code.split('-');
  var msgLength = messages.length;
  var txt='';
  

  if (code == "periode") {  // Hub Tirages
    var position = $(this).position();
    txt = $(this).attr("rel");

    //If message is not empty, position the tooltip, insert messages and show
    if(txt != '')  {
      $('#comment_tip').css({
       width:'auto',
       padding:3,
       'padding-left':10,
       'padding-right':12,
       margin:0,
       top:position.top-1,
       left:position.left+24
     })
     .html(txt)
     .fadeIn('fast');
   
   }
  }
  else { // Tableaux
  
    //Insert messages in <li> tags from the messages array
    if(messages[0] != '#') {
  	for(i=0;i<msgLength;i++) {
  		txt=txt+'<li>'+msgs[messages[i]]+'</li>';
  	}
    
  
  //If message is not empty, position the tooltip, insert messages and show
  if(txt != '')
  {
  	txt = '<ul>'+txt+'</ul>';
  	$('#comment_tip').css({
      	top:event.pageY+8,
      	left:event.pageX-125
      })
      .html(txt)
      .fadeIn('fast');
    }
  }
 }
}

function out(event) {
	$('#comment_tip').fadeOut("fast");
}
