
// projects browser
projects = {	
		
	settings : {
		'scroll_speed' : 500, // milliseconds 
		'thumb_scroll_increment' : 600, // pixels
		'container_width' : 850,
		'mouse_wheel_increment' : 50 // pixels		
	},
		
	init : function() {
		width = this.set_container_width();
		if(width >= this.settings.container_width) {
			this._insert_navigation();
			this._init_navigation();			
			this.animate_thumb(0, false); //weird bug with Safari (foo call)
			this.scroll_to_panel_page();
		}		
	},
	
	set_container_width: function() {
		// set container width 
		var width = 0;
		$('#projects div ul img').each(function(el) { 
			width += $(this).width() + 3;
		});
		$('#projects div ul').css('width', width);
		return width;
	},
	
	scroll_thumbs: function(direction) {
		increment = this.settings.thumb_scroll_increment; // increment value	
		page_number = parseInt($('input#panel_page').val()); //storing page number in a hidden input
		
		if(!$('#projects div ul').hasClass('animating')){ // do nothing if block is animated 

			if(direction == 'left') {				
				left = parseInt($('#projects div ul').css('left'));
				if(left < 0) {
					$('input#panel_page').attr('value', page_number -= 1);
					new_left = left + increment;
				}	else {
					new_left = 0;	
				}
				this.animate_thumb(new_left, true);
			} else if (direction == 'right') {
				increment = increment * -1;
				left = parseInt($('#projects div ul').css('left'));
				max = $('#projects div ul').width() + left;
					
				if(max < $('#projects').width()) {
					new_left = left;	
				} else {
					$('input#panel_page').attr('value', page_number += 1);
					new_left = left + increment;				
				}			
				this.animate_thumb(new_left, true);	
			}		
		}	
	},
	
	// called on init() only
	scroll_to_panel_page : function() {
		increment = this.settings.thumb_scroll_increment; // increment value	
		page_number = parseInt($('input#panel_page').val());
		if (page_number > 0) {
			new_left = (increment * page_number) * -1;
			this.animate_thumb(new_left, false);
		}
	},
	
	animate_thumb : function(value, animate) {
		$('#projects div ul').addClass('animating');	
		if(animate) {
			$('#projects div ul').animate({ 
			        left: value
			}, this.settings.scroll_speed, 'swing', this.after_scroll);			
		} else {
			$('#projects div ul').css('left', value);
			this.after_scroll();
		}
	},
	
	after_scroll : function() {
		$('#projects div ul').removeClass('animating');		
		$('#projects div ul li a').each(function() {
			// remove any params to href
			var strHref = $(this).attr("href");
	  	if (strHref.indexOf("?") > -1 ){
				strHref = strHref.substr(0, strHref.indexOf("?"));
	  	}

			new_href = strHref + '?panel_page=' + $('input#panel_page').val();
			$(this).attr('href', new_href);
		});
	},
	
	_insert_navigation : function() {
		$('#projects').append('<a href="#" class="browser-btn left" rel="left">Left</a>');		
		$('#projects').append('<a href="#" class="browser-btn right" rel="right">Right</a>');
	},
	
	_init_navigation : function() {
		$('#projects a.browser-btn').each(function() {
			$(this).bind('click', function(){
				projects.scroll_thumbs(this.rel);
				return false;
			});
		});
	}

};

project_images = {

	settings : {
		'scroll_speed' : 500, // milliseconds 
		'container_width' : 600, // pixels
		'container_height' : 450 // pixels		
	},

	init : function() {		
		el = '.project-images';				
		var items = $(el + ' .slide-content ul li');
		
		if(items.length > 1) {						
			this._insert_navigation(items);
			
			// set container width 
			$(el + ' .slide-content ul').css('width', items.length * this.settings.container_width + "px");
			
			// init nav
			this._activate_menu_item(0);
		}
			
		// center all large images in their container
		this._center_images(items);						
	},

	scroll_thumbs: function(pos) {
		$('.project-images .slide-content ul').animate({ 
		        left: this._current_position(pos)
		}, this.settings.scroll_speed);		
		this._activate_menu_item(pos);
	},

	_insert_navigation : function(items) {
		// adding navigation container & links
		$('.project-images').append('<ul id="images_mini_nav"></ul>');				
		items.each(function(i) {
			$('.project-images #images_mini_nav').append("<li><a name='"+i+"'>"+(i+1)+"</a></li>");
		});

		this._init_navigation();
	},

	_init_navigation : function() {
		$('.project-images #images_mini_nav a').each(function() {
			$(this).bind('click', function(){
				project_images.scroll_thumbs(this.name);
			});
		});
	},
	
	_current_position : function(pos) {
		return pos * this.settings.container_width * -1;
	},
	
	// activate menu item (removing any active then adding to the current position)
	_activate_menu_item : function(pos) {
		$('.project-images #images_mini_nav a').each(function() { $(this).removeClass('active'); });
		$('.project-images #images_mini_nav a:eq('+pos+')').addClass('active');
	},
	
	_center_images : function(items) {
		items.each(function(i) {
			src = $(this).children('img')[0].src;
			$(this).css('background-image', 'url("'+src+'")');
		});
	}

};