jQuery.fn.gallSlide = function(_options){

	// defaults options	

	var _options = jQuery.extend({

		duration: 700,

		autoSlide: 1000

	},_options);



	return this.each(function(){

		var _hold = $(this);

		var _speed = _options.duration;

		var _timer = _options.autoSlide;

		var _wrap = _hold.find('ul.slider');

		var _el = _hold.find('ul.slider > li');

		var _count = _el.length-1;

		var _w = _el.outerWidth();

		_wrap.css('margin-left', -(_w*(_el.length-1)));

		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);

		var _t;

		var _active = _count;

		function scrollEl(){

			_wrap.eq(0).animate({

				marginLeft: -(_w * _active) + "px"

			}, {queue:false, duration: _speed});

		}

		function runTimer(){

			_t = setInterval(function(){

				_active--;

				if (_active > 0){

					scrollEl();

				}

			}, _timer);

		}

		runTimer();

	});

}

jQuery.fn.fader = function(_options){

	// defaults options

	var _options = jQuery.extend({

		duration: 200,

		autoSlide: 5000

	},_options);

	

	return this.each(function(){

		var _hold = $(this);

		var _speed = _options.duration;

		var _timer = _options.timer;

		var _box = _hold.find('li a > img.appear');

		_box.css('visibility', 'visible');

		_box.css('opacity', 0);

		var _time;

		var _f = true;

		var _mas = [];

		_box.each(function(_i){

			_mas.push(_i);

		});

		var _length, _this;

		_length = _mas.length;

		function RunFn(){

			_time = setTimeout(function(){

				if(_length <= 0){

					_f = false;

				}

				if(_f == false) clearInterval(_time);

				var _rnd = Math.floor(Math.random()*_length);

				animateOne(_rnd);

				if(_f != false){

					RunFn();

				}

			}, (_timer/25000)/(1/(_box.length*_box.length) / ((_length+1)*(_length+1))));

		}

		RunFn();

		function animateOne(_rnd){

			var _this = _box.eq(_mas[_rnd]*1);

			_mas.splice(_rnd,1);

			_length = _mas.length;

			_this.addClass('activated').animate({opacity: 1}, {queue:false, duration: _speed, complete:function(){}});

		}

	})

}



function initOpen(){

	var _speed = 600;

	var _h = $('div.slider-box').outerHeight();

	$('ul.piple-holder > li > a, ul.work-holder > li > a').each(function(){

		var _box = $(this);

		var _hold = $(_box.attr('href'));

		_hold.show();

		_hold.height(0).show();

		var _img = _box.find('img');

		var _imgColor = _box.find('img.colored-image');

		$('.colored-image').css('opacity', 0);

		_box.click(function(){

			if(_hold.hasClass('opened')){

				faceFadeIn();

				colorFadeOut();

				$('div.slider-box').animate({height: 0}, {queue:false, duration: _speed-200, complete:function(){

				

						if($('.work-info').length != 0){

							galleries[$(this).attr('id')].reset();

						}

					hideBlocks();

				}}).removeClass('opened');

			}else{

				$('div.slider-box').animate({height: 0}, {queue:false, duration: _speed-200, complete:function(){

					hideBlocks();

					faceFadeOut();

					colorFadeOut();

					if($('.work-info').length != 0){

						galleries[$(this).attr('id')].reset();

					}

					_img.animate({opacity: 1}, {queue:false, duration: _speed, complete:function(){}});

					_imgColor.css('z-index', 15).animate({opacity: 1}, {queue:false, duration: _speed, complete:function(){

						$('ul.piple-holder > li img.colored-image').css('z-index', 1);

						$(this).css('z-index', 15);

					}})

					_hold.animate({height: _h}, {queue:false, duration: _speed, complete:function(){

						_img.css('opacity', 1);

						showBlocks(_hold);

						if($('.work-info').length != 0){

							galleries[$(this).attr('id')].reset();

						}

					}}).addClass('opened');

				}}).removeClass('opened');

			}

			return false;

		});

	});

	hideBlocks();

	$('div.slider-box a.close').click(function(){

		faceFadeIn();

		colorFadeOut();

		$('div.slider-box').animate({height: 0}, {queue:false, duration: _speed, complete:function(){}}).removeClass('opened');

		if($('.work-info').length != 0){

			for(var _i = 0; _i<=galleries.length; _i++){

				galleries[_i].reset();

			}

		}

		return false;

	});

	function colorFadeOut(){

		$('ul.piple-holder > li img.colored-image').animate({opacity: 0}, {queue:false, duration: _speed, complete:function(){

		}});

	}

	function colorFadeIn(){

		$('ul.piple-holder > li img.colored-image').css('z-index', 15).animate({opacity: 1}, {queue:false, duration: _speed, complete:function(){}});

	}

	function faceFadeOut(){

		$('ul.piple-holder > li img.appear, ul.work-holder > li img').animate({opacity: 0.5}, {queue:false, duration: _speed, complete:function(){}});

	}

	function faceFadeIn(){

		$('ul.piple-holder > li img.appear, ul.work-holder > li img').animate({opacity: 1}, {queue:false, duration: _speed, complete:function(){}});

	}

	function hideBlocks(){

		//$('.slider-box div.img').css('margin-left', -552);

		//$('.slider-box div.info').css('margin-right', -359);

	}

	function showBlocks(_holder){

		_holder.find('div.img').animate({marginLeft: 0}, {queue:false, duration: _speed, complete:function(){}})

		_holder.find('div.info').animate({marginRight: 0}, {queue:false, duration: _speed, complete:function(){}})

	}

	

}

