
function swapNodes(item1,item2) {

if (item1!=item2){

  //////////////////////////////////////////////////
  // We need a clone of the node we want to swap
  //////////////////////////////////////////////////
  var itemtmp = item1.cloneNode(1);

  // We also need the parentNode of the items we are going to swap.
  var parent = item1.parentNode;
  
  ///////////////////////////////////////////////////////////////////
  // First replace the second node with the copy of the first node
  // which returns a the new node
  ////////////////////////////////////////////////////////////////////
  item2 = parent.replaceChild(itemtmp,item2);


  ///////////////////////////////////////////////////////////////////
  //Then we need to replace the first node with the new second node
  ///////////////////////////////////////////////////////////////////
  parent.replaceChild(item2,item1);

  ////////////////////////////////////////////////////////////////////
  // And finally replace the first item with it's copy so that we
  // still use the old nodes but in the new order. This is the reason
  // we don't need to update our Behaviours since we still have
  // the same nodes.
  ////////////////////////////////////////////////////////////////////
  parent.replaceChild(item1,itemtmp);

  /////////////////////////////////////////////////////////////////////
  // Free up some memory, we don't want unused nodes in our document.
  /////////////////////////////////////////////////////////////////////
  itemtmp = null;
  
  }

}

function swapElement(array,pos1,pos2){
		
	temp = array[pos1];
	
	swapNodes(array[pos1],array[pos2])
	
	array[pos1] = array[pos2];
	
	array[pos2] = temp;
		
	return array;
}

function partition(array, begin, end, pivot)
{
        var piv;
 
        a = array[pivot].id.split("_");

	//var piv=a[index].toLowerCase();

        //////////////////////////////////
        // if this is the first position
        // base 64 decode it
        //////////////////////////////////
        if (index==0){

          piv = Base64.decode(a[index]);

        }else{

          piv = a[index];

        }

	piv = piv.toLowerCase();
	
	array = swapElement(array,pivot, end-1);
	
	var store=begin;
	
	var ix;
	
	for(ix=begin; ix<end-1; ++ix) {
	
	    b = array[ix].id.split("_");
	    
            //////////////////////////////////
            // if this is the first position
            // base 64 decode it
            //////////////////////////////////
            if (index==0){
    
              b[0] = Base64.decode(b[0]);
    
            }
    
                if (sortOrder == 0 ){

		  if(b[index].toLowerCase()<=piv) {
	
			array = swapElement(array, store, ix);
	
			++store;
		  }


                }else{


		  if(b[index].toLowerCase()>piv) {
	
			array = swapElement(array, store, ix);
	
			++store;
		  }


                }
	}

	array = swapElement(array, end-1, store);

	return store;
}

var index     =0;
var sortOrder =0;

function qsort(array, begin, end)
{
	if(end-1>begin && index>=0) {
	 
		var pivot=begin+Math.floor(Math.random()*(end-begin));

		pivot=partition(array, begin, end, pivot);

		qsort(array, begin, pivot);

		qsort(array, pivot+1, end);
	}
}



function quick_sort(array)
{
    ///////////////////////// 
    // Check the sort order
    ///////////////////////// 
    if (index == 2){

      sortOrder = 1;
 
    }else{

      sortOrder = 0;

    }


    qsort(array, 0, array.length);

}

function showArray(array){
	
	for (i=0;i<array.length;i++){
		
		update(array[i].id);
		
	}
}

/////////////////////////////
// Turn on automatic filter
/////////////////////////////
setInterval(filter,1000);


/////////////////////////////
// Record the last filter
/////////////////////////////
var last_filter;

//////////////////////
// List Item Filter
//////////////////////
function filter(){
   
  current_filter = document.getElementById("filter").value;
    
  if (current_filter!=last_filter){

     items = document.getElementById("media");

     listItems = $('media').immediateDescendants();

     for (i=0;i<listItems.length;i++){

       listItemName = listItems[i].id.split("_");

       searchItem = listItemName[0];

       searchItem = Base64.decode(listItemName[0]);
  
       if(searchItem.toLowerCase().indexOf(current_filter.toLowerCase())>=0){

        $(listItems[i].id).style.display="block";
		
       }else{
	
         $(listItems[i].id).style.display="none";
	
       }


    } 
		
  }	
  
  //////////////////////////////////////
  // Save the this item as the last
  // filter
  //////////////////////////////////////
  last_filter = current_filter;

}



function update(text){
	
	document.getElementById("search").innerHTML=document.getElementById("search").innerHTML + "<br>" + text;
}


function setIndex(index_value){
	
	index=index_value
}
