$.widget('ui.timeline', {

	_init: function() {
		var obj = this;
		var parent = this.element;
		var list = parent.find('.timeline-list');
		var listitem = parent.find('.timeline-list .time-line-item');
		var coordsx = 450;

		list.mouseenter(function(event){
			$(this).addClass('hover');
		});
		list.mouseleave(function(){
			$(this).removeClass('hover');
		});

		parent.mousemove(function(event){
			coordsx = event.pageX - parent.offset().left;
			list.addClass('hover');
		});
		parent.mouseleave(function(){
			coordsx = 450;
			list.removeClass('hover');
			if(list.hasClass('left')){
				obj.scrollLeft(list); //-3560px
			}else{
				list.removeClass('left').addClass('right');
				obj.scrollRight(list);
			}
		});

		parent.find('.next').click(function(){
			list.animate({
				'margin-left': '-4365px'
			}, 3000);
		});
		parent.find('.prev').click(function(){
			list.animate({
				'margin-left': '1px'
			}, 3000);
		});

		setInterval(function() {
			if(coordsx < 300){
				if(parseInt(list.css('margin-left')) < '0'){
					if(coordsx > 150){
						var time = 1;
					}else{
						var time = 2;
					}
					obj.scrollRightTime(list, time);
					list.removeClass('left').addClass('right');
				}
			}
			if(coordsx > 600){
				if(parseInt(list.css('margin-left')) >= '-3877'){
					if(coordsx < 750){
						var time = 1;
					}else{
						var time = 2;
					}
					obj.scrollLeftTime(list, time);
					list.removeClass('right').addClass('left');
				}
			}
		}, 1);

		listitem.click(function(){
			var item = $(this);
			if(item.find('.infostate').length == 0){
				var position  = $(this).position();
				if(!$.browser.msie){
					var bpos = $(this).css('background-position').split(' ');
				}
				if(!$.browser.msie){
					var bposx = parseInt(bpos[0])+1;
				}else{
					var bposx = parseInt($(this).css('background-position-x'));
				}
				if(!$.browser.msie){
					var bposy = parseInt(bpos[1]);
				}else{
					var bposy = $(this).css('background-position-y');
				}
				var index = listitem.index($(this));

				if(index % 2 == 0){
					bposy = '-206px';
				}else{
					bposy = '-15px';
				}
				if(index > 6 && index < 14){
					bposx-=1144;
				}
				if(index > 13 && index < 21){
					bposx-=2250;
				}
				if(index > 20){
					bposx-=3356;
				}
				var newbpos = bposx+'px '+bposy;
				if(!$.browser.msie){
					parent.find('.infostate').fadeOut(function(){
						$(this).remove();
					});
				}else{
					parent.find('.infostate').remove();
				}

				if(!$.browser.msie){
					var posleft = item.position().left;
					item.clone().addClass('infostate').css('background', 'url(/sites/all/themes/fabric/images/timeline/large-circles.png) no-repeat').css('background-position', ''+newbpos).css('left', posleft).insertAfter(item).stop(true,true).fadeIn('slow');

					item.next('.infostate').find('.close').click(function(){
						$(this).parent().fadeOut(function(){
							$(this).remove();
						});
					});
				}else{
					var posleft = item.position().left;
					item.clone().addClass('infostate').css('background', 'url(/sites/all/themes/fabric/images/timeline/large-circles.png) no-repeat').css('background-position-x', ''+bposx+'px').css('background-position-y', ''+bposy).css('left', posleft).insertAfter(item).stop(true,true).show();

					item.next('.infostate').find('.close').click(function(){
						$(this).parent().remove();
					});
				}
			}
		});

		/*var leftInterval = setInterval(function() {
			if(parseInt(list.css('margin-left')) >= '-3560'){
				if(list.hasClass('left') && !list.hasClass('hover')){
					obj.scrollLeft(list); //-3560px
				}
			}else{
				list.removeClass('left').addClass('right');
			}
		}, 5);

		var rightInterval = setInterval(function() {
			if(parseInt(list.css('margin-left')) < '0'){
				if(list.hasClass('right') && !list.hasClass('hover')){
					obj.scrollRight(list); //-3528px
				}
			}else{
				list.removeClass('right').addClass('left');
			}
		}, 5);*/
		list.addClass('left');
		obj.scrollLeft(list);
		
	},

	scrollLeft: function(el){
		var obj = this;
		//var left = parseInt(el.css('margin-left')) - 1;
		el.animate({
			'margin-left': '-=1px'
		}, 10, 'linear', function(){
			if(parseInt(el.css('margin-left')) >= '-4365'){
				if(el.hasClass('left') && !el.hasClass('hover')){
					obj.scrollLeft(el); //-3560px
				}
			}else{
				el.removeClass('left').addClass('right');
				obj.scrollRight(el);
			}
		});
		//el.css('margin-left', left+'px');
	},

	scrollRight: function(el){
		var obj = this;
		//var left = parseInt(el.css('margin-left')) + 1;
		el.animate({
			'margin-left': '+=1px'
		}, 10, 'linear', function(){
			if(parseInt(el.css('margin-left')) < '0'){
				if(el.hasClass('right') && !el.hasClass('hover')){
					obj.scrollRight(el); //-3528px
				}
			}else{
				el.removeClass('right').addClass('left');
				obj.scrollLeft(el);
			}
		});
		//el.css('margin-left', left+'px');
	},

	scrollLeftTime: function(el, time){
		var left = parseInt(el.css('margin-left')) - (1 + time);
		el.css('margin-left', left+'px');
	},

	scrollRightTime: function(el, time){
		var left = parseInt(el.css('margin-left')) + (1 + time);
		el.css('margin-left', left+'px');
	}
	
});

$.widget('ui.interiorblocks', {

	 _init: function() {
		var obj = this;
		var parent = this.element;
		this.startPos = parseInt( parent.css('bottom') );

		parent.closest('.block').mouseenter(function(){
			parent.stop(true, false).animate({
				bottom: 0
			}, 500);
		});
		parent.closest('.block').mouseleave(function(){
			parent.stop(true, false).animate({
				bottom: obj.startPos
			}, 500);
		});
	}
	
});

$(document).ready(function(){
	$('.about-container .timeline').timeline();
	$('.archiclad_page .interior-nav .block .bottom').interiorblocks();
});

