

/* MOVE CONTENT BASED ON MOUSE FUNCTIONS
*************************************************************/
function content() {
  //shortcut for encapsulation
  var self = this;
  
  //INIT VARIABLES
  self.grid_width = 148;
  self.grid_height = 121;
  self.grid_padding = 6;
  self.init_top = 0;
  self.init_left = 0;
  self.clone_width = 0;
  self.curr_top = -508;
  self.curr_left = -600;
  self.mouse_x = 0;
  self.mouse_y = 0;
  self.container  = "";
  self.shell_w = 0;
  self.shell_h = 0;
  self.shell_l = 0;
  self.shell_t = 0;
  self.max_right = 0;
  self.max_top = 0;
  self.move_by = 5;
  self.timing = 20;
  self.menu_height = 31;
  self.started = false;
  self.debug = false;
  self.IE = false;
  self.paused = false;
  
  self.init = function(){
		if(self.debug){
			$("#container").append("<p id='debug'>Mouse: </p>");
		}
		self.container = $("#home #content_container #pos_container");
		
		self.IE = (document.all)?true:false;
		if (!self.IE) document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = self.getMouseXY;
		
		self.container.addClass("pos_container");	
		self.setup_links();
		
		if ($.browser.name.toLowerCase() == 'firefox') {
		  self.move_by = self.grid_padding*2;
		  self.timing = 1;
		}
		
		self.shell_w = parseInt($("#shell").css('width').split("p")[0]);
		self.shell_h = parseInt($("#shell").css('height').split("p")[0]);
		self.init_left = parseInt(self.container.css('left'));
		self.init_top =  parseInt(self.container.css('top'));
		self.curr_top = parseInt(self.container.css('top'));
		self.curr_left = parseInt(self.container.css('left'));
		self.shell_t = parseInt($("#shell").offset().top);
		self.shell_l = parseInt($("#shell").offset().left);
		self.max_right = parseInt($("#shell").width()) - parseInt(self.container.width())-self.grid_padding;
		self.max_top = (parseInt($("#shell").height())) - parseInt(self.container.height()) - self.menu_height - self.grid_padding;
		
		$("#navigation li a").mouseover(function(){
			self.check_grid();
		});
		
		$("#navigation").mouseover(function(){
			self.started = false;
		});
	}
  
  self.setup_links = function(){
		$('#content_container div.home_container div.home_entry a.home_entry_link').each(function(){
			//setup the container now that it's added to the DOM
			var shortcut = $(this).parent();
			var image = shortcut.children('a.home_entry_link').children('img');
			tmp_width = parseInt(parseInt(image.width()));
			shortcut.css('width', tmp_width + "px");
			shortcut.css('overflow', "hidden");
		});
		
		$('#content_container div.home_container div.home_entry a.home_entry_link').unbind('click').click(function(){
				if($(this).parent().children('div.slider').length > 0){
					self.paused = true;
					var parent_div = $(this).parent().parent();
					var cloned = $(this).parent().clone();
					var cloned_top = Math.abs(self.container.css('top') + self.container.height());
					var adjustment = cloned_top - $(this).parent().css('top');
					if(adjustment > 0){
						self.container.css('top', (self.container.css('top') + adjustment));
					}
												  
					$('#content_container div.home_container div.home_entry.active').remove();
					$('#content_container div.home_container div.home_entry').fadeOut(250);
					$('#content_container #pos_container').unbind('click').click(function(){
						self.close_slideout();
						return false;
					});
					cloned.addClass('active');
					parent_div.append(cloned);
					
					//setup the container now that it's added to the DOM
					var shortcut = $('#content_container div.home_container div.home_entry.active');
					var slider = shortcut.children('div.slider');
					var home_entry_link = shortcut.children('a.home_entry_link');
					var image = shortcut.children('a').children('img');
					tmp_height = shortcut.height();
					tmp_width = parseInt(parseInt(image.width()) * 2) + self.grid_padding;
					
					shortcut.css('width', tmp_width + "px");
					shortcut.css('overflow',"visible");
					shortcut.css('opacity','1');
					slider.css('display','block');
					slider.css('top', 0-self.grid_padding);
					
					if(self.which_side_hit() == 'right'){
						var tmp_slider_move = parseInt(image.width()) + (self.grid_padding *2);
						var shortcut_width = (parseInt(image.width()) + self.grid_padding)*2;
						if(image.width() < 200){
							tmp_slider_move = (parseInt(image.width()) * 2) + (self.grid_padding *3);
							shortcut_width = (parseInt(image.width()) + self.grid_padding)*3;
						}
						tmp_slider_move = parseInt(shortcut.css('left')) - tmp_slider_move;
						shortcut.css('width', shortcut_width + "px");
						shortcut.css('left', tmp_slider_move + 'px');
						slider.css('float', 'left');
						home_entry_link.css('float', 'right');
					} else {
						slider.css('left', parseInt(image.width()) + 'px');
					}
					slider.children('div.innerSlider').children('p').children('a').unbind('click').click(function(){
						location.href=$(this).attr('href');
						return false;
					});
					slider.css('height', tmp_height);
					tmp = slider.children('h1').html();
					slider.children('h1').html("<span>" +tmp+"</span>");
					slider.children('h1').append("<a href='#' class='close'>close</a>");
					slider.children('h1').children('a.close').unbind('click').click(function(){
						self.close_slideout();
						return false;
					});
					
					self.check_grid();
					self.check_grid_height();
					
					return false;
				} else {
					location.href=$(this).attr('href');
					return true;
				}
		});
  }
  
  self.close_slideout = function(){
	$('#content_container div.home_container div.home_entry.active').remove();
	$('#content_container div.home_container div.home_entry').fadeIn(250);
	$('#content_container div.home_container div[class*=logo]').click(function(){
		location.href = $(this).children('a').attr('href');
		return false;
	});
	$('#content_container div.pos_container').unbind('click').click(function(){ return false });
	self.paused = false;
  }
  
  self.which_side_hit = function() {
	var output = "";
	if(self.mouse_x < (self.shell_l+(self.shell_w/2))){
		output = 'left';	
	} else if(self.mouse_x > (self.shell_l+(self.shell_w/2))) {
		output = 'right';
	}
	return output;
  }
  
  self.check_grid = function(){
	  var current_shift = parseInt(self.container.css('left')) - self.init_left;
		  var tmp = (current_shift % self.grid_width);
		  if($('#content_container div.home_container div.home_entry.active').length > 0){
			hit_area = self.check_active();	
						
			if(hit_area.side == 'left'){
				tmp = (current_shift % self.grid_height) - (self.grid_width + self.grid_padding);
			} else if(hit_area.side == 'right'){
				tmp = (current_shift % self.grid_height) + (self.grid_width + self.grid_padding);
			} else if (hit_area.side == 'left_edge' || hit_area.side == 'right_edge'){
				tmp = 0;
			}
		  }
	  if(current_shift != 0){
	  }
	  if(tmp != 0){
		  self.container.animate({
			left: parseInt(self.container.position().left) - tmp
		  }, 250);		
	  }
  }
  
  self.check_grid_height = function(){
	  var current_shift = parseInt(self.container.css('top')) - self.init_top;
	  if($('#content_container div.home_container div.home_entry.active').length > 0){
		shortcut = $('#content_container div.home_container div.home_entry.active');
		hit_area = self.check_active();	
		var tmp = (current_shift % self.grid_height);
		if(hit_area.top == 'top'){
			tmp = (current_shift % self.grid_height) - self.grid_height;
		} else if(hit_area.top == 'bottom'){
			tmp = (current_shift % self.grid_height) + self.grid_height;
		} else if (hit_area.top == 'top_edge' || hit_area.top == 'bottom_edge'){
			tmp = 0;
		}
		if(tmp != 0){
			if(parseInt(self.container.position().top) - tmp < (0-self.grid_padding)){
				if((parseInt(self.container.position().top) - tmp > self.max_top) && parseInt(self.container.css('top')) - parseInt(shortcut.height()) > self.max_top){
					self.container.animate({
						top: parseInt(self.container.position().top) - tmp
					}, 250);
				} else {
					self.container.animate({
						top: self.max_top
					}, 250);
				}
			} else {
				self.container.animate({
					top: self.grid_padding
				}, 250);	
			}
		}
	  }
  }
  
  self.check_active = function(){
	  var hit_position_side = 'none';
	  var hit_position_top = 'none';
	  
	  if($('#content_container div.home_container div.home_entry.active').length > 0){
		  relative_left = parseInt($('#content_container div.home_container div.home_entry.active').css('left')) + parseInt(self.container.css('left'));
		  active_width = parseInt($('#content_container div.home_container div.home_entry.active').width());
		  relative_top = Math.abs(parseInt($('#content_container div.home_container div.home_entry.active').css('top')) + parseInt(self.container.css('top'))+self.menu_height);
		  active_height = parseInt($('#content_container div.home_container div.home_entry.active').height());
		  
		  //console.log(relative_left + " | " + self.grid_padding + " | " +  Math.round(self.grid_width/2));
		  
		  if(parseInt(self.container.css('left')) == self.grid_padding){
			  hit_position_side = 'left_edge';
		  } else if(parseInt(self.container.css('left')) == self.max_right){
			  hit_position_side = 'right_edge';
		  } else if(relative_left < Math.round(self.grid_width/2)){
			  hit_position_side = 'left';
		  } else if((relative_left + active_width) > (self.shell_w - self.grid_width)){
			  hit_position_side = 'right';
		  }
		  		  
		  if(parseInt(self.container.css('top')) == self.grid_padding){
			  hit_position_top = 'top_edge';
		  } else if(parseInt(self.container.css('top')) == self.max_top && relative_top > (self.grid_height+self.menu_height)){
			  hit_position_top = 'bottom_edge';
		  } else if(relative_top <= (self.grid_height+self.menu_height)){
			  hit_position_top = 'top';
		  } else if((relative_top + active_height) > (self.shell_h-self.grid_height)){
			  hit_position_top = 'bottom';
		  }		  
	  }
	  
	  return {"top":hit_position_top,"side":hit_position_side};
  }
  
  self.getMouseXY = function(e) {
		if (self.IE) { // grab the x-y pos.s if browser is IE
			self.mouse_x = event.clientX + document.body.scrollLeft;
			self.mouse_y = event.clientY + document.body.scrollTop;
		}
		else {  // grab the x-y pos.s if browser is NS
			self.mouse_x = e.pageX;
			self.mouse_y = e.pageY;
		}  
		if (self.mouse_x < 0){self.mouse_x = 0;}
		if (self.mouse_y < 0){self.mouse_y = 0;}  
		if(self.detect_collision()){
			if(!self.started){
				self.started = true;
				self.move();
			}
		} else {
			self.started = false;
		}
		return true;
	}
  
  self.detect_collision = function(){
		if(((self.mouse_x > self.shell_l && self.mouse_x < (self.shell_l+self.shell_w)) && (self.mouse_y > (self.shell_t+self.menu_height) && self.mouse_y< (self.shell_t+self.shell_h)) && $("#navigation li ul:visible").length == 0) && !self.paused){
			return true;
		} else {
			if(self.debug){
				$("#debug").html("Mouse: no-hit <br />mousex:" + self.mouse_x + " mousey: " + self.mouse_y);
			}
			return false;
		}
  }
  
  self.move = function(){
		self.curr_top = parseInt(self.container.css('top'));
		self.curr_left = parseInt(self.container.css('left'));
		
		if(self.mouse_x < (self.shell_l+(self.shell_w/8))){
			if(self.debug){
				$("#debug").html("Mouse: hit left <br />mousex:" + self.mouse_x + " mousey: " + self.mouse_y + "<br /> Higher than the left: " +(self.shell_l) + "\n Less than the width: " + (self.shell_l+self.shell_w) + "<br />Higher than the top: " +  (self.shell_t) + "\n Less than the height: " + ((self.shell_t+self.shell_h)));
			}
			if(self.curr_left < 0){
				self.container.css('left', parseInt(parseInt(self.curr_left) + (self.move_by)) + "px");
			} else {
				self.container.css('left', 0+self.grid_padding);
			}
		} else if(self.mouse_x > (self.shell_l+(self.shell_w*.875))) {
			if(self.debug){
				$("#debug").html("Mouse: hit right <br />mousex:" + self.mouse_x + " mousey: " + self.mouse_y + "<br />Higher than the left: " +(self.shell_l) + "\n Less than the width: " + (self.shell_l+self.shell_w) + "<br />Higher than the top: " +  (self.shell_t) + "\n Less than the height: " + ((self.shell_t+self.shell_h)));	
			}
			if(self.curr_left > self.max_right){
				self.container.css('left', parseInt(parseInt(self.curr_left) - (self.move_by)) + "px");
			} else {
				self.container.css('left', parseInt(self.max_right) + "px");
			}
		}
		if(self.mouse_y < (self.shell_t+(self.shell_h/8))){
			if(self.debug){
				$("#debug").html("Mouse: hit top <br />mousex:" + self.mouse_x + " mousey: " + self.mouse_y + "<br /> Higher than the left: " +(self.shell_l) + "\n Less than the width: " + (self.shell_l+self.shell_w) + "<br />Higher than the top: " +  (self.shell_t) + "\n Less than the height: " + ((self.shell_t+self.shell_h)));
			}
			if(self.curr_top < 0){
				self.container.css('top', (parseInt(self.curr_top) + self.move_by) + "px");
			} else {
				self.container.css('top', 0+self.grid_padding);
			}
		} else if(self.mouse_y > (self.shell_t+(self.shell_h*.875))) {
			if(self.debug){
				$("#debug").html("Mouse: hit bottom <br />mousex:" + self.mouse_x + " mousey: " + self.mouse_y + "<br />Higher than the left: " +(self.shell_l) + "\n Less than the width: " + (self.shell_l+self.shell_w) + "<br />Higher than the top: " +  (self.shell_t) + "\n Less than the height: " + ((self.shell_t+self.shell_h)));
			}
			if(self.curr_top > self.max_top){
				self.container.css('top', (parseInt(self.curr_top) - self.move_by) + "px");
			} else {
				self.container.css('top',parseInt(self.max_top) + "px");
			}
		}
		if(self.started){
			setTimeout("contentObj.move()", self.delay);
		}
	}

}