$(document).ready(function() {	

	function parseResponse(response) {
		var header = response['HEADER'];
		var results = [];
		
		for(var i in response['ROWS']) {
			var row = {};
			for(var j in header) {
				row[header[j]] = response['ROWS'][i][j];
			}
			
			results.push(row);
		}
		
		return results;
	}
	
	function createItem(name, description) {
		
		var p = document.createElement('p');
		var strong = document.createElement('strong');
		$(strong).text(name + ':');
		$(p).append(strong);
		$(p).append('&nbsp;' + description);
		
		return p;
	}
	
	$('.slider').slider({
		orientation: 'vertical',
		min: 0,
		max: 100,
		value: 100,			
		slide: function(event, ui) {

			var val = (100 - ui.value) / 100;
			var container = $(this).getContainer();
			
			var h = container.find('.scrollarea')[0].scrollHeight - container.find('.scrollarea').height();

			container.find('.scrollarea')[0].scrollTop = h * val;
		}
	});

	$('.items li').entwine({
		onclick: function() {
			this.notify('selected', this.find('h3').text());
		}	
	});			

	$('#col1').respond('selected', function(project, details) {
						
		$('#col2').notify('reset', true);
		$('#col3').notify('reset', false);		
		
		this.find('.active').removeClass('active');
		details.getOrigin().addClass('active');
		
		$.ajax({				 
			type: 'GET',
			url: 'getexperience/' + project,
			dataType: 'json',
			success: function(response) {
				var results = parseResponse(response);
				
				if (results.length == 0) {
					return $('#col2').notify('reset', false);
				}				
				
				$('#col2').removeClass('idle').animate({'opacity': 1});
				$('#col2 .scrollarea').html(results[0].description);				
			}
		});
	});

	
	
	$('#col2').respond('selected', function(tag, details) {
		
		$.ajax({				 
			type: 'GET',
			url: 'getdetail/' + tag,
			dataType: 'json',
			success: function(response) {
				var results = parseResponse(response);								
				
				if (results.length == 0) {
					return $('#col3').notify('reset', false);
				}
												
				$('#col3').removeClass('idle').animate({'opacity': 1});				
				$('#col3 .scrollarea').html('').append(createItem('Tag', results[0].name));
				$('#col3 .scrollarea').append(createItem('Experience', results[0].years + ' years'));
				$('#col3 .scrollarea').append(results[0].description);
			}
		});			
	});			
	
	$('#columns .widget').respond('reset', function(partial) {	
		
		this.find('.scrollarea')[0].scrollTop = 0;	
		this.find('.slider').slider('value', 100);	
		
		if (!partial) {
			this.find('.scrollarea').html('');
			this.addClass('idle');
			this.stop().animate({'opacity': .1}, 500, 'easeOutQuad')
		}
	});
	
		
	$('p.tags span').entwine({
		onmatch: function() {	
			var a = document.createElement('a');			
			$(a)
				.text(this.text())
				.attr('href', '#')
				.click(function() {
					$(this).notify('selected', $(this).text());
					return false;
				});											
			
			this.html(a);
		}			
	});
	
	$('a[href="#contact"]').click(function() {
		
		if ($('#call').is(':visible')) {
			$(this).text('Contact');
			$('#call').fadeOut();
		} else {
			$(this).text('Contact [X]');
			$('#call').fadeIn();
		}
			
		return false;
	});
	
	$('a[rel=lightbox]').entwine({
		onmatch: function() {
			this.lightBox()
		}
	});
	
	var ScrollWidget = function() {
		
		var self = null;
		var slider = null;
		var scrolling = false;
		var height = null;
		var dx = 0;
		var dy = 0;
		var lx = -1;
		var ly = -1;

		function stopScroll() {
			lx = ly = -1;
			height = this[0].scrollHeight - this.height();
			
			this.unbind('mousemove');
			scrolling = false;

			if (dy > 10) {
				dy = 10;
			}
			
			if (dy < -10) {
				dy = -10;
			}
			
			letsMove();
			return false;			
		}
		
		function letsMove() {
				
			self[0].scrollTop = parseInt(self[0].scrollTop - dy);
			slider.slider('value', parseInt(100 - ((self[0].scrollTop / height) * 100)));
		
			var bdy = dy;
			
			if (dy > 0) {
				dy -= .1;
			} else {
				dy += .1;
			}
			
			if (dy < 0 && bdy >= 0) {
				dy = 0;
			}
			
			if (dy > 0 && bdy <= 0) {
				dy = 0;
			}
			
			if (dy != 0 && !scrolling) {
				setTimeout(letsMove, 30);
			}
		}
		
		return {
			onmatch: function() {
				self = this;
				slider = this.prev('.slider');								
				height = this[0].scrollHeight - this.height();
				
				this.bind('DOMMouseScroll', function(e) {				
													
					e = e ? e : window.event;
				  var delta = e.detail ? e.detail * -1 : e.wheelDelta / 40;
				  
					this.scrollTop -= delta * 10;
					var height = this.scrollHeight - $(this).height();
					
					$(this).prev('.slider').slider('value', 100 - ((this.scrollTop / height) * 100));
				  
				  return false;
				});
		},
			onmousedown: function(event) {												
				this.unbind('mousemove');
				
				scrolling = true;
				height = this[0].scrollHeight - this.height();
				
				this.bind('mousemove', function(event) {
					if (ly == -1) {
						lx = event.clientX;
						ly = event.clientY;
						return;
					}					
					dx = ((event.clientX - lx) + dx / 2);
					dy = ((event.clientY - ly) + dy / 2);
					lx = event.clientX;
					ly = event.clientY;
					
					self[0].scrollTop -= dy;					
					slider.slider('value', 100 - ((self[0].scrollTop / height) * 100));					
				});
				
				return false;
			},
			onmouseup: stopScroll,
			onmouseleave: stopScroll 
		}
	};			
	
	$.entropy.registerModule('#col1 .scrollarea', ScrollWidget);											
	$.entropy.registerModule('#col2 .scrollarea', ScrollWidget);
	$.entropy.registerModule('#col3 .scrollarea', ScrollWidget);
});	
