﻿var speed=100; //timeout speed
var myRow=1;
var myCol=1;
var speed=100; //timeout speed
var myTable;
var noRows;
var myCells,ID;
(function($) {
    $.FreezePanes = function(data, klass) {
        
    }
    $.extend($.FreezePanes,{
            settings: {
                buttonUp:"/CSS/Images/uF035.png",
                buttonDn:"/CSS/Images/uF036.png",
                buttonRight:"/CSS/Images/uF033.png",
                buttonLeft:"/CSS/Images/uF034.png",
                freezeRow:1, //
                freezeCol:1 //
                
            },
            FreezeHtml: '\
                <div class="div_freezepanes_wrapper" id="FreezePanes">\
                    <div id="FreezePanesTable">\
                    </div>\
                    <div class="div_verticalscroll"> \
                        <div id="div_verticalscrollUp" onmousedown="upp();" onmouseup="upp(1);" style="height: 50%;">\
                            <img class="buttonUp" alt="Scroll Up"></div>\
                        <div id="div_verticalscrollDn" onmousedown="down();" onmouseup="down(1);" style="height: 50%;">\
                            <img class="buttonDn" alt="Scroll Down"></div>\
                    </div>\
                    <div class="div_horizontalscroll">\
                        <div id="div_horizontalscrollRight" onmousedown="right();" onmouseup="right(1);" style="float: left; width: 50%; height: 100%;">\
                            <img class="buttonRight" alt="Scroll Right"></div>\
                        <div id="div_horizontalscrollLeft" onmousedown="left();" onmouseup="left(1);" style="float: right; width: 50%; height: 100%;">\
                            <img class="buttonLeft" alt="Scroll Left"></div>\
                    </div>\
                </div>',
            init:  function(Content,pContent) {
                var ContentHeight = Content.height();
                var ContentWidth = Content.width();
                pContent.empty();
                if(pContent.find("#FreezePanesTable").length > 0){
                    return;
                }
                pContent.append($.FreezePanes.FreezeHtml);
                pContent.find("#FreezePanesTable").append(Content);
                $(".buttonUp").attr("src", $.FreezePanes.settings.buttonUp);
                $(".buttonDn").attr("src", $.FreezePanes.settings.buttonDn);
                $(".buttonRight").attr("src", $.FreezePanes.settings.buttonRight);
                $(".buttonLeft").attr("src", $.FreezePanes.settings.buttonLeft);
                //alert($("#FreezePanes").height() +' '+ ContentHeight);
                if($("#FreezePanes").height() > ContentHeight){
                    $(".div_verticalscroll").css("display","none");
                }
//                else{
//                    $(".div_verticalscroll").css("display","block");
//                }
                if($("#FreezePanes").width() > ContentWidth){
                    $(".div_horizontalscroll").css("display","none");
                }
//                else{
//                    $(".div_horizontalscroll").css("display","block");
//                }                
            }
        }
    )
    
    $.fn.FreezePanes = function(freezeRow, freezeCol) {
        $.FreezePanes.settings.freezeRow = freezeRow;
        $.FreezePanes.settings.freezeCol = freezeCol;
        myRow = freezeRow;
        myCol = freezeCol;
        setUp($(this).get(0));
        $.FreezePanes.init($(this),$(this).parent())
        
    }
})(jQuery);

function setUp(Content){
    //if(!myTable){}	  
    myTable=Content;
          
    myCells = myTable.rows[0].cells.length;
    noRows = myTable.rows.length;

    for( var x = 0; x < myTable.rows[0].cells.length; x++ ) {
        colWdth=myTable.rows[0].cells[x].offsetWidth;
        //myTable.rows[0].cells[x].setAttribute("width",colWdth-4);

    }
}

function right(up){
    if(up){window.clearTimeout(ID);return;}
    //if(!myTable){setUp();}
    if(myCol<(myCells)){
	    for( var x = 0; x < noRows; x++ ) {
		    myTable.rows[x].cells[myCol].style.display="";
	    }
	    if(myCol >$.FreezePanes.settings.freezeCol){myCol--;}
	    ID = window.setTimeout('right()',speed);
    }
}

function left(up){
    if(up){window.clearTimeout(ID);return;}
    //if(!myTable){setUp();}
    if(myCol<(myCells-1)){
        for( var x = 0; x < noRows; x++ ) {
	        myTable.rows[x].cells[myCol].style.display="none";
        }
        myCol++
        ID = window.setTimeout('left()',speed);

    }
}

function down(up){
    
    if(up){window.clearTimeout(ID);return;}
    //if(!myTable){setUp();}
    if(myRow<(noRows-1)){
        myTable.rows[myRow].style.display="none";
        myRow++	;
        ID = window.setTimeout('down()',speed);
    }
}

function upp(up){
    if(up){window.clearTimeout(ID);return;}
    //if(!myTable){setUp();}
    if(myRow<=noRows){
	    myTable.rows[myRow].style.display="";
	    if(myRow >$.FreezePanes.settings.freezeRow){myRow--;}
	    ID = window.setTimeout('upp()',speed);
    }
}
