$(document).ready(function(){
    var menuConfig = {
        over: makeTall, // function = onMouseOver callback (REQUIRED)
        timeout: 500, // number = milliseconds delay before onMouseOut
        out: makeShort // function = onMouseOut callback (REQUIRED)
    };
    function makeTall(){
        $(this).addClass('hover');        
		if( $.browser.msie && $.browser.version <= 7){
			$('> ul', $(this)).show();
		}
		else{
			$('> ul', $(this)).slideDown();
		}
    }
    function makeShort(){
        $(this).removeClass('hover');
        $('> ul', $(this)).slideUp('fast');
    }

    $("#top-menu ul.menu > li").hoverIntent(menuConfig);

    $('#side-menu .menu > li > span > a').click(function(){        
        var subMenuEl = $(this).parent().next('ul');        
        $('#side-menu .menu ul').each(function(){
            if(!(subMenuEl.is(':visible')))
                {
                    $('#side-menu .menu ul').slideUp(250);
                }
        });
        subMenuEl.animate({
            height: 'toggle'
        }, 250);
    });
    $('.price-table').each(function(){
        var wrapTable = $(this), tableEl = $('table', wrapTable), newTableHeader = '',
        fullMainWidth = $('th', tableEl).innerWidth();
        var headText = new Array();
        $('th', tableEl).each(function(ind){
           headText[ind] = $(this).text();
           $(this).remove();
        });
        $('tr', tableEl).each(function(){
            $('td:first', this).addClass('first');
        })
        wrapTable.css({'padding-left': fullMainWidth + 'px', 'overflow': 'hidden'});
        var prevWrap = '', nextWrap = '';
        for(var el in headText){
            prevWrap = '<tr><th>';
            nextWrap = '</th></tr>';
            if(el == 0){
                prevWrap = '<thead><tr><th>';
                nextWrap = '</th></tr></thead>';
            }
            if(el == 1){
                prevWrap = '<tfoot><tr><th>';
                nextWrap = '</th></tr></tfoot>';
            }
            newTableHeader = newTableHeader + prevWrap + headText[el] + nextWrap;
        }
        newTableHeader = '<table>' + newTableHeader + '</table>';

        $(newTableHeader).insertBefore(tableEl).addClass('red-table')
            .css({
               'width': fullMainWidth + 'px',
               'position': 'absolute',
               'left': '0px',
               'top': '0px',
               'z-index': '100'
            });
			
		if( tableEl[0].offsetWidth <= wrapTable[0].offsetWidth){
			tableEl.addClass('noDrag');
		}
        dragResize(wrapTable, tableEl, fullMainWidth);
        $(window).resize(function(){
            dragResize(wrapTable, tableEl, fullMainWidth);
        });
    });
    $('.red-table tr').hover(
        function(){
            var hoverEl = $(this), parentMainEl = $(this).parent().parent().parent(), rowInd = $(this)[0].sectionRowIndex;            
            $('table', parentMainEl).each(function(){
                if(hoverEl.parent()[0].nodeName == 'TBODY'){
                    $('tbody > tr:eq('+ rowInd +')', this).addClass('hoverRow');
                }
                else if(hoverEl.parent()[0].nodeName == 'THEAD'){
                    $('thead > tr:eq('+ rowInd +')', this).addClass('hoverRow');
                }
                else{
                    $('tfoot > tr:eq('+ rowInd +')', this).addClass('hoverRow');
                }
            });
        },
        function(){
            var hoverEl = $(this), parentMainEl = $(this).parent().parent().parent(), rowInd = $(this)[0].sectionRowIndex;
            $('table', parentMainEl).each(function(){
                if(hoverEl.parent()[0].nodeName == 'TBODY'){
                    $('tbody > tr:eq('+ rowInd +')', this).removeClass('hoverRow');
                }
                else if(hoverEl.parent()[0].nodeName == 'THEAD'){
                    $('thead > tr:eq('+ rowInd +')', this).removeClass('hoverRow');
                }
                else{
                    $('tfoot > tr:eq('+ rowInd +')', this).removeClass('hoverRow');
                }
            });
        }
    );
});
function dragResize(mainEl, dragEl, titleWidth){    	
    var ofsetRight =  $('#wrap')[0].offsetLeft + mainEl[0].offsetLeft + titleWidth;// 749;
    var ofsetLeft =  $('#wrap')[0].offsetLeft + mainEl[0].offsetLeft + mainEl.outerWidth() - dragEl.outerWidth(); //571;
    
    dragEl.draggable({
        axis: "x",
        containment: [ ofsetLeft,0, ofsetRight, 0],
        scroll: false,
        handle: 'td'
    });
}
function showDetail(el, showBlock){    
    if($(el).find('.off').is(':visible')){
        $(showBlock).slideDown(500, function(){
            $(el).find('.off').hide();
            $(el).find('.on').css({'display': 'inline-block'});
        });
    }
    else{
        $(showBlock).slideUp(500, function() {
            $(el).find('.on').hide();
            $(el).find('.off').css({'display': 'inline-block'});
        });
    }

    return false;
}
