/* allVideoSorter.js 
 * copyright Juicyorange, LLC 2009
 * all rights reserved
 */

// sorting indecies
var allVideoIndexes_obj = {};
allVideoIndexes_obj.byName_arr = new Array(1891,1897,1899,1905,1893,1901,1885,1895,1903,1889,1907,1887,1855,2581,1757,1449,1572,1640,1682,1837,6521,6986,1875,7489,1570,1690,7761,5522,1602,1365,1686,1373,1554,8777,3977,1397,1666,543,1714,1375,1441,1540,1458,1421,6646,5528,5530,1439,1530,1558,1680,1678,1550,1827,1377,9911,1839,1821,1508,1618,1624,1612,1610,1620,1604,1616,1614,1622,1608,1626,1606,6260,5111,8964,4750,1574,1560,1741,1696,2276,1433,8198,8292,523,6516,1516,1775,9372,1534,1395,1853,349,1476,1351,1405,1849,6263,1727,1472,9370,1829,1369,353,1813,1803,1789,5114,9249,1592,1379,1586,1600,1722,1578,249,1841,1769,1650,5153,1809,1783,8819,5137,1712,1745,1662,5159,1644,7076,9144,5162,620,1652,1544,1835,569,5184,1735,1706,1781,1692,2300,6741,6747,6743,6731,6745,6737,6733,6739,6735,6752,7074,1646,1861,1863,1859,2271,1504,627,1913,645,310,5882,1450,6518,1688,1482,7260,619,3561,8287,350,1881,351,3971,1427,9909,3558,1462,1480,1496,1799,5156,5154,1815,1708,1642,5186,1747,1702,5158,1787,1797,1765,5138,9735,5109,568,6081,2107,1700,2274,1580,1710,1704,1743,1751,2297,1793,5139,1694,5533,1819,8289,1883,1920,1817,1576,1598,5157,1698,1636,1520,1470,1468,1676,611,1879,544,5151,1500,1779,1873,1871,1869,1867,1865,9247,9374,7986,1399,1596,1749,1445,4455,1845,8195,1466,1532,1425,1847,1771,1474,1588,3975,7989,1359,1486,1484,1367,1391,1389,1454,5386,1447,1452,1419,1791,5526,1387,1413,1843,1857,1407,1638,1720,9028,1753,6650,1739,1562,1417,1361,1393,1460,1552,1584,1464,1546,355,1718,5620,1811,1916,1918,1490,5152,1656,5187,1737,1807,5185,1654,1823,1437,1536,1755,1582,1725,1564,1498,9244,1431,3741,1478,1660,8885,5524,1672,1763,1518,1357,9596,1371,1512,4452,1542,1538,1668,1733,1731,1729,1409,1429,1674,1566,1456,1556,1851,8737,1914,1488,1443,1494,8241,8082,1805,1664,1670,1492,6716,6750,1423,1385,352,1831,1403,1911,659,1773,6388,1514,4602,1411,1590,1716,1658,1648,1568,1548,8425,1877,354,1355,1777,5422,4173,1415,1383,1381,1353,1502,7079,1833,1510,1401,1594,1435,1684,1825);
allVideoIndexes_obj.byCategory_arr = new Array(1907,1905,1903,1901,1899,1897,1895,1893,1891,1889,1887,1885,7761,5522,8777,8241,5151,5154,249,5152,5139,5137,5153,5185,5138,5156,5184,5158,5157,5187,5159,5186,1600,1722,1636,1592,1578,1558,1530,8195,7074,5111,2276,659,620,611,523,1914,1911,1883,1879,1875,1857,1853,1851,1849,1845,1847,1843,1841,1839,1837,1835,1833,1831,1829,1827,1817,1819,8964,8737,4750,1741,1696,1574,1560,1863,1861,1859,1813,1803,1789,8819,1809,1783,9144,5162,1745,1712,1662,1650,1644,1684,1686,1688,1682,1680,1664,1670,1676,1666,1674,1668,1672,1678,1873,1871,1869,1867,1865,8885,1749,1739,1596,1584,1562,1554,1550,1532,1351,1720,1604,1606,1608,1610,1612,1614,1616,1618,1620,1622,1624,1626,5620,2581,1811,1807,1757,1737,1718,1570,1656,1556,1725,1582,1564,8082,7489,6263,4452,1805,6750,6747,6745,6743,6741,6739,6737,6735,6733,6731,6752,8425,1716,1658,1648,1590,1568,1548,1546,5422,4173,1916,1918,1735,1706,1692,1423,1425,1427,1602,7260,5386,3561,619,1464,1458,1714,1460,1462,4602,543,544,1572,1544,9596,3741,6716,1855,3558,1920,1913,1881,1877,1588,1586,645,310,6986,5882,1791,1496,6521,6518,6516,1797,1787,1765,1747,1708,1702,1642,1815,1799,2274,2271,1435,1437,1652,1654,1823,1825,1538,1542,1821,1536,1369,1534,1540,9735,6081,5109,2107,568,9374,9372,9370,1456,6260,5533,5530,5528,5526,5524,7989,7986,1502,1504,627,1494,1779,1781,1775,1777,1490,1773,1769,1771,1445,1447,1443,1449,1433,1646,1640,1638,1431,7079,7076,1733,1731,1466,1729,2297,1793,1751,1743,1710,1704,1694,1594,1580,1353,1429,1486,1484,1763,1488,1498,1500,1371,1552,353,352,354,355,569,1520,1518,1516,1454,1700,8198,1377,1755,1482,1492,1361,1365,1357,1367,1421,1660,1439,1441,8292,8289,8287,2300,1508,1510,1514,1512,1419,1417,1415,1413,9028,1476,1474,1379,1381,1383,1385,1387,9911,9909,6650,6646,1698,1598,1576,1566,1391,1389,1480,1753,1478,1450,1452,1690,3971,3975,3977,6388,5114,1375,1373,4455,1359,1355,349,350,351,1399,1403,1411,1409,1401,1405,1407,1393,1395,1397,9249,9247,9244,1472,1727,1470,1468);
allVideoIndexes_obj.byDate_arr = new Array(9911,9909,9735,9596,9028,9374,9372,9370,9249,9247,9244,9144,8964,8885,8819,8777,8737,8425,8292,8289,8287,8241,8195,8198,8082,7989,7986,7761,7489,1379,1381,1383,1385,1387,7260,7074,7079,7076,6986,6750,6747,6745,6743,6741,6739,6737,6735,6733,6731,6752,3741,6716,6650,6646,6521,6518,6516,6388,6260,6263,6081,5882,5620,5522,5533,5530,5528,5526,5524,5422,5386,5162,4602,5111,5114,5109,4750,4455,4452,1855,4173,3971,3975,3977,3558,3561,2581,2300,2297,2274,2276,2271,2107,645,619,627,568,659,543,620,5151,544,611,569,310,349,523,350,351,353,352,354,355,5154,1815,1813,1520,1516,1518,249,5152,1811,5139,5137,1508,1510,1512,1514,1809,1920,5153,1807,1916,1918,5185,1502,1504,1805,5138,1913,1914,1803,1498,1500,1799,1911,5156,1600,1797,1907,1905,1903,1901,1899,1793,1897,1895,1893,1891,1889,1887,1496,1791,1885,1789,1588,1586,1787,1492,1494,1783,1883,1490,1775,1777,1779,1781,1881,1769,1771,1773,1765,1484,1486,1488,1763,1482,1755,1757,1879,1478,1480,1751,1753,1474,1476,1749,1747,1745,1743,1741,1739,1737,1735,5184,1733,1731,1466,1468,1470,1472,1727,1729,1875,1877,1725,1722,1720,1718,1716,1458,1460,1462,1464,1714,1865,1867,1869,1871,1873,1570,1712,1710,1863,1861,1859,1708,1706,1456,1857,1704,1702,5158,1454,1700,1698,5157,1696,1692,1694,1450,1452,1690,1853,1664,1666,1668,1670,1672,1674,1676,1678,1680,1682,1684,1686,1688,1443,1445,1447,1449,1851,1662,1439,1441,1660,1849,1658,1656,5187,1435,1437,1652,1654,1845,1847,1650,1843,1433,1646,1648,1644,5159,1431,1638,1640,1642,1841,5186,1429,1636,1839,1423,1425,1427,1604,1837,1606,1608,1610,1612,1614,1616,1618,1620,1622,1624,1626,1602,1421,1596,1598,1594,1590,1592,1419,1417,1413,1415,1582,1584,1835,1399,1401,1403,1405,1407,1409,1411,1393,1395,1397,1578,1580,1389,1391,1574,1576,1572,1833,1566,1568,1562,1564,1377,1558,1560,1831,1373,1375,1554,1556,1829,1371,1546,1548,1550,1827,1552,1353,1369,1534,1536,1538,1540,1542,1544,1821,1823,1825,1359,1532,1351,1530,1355,1817,1819,1357,1361,1365,1367);
// testing sort indicies
// var allVideoIndexes_obj = {};
// allVideoIndexes_obj.byName_arr = new Array(1296,1298,1300,1302);
// allVideoIndexes_obj.byCategory_arr = new Array(1296,1298,1302,1300);
// allVideoIndexes_obj.byDate_arr = new Array(1300,1302,1296,1298);

