fw.libraries.skim = new Class({
	initialize:function(options){
		this.name = 'fw_libraries_Skim';
		this.mapName = 'fw_libraries_Skim_mapName';
		this.mapClass = 'fw_libraries_Skim_mapClass';
		this.mapId = 'fw_libraries_Skim_mapId';
		this.imageId = 'fw_libraries_Skim_imageId';
		this.imageClass = 'fw_libraries_Skim_imageClass';
		this.html = '';
		this.setOptions(options||{})
	},
	
	setOptions:function(options){
		var options = options || {};
		for(var k in options){
			this[k] = options[k];
		}
	},
	clear:function(){
		this.initialize({'images':[]});
	},
	
	createHtml:function(){	
		var str = '<img height="'+this.mapHeight+'" width="'+this.mapWidth+'" src="'+(this.defaultSrc?this.defaultSrc:this.images[0].src)+'" usemap="#'+this.mapName+'" id="'+this.imageId+'" class="'+this.imageClass+'" /><map id="'+this.mapId+'" name="'+this.mapName+'" class="'+this.mapClass+'">';
		this.addWidthInfo(this.images, Math.floor(this.mapWidth / this.images.length), this.mapWidth % this.images.length);
		var currentX = 0;
		for(var i=0; i<this.images.length; i++){
			str += this.getAreaTag(this.images[i], currentX);
			currentX += this.images[i].width;
		}
		this.html = str+'</map>';
	},
	
	getAreaTag:function(image, currentX){
		return '<area shape="rect" onmouseover="$id(\''+this.imageId+'\').src=\''+image.src+'\';" coords="'+currentX+',0,'+(image['width']+currentX)+','+this.mapHeight+'" href="'+((image['href'])?image['href']:this.defaultHref)+'" />';
	},
	
	addWidthInfo:function(images, width, numExtra){
		for(var i=0; i<images.length; i++){
			(numExtra-- > 0) ? images[i]['width'] = width+1 : images[i]['width'] = width;
		}
	},
	
	getElement:function(){
		var el = document.createElement('div');
		el.innerHTML = this.render();
		return el;
	},
	
	render:function(inContainer){
		if(!this.html) this.createHtml();
		if(inContainer){ 
			$html(inContainer, this.html);
		}else{
			return this.html;
		}
	
	}	
});

fw.libraries.jquery = new Class({
	initialize:function(){
		fwi = fw.getInstance();
		this.$ = jQuery.noConflict();
	}
});

fw.libraries.cookie = new Class({
	initialize:function(options){
		 this.domain = false;
		 this.path = false;
		this.duration = false;
		this.secure = false;
		$extend(this,options||{});
	},
	
	set:function(key, value, options){
		$extend(this,options||{});
		value = encodeURIComponent(value);
		if(this.domain) value += '; domain='+this.domain;
		if(this.path) value += '; path='+this.path;
		if(this.duration){
			var date = new Date();
			date.setTime(date.getTime() + this.duration * 24 * 60 * 60 * 1000);
			value += '; expires=' + date.toGMTString();		
		}
		if(this.secure) value += '; secure';
		document.cookie = key + '=' + value;
		return $extend(this, {'key':key, 'value':value});
	},
	
	get:function(key){
		var value = document.cookie.match('(?:^|;)\\s*' + key.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1') + '=([^;]*)');
		return value ? decodeURIComponent(value[1]) : false;
	},
	
	remove:function(cookie, options){
		if($type(cookie) == 'object') this.set(cookie.key, '', $merge(cookie, {duration:-1}));
		else this.set(cookie, '', $merge(options, {duration:-1}));
	}
	
});

fw.libraries.image_loader = new Class({
	initialize:function(options){
		this.loaded = [];
		this.all_loaded = false;
		this.images = [];
		this.not_loaded = [];
		this.onload = false;
		this.cancelTimer = false;
	},
	
	setOption:function(key,val){
		this[key] = val;
	},
	
	load:function(images, onload, progress){
		this.num_images = images.length;
		onload ? this.onload = onload : '';
		progress ? this.inflight = progress : '';
		$each(images, function(img, i){
			var image = {loaded:false}
			image.obj = new Image();
			image.obj.src = img.src;
			image.obj.onload = function(){
				image.loaded = true;
			}
			this.images.push(image);
		}, this);
		this.handleTimer();
	},
	
	allLoaded:function(){
		if(this.all_loaded) return true;
		return false;
	},
	
	numNotLoaded:function(){
		this.checkLoaded();
		return this.not_loaded.length;
	},
	
	handleTimer:function(){
		if(!this.cancelTimer && this.loading()){
			this.timer = $timer(this.handleTimer, 1500, this);
		}
	},

	cancel:function(){
		$timer(this.timer);
		this.cancelTimer = true;
	},
	
	loading:function(){
		this.not_loaded = [];
		this.loaded = [];		
		$each(this.images, function(img){
			img.loaded ?  this.loaded.push(img) : this.not_loaded.push(img);
		}, this);
			
		this.num_loaded = this.loaded.length;
		
		if(this.not_loaded.length < 1){
			this.all_loaded = true;
			this.finishedLoading.call(this);
			return false;
		}else{
			this.inflight.call(this, this.not_loaded.length);
		}
		return this.not_loaded.length;
	},
	
	finishedLoading:function(){
		if(this.onload) this.onload.call(this);
	}
});
