// imgselector
//
// imgselector cloud v0.5 - jQuery effect
// Copyright (c) 2009 Peter Medl
//
// License: MIT and GPL
// http://www.opensource.org/licenses/mit-license.php
// http://www.gnu.org/licenses/gpl.html
// do anything you want with the code but don`t claim it`s yours
// Homebase: http://www.atomad.de
//
// Another image slider/carousel/show
//
// Usage (from html doc)
// takes list <li> with (thumbnail) img entires and displays
// animated inline, click/hover to show parent full img 
// 
// var options = {
//  color: '#808080',
//  speed: 100
// }
// $('myDiv').imgselector(options);
//
// add CSS entry to style area 
// .imgselector{
//	position: relative;
//	width: 560px;
//	height: 120px;
//	border: 0px solid #f00;
//	overflow: hidden;
//}
//
//.imgselthumb{
//      position: absolute;
//}


(function($) { 
$.fn.imgselector = function(options) {
	var config = {'speed': '100','height': '120','width':'160','hovercallback':null,'clickcallback':null,'mousesensivity':null};
	selectorobj = function(hcb,ccb){
		this.items = Array();
		this.timer = null;
		this.pos = 0;
		this.maxright = 0;
		this.diff = 10;
		this.impact = 0;
		this.dircetion = 1;
		this.hovercallback = hcb;
		this.clickcallback = ccb;
		this.mousesensivity = 30;
		this.arrowspeed = 0;
		this.spread = 0;
		this.xcenter = 0;
		this.ycenter = 0;
	}
	imgitem = function(id,href)
	{
  		this.id = id;
		this.x = 0;
		this.y = 0;
		this.w = 0;
		this.h = 0;
		this.href = href;
	}
	theselector = new selectorobj(options['hovercallback'],options['clickcallback']); 
   	if (options) $.extend(config, options);

	return this.each(function() {
		var myobj = $(this);
		// get all li
		var myimages = $("li img", myobj); 
		
		// iterate through resolution number of img
		for (i=0;i<myimages.length;i++){
			//alert(myimages.eq(i).attr('src'));
			var mydiv ='<div class="imgselthumb" id="imgsel'+i+'"></div';

			myobj.append(mydiv);
			
			var newimg = new Image();
			newimg.src = myimages.eq(i).attr('src');
			$(newimg).attr('alt',myimages.eq(i).attr('alt'));
			$(newimg).attr('caction',myimages.eq(i).attr('caction'));
			// preload fullimg
			var cachedimg = new Image();
			cachedimg.src=myimages.eq(i).attr('fullimg');					  
			// add img to div
			$('#imgsel'+i).append(newimg);
			theselector.items[theselector.items.length]= new imgitem("imgsel"+i,"#");
			theselector.items[theselector.items.length-1].x = i*(options['width']+theselector.diff);
			theselector.items[theselector.items.length-1].y = options['height']/2;
			// bind callbacks to div
			$('#imgsel'+i).live('click',theselector.clickcallback);
			$('#imgsel'+i).live('mouseover',theselector.hovercallback);
		  }
		  // movement options + mouse detection
		  $(myobj).mousemove(function(e){
				if(theselector.mousesensivity>0){			
					newimpact = (e.pageX - ($(myobj).width()/2));
					theselector.impact = newimpact/theselector.mousesensivity;
				}
			});
			
		  // finally remove org list
		  $("li", myobj).remove();
		  $("ul", myobj).remove();
		  theselector.timer = setInterval(anim,options['speed']);
		  // adjust virtual max div border
		  theselector.maxright = (theselector.items.length-1)*(options['width']+theselector.diff);
		  theselector.spread = (theselector.maxright-(2*(options['width']+theselector.diff)))/2
		  theselector.xcenter = options['width']/2
		  theselector.ycenter = options['height']/2;
      });
	
	
// timer effect function
	function anim(){
		// decrease impact
		theselector.impact*=0.992;
		//var thespread = theselector.maxright-(2*(options['width']+theselector.diff));
		for (i=0;i<theselector.items.length;i++){
			
			theselector.items[i].x +=theselector.impact;
			//left out
			if (theselector.items[i].x<-(options['width']+theselector.diff))theselector.items[i].x = theselector.maxright;
			// right out
			if (theselector.items[i].x>theselector.maxright)theselector.items[i].x = -(options['width']+theselector.diff);
			$('#imgsel'+i).css('left',theselector.items[i].x-theselector.xcenter+"px");
			$('#imgsel'+i).css('top',theselector.items[i].y-theselector.ycenter+"px")
			// opacity mid=1.0 left or right 0.x
			var xtrans = theselector.items[i].x-(theselector.spread/2);
			//$('#debug').text(thespread);
			var of = 1.00000-Math.abs(((theselector.items[i].x-(theselector.spread/2))/(theselector.spread/2)));
			if (of>1.00000)of=1.00000;
			//var thewidth = $('#imgsel'+i).width();
			//$('#imgsel'+i+' img').width(20+(thewidth*of));
			$('#imgsel'+i).css('opacity',of+0.1);
			//if(i==0)$('#debug').text(thewidth*of);
		}
	}
	// 
	function myCenter(thedivid){
		var center = new Array();
		// j is to slow here
		// center['x'] = $('#'+thedivid).width()/2;
		// center['y'] = $('#'+thedivid).height()/2;
		center['x'] = theselector.xcenter;
		center['y'] = theselector.ycenter;
		return center;
	}
	
 ;}
 
 $.fn.setMouseSens = function(n) { 
 	theselector.mousesensivity = n;
 }
 
  $.fn.setArrowSpeed = function(n) { 
	theselector.impact=n;
 }


})(jQuery);