// ---- set up sorter ----
allVideoIndexes_obj.setUpSorter = function(args_arr){
    // -- initiate variables --
    
    // variables local to this function
    var curRowId = null;
    var curRowHTMLID = null;
    
    
    // variables for object
    this.ref_arr = [];
    // reference to the last inserted object
    this.lastInserted_ref = null;
    this.headRow_ref = null;
    this.par_ref = null;
    this.activeSort = [];
    this.activeSort['name'] = null;
    this.activeSort['ascending'] = true;
     
    this.statusImageRefs_arr = [];
    this.statusImageRefs_arr['name'] = null;
    this.statusImageRefs_arr['category'] = null;
    this.statusImageRefs_arr['date'] = null;
    this.statusImageSrcs_arr = [];
    // use this to count because string keys don't always increment correctly
    // this supplimental array is used instead
    this.statusMap_arr = ['name','category','date'];
    this.statusImageSrcs_arr['desc'] = null;
    this.statusImageSrcs_arr['asc'] = null;
    this.statusImageSrcs_arr['blank'] = null;
    
    this.statusImageRefs_arr['name'] = document.getElementById('nameStatusImage');
    this.statusImageRefs_arr['category'] = document.getElementById('categoryStatusImage');
    this.statusImageRefs_arr['date'] = document.getElementById('dateStatusImage');
    
    this.rowHTMLIDPrefix = 'not set';
    
    this.rowHtmlClasses = [];
    this.rowHtmlClasses['odd'] = null;
    this.rowHtmlClasses['even'] = null;
     
    this.byNameDesc_arr = [];
    this.byCategoryDesc_arr = [];
    this.byDateDesc_arr = [];
    this.matches_arr = [];

    this.lastestWasOdd = false;
    
    if(args_arr['initialSortName']){
        this.activeSort['name'] = args_arr['initialSortName'];
    }
     
    // avoided setting prefix to null as null will match anything in 
    // a prefix because null.length has length of zero
    // or will fail because null is not a string
    if(args_arr['rowHtmlClasses']['odd']){
        this.rowHtmlClasses['odd'] = args_arr['rowHtmlClasses']['odd'];
    }
    
    if(args_arr['rowHtmlClasses']['even']){
        this.rowHtmlClasses['even'] = args_arr['rowHtmlClasses']['even'];
    }
     
    if(args_arr['rowHTMLIDPrefix']){
        this.rowHTMLIDPrefix = args_arr['rowHTMLIDPrefix'];
    }
     
    if(args_arr['statusDesc']){
        this.statusImageSrcs_arr['desc'] = args_arr['statusDesc'];
        // testing code
        // alert('desc:' + this.statusImageSrcs_arr['desc']);
    }
    
    if(args_arr['statusAsc']){
        this.statusImageSrcs_arr['asc'] = args_arr['statusAsc'];
        // testing code
        // alert('asc:' + this.statusImageSrcs_arr['asc']);
    }
    
    if(args_arr['statusBlank']){
        this.statusImageSrcs_arr['blank'] = args_arr['statusBlank'];
        // testing code
        // alert('blank:' + this.statusImageSrcs_arr['blank']);
    }
    
    // create reference to the parent node 
    if(args_arr['parHTMLID']){
        this.par_ref = document.getElementById(args_arr['parHTMLID']);
        // testing code
        // alert(this.par_ref.id);
    }
    
    if(args_arr['searchInput'] != null){
        this.searchInp_ref = document.getElementById(args_arr['searchInput']);
    }else{
        this.searchInp_ref = null;
    }
    
    this.statusImg = [];
    
    if(args_arr['statusHTMLID'] != null){
        this.statusImg['ref'] = document.getElementById(args_arr['statusHTMLID']);
    }else{
        this.statusImg['ref'] = null;
    }
    
    this.statusImg['normalSrc'] = args_arr['statusSrc'];
    this.statusImg['backSrc'] = args_arr['statusBackSrc'];
    this.statusImg['backOverSrc'] = args_arr['statusBackOverSrc'];


    // if parent node not set return false 
    if(!this.par_ref){
        return false;
    }
        
    // testing code
    // alert('par ref id: ' + this.par_ref.id);
     
    // create descending indexes for each
    // byName_arr
    bi = this.byName_arr.length - 1;
    for(i=0 ; i<this.byName_arr.length ; i++){
       this.byNameDesc_arr.push(this.byName_arr[bi]);
       bi--; 
    }
     
    // testing code
    // alert(this.byNameDesc_arr.length);
     
    // byCategory_arr
    bi = this.byCategory_arr.length - 1;
    for(i=0 ; i<this.byCategory_arr.length ; i++){
       this.byCategoryDesc_arr.push(this.byCategory_arr[bi]);
       bi--; 
    }

    // testing code
    // alert(this.byNameDesc_arr.length);
    
    // byDate_arr
    bi = this.byDate_arr.length - 1;
    for(i=0 ; i<this.byDate_arr.length ; i++){
       this.byDateDesc_arr.push(this.byDate_arr[bi]);
       bi--; 
    }

    // testing code
    // alert(this.byNameDesc_arr.length);

    // create array of references

    // loop through byName_index and create the array keys 
    for(i=0 ; i<this.byName_arr.length ; i++){
        this.ref_arr[this.byName_arr[i]] = null;
    }
     
    // -- loop through the childnodes checking for the video row prefix -- 
    
    // get all child nodes of the parent element to the rows
    rows_arr = this.par_ref.childNodes;
            
    for(i=0;i < rows_arr.length; i++){
        // handle further if "Element" nodeType
        if(rows_arr[i].nodeType == 1){
            // alert('row id:' + rows_arr[i].id)
            // make the header references if header 
            if(rows_arr[i].id == 'allVideoHeader'){
                // testing code
                // alert('header found') ;
                 
                // create reference to the header row
                this.headRow_ref = rows_arr[i];
            }
            
            // scan for elemts whose id begings with the prefix 
            srch = this.rowHTMLIDPrefix;
            srchLen = srch.length;
            curRowHTMLID = rows_arr[i].id;
            if(curRowHTMLID.substring(0,srchLen) == srch){
                // grab the video id from the prefix
                curRowId = curRowHTMLID.substring(srchLen,rows_arr[i].id.length);
                // testing code
                // alert('video found:' + curRowId);
                // add as reference to the array 
                this.ref_arr[curRowId] = rows_arr[i];
            }
        }
    }
    
    // set initial sort
    if(this.activeSort['name'] != null){
         this.setActiveSort(this.activeSort['name'],this.activeSort['ascending']);
    }
}    

