﻿/* 
Version 1.0
Copyright SABIS® Creative 2009
http://www.sabis.net/creative
*/

(function($){
	$.fn.tooltips = function(options, selector){
		$.tooltips = this;
		an = 1;
		selector = selector || "*[rel^='tooltip']";
		this.els = $(selector);
		$.extend(this, {
			duration: 200,
			openEasing: "easeInCubic",
			closeEasing: "easeOutCubic"
		}, options);
		this.tips = {};
		
		this.els.each(function(i, el){
			el.tipText = el.alt || el.title;
			el.alt = el.title = "";
			el.tip = $(document.createElement("div")).html("<p>" + el.tipText + "</p>");
			$(document.body).append(el.tip);
			el.tip._parent = el;
			el.id = "tooltip" + (an++);
			el.tip.addClass("tooltip");
			var $el = $(el);
			el.tip._show = function(){
				el.tip.css({
					visibility: "visible",
					top: $el.offset().top + 5,
					left: $el.offset().left + $el.width() - 4
				});
				el.tip.animate({width:250}, { 
					queue: false, 
					duration: $.tooltips.duration,
					easing: $.tooltips.openEasing
				});
				$.tooltips.tips[el.tip.id] = el.tip;
			};
			el.tip._hide = function(e, id){
				el.tip.animate({width: 0}, {
					queue: false,
					duration: $.tooltips.duration,
					easing: $.tooltips.closeEasing,
					complete: function(){
						delete $.tooltips.tips[el.tip.id];
					}
				});
			};
			$el.bind("mouseover", function(e){
				clearTimeout(this._timer);
				this.tip._show();
			});
			$el.bind("mouseout", function(e){
				this._timer = window.setTimeout(this.tip._hide, 10);
			});
			el.tip.bind("mouseover", function(e){
				clearTimeout(el.tip._parent._timer);
			});
			el.tip.bind("mouseout", function(e){
				el.tip._parent._timer = window.setTimeout(el.tip._hide, 10);
			});
		});
	};
	$.fn.accordian = function(selector, options){
		var defaults = {
			direction: "horizontal",
			maxWidth: 0,
			maxHeight: 0,
			animDuration: 300,
			easing: null,
			an: 0
		};
		$.extend(this, defaults, options);
		if(!selector)
			return;
		this.selector = selector;
		this.els = $(this.selector +" a.nav");
		
		this.els.bind("click", this, function(e){
			if(!this._parent){
				this._parent = this.parentNode;
				while(this._parent != null && this._parent.nodeName.toLowerCase() != "div")
					this._parent = this._parent.parentNode;
			}
			if(!this._parent)
				return false;
			var ac = e.data;
			if(ac.lastBlock && ac.lastBlock.an == this.an)
				return false;
				
			var p = $(this._parent);
			
			with(p){
				if(ac.direction == "horizontal")
					animate({width: ac.maxWidth}, {
						queue:false, 
						duration: ac.animDuration,
						easing: ac.easing
					});
				else
					animate({height: ac.maxHeight}, {
						queue:false, 
						duration: ac.animDuration,
						easing: ac.easing
					});
					
				animate({opacity: 1}, {queue:false, duration: ac.animDuration});
			}
						
			if(ac.lastBlock){
				with(ac.lastBlock){
					if(ac.direction == "horizontal")
						animate({width: navWidth}, {
							queue:false, 
							duration: ac.animDuration,
							easing: ac.easing
						});
					else
						animate({height: navHeight}, {
							queue:false, 
							duration: ac.animDuration,
							easing: ac.easing
						});
						
					animate({opacity: .75}, {queue:false, duration: ac.animDuration});
				}
			}
			ac.lastBlock = $.extend(p, {navHeight: $(this).height(), navWidth: $(this).width(), an: this.an});
			return false;
		});
		var ac = this;
		this.els.each(function(e, el){
			el.an = ac.an++;
		});
		var i = Math.round(Math.random()*(this.els.length - 1));
		$(this.els[0]).trigger("click");
	};
	$.fn.popup = function(selector, options){
		$.popups = this;
		var defaults = {
			direction: "horizontal",
			maxWidth: 0,
			maxHeight: 0,
			animDuration: 200,
			easing: "easeInCubic",
			an: 0
		};
		$.extend(this, defaults, options);
		this.selector = selector || "a[rel='popup']";
		if(!this.selector)
			return;
		this.els = $(this.selector);
		this.els.each(function(i, el){
			el.content = "";
			el.fill = function(data){
				$.popups.tbody.removeClass("loading");
				$.popups.tbody.html(data);
			};
		});
		this.els.bind("click", function(){
			with($.popups){
				overlay._show();
				loading();
			};
			if(!this.content){
				$.get(this.href + "?template=neutre", {}, this.fill);
			}
			return false;
		});
		var o = $.popups.overlay = $(document.createElement("div"));
		o.addClass("overlay");
		o._show = function(){
			this.css({
				width: GetViewWidth(true),
				height: GetViewHeight(true),
				opacity: 0,
				visibility: "visible",
				display: "block"
			});
			this.animate({opacity: .8} ,{queue: false, duration: 600});
		}
		$(document.body).append(o);
		var p = $.popups.p = $(document.createElement("div"));
		p.addClass("popup");
		p.html(["<div class=\"top\"><div class=close title=\"Close\"></div></div>",
			"<div class=pbody><div class=t></div><div class=f></div></div>",
			"<div class=bottom></div>"].join(""));
		
		$(document.body).append(p);
		this.tbody = $(".popup .pbody .t");
		this.loading = function(){
			this.tbody.html("");
			this.p.css({
				left: parseInt(GetViewWidth(true)/2 - this.p.width()/2),
				top: -600,
				display: "block",
				visibility: "visible"
			});
			this.tbody.addClass("loading");
			this.p.animate({
				top: parseInt(GetViewHeight(true)/2 -  this.p.height()/2)
			}, {
				duration: 1000,
				easing: "easeInOutBounce"
			});
		};
		this.close = $(".popup .close");
		this._close = function(){
			$.popups.p.animate({
				top: -600
			}, {
				duration: 400,
				easing: "easeOutCirc"
			});
			$.popups.overlay.animate({
				opacity: 0
			}, {
				duration: 400, 
				complete: function(){
					$.popups.overlay.css({
						display: "none"
					});
				}
			});
		}
		this.close.bind("click", this._close);
		$(document.body).bind("keyup", function(e){
			if(e.keyCode === 27)
				$.popups._close();
		});
	};
	$.fn.dropmenus = function(options, selector){
		$.dropmenus = this;
		an = 1;
		selector = selector || "*[rel^='drop']";
		this.els = $(selector);
		$.extend(this, {
			openDuration: 600,
			closeDuration: 200,
			openEasing: "easeOutBounce",
			closeEasing: "easeOutCubic",
			hideDuration: 100
		}, options);
		this.menus = {};
		
		this.els.each(function(i, el){
			el.menu = ($(el.parentNode)).children("div");
			var $el = $(el);
			if(!el.menu)
				return;
			$.extend (el.menu, {
				_anim: $el.attr("rel").split("/").length == 2? $el.attr("rel").split("/")[1]: "height",
				_parent: el,
				_height: null,
				_width: null,
				id: "DropMenu" + (an++),
				_show: function(){
					with(el.menu)
					{
						if($.dropmenus.menus[id])
							return;
						if(_height === null || _width === null)
						{
							_height = height();
							_width = width();
						}
						css("visibility", "visible");
						css(_anim, 0);
						var aObj = _anim == "height"? {height: _height}: {width: _width};
						animate(aObj, { 
							queue: false, 
							duration: $.dropmenus.openDuration,
							easing: $.dropmenus.openEasing,
							complete: function(){
								if(!$.dropmenus.menus[id])
									_parent._timer = window.setTimeout(_hide, $.dropmenus.hideDuration);
							}
						});
						$.dropmenus.menus[id] = el.menu;
					}
				},
				_hide: function(e, id){
					var animObj = el.menu._anim == "height"? {height: 0}: {width: 0};
					delete $.dropmenus.menus[el.menu.id];
					el.menu.animate(animObj, {
						queue: false,
						duration: $.dropmenus.closeDuration,
						easing: $.dropmenus.closeEasing,
						complete: function(){
							//delete $.dropmenus.menus[el.menu.id];
						}
					});
				}
			});
			$el.bind("mouseover", function(e){
				clearTimeout(this._timer);
				this.menu._show();
			});
			$el.bind("mouseout", function(e){
				this._timer = window.setTimeout(this.menu._hide, $.dropmenus.hideDuration);
			});
			el.menu.bind("mouseover", function(e){
				clearTimeout(el.menu._parent._timer);
			});
			el.menu.bind("mouseout", function(e){
				el.menu._parent._timer = window.setTimeout(el.menu._hide, $.dropmenus.hideDuration);
			});
		});
	};
	$.fn.gallery = function(side, content, desc, defaults){
		$.gallery = this;
		$.extend(this, {
				_content: $(content),
				_desc: $(desc),
				_side: $(side),
				_list: $(side + " .list"),
				_up: $(side + " .up"),
				_down: $(side + " .down"),
				_speed: 20000
			}, defaults);
		this._content.html("<div></div>");
		
		this._scroll = true;
		
		if(this._list.children("a").length > 4){
			with($.gallery){
				_up.css("visibility", "visible");
				_down.css("visibility", "visible");
			}
		}
		this._list.bind("mousemove", function(e){
			return;
			var l = $(e.srcElement);
			var mouseOffset = {
				top: l.pageY - l.offset().top,
				left: l.pageX - l.offset().left
			};
			if(mouseOffset.top < l.height() < 2)
				$.gallery._up.trigger("mouseover");
			else
				$.gallery._down.trigger("mouseover");
		});
		this._oSpeed = this._speed;
		this._stop = function(){
			if(!$.gallery._scroll)
				return;
			$.gallery._speed = $.gallery._oSpeed;
			$.gallery._list.stop();
		};
		this._startMovement = function(){
			this._height = this._list.height();
			this._scrollHeight = this._list[0].scrollHeight;
		};
		this._down.bind("mouseover", function(e){
			
			var _ = $.gallery;
			_._startMovement();
			_._list.animate({scrollTop: _._scrollHeight + _._height}, {
				duration: _._speed
			});
		});
		this._up.bind("click", function(e){
			var _ = $.gallery;
			_._scroll = false;
			_._startMovement();
			_._list.stop();
			_._list.animate({scrollTop: 0}, {
				duration: 1000, easing: "easeOutBounce", complete: function(){
					_._scroll = true;
				}
			});
		});
		this._down.bind("click", function(e){
			var _ = $.gallery;
			_._scroll = false
			_._startMovement();
			_._list.stop();
			_._list.animate({scrollTop: _._scrollHeight - _._height}, {
				duration: 1000, easing: "easeOutBounce", complete: function(){
					_._scroll = true;
				}
			});
		});
		this._up.bind("mouseover", function(e){
			var _ = $.gallery;
			_._startMovement();
			_._list.animate({scrollTop: -1*_._height}, {
				duration: _._speed
			});
		});
		this._list.bind("mousewheel", function(e, d){
			var _ = $.gallery._list;
			if(_.wheelscroll)
				return;
			_.wheelscroll = true;
			 $.gallery._startMovement();
			_.animate({scrollTop:  d < 0? "+=" +  $.gallery._height: "-=" + $.gallery._height},{
				duration: 500, easing: "easeInQuad", complete: function(){
					_.wheelscroll = false;
				}
			});
		});
		this._down.bind("mouseout", this._stop);
		this._up.bind("mouseout", this._stop);
		
		var $a = this._a = this._list.children("a");
		$a.bind("click", function(e){
			var url = this.href;
			var website = $a.attr("website");
			
			var temp = url.split(".");
			var extension = temp.length > 1? temp[temp.length-1]: "";
			var displayType = "";
			switch(extension.toLowerCase()){
				case "jpg":
				case "gif":
				case "png":
				case "bmp":
					displayType = "image";
					break;
				default:
					break;
			}
			if(displayType == "")
				return true;
			
			var _ = $.gallery;
			_._content.addClass("loading");
			_._desc.html("");
			_._desc.html([
				website? "<a title=\"" + this.title + "\" href=\"" + website + "\" target=_blank>": "",
				$(this).children("p.desc").html().replace(/\s*$/, ""),
				website? ", <span class=web>" + website + "</span>": "",
				website? "</a>": ""				
			].join(""));
			_._content.children("*").fadeOut("fast", function(){
				switch(displayType)
				{
					case "image":
						_._content.html("<img src=" + url + " />");
						var $img = _._content.children("img");
						$img.bind("load", function(){
							_._content.removeClass("loading");
							$img.fadeIn("slow");
						});
						break;
				}
			});
			return false;
		});
		if($a.length > 0)
			$($a[0]).trigger("click");
	};
})(jQuery);
liteweb.AppendInit(function() {
	new $.fn.tooltips();
	var accordian = new $.fn.accordian("div.np", {
		maxWidth: 876
	});
	new $.fn.popup();
	new $.fn.dropmenus();
	
	
	$("a[title^='Bookmark Us']").click(function(){
		if($.browser.msie)
			window.external.AddFavorite(location.href, document.title);
		else{
			try{
				window.sidebar.addPanel(document.title, location.href, "");
			}
			catch(e){
				alert("Press CTRL-D or CTRL-T(Opera) to bookmark");
			}
		}
		return false;
	});
});