function fadeGall(){

	galleries = {}

	if($('.work-info').length != 0){

		$('div.slider-box').each(function(){

			var _item = $(this).attr('id');

			galleries[_item] = new Gallery( $(this),{

				effect: 'fade',

				speed : 1200,

				autoRotation : 0,

				slider: 'ul.fader',

				autoPlay : false,

				pager : 'div.paging-holder ul'

			});

		});

	}

}





function Gallery(el, o){

	this.el = el;

	this.ef = o.effect || 'slide';

	this.speed = o.speed || 800;

	this.aR = o.autoRotation || false;

	this.Fplay = (o.autoPlay == undefined) ? true : o.autoPlay;

	this.slider = this.el.find(o.slider || 'ul');

	this.holder = this.slider.parent();

	this.list = this.slider.children();

	this.prev = this.el.find(o.prev || '.prev, .link-prev, .btn-prev, .arrow-l');

	this.next = this.el.find(o.next || '.next, .link-next, .btn-next, .arrow-r');

	this.pager = this.el.find(o.pager || 'ul.swicher, .switcher ul');

	this.p = (o.dinamicPagination == undefined) ? true : o.dinamicPagination;

	this.vert = (o.vertical == undefined) ? false : o.vertical;

	this.inf = (o.infinitive == undefined) ? true : o.infinitive;

	this.ie = o.ieVersion || 6;

	this.init(o.slider);

}