// ---- sorter function accepts index name
allVideoIndexes_obj.sortAllVideos = function(indexName){
    // initiate variablse local to this function
    var curRowRef = null;
    var nextSibling =  null;

    var indexToUse = [];

    // check against current sort, switch asc or desc if same
    // else if different set as  ascending sort
    if(indexName != this.activeSort['name']){
            this.activeSort['ascending'] = true;
    }else{
        if(this.activeSort['ascending']){
            this.activeSort['ascending'] = false;
        }else{
            this.activeSort['ascending'] = true;
        }
    }
    
    // set index to use variable by indexName argument
    if(indexName == 'name'){
        // check ascending or not and designate respective index for sorting
        if(this.activeSort['ascending']){
            indexToUse = this.byName_arr;
        }else{
            indexToUse = this.byNameDesc_arr;
        }
    }else if(indexName == 'category'){
        if(this.activeSort['ascending']){
            indexToUse = this.byCategory_arr;
        }else{
            indexToUse = this.byCategoryDesc_arr;
        }
    }else if(indexName == 'date'){
        if(this.activeSort['ascending']){
            indexToUse = this.byDate_arr;
        }else{
            indexToUse = this.byDateDesc_arr;
        }
    }
    
    // set the last inserted element ref to the header row
    this.lastInserted_ref = this.headRow_ref;
    
    // loop through index array
    for(i=0 ; i< indexToUse.length ; i++){
        curRowRef =  this.ref_arr[indexToUse[i]];
        // alert('video ' + i + ' under sort method ' + indexName + ' is ' + curRowRef.id );
        // move the video row div by adding it as a child element of the parent after the last inserted element
        
        // set background to be even or odd conditionally
        if(curRowRef.style.display != 'none'){
            this.labelOdd(curRowRef);
        }

        nextSibling = this.lastInserted_ref.nextSibling;
        this.par_ref.insertBefore(curRowRef,nextSibling);
        // testing code
        //this.par_ref.insertBefore(txt,this.lastInserted_ref);

        // make just inserted element last inserted
        this.lastInserted_ref = curRowRef;
        // repeat
    } 
         
    // setActive sort by calling set active sort function 
    this.setActiveSort(indexName,this.activeSort['ascending']);
}

