/****************************************************************
      Project selector
      requires: jQuery
      
      author:  erational <http://www.erational.org>
      version: 0.1
      date:    2007.05.07   
      
*******************************************************************/

// global parameters
url_skel = "./skel/";

// global var
filter = new Array();  // store filter

// --------------------------
// prototype
// --------------------------
// in_array 
// ref. http://www.codingforums.com/showthread.php?t=63796
Object.prototype.in_array = function(datum, strict) {
    if (strict) function equals(a,b) { return a === b }
    else function equals(a,b) { return a == b }

    for (var i in this) {
        if (equals(this[i], datum)) return true;
    }
    return false;
}

// adding it as a prototype object enables it to be used from any array
// ref. http://webdevel.blogspot.com/2003/07/javascript-remove-items-from-array.html
Array.prototype.removeItems = function(itemsToRemove) {

    if (!/Array/.test(itemsToRemove.constructor)) {
        itemsToRemove = [ itemsToRemove ];
    }

    var j;
    for (var i = 0; i < itemsToRemove.length; i++) {
        j = 0;
        while (j < this.length) {
            if (this[j] == itemsToRemove[i]) {
                this.splice(j, 1);
            } else {
                j++;
            }
        }
    }
}

// --------------------------
// function
// --------------------------

//
// simple rollover 
// 
function pimg(url_img)   {  if (document.images && url_img !="") document.images['project_preview_img'].src = url_img;}
function pimg_restore()  {  if (document.images) document.images['project_preview_img'].src = url_skel + "img/empty.gif";}

//
// add a filter
//
function add_filter(id_mot,id_cat) {
    // update cat    
    $("#cat_"+id_cat).empty();
    $("#cat_"+id_cat).append("<a href='#' onclick='remove_filter("+id_cat+");return false;'><img src='"+ url_skel + "img/but_close.png' alt='close' /></a>");
    for (i=0;i<category[id_cat].length;i++) {  // remove all keyword from this category
        filter.removeItems(category[id_cat][i]);
        $("#mot_"+category[id_cat][i]).removeClass("selected");
    }    
    // add filter
    filter.push(id_mot);
    $("#mot_"+id_mot).addClass("selected");
    
    // refresh GUI
    refresh_project();    
}

//
// remove a filter
//
function remove_filter(id_cat) {
    // update cat    
    $("#cat_"+id_cat).empty();
    for (i=0;i<category[id_cat].length;i++) {  // remove all keyword from this category
        filter.removeItems(category[id_cat][i]);
        $("#mot_"+category[id_cat][i]).removeClass("selected");
    }      
    // refresh GUI
    refresh_project();    
}



//
// display project (with filters)
//
function refresh_project() {
    //alert(filter.length);
    
    // scan all projects
    for (p in project) {
        //alert(p+":"+project[p]);
        found = true;
        for (i=0;i<filter.length; i++) {
           if (!project[p].in_array(filter[i])) found = false;            
        } 
        
        if (found) {           
            $("#art_"+p).show();           
        } else {
           $("#art_"+p).hide();        
          //bug if loaded dynamically
          // $(item).hide("slow");*/
          // callback ???
        }       
    }
}