Gallery.prototype = {

	calc: function(){

		if (this.ef == 'slide'){

			//------visible elements------

			if (this.vert) {this.d = this.list.eq(0).outerHeight(true);

							this.vis = Math.ceil(this.holder.height()/this.d);}

			else {this.d = this.list.eq(0).outerWidth(true);

				 this.vis = Math.ceil(this.holder.width()/this.d);}

			//------append slides------

			if (this.inf) {

				for	(var i=0; i < this.vis; i++){this.list.eq(i).clone().appendTo(this.slider);};

			}

		}

		//------pager------

		if(this.p){

			this.pager.empty();

			var obj = this;

			this.list.each(function(i){jQuery('<li><a href="#">'+(i+1)+'</a></li>').appendTo(obj.pager);});

		};

		this.thumb = this.pager.find('li');

		//------active elements------

		this.a = this.list.index(this.list.filter('.active'));

		if (this.a == -1) this.a = 0; 

		this.thumb.removeClass('active').eq(this.a).addClass('active');

		this.list.removeClass('active').eq(this.a).addClass('active');

		

		if (this.ef == 'fade'){

			this.inf = false;

			this.old = this.a;

			if (jQuery.browser.msie && jQuery.browser.version < this.ie) this.list.hide().eq(this.a).show();

			else this.list.show().css({opacity:0}).eq(this.a).css({opacity:1});
			//else this.list.show().css({'display':'none'}).eq(this.a).css({'display':'block'});

		}

	},

	init: function(){

		var obj = this;

		this.calc();

		//------INIT------

		this.thumb.click(function(){	obj.a = obj.thumb.index($(this)); obj.slide(); return false; });

		this.next.click(function(){
			obj.nextSlide();return false;
		});

		this.prev.click(function(){
			obj.prevSlide();return false;
		});

		if (this.Fplay){if (this.aR) this.run(this.a);}
	},

	run: function (){

		if (!this.aR) return;

		var obj = this;

		this.Fplay = true;

		this.t = setTimeout(function(){obj.nextSlide()}, obj.aR);

	},

	stop: function (){

		this.Fplay = false;

		if (this.t) clearTimeout(this.t);

	},

	reset:function(){

		var obj = this;

		this.a = 0;

		this.Fplay = true;

		this.slide();

		

	},

	slide: function (){

		var obj = this;

		this.x = this.a * this.d;

		if (this.inf) {if (this.a == this.list.length){this.a=0;}}

		this.list.removeClass('active').eq(this.a).addClass('active');

		this.thumb.removeClass('active').eq(this.a).addClass('active');

		if (this.ef == 'slide'){

			if (this.vert){

				this.slider.animate({top: -this.x}, {queue:false, duration:this.speed, easing:'linear', complete:function(){

					if (obj.inf) {if (obj.a == 0){obj.slider.css({top:0});}}

				}});

			}else{

				this.slider.animate({left: -this.x}, {queue:false, duration:this.speed, easing:'linear', complete:function(){

					if (obj.inf) {if (obj.a == 0) {obj.slider.css({left:0});}}

					

				}});

			}

		}else{

			if( this.a != this.old){

				if(jQuery.browser.msie && jQuery.browser.version < this.ie){

					this.list.hide().eq(this.a).show();

				}else{

					this.list.eq(this.old).animate({opacity:0}, {queue:false, duration:this.speed});

					this.list.eq(this.a).animate({opacity:1}, {queue:false, duration:this.speed});

				}

				this.old = this.a;

			}

		}

		if(this.t) clearTimeout(this.t);

		if (this.Fplay){if (this.aR) this.run(this.a);}

	},

	nextSlide: function (){

		if (this.inf) {

			if (this.a < this.list.length){this.a++

			}else {	this.a = 1;	};

		}else{

			if (this.a < this.list.length - 1){this.a++

			}else {	this.a = 0;	};

		}

		this.slide();

	},

	prevSlide: function (){

		if (this.a>0){this.a--

		}else {

			if (this.inf) {

				if (this.vert) this.slider.css({top: -this.list.length*this.d});

				else this.slider.css({left: -this.list.length*this.d});

			}

			this.a = this.list.length - 1;

		};

		this.slide();

	},

	resetTo: function (a){

		this.a = a;

		if (this.vert) this.slider.css({left: -this.a*this.d});

		else this.slider.css({top: -this.a*this.d});

	},

	refresh: function(){

		this.slider = this.el.find('ul');

		this.holder = this.slider.parent();

		this.list = this.slider.children();

		this.calc();

	}

}



function fadeGall123(){

	var wait_time = 10000; // in ms

	var change_speed = 1200; // in ms

	var _hold = $('div.work-info');

	_hold.each(function(){

		var _hold = $(this);

		if(_hold.length){

			var _t;

			var _f = true;

			var _list = _hold.find('ul.fader > li');

			var _prev = _hold.find('span.arrow-l');

			var _next = _hold.find('span.arrow-r');

			var _btn = $('<ul class="paging"></ul>');

			_list.each(function(_i){

				_btn.append('<li><a href="#">'+(_i+1)+'</a></li>');

			});

			_hold.find('div.paging-holder').empty().append(_btn);

			_btn = _btn.find('a');

			var _a = _list.index(_list.filter('.active:eq(0)'));

			if(_a == -1) _a = 0;

			

			_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
			
			_btn.eq(_a).parent('li').addClass('active');



			_btn.click(function(){

				changeEl(_btn.index(this));

				return false;

			});

			_prev.click(function(){

				if(_a <= 0) changeEl(_list.length-1);

				else changeEl(_a-1);

				return false;

			});

			_next.click(function(){

				if(_a < _list.length - 1) changeEl(_a + 1);

				else changeEl(0);

				return false;

			});

			

			_hold.mouseenter(function(){

				_f = false;

				if(_t) clearTimeout(_t);

			}).mouseleave(function(){

				_f = true;

				if(_t) clearTimeout(_t);

				if(_f && wait_time){

					_t = setTimeout(function(){

						if(_a < _list.length - 1) changeEl(_a + 1);

						else changeEl(0);

					}, wait_time);

				}

			});

			if(_f && wait_time){

				_t = setTimeout(function(){

					if(_a < _list.length - 1) changeEl(_a + 1);

					else changeEl(0);

				}, wait_time);

			}

			function changeEl(_ind){

				if(_t) clearTimeout(_t);

				if(_ind != _a){

					_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:change_speed});

					_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration:change_speed});

					_btn.eq(_a).parent('li').removeClass('active');

					_btn.eq(_ind).parent('li').addClass('active');

					_a = _ind;

				}

				if(_f && wait_time){

					_t = setTimeout(function(){

						if(_a < _list.length - 1) changeEl(_a + 1);

						else changeEl(0);

					}, wait_time);

				}

			}

		}

	});

}