// -- set the row background image to alternate between even and odd row colors
allVideoIndexes_obj.labelOdd = function(curRowRef){
    if(this.latestWasOdd){
        curRowRef.className = this.rowHtmlClasses['even'];
        this.latestWasOdd = false;
    }else{
        curRowRef.className = this.rowHtmlClasses['odd'];
        this.latestWasOdd = true;
    }
}

// -- show which category is visually sorted ----
allVideoIndexes_obj.setActiveSort = function(column,ascending){

    // initiate variables local to this function
    var activeSrc = '';
    var blankSrc = '';
    var curColumn = '';

    // type check the input arguments
    if(!(column == 'name' || column == 'category' || column == 'date')){
        return false;
    }else{

        this.activeSort['ascending'] = ascending;
        this.activeSort['name'] = column;

        // set image src url strings variables
        if(ascending){
            activeSrc = this.statusImageSrcs_arr['asc'];
        }else{
            activeSrc = this.statusImageSrcs_arr['desc'];
        }
        blankSrc = this.statusImageSrcs_arr['blank'];

        for(i=0 ; i<this.statusMap_arr.length ; i++){
            curColumn = this.statusMap_arr[i];
            if(curColumn == column){
                this.statusImageRefs_arr[curColumn].src = activeSrc;
            }else{
                this.statusImageRefs_arr[curColumn].src = blankSrc;
            }
        }
    }
}

