/*
jQuery common scripts for Thumbail Circle 
- Sukiman, suki@webexcellence.net, http://www.webexcellence.net/
*/
(function($) {
	$.loewy = $.loewy || {};
	
	$.fn.loewyCycle = function(o) {
		return this.each(function() {
			new $.loewy.cycle(this,o);
		});
	}
	
	$.fn.loewyCycleDestroy = function(o) {
		return this.each(function() {
			obj = $.data(this,"loewyCycle");
			if (obj) {
				obj.destroy();
			}
		});
	}
	
	$.loewy.cycle = function(ele, o) {
		var self = this;
		this.options = $.extend({ 
			width: 152,
			height: 152,
			cycleSpeed: 800,
			mouseoverDelay: 500
		},o);
		this.self = this;
		this.p = ele;
		this.f = false;
		this.bAnim = false;
		this.cur = 0;
		this.bOver = false;
		this.smove = 0;
		this.overdelay = 0;
		this.offset = $(ele).offset();
		this.bound = {
			left: this.offset.left,
			top: this.offset.top,
			right: this.offset.left + this.options.width,
			bottom: this.offset.top + this.options.height
		};
		
		$.data(ele, "loewyCycle", this);
		
		this.mousemove = function(e) {
			//self._mousemove(self,e);
			
			if (!self.bOver) { return; }
			
			b = false
			if(e.pageX >= self.bound.left && e.pageX <= self.bound.right) {
				if (e.pageY >= self.bound.top && e.pageY <= self.bound.bottom) {
					b = true;
				}
			}
			if (!b && self.bOver) {
				self.bOver = false;
				
				if (self.overdelay > 0) {
					window.clearTimeout(self.overdelay);
				}
				
				$(self.e).unbind("mousemove", self.mousemove);
				
				if(!self.bAnim) {
					if (self.cur == 1) {
						self.cycle();
					}
				}
			}
		};
		
		this.mouseover = function(e) {
			if (!self.bOver) {
				$(self.e).bind("mousemove", self.mousemove);
				
				self.bOver = true;
				self.smove = (new Date()).getTime();
				
				if (self.overdelay > 0) {
					window.clearTimeout(self.overdelay);
				}
				
				if(self.options.mouseoverDelay && self.options.mouseoverDelay > 0) {
					self.overdelay = window.setTimeout( function() {
						if (!self.bAnim) {
							if (self.bOver && self.cur == 0) {
								self.cycle();	
							}
						}
					}, self.options.mouseoverDelay);
				}
			}
		};
		
		this.click = function() {
			if ($(self.p).attr("rel")) {
				document.location.href = $(self.p).attr("rel");
			}
			return false;
		};
		
		this.cycle = function(){
			var opt = self.options;
			
			self.bAnim = true;
			
			self.container.animate({left:self.xs1+"px"}, self.xss, "");
			self.container.queue(function(){
				$(this).animate({left:self.xs2+"px"}, self.xs3, "");
				$(this).dequeue();
			});
			self.container.queue(function(){
				$(this).animate({left:0}, opt.xss,"", function() {
					self.switchFrame();
					self.bAnim = false;
					if(self.cur == 1 && !self.bOver) {
						self.cycle();
					}
					if(self.cur == 0 && self.bOver) {
						self.cycle();
					}
				});
				$(this).dequeue();
			});
		};
		
		this.init();
	}
	
	$.extend($.loewy.cycle.prototype, {
		init: function() {
			var self = this;
			var opt = self.options;
			
			if (opt.width == undefined || opt.width < 1) {
				alert("Invalid width parameter");
				return;
			}
			
			self.thumbImage = $(self.p).find(".thumbImage");
			self.thumbInfo = $(self.p).find(".thumbInfo");
			
			self.thumbImage.css({left:opt.width+"px", top:"0px"});
			self.thumbInfo.css({left:"0px", top:"0px"});
			
			//some precalculation
			self.xs1 = -opt.width - Math.round(opt.width/10);
			self.xs2 = Math.round(opt.width/10);
			
			self.xss = Math.round(opt.cycleSpeed/7);
			self.xss2 = opt.cycleSpeed - self.xss;
			//
			
			s = "width:"+(opt.width*3)+"px; height:"+opt.height+"px";
			
			$(self.p).wrapInner('<div class="cycleClip" style="width:'+opt.width+'px; height:'+opt.height+'px; overflow: hidden; position: relative; left:0; top:0;"><div style="'+s+'; position: relative; left:-'+opt.width+'px; top:0" class="cycleContainer"></div></div>');
			self.container = $(self.p).find(".cycleContainer");
			
			$(($(self.p).find(".cycleClip"))[0]).bind("mouseover", self.mouseover);
			
			$(self.p).bind("click", self.click);
		},
		
		destroy: function() {
			var self = this;
			var opt = self.options;
			$(($(self.p).find(".cycleClip"))[0]).unbind("mouseover", self.mouseover);
			$(self.e).bind("mousemove", self.mousemove);
			$(self.p).unbind("click", self.click);
		},
		
		switchFrame: function() {
			var self = this;
			var opt = self.options;
			if (self.cur==0) {
				
				//$(self.p).find(".thumbImage").css({left:"0px", top:"0px"});
				//$(self.p).find(".thumbInfo").css({left:opt.width+"px", top:"0px"});
				self.thumbImage.css({left:"0px", top:"0px"});
				self.thumbInfo.css({left:opt.width+"px", top:"0px"});
				
				self.container.css({left:-opt.width+"px"});
				
				self.cur = 1;
			} else {
				//$(self.p).find(".thumbImage").css({left:opt.width+"px", top:"0px"});
				//$(self.p).find(".thumbInfo").css({left:"0px", top:"0px"});
				self.thumbImage.css({left:opt.width+"px", top:"0px"});
				self.thumbInfo.css({left:"0px", top:"0px"});
				self.container.css({left:-opt.width+"px"});
				self.cur = 0;
			}
		},
		
		cycle: function(){
			var self = this;
			var opt = self.options;
			
			self.bAnim = true;
			
			self.container.animate({left:self.xs1+"px"}, self.xss, "easeIn");
			self.container.queue(function(){
				$(this).animate({left:self.xs2+"px"}, self.xs3, "");
				$(this).dequeue();
			});
			self.container.queue(function(){
				$(this).animate({left:0}, opt.xss,"easeIn", function() {
					self.switchFrame();
					self.bAnim = false;
					if(self.cur == 1 && !self.bOver) {
						self.cycle();
					}
					if(self.cur == 0 && self.bOver) {
						self.cycle();
					}
				});
				$(this).dequeue();
			});
		}		
	});
})($);