/**
 * Common scripts
 *
 * @author  Paul Kolbovich <paulkolbovich@gmail.com>
 * @version 0.6.4.1
 */
jQuery.noConflict();

jQuery(function($) {
	/**
	 * setup datePicker
	 */
	var wDatePicker = $('.wDatePicker');
	wDatePicker.datePicker({
		startDate:  '01/01/04',
		endDate:    (new Date()).asString(),
		clickInput: true
	}).bind('dateSelected', function() {
		if (this.id == 'openPositionsDate') {
			submitButton.trigger('click');
		}
	}).live('keyup', function(event) {
		if (event.keyCode != 13) {
			wDatePicker.trigger('change');
		}
	});
	/**
	 * clear tooltips
	 */
	function clearWTooltip() {
		$('.wTooltip').hide().removeClass('wTooltip');
	}
	function clearAcroheTooltip() {
		$('.acroHeTooltip').hide().removeClass('acroHeTooltip');
	}
	function clearMeHiTooltip() {
		$('.meHiTooltip').hide().removeClass('meHiTooltip');
	}	
	/**
	 * setup tooltips
	 */
	$('.tbl').mouseover(function(event) {
	    if ($(event.target).is('.hint > a span')) {
	    	clearWTooltip();
		    var link = $(event.target).parent();
		    var hint = link.find('~ span').addClass('wTooltip').show().hover(function () {
		    }, function () {
		    	clearWTooltip();
		    });
		}
	}); 
	$('.acroHe').mouseover(function(event) {
		clearAcroheTooltip();
		var link = $(event.target);
		var hint = link.find('~ span').addClass('acroHeTooltip').show().hover(function () {
		}, function () {
			clearAcroheTooltip();
		});
	});
	$('.acroHe').mouseout(function(event) {
		clearAcroheTooltip()		
	});				
	$('.meHi').mouseover(function(event) {
	    if ($(event.target).is('.hint > a span')) {
	    	clearMeHiTooltip();
		    var link = $(event.target).parent();
		    var hint = link.find('~ span').addClass('meHiTooltip').show().hover(function () {
		    }, function () {
		    	clearMeHiTooltip();
		    });
		}
	});
	
	clearWTooltip();
	clearAcroheTooltip();
	clearMeHiTooltip();
	
    /**
     * expand / collapse trade operations
     * {{{
     */
    /**
     * collapse all
     */
    function collapseAll() {
        $('tr.child').hide();
        $('tr.parent td.anchor').removeClass('expanded').addClass('collapsed');
    }
    /**
     * expand all
     */
    function expandAll() {
        $('tr.child').show();
        $('tr.parent td.anchor').addClass('expanded').removeClass('collapsed');
    }
    collapseAll();
    $('tr.parent td.anchor').click(function () {
        var tr = $(this);
        if (tr.hasClass('collapsed')) { //expand
            tr = tr.addClass('expanded').removeClass('collapsed').parent().next();
            while (tr.hasClass('child')) {
                tr.show();
                tr = tr.next();
            }
        } else { // collapse
            tr = tr.removeClass('expanded').addClass('collapsed').parent().next();
            while (tr.hasClass('child')) {
                tr.hide();
                tr = tr.next();
            }
        }
    });
    $('#expandAll').click(function () {
        expandAll();
    });
    $('#collapseAll').click(function () {
        collapseAll();
    });
    /**
     * }}}
     */
});