/*Coding for styled upload box*/

var SITE = SITE || {};



SITE.fileInputs = function() {

  var $this = $(this),

      $val = $this.val(),

      valArray = $val.split('\\'),

      newVal = valArray[valArray.length-1],

      $button = $this.siblings('.button'),

      $fakeFile = $this.siblings('.file-holder');

  if(newVal !== '') {

    $button.text('File Chosen');

    if($fakeFile.length === 0) {

    } else {

    }

  }

};













$(window).load(function(){



	$('.file-wrapper input[type=file]').bind('change focus click', SITE.fileInputs);

  

  

	$('div.gallery').gallSlide({

		duration: 700,

		autoSlide: 5000

	});

	$('ul.piple-holder').fader({

		duration: 300,

		timer: 100

	});

	$('ul.work-holder').fader({

		duration: 300,

		timer: 300

	});

	initOpen();

	fadeGall();



	/*JIVA CHANGES*/

	$(".e4").each(function(index) {

	    $(this).delay(1000*index).fadeIn(1000);

	});



	$(".purpose").each(function(index) {

	    $(this).delay(1000*index).fadeIn(1000);

	});

	

	$(".services").each(function(index) {

	    $(this).delay(1000*index).fadeIn(1000);

	});

	/*JIVA CHANGES*/

	

	

	

/*

	var _datastore = [

		{

			'year' : 1978,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/1978.png" /></div><div class="sidecontent"><strong class="ttl">1978</strong><p>Jim Stokes opens up shop as JSA</p></div></div>',

			'dir' : 'top',

			'pos' : 0,

			

			'style' : 'display: block;'

		},

		

		{

			'year' : 1980,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/1980.png" /></div><div class="sidecontent"><strong class="ttl">1980</strong><p>Dan Stokes joins brother Jim Stokes as Executive<br />Vice President</p></div></div>',

			'dir' : 'bottom',

			'pos' : 7

		},

		

		{

			'year' : 1981,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/1981.png" /></div><div class="sidecontent"><strong class="ttl">1981</strong><p>JStokes named agency of record for Bishop Ranch</p></div></div>',

			'dir' : 'top',

			'pos' : 11

		},



		{

			'year' : 1984,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/1984.png" /></div><div class="sidecontent"><strong class="ttl">1984</strong><p>JStokes acquires East Bay agency Petler, Degrassi & Hill</p></div></div>',

			'dir' : 'bottom',

			'pos' : 17

		},

		

		{

			'year' : 1986,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/1986.png" /></div><div class="sidecontent"><strong class="ttl">1986</strong><p>JStokes hosts new office opening party which gets national media attention; Clara "Wheres the Beef" Peller attends party</p></div></div>',

			'dir' : 'top',

			'pos' : 22

		},

		

		{

			'year' : 1987,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/1987.png" /></div><div class="sidecontent"><strong class="ttl">1987</strong><p>JStokes wins first million-dollar client&mdash;Kauffman & Broad (now KB Homes)</p></div></div>',

			'dir' : 'bottom',

			'pos' : 26

		},

		

		{

			'year' : 1988,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/1988.png" /></div><div class="sidecontent"><strong class="ttl">1988</strong><p>JStokes celebrates 10 years of operation!</p></div></div>',

			'dir' : 'top',

			'pos' : 32

		},

		

		{

			'year' : 1989,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/1989.png" /></div><div class="sidecontent"><strong class="ttl">1989</strong><p>JStokes wins first Subway account</p></div></div>',

			'dir' : 'bottom',

			'pos' : 38

		},

		

		{

			'year' : 1990,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/1990.png" /></div><div class="sidecontent"><strong class="ttl">1990</strong><p>Jim and Dan Stokes featured on cover of Diablo Business</p></div></div>',

			'dir' : 'top',

			'pos' : 43

		},

		

		{

			'year' : 1992,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/1992.png" /></div><div class="sidecontent"><strong class="ttl">1992</strong><p>JStokes launches the innovative Giants pitching change<br />feature&mdash;"When it&rsquo;s time for a change, think SpeeDee Oil Change & Tune-Up"</p></div></div>',

			'dir' : 'bottom',

			'pos' : 48

		},

		

		{

			'year' : 1993,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/1993.png" /></div><div class="sidecontent"><strong class="ttl">1993</strong><p>JStokes named agency of record for Bay Alarm, creates "What have you got to lose?" campaign</p></div></div>',

			'dir' : 'top',

			'pos' : 53

		},

		

		{

			'year' : 1998,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/1998.png" /></div><div class="sidecontent"><strong class="ttl">1998</strong><p>JStokes celebrates its 20th anniversary</p></div></div>',

			'dir' : 'bottom',

			'pos' : 59

		},

		

		{

			'year' : 1999,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/1999.png" /></div><div class="sidecontent"><strong class="ttl">1999</strong><p>JStokes hosts first Holiday Bowling Party!</p></div></div>',

			'dir' : 'top',

			'pos' : 65

		},

		

		{

			'year' : 2001,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/2001.png" /></div><div class="sidecontent"><strong class="ttl">2001</strong><p>JStokes awarded Subways\' annual Agency of the Year trophy</p></div></div>',

			'dir' : 'bottom',

			'pos' : 71

		},

		

		{

			'year' : 2002,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/2002.png" /></div><div class="sidecontent"><strong class="ttl">2002</strong><p>Client Bishop Ranch celebrates its 50-year anniversary<br />with unique, hand-delivered party invitations</p></div></div>',

			'dir' : 'top',

			'pos' : 76

		},

		

		{

			'year' : 2008,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/2008.png" /></div><div class="sidecontent"><strong class="ttl">2008</strong><p>JStokes celebrates 30 years of operation!</p></div></div>',

			'dir' : 'bottom',

			'pos' : 87

		},

		

		{

			'year' : 2009,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/2009.png" /></div><div class="sidecontent"><strong class="ttl">2009</strong><p>JStokes wins 3 Addys and 3 Tellys!</p></div></div>',

			'dir' : 'top',

			'pos' : 92

		},

		

		{

			'year' : 2010,

			'data' : '<div class="context-wrapper"><div class="img-bg"><img src="design/images/timeline/2010.png" /></div><div class="sidecontent"><strong class="ttl">2010</strong><p>JStokes Subway markets hit the 1,500 location mark</p></div></div>',

			'dir' : 'bottom',

			'pos' : 97

		}

	]

	

	

	

	// 'dir' - class which will add to date (top or bottom of the line);

	timeline = new Dateline($('div.dateline'),{

		min : 1970,

		max : 2011,

		duration: 500,

		timeout: 2000,

		IE : true,

		proportional: false, //make dates driven by position - pos (in percent) if true - by sorted date propotionally

		data : _datastore // json source

	});
	
*/

	

	

	

	// hover tmp

	var animSpeed = 500;

	$('ul.work-holder').each(function(){

		var list = $(this);

		var items = list.children();

		items.each(function(ind){

			var curItem = $(this);

			var frame = curItem.find('.frame');

			var direction = ind % 2 ? 1 : -1;

			var animWidth = frame.width();

			frame.css({display:'block',marginLeft:direction*animWidth})

			curItem.hover(function(){

				frame.animate({marginLeft:0},{duration:animSpeed,queue:false});

			},function(){

				frame.animate({marginLeft:direction*animWidth},{duration:animSpeed,queue:false});

			});

		});

	});

	

	// hover piple

	var animSpeed = 500;

	$('ul.piple-holder').each(function(){

		var list = $(this);

		var items = list.children();

		items.each(function(){

			var curItem = $(this);

			var frame = curItem.find('.frame');

			frame.css({display:'none'})

			curItem.mouseenter(function(){

				frame.fadeIn(400);

			});

			curItem.mouseleave(function(){

				frame.fadeOut(50);

			});

		});

	});

});



