if (typeof(Ascribe) == "undefined") {
  var Ascribe = {};
}

if (typeof(Ascribe.ListSwitcher) == "undefined") {
  Ascribe.ListSwitcher = Class.create({
     initialize: function(config){
       this.id = 0;
       this.project_list = new Hash;
       this.projects = new Hash;
      
       // config variables from object creation
       this.menu_id = config.menu_id || "list-switcher-menu";
       this.list_id = config.list_id || "list-switcher-list";
       this.slider = config.slider || false;
       this.list_output = config.list_output || this.default_list_output;
       this.possible = config.menu_items || new Array('#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
       this.create_key = config.create_key || this.default_create_key;

       this.menu_sort = false;
       if (typeof(config.menu_sort) != "undefined") { 
         this.menu_sort = config.menu_sort;
       }
       
       this.inject_keys = true;
       if (typeof(config.inject_keys) != "undefined") { 
         this.inject_keys = config.inject_keys;
       }
       
       this.write_menu.bind(this);
       this.jar = new CookieJar();
     },
   
    default_create_key: function(project) {    
      if (project.title.charCodeAt(0) >= 48 && project.title.charCodeAt(0) < 58) {
        key = "#"
      }
      else {
        key = project.title.charAt(0).toUpperCase();
      }

      return key;
    },

    create_project: function(values) {
      var keys = this.create_key(values);
      if (keys == false || keys == "") {
        return false;
      }
      
      if (typeof(keys) != "object") {
        keys = [keys];
      }

      this.id++;
      values.id = this.id;
      this.project_list.set(this.id, values);
      
      keys.each((function(key){
        // initialize project if needed      
        if (this.inject_keys && !this.possible.include(key)) {
          //console.log(values.title + " => " + key);
          this.possible.push(key);
        }
      
        if ( !this.projects.get(key)) {
          this.projects.set(key, new Array);
        }

        this.projects.get(key).push(this.id);
      }).bind(this));
    },

    write_menu: function(load_first) {
      var output = "";
      var used = this.projects.keys();
      if (this.menu_sort) {
        this.possible = this.possible.sortBy(this.menu_sort);
      }
      
      this.possible.each(function(item){
        if (used.include(item)) {
          output += '<li id="entry-' + item + '" class="available">' + item + '</li>';
        }
        else {
          output += '<li class="unavailable">' + item + '</li>';
        }
      });

      $(this.menu_id).update("<ul>" + output + "</ul>");
    
      $$('.available').each((function(item){
        Event.observe(item, 'click', this.menu_click_callback.bindAsEventListener(this));
      }).bind(this));

      /* history */
      if (this.jar.get('list')){
        this.write_list(this.jar.get('list'));
      }
      else if (load_first == true) {
        this.first_list();
      }
    },
  
    menu_click_callback: function(event) {
      var item = event.element();
      this.write_list(item.innerHTML);
      
      if (this.current) {
        this.current.removeClassName('selected');
      }
      
      item.addClassName('selected');
      this.current = item;
    },

    write_list: function(index) {
      var output = "";
      var counter = 0;
      
      if (!this.projects.get(index) && this.jar.get('list') == index) {
        this.first_list();
        return;
      }
      if (this.projects.get(index)) {
        this.projects.get(index).each((function(item) {
          counter++;
          project = this.project(item);
          output += this.list_output(project, counter);
        }).bind(this));
      
        var item = $('entry-' + index);

        if (this.current) {
          this.current.removeClassName('selected');
        }

        item.addClassName('selected');
        this.current = item;
        this.jar.put('list', item.innerHTML);
      }      
      
      if (output == "") {
        output = "<li>No projects found</li>";
      }

      $(this.list_id).update(output);
      
      if (this.slider) {
        var slider = this.slider;
      
        // disable vertical scrolling if text doesn't overflow the div
        if ($('portfolio-scroller').scrollHeight <= $('portfolio-scroller').offsetHeight) {
      		slider.setDisabled();
      		$('portfolio-scroller-track').hide();
      	}
      	else {
      	  slider.setEnabled();
      		$('portfolio-scroller-track').show();
      	}
    	
      	// reset scroller window and slider to top
    	  $('portfolio-scroller').scrollTop = 0;             // window
    	  slider.setValue(slider.translateToValue(0));       // slider
	    }
    },
  
    project: function(index) {
      return this.project_list.get(index);
    },
  
    default_list_output: function(project, counter){
      var output = '<li class="' + (counter % 2 == 0 ? '' : 'alternate') + '">';
      output += '<a href="' + project.path + '" title="' + project.title + ' ">' + project.title + '</a>';
  		output += '</li>';
  		return output;
    },
    
    first_list: function() {
      var first_id = this.projects.keys().sort().first();
      this.write_list(first_id);
    }
  });
}


/**
 * Javascript code to store data as JSON strings in cookies. 
 * It uses prototype.js 1.5.1 (http://www.prototypejs.org)
 * 
 * Author : Lalit Patel
 * Website: http://www.lalit.org/lab/jsoncookies
 * License: Creative Commons Attribution-ShareAlike 2.5
 *          http://creativecommons.org/licenses/by-sa/2.5/
 * Version: 0.4
 * Updated: Aug 11, 2007 10:09am
 * 
 * Chnage Log:
 *   v 0.4
 *   -  Removed a extra comma in options (was breaking in IE and Opera). (Thanks Jason)
 *   -  Removed the parameter name from the initialize function
 *   -  Changed the way expires date was being calculated. (Thanks David)
 *   v 0.3
 *   -  Removed dependancy on json.js (http://www.json.org/json.js)
 *   -  empty() function only deletes the cookies set by CookieJar
 */

if (typeof(CookieJar) == 'undefined') {
  var CookieJar = Class.create();

  CookieJar.prototype = {

  	/**
  	 * Append before all cookie names to differntiate them.
  	 */
  	appendString: "__CJ_",

  	/**
  	 * Initializes the cookie jar with the options.
  	 */
  	initialize: function(options) {

  		this.options = {
  			expires: 3600,		// seconds (1 hr)
  			path: '',			// cookie path
  			domain: '',			// cookie domain
  			secure: ''			// secure ?
  		};
  		Object.extend(this.options, options || {});

  		if (this.options.expires != '') {
  			var date = new Date();
  			date = new Date(date.getTime() + (this.options.expires * 1000));
  			this.options.expires = '; expires=' + date.toGMTString();
  		}
  		if (this.options.path != '') {
  			this.options.path = '; path=' + escape(this.options.path);
  		}
  		if (this.options.domain != '') {
  			this.options.domain = '; domain=' + escape(this.options.domain);
  		}
  		if (this.options.secure == 'secure') {
  			this.options.secure = '; secure';
  		} else {
  			this.options.secure = '';
  		}
  	},

  	/**
  	 * Adds a name values pair.
  	 */
  	put: function(name, value) {

  		name = this.appendString + name;
  		cookie = this.options;
  		var type = typeof value;
  		switch(type) {
  		  case 'undefined':
  		  case 'function' :
  		  case 'unknown'  : return false;
  		  case 'boolean'  : 
  		  case 'string'   : 
  		  case 'number'   : value = String(value.toString());
  		}
  		var cookie_str = name + "=" + escape(Object.toJSON(value));
  		try {
  			document.cookie = cookie_str + cookie.expires + cookie.path + cookie.domain + cookie.secure;
  		} catch (e) {
  			return false;
  		}
  		return true;
  	},

  	/**
  	 * Removes a particular cookie (name value pair) form the Cookie Jar.
  	 */
  	remove: function(name) {

  		name = this.appendString + name;
  		cookie = this.options;
  		try {
  			var date = new Date();
  			date.setTime(date.getTime() - (3600 * 1000));
  			var expires = '; expires=' + date.toGMTString();
  			document.cookie = name + "=" + expires + cookie.path + cookie.domain + cookie.secure;
  		} catch (e) {
  			return false;
  		}
  		return true;
  	},

  	/**
  	 * Return a particular cookie by name;
  	 */
  	get: function(name) {

  		name = this.appendString + name;
  		var cookies = document.cookie.match(name + '=(.*?)(;|$)');
  		if (cookies) {
  			return (unescape(cookies[1])).evalJSON();
  		} else {
  			return null;
  		}
  	},

  	/**
  	 * Empties the Cookie Jar. Deletes all the cookies.
  	 */
  	empty: function() {

  		keys = this.getKeys();
  		size = keys.size();
  		for(i=0; i<size; i++) {
  			this.remove(keys[i]);
  		}
  	},

  	/**
  	 * Returns all cookies as a single object
  	 */
  	getPack: function() {

  		pack = {};
  		keys = this.getKeys();

  		size = keys.size();
  		for(i=0; i<size; i++) {
  			pack[keys[i]] = this.get(keys[i]);
  		}
  		return pack;
  	},

  	/**
  	 * Returns all keys.
  	 */
  	getKeys: function() {

  		keys = $A();
  		keyRe= /[^=; ]+(?=\=)/g;
  		str  = document.cookie;
  		CJRe = new RegExp("^" + this.appendString);
  		while((match = keyRe.exec(str)) != undefined) {
  			if (CJRe.test(match[0].strip())) {
  				keys.push(match[0].strip().gsub("^" + this.appendString,""));
  			}
  		}
  		return keys;
  	}
  };
}