// ---- search handler ----------
allVideoIndexes_obj.setToSearch = function(matches_arr,nonmatches_arr){

    var matchedCount = matches_arr.length;
    var notMatchedCount = nonmatches_arr.length;

    // alert(matchedCount + ' matches ' + notMatchedCount + 'nonmatches'); 

    // set the matches array 
    this.matches_arr = matches_arr;
    
    // loop through and hide non-matches 
    for(var i=0 ; i < nonmatches_arr.length; i++){
        curRowRef = this.ref_arr[nonmatches_arr[i]];
        curRowRef.style.display = 'none';
    }
    
    // loop through and order matches - relabel even odd
    for(var i=0 ; i < matches_arr.length ; i++){
        // alert('labeling');
        curRowRef = this.ref_arr[matches_arr[i]];
        curRowRef.style.display = 'block';
        this.labelOdd(curRowRef);
    }
    
    // show the search button
    this.setSearchStatus(true);

}

// -- clear search --
allVideoIndexes_obj.clearSearch = function(){
    var indexToUse = null;
    var curRowRef = null;
    // alert('clear search');
    
    // get the current sort method
    if(this.activeSort['name'] == 'name'){
        // check ascending or not and designate respective index for sorting
        if(this.activeSort['ascending']){
            indexToUse = this.byName_arr;
        }else{
            indexToUse = this.byNameDesc_arr;
        }
    }else if(this.activeSort['name'] == 'category'){
        if(this.activeSort['ascending']){
            indexToUse = this.byCategory_arr;
        }else{
            indexToUse = this.byCategoryDesc_arr;
        }
    }else if(this.activeSort['name'] == 'date'){
        if(this.activeSort['ascending']){
            indexToUse = this.byDate_arr;
        }else{
            indexToUse = this.byDateDesc_arr;
        }
    }


    // scroll through and set each row to visible
    for(i=0 ; i< indexToUse.length ; i++){
        curRowRef = this.ref_arr[indexToUse[i]];
        // make sure the node is visible - may have been hidden if 
        // not a match in a search
        curRowRef.style.display = 'block';
        // testing code
        // if(i < 20){
        //    alert('labeling');
        // }
        this.labelOdd(curRowRef);
    }

    // clear the search bar
    this.searchInp_ref.value = '';

    // hide the back to button
    this.setSearchStatus(false);
}

// -- function to set the status of weather a search is in order or not
allVideoIndexes_obj.setSearchStatus = function(active){
    
    // set image and any other display items for active search status
    if(active){
        this.statusImg['ref'].src = this.statusImg['backSrc'];
        this.statusImg['ref'].onmouseover = function(){
            this.src = allVideoIndexes_obj.statusImg['backOverSrc'];
        }
        this.statusImg['ref'].onmouseout = function(){
            this.src = allVideoIndexes_obj.statusImg['backSrc'];
        }
        this.statusImg['ref'].onclick = function(){
            allVideoIndexes_obj.clearSearch();
        }

    // set image and any other display items for non-search status
    }else{
        this.statusImg['ref'].src = this.statusImg['normalSrc'];
        this.statusImg['ref'].onmouseover = null;            
        this.statusImg['ref'].onmouseout = null;            
        this.statusImg['ref'].onclick = null;
    }

}

// ---- function wrappers to make calls globally accessible ----
function sortAllVideo(name){
    allVideoIndexes_obj.sortAllVideos(name);
}
