/**
 * Name: TrackMate - common.js
 * Version: 0.1 
 * Author: Sjoerd 
 * Goal: Provides functionality used all over the site.
 */
var TM = TM || {};

/**
 * Run jQuery No Conflict
 * use $j as selector function
 */
var $j = jQuery.noConflict();


/**
 * Document Ready
 *
 */
$j(function(){
    
    // styling & defining UI items
    $j( "button, input:submit, input:button" ).button();
    $j( ".go" ).button( { "icons" : {secondary:'ui-icon-triangle-1-e'} } );
    $j( "input.date" ).datepicker({
        dateFormat: 'yy-mm-dd',
        beforeShow: function() { setTimeout("$j('#ui-datepicker-div').maxZIndex()", 500); }
    });
    
    /*
    // login menu expanding
    var loginMenuOptions = $j('.loginMenu .options');
    loginMenuOptions.hide();
    $j('.loginMenu .expand').click(function() {
        loginMenuOptions.slideToggle();
    });
    */
    
    // zebra-style table rows
    $j('table.listing tbody tr').each(function(index){
        if (index%2)
            $j(this).addClass('odd')
        else
            $j(this).addClass('even')
    });
    
    // Adding elements for visual appearance
    // making a link look like a button
    $j('a.mainBtn').each(function(index,el){
        var el = $j(el);
        var label = el.html();
        el.html('<span class="left"></span><span>'+label+'</span>');
    });
/*    
    // deviding a table form in once cell over more cells
    // by keeping the whole form in that cell
    // but cloning text elements to another cell
    $j('.tableForm').each(function(){
        var parent = $j(this).parent();    
        $j('input[type=text]',this).each(function(index,el){
            var master = $j(this);
            var clone = master.clone();
            clone.attr('id',clone.attr('id')+'_clone');
            clone.bind('change',function(){
                master.val(clone.val());
            });
            $j('.errorlist', parent).appendTo('.inputClone',parent);
            clone.appendTo('.inputClone',parent);
            master.parent().append( $j('<div>').hide().append( master ) );
        });
    });
*/    
    // creating print button
    $j('.printBtn').click(function(){
        window.print();
    });
});



$j.maxZIndex = $j.fn.maxZIndex = function(opt) {
    /// <summary>
    /// Returns the max zOrder in the document (no parameter)
    /// Sets max zOrder by passing a non-zero number
    /// which gets added to the highest zOrder.
    /// </summary>    
    /// <param name="opt" type="object">
    /// inc: increment value, 
    /// group: selector for zIndex elements to find max for
    /// </param>
    /// <returns type="jQuery" />
    var def = { inc: 10, group: "*" };
    $j.extend(def, opt);
    var zmax = 0;
    $j(def.group).each(function() {
        var cur = parseInt($j(this).css('z-index'));
        zmax = cur > zmax ? cur : zmax;
    });
    
    if (!this.jquery)
        return zmax;

    return this.each(function() {
        zmax += def.inc;
        $j(this).css("z-index", zmax);
    });
}

