/*
Blocks script -> navigating page in 2 dimensions
v. 0.98
*/
(function($) {
		  
  $.fn.blocks = function(options) {
	
	//Setting default settings
	defaults = {
		animation : true,
		width: "window",
		pages: ".pageblock",
		attr: 'block',
		active_attr : 'activeblock',
		gotoAttr : 'block',
		next : '.gotoNextBlock',
		prev : '.gotoPrevBlock',
		goto : '.gotoBlock',
		start : 1,
		externalLinks : '.gotoBlockLink'
	}
	
	//Parsing default values with user arguments
	var settings = $.extend(defaults, options);
	
	//For each of the selector
	return this.each(function (i) {
		data = settings;
		
		//data that will be sendt with the functions
		data = $.extend(data, {parent:this});
		e={data:data};
		
		//Sett the right size for the blocks
		resizeBlocks(e);
		
		$(data.externalLinks).bind('click', data, gotoBlock);
		
		if(data.width== "window"){
			$(this).css({height : $(window).height(),width  : $(window).width()}, 600);
			
			//Resize the blocks when the window is resized
			$(window).bind('resize', data, resizeBlocks);
			//Find the right top variables if the windows is resized!
			$(window).bind('resize', data, gotoCurrentBlock);
		}
		
		//For each of the pages
		$(this).find(data.pages).each(
			function(x){
				//Find any buttons
				$(this).find(data.next).bind('click', data, gotoNextBlock);
				$(this).find(data.prev).bind('click',data, gotoPrevBlock);
				$(this).find(data.goto).bind('click', data, gotoBlock);
				
				$(this).attr(data.attr, x+1);
				
				$(data.parent).css({width:$(window).width()*(x+1)})
				if((x+1)==data.start){
					//Mark the first div is active
					moveToBlock(x+1,e);
				}
			}
		);
		
	});
	
	//Function for resizing the boxes if the browser is resized
	function resizeBlocks(e){
		$this = $(e.data.pages);
		css = {
			height : $(window).height(),
			width  : $(window).width()
		}
		$this.stop().css({height : $(window).height()}).animate({width  : $(window).width()}, 300);
	}
	
	//Function that navigates to a certain box
	function moveToBlock(i, e){
		$this = $(e.data.parent);
		$this.find(e.data.pages+'['+e.data.active_attr+'="'+e.data.active_attr+'"]').attr(e.data.active_attr,"");
		$this.find(e.data.pages+'['+e.data.attr+'="'+(i)+'"]').attr(e.data.active_attr, e.data.active_attr);
		$this.stop().animate({top: -($(window).height())*(i-1)},900);		
		//$(window).scrollTop(($(window).height())*(i-1));
		$('.navigation > ul > li').stop().removeClass('selected',300).eq(i-1).addClass('selected',300);
	}
	
	//Function for box-links
	function gotoBlock(e){
		i = $(this).attr(e.data.gotoAttr) * 1;
		moveToBlock(i,e);		
	}
	
	//Function to send to the next block
	function gotoNextBlock(e){
		i = $(e.data.parent).find(e.data.pages+'['+e.data.active_attr+'="'+e.data.active_attr+'"]').attr(e.data.attr) * 1;
		moveToBlock(i+1,e);	
	}
	
	//Function to send to the previous block
	function gotoPrevBlock(e){
		i = $(e.data.parent).find(e.data.pages+'['+e.data.active_attr+'="'+e.data.active_attr+'"]').attr(e.data.attr) * 1;
		moveToBlock(i-1,e);		
	}
	
	//Function to send to update top css variable if window is resized
	function gotoCurrentBlock(e){
		i = $(e.data.parent).find(e.data.pages+'['+e.data.active_attr+'="'+e.data.active_attr+'"]').attr(e.data.attr);
		$(e.data.parent).stop().css({top: -($(window).height())*(i-1)});
	}
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  $.fn.rows = function(options) {
	
	//Setting default settings
	defaults = {
		animation : true,
		width: "window",
		pages: ".rowblocks",
		attr: 'col',
		active_attr : 'activecol',
		gotoAttr : 'col',
		next : '.gotoNextRowBlock',
		prev : '.gotoPrevRowBlock',
		goto : '.gotoRowBlock',
		start : 1,
	}
	
	//Parsing default values with user arguments
	var settings = $.extend(defaults, options);
	
	//For each of the selector
	return this.each(function (i) {
		data = settings;
		
		//data that will be sendt with the functions
		data = $.extend(data, {parent:this, pagenumbers:$(this).find(data.pages).size()});
		e={data:data};
		
		//Sett the right size for the blocks
		resizeRows(e);
		
		if(data.width== "window"){
			$(this).css({height : $(window).height(),width  : "5000px"}, 600);
			
			//Resize the blocks when the window is resized
			$(window).bind('resize', data, resizeRows);
			//Find the right top variables if the windows is resized!
			$(window).bind('resize', data, gotoCurrentRow);
		}
		
		//For each of the pages
		$(this).find(data.pages).each(
			function(x){
				//Find any buttons
				$(this).find(data.next).bind('click', data, gotoNextRow);
				$(this).find(data.prev).bind('click',data, gotoPrevRow);
				$(this).find(data.goto).bind('click', data, gotoRow);
				
				//Give a number to all the divs
				$(this).attr(data.attr, x+1);
				
				$(data.parent).css({width:$(window).width()*(x+1)})
				if((x+1)==data.start){
					//Mark the first div is active
					moveToRowBlock(x+1,e);
				}
				
				
				
				
			}
		);
		
	});
	
	//Function for resizing the boxes if the browser is resized
	function resizeRows(e){
		$this = $(e.data.parent).find(e.data.pages+'['+e.data.active_attr+'!="'+e.data.active_attr+'"]');
		$active =  $(e.data.parent).find(e.data.pages+'['+e.data.active_attr+'="'+e.data.active_attr+'"]');
		css = {
			height : $(window).height(),
			width  : $(window).width()
		}
		$(e.data.parent).css({width:$(window).width()*(e.data.pagenumbers+1)});
		$this.stop().css({height : $(window).height(),width  : $(window).width()});
		$active.stop().css({height : $(window).height()}).animate({width  : $(window).width()}, 300);
		
	}
	
	//Function that navigates to a certain box
	function moveToRowBlock(i, e){
		$this = $(e.data.parent);
		$this.find(e.data.pages+'['+e.data.active_attr+'="'+e.data.active_attr+'"]').attr(e.data.active_attr,"");
		$this.find(e.data.pages+'['+e.data.attr+'="'+(i)+'"]').attr(e.data.active_attr, e.data.active_attr);
		$this.stop().animate({left: -($(window).width())*(i-1)}, 900, "easeInOutBack");		
	}
	
	//Function for box-links
	function gotoRow(e){
		i = $(this).attr(e.data.gotoAttr) * 1;
		moveToRowBlock(i,e);		
	}
	
	//Function to send to the next block
	function gotoNextRow(e){
		i = $(e.data.parent).find(e.data.pages+'['+e.data.active_attr+'="'+e.data.active_attr+'"]').attr(e.data.attr) * 1;
		moveToRowBlock(i+1,e);	
	}
	
	//Function to send to the previous block
	function gotoPrevRow(e){
		i = $(e.data.parent).find(e.data.pages+'['+e.data.active_attr+'="'+e.data.active_attr+'"]').attr(e.data.attr) * 1;
		moveToRowBlock(i-1,e);		
	}
	
	//Function to send to update top css variable if window is resized
	function gotoCurrentRow(e){
		i = $(e.data.parent).find(e.data.pages+'['+e.data.active_attr+'="'+e.data.active_attr+'"]').attr(e.data.attr);
		$(e.data.parent).stop().css({left: -($(window).width())*(i-1)});
	}
  }
  
  
  
  
  
})(jQuery);