function Dateline(el, options) {

	this._hold = el;

	this.initOptions(options);

	this._t;

	this.initialize();

}

Dateline.prototype = {

	initOptions: function(_obj){

		this.options = {

			duration: 500,

			timeout: 2000,

			min : 1970,

			max : 2011,

			proportional:true,

			structure: '<div class="inner"><div class="frame"><div class="timeline"></div></div></div>',

			data : {nodata : '<div>No data found!</div>'},

			IE: false

		};

		for(key in _obj) this.options[key] = _obj[key];

	},

	initialize: function(){

		var _this = this;

		_this._box = $(_this.options.structure);

		_this._box.find('div.timeline').append('<ul class="datesgrid"></ul>');

		//var _fullWidth = _this._hold.outerWidth();

		var _fullWidth = 690;

		var oneYear = _fullWidth/(_this.options.max*1-_this.options.min*1);



		try{

			var _dates = eval(_this.options.data);

		}catch(ex){console.info('wrong data format' + ex)}

		var _ind = 0;

		if(_this.options.proportional){

			_dates = _this.sort(_dates);

		}

		_this.number = _dates.length;

		for(cell in _dates){

			var dataHead = _dates[cell].year;

			var dataInfo = _dates[cell].data;

			

			var style = _dates[cell].style;

			var _item = $('<li><div rel="'+dataHead+'" class="datehead"><em class="bullet"></em><span>'+dataHead+'</span></div><div title="'+dataHead+'" class="dateinfo" style="'+style+'"><div class="pointer"></div><div class="visuals">'+dataInfo+'</div></div></li>');

			var _leftPos;

			if(_this.options.proportional){

				_leftPos = (dataHead*1 - _this.options.min*1)*(oneYear);

				if(_ind%2==0){

					_item.addClass("top");

				}else{

					_item.addClass("bottom");

				}

			}else{

				var _thisPos = (_dates[cell].pos)*1;

				_leftPos = _fullWidth/100*_thisPos;

				_item.addClass(_dates[cell].dir);

			}

			_item.find('div.datehead').css('left', _leftPos);

			_item.find('div.pointer').css('left', _leftPos+16);

			_this._box.find('ul.datesgrid').append(_item);

			_ind++;

		}

		_this._box.css('visibility', 'hidden');

		_this._hold.append(_this._box);

		$('div.datehead').mouseenter(function(){

			$(this).parents('li').addClass('hovered');

		}).mouseleave(function(){

			$(this).parents('li').removeClass('hovered');

		});

		_this.run();

		$('div.datehead').click(function(){

			var _ttl = $(this).attr('rel');

			var _box = $('div[title = '+_ttl+']');

			if(_this.options.IE){

				if($.browser.msie){

					$('div.dateinfo').hide();

					_box.show();

				}else{

					$('div.dateinfo').fadeOut();

					_box.fadeIn();

				}

				$('div.dateinfo').parents('li').removeClass('active');

				_box.parents('li').addClass('active');

			}else{

				$('div.dateinfo').fadeOut();

				$('div.dateinfo').parents('li').removeClass('active');

				_box.fadeIn();

				_box.parents('li').addClass('active');

			}

			return false;

		});

	},

	run: function (){

		_this = this;

		_this._box.css('visibility', 'visible');

		_this._items = _this._box.find("ul.datesgrid > li");

		_this._items.css('opacity', 0);

		_this._items.each(function(_ind){

			var _t = $(this);

			setTimeout(function(){

				_t.animate({opacity:1}, {queue:false, duration: _this.options.duration})

			},(_this.options.timeout/_this.number)*_ind);

		});

	},

	sort: function(_dates){

		var _stop = true;

		var _misc;

		while(_stop == true){

			_stop = false;

			for(var _i=0; _i<_dates.length-1; _i++){

				if((_dates[_i].year*1) >(_dates[_i+1].year*1)){

					_misc = _dates[_i];

					_dates[_i] = _dates[_i+1];

					_dates[_i+1] = _misc;

					_stop = true;

				}

			}

		}

		return _dates;

	}

}
