$(document).ready(function(){    

    //Table highlighting
//    $("table.datatable").not('.striped').find("tbody tr").hover(function(){                                 
//        $(this).addClass("hover");
//    },function(){
//        $(this).removeClass("hover");
//    });
    
    //People search result features

//    $("#profile_search_result tbody tr").click(function(){
//        if ($(this).find("a").attr("href") != null)
//        {
//            document.location = $(this).find("a").attr("href");             
//        }
//    });
        
    $("#profile_search_result tbody tr a").hover(function(){                                             
        tab_hide_field($(this).parents('tr'), 1);
    },function(){            
        tab_hide_field($(this).parents('tr'), 0);
    });

    //FAQs
    $('ul.faqlist li div').hide();
    $('ul.faqlist li a.link_question').click(function(){
        $(this).find('+ div').slideToggle('fast');
        $(this).toggleClass('open');
        return false;
    });

	//Forms        
    InitInputDefaultValue(".defaultvalue_input");    
    InitSubmitDefaultValue(".defaultvalue_submit", ".defaultvalue_input");
    selectSubmit('.select_submit');
    init_dropdown_withtextbox();        
});

if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', FirefoxFixOrderedForm, false);

//--------------------------------------------- Forms
/* initialize the inputs which requires a 'default value' system */
function InitInputDefaultValue(input_class){
      
    $(input_class).each(function(i){   
        var current_input_id =  $(this).attr("id");
        var default_value = GetInputDefaultValue(current_input_id);        
        if($(this).val() == ""){
            $(this).val(default_value);
        }
        InitInputDefaultValueEvents("#" + current_input_id, default_value)
    });            
}

/* set the focus and blur events for the 'default value' inputs */
function InitInputDefaultValueEvents(input_id, default_value){

    $(input_id).click(function(){
        if($(this).val() == default_value)
            $(this).val("");
    });
    
    $(input_id).blur(function(){
        if($(this).val() == "")
            $(this).val(default_value);
    });
}

/* return the value of the label associated to the input */
function GetInputDefaultValue(input_id){
         
    var input_default_value = "";    
    $("label").each(function(j){
        if($(this).attr("for") == input_id){
            input_default_value = $.trim($(this).text());
        }
    });
    return input_default_value;
}

/* clear the value of the input still using their 'default value' when the form is submitted */
function InitSubmitDefaultValue(submit_class, defaultvalue_class){
    
    $(submit_class).click(function(){                                        
        $(this).parent().find(defaultvalue_class).each(function(i){
            var default_value = GetInputDefaultValue($(this).attr("id"));
            if($(this).val() == default_value){
                $(this).val("");
            }
        });        
    });
}

function selectSubmit(select_id)
{
    $(select_id).change(function(){
        if ($(this).val() != '')
        {
            $(this).parents('form').submit();
        }
        return false;
    });    
}

function init_dropdown_withtextbox()
{
   //init
   $('input.input_text_fordropdown').attr('disabled', 'disabled');      
   //$('input.input_text_fordropdown').hide();      
   //$('label.label_forothertextbox').hide();
   
   //action
   $('select.dropdown_withtextbox').change(function(){
        var textbox = $(this).parents('li').find('input.input_text_fordropdown');
        var textbox_label = $('label[for=' + textbox.attr('id') + ']');

        if($(this).val() == 'other')
        {
            textbox.attr('disabled', '');
            //textbox_label.show();
            //textbox.show();
            textbox.focus();
            //ValidatorEnable($("#<%= rfvRoleOther.ClientID %>"), true);  
        }
        else
        {
            textbox.attr('disabled', 'disabled'); 
            //textbox_label.hide();       
            //textbox.hide();
            textbox.attr('value', '');
            //ValidatorEnable($("#<%= rfvRoleOther.ClientID %>"), false);  
        }        
   });
}
 
//--------------------------------------------- Table
function getPosition(e) 
{
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) 
    {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    }
    else 
    {
        cursor.x = e.clientX +
            (document.documentElement.scrollLeft ||
            document.body.scrollLeft) -
            document.documentElement.clientLeft;
            
        cursor.y = e.clientY +
            (document.documentElement.scrollTop ||
            document.body.scrollTop) -
            document.documentElement.clientTop;            
    }
    return cursor;
}


function tab_hide_field(row, state)
{
    var offX = -360;          // X offset from mouse position
    var offY = 5;          // Y offset from mouse position

    //Specific position on MacOS
    
    if (navigator.userAgent.search("Macintosh") > 0)
    {
        offX = -30;         
    }

    var row_id_number = row.attr("id").substring(4);    
    var hidden_field = $("#profile_thumb_" + row_id_number);        
            
    if(hidden_field.attr("id") != '')
    {        
        if (state == 1)
        {
            //hidden_field.fadeIn("fast");          
            hidden_field.show();
            //hidden_field.css("visibility", "visible");
            
            row.mousemove(function(e){                        
                var mouseposition = getPosition(e);
                
                //$("#debug").text("row_id_number:" + row_id_number + ";x:" + mouseposition.x + "; y:" + mouseposition.y);
                
                hidden_field.css("left", (parseInt(mouseposition.x)+offX) + "px");
                hidden_field.css("top", (parseInt(mouseposition.y)+offY) + "px");

            });        
        }
        else
        {
            //hidden_field.fadeOut("fast");
            hidden_field.hide();
            //hidden_field.css("visibility", "hidden");
        }
    }
}

/* fix Firefox formating issue for Forms using ordered list */
function FirefoxFixOrderedForm(){
  // Hide forms
  $( '.form_inline' ).hide().end();

  // Processing
  $( '.form_inline' ).find( 'li label' ).each( function( i ){    
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = null;
    this.appendChild( labelSpan );
  } ).end();

  // Show forms
  $( '.form_inline' ).show().end();
}