// Uneven spaces - an image gallery script (not lightbox driven)
/*

Random Uneven Subdivition Gallerg

Picture Gallery of Randum Uneven Subdivitions

pGorus

Rule 1) all items in list must exist as subdivided spaces within the main space.

X -Rule 2) when subdividing a space, always split by the bigger dimension within the space. (THIS RULE IS OVERRIDDEN IN THE DESIGN. Consider this weighted instead)

Rule 3) one space may not be subdivided more than once. (because the spaces within that subdivision are what need to be subdivided)

Rule 4) Always divide spaces by two or three when creating new spaces.

Rule 5) The difference the between the biggest space and the smallest space must not be greater than x
	a) x should be determined based on the number of spaces that need to be placed into the the main space.


$(document).ready(function(){
	rGeoGallery();
});*/


/*
jQuery.fn.href = function(href) {
  return this.prop("href", href);
};
*/


var rGeoGallery = function(){
	var theBox = ".theRGGbox";
	var mh = $(theBox).css("height");
	var mw = $(theBox).css("width");
	if(mh.match("px")){
		mh = mh.substring(0,mh.indexOf("px"));
		mw = mw.substring(0,mw.indexOf("px"));
	}
	//var theList = ".theRGGlist";
	var theList = ".theRGGlist .RGG-item";
	var sArray = new Array();
	sArray[0] = new space(mw,mh,0,0);
	var gutterH = 10;//10 / mh * 100; //10px -this should be in percentages
	var gutterW = 10;//10 / mw * 100; //10px -this should be in percentages
	var TSP = 1; // Total Spaces Created. (add AND remove accordingly) one space exists in the container itself, therefore, it starts with 1, not 0
	//var usableArray = [0];//starts with only 0 as a usable creation starting point.
	
	/*
	The Main Loop:
	
	This function does the work of looping through the list and creating a new subdivision for each item (or set of new spaces)
	until it's filled the space with areas of subdivition.
	*/
	for(var i=0;i<$(theList).length;i++){
		a = get2or3();

		if(TSP<$(theList).length){
			if(TSP+2 <=$(theList).length){
				//do nothing, can be two or three.	
			}else{
				a = 2;
			}
			createNewSpaces(a,chooseSpace());
		}
	}

	//alert("final array size:"+sArray.length);
	displaySpaces();
	
	function space(width,height,leftP,topP){ //var space1 = new space(10,20,0,0);
		this.width=width;
		this.height=height;
		this.topP=topP;
		this.leftP=leftP;
		this.volume=width*height;
		this.uspace=true;//uspace standes for whether or not it is an undivided space.
	}
	
	function chooseSpace(){
		var usableArray = new Array();
		for(var i=0;i<sArray.length;i++){
			if(sArray[i].uspace == true){
				usableArray.push(i); 
			}
		}
		var ua2 = new Array();
		var a=0;//averageVolume
		for(var i=0;i<usableArray.length;i++){
			a+= sArray[(usableArray[i])].volume;
		}
		var lowerAverage = Math.round((a/usableArray.length)/1.5);
		var weightStrength = 2; // 2 is default, not particularly strong
		//alert(lowerAverage);
		var weightArray = new Array();
		var removeArray = new Array();
		
		for(var i=0;i<usableArray.length;i++){ //Loop through usable array
			if(sArray[(usableArray[i])].volume > lowerAverage){ //If usable space volume is greater than the average, the do the while loop
				var b = sArray[(usableArray[i])].volume; //set variable "b" to be the space volume
				while(b > lowerAverage){//as long as b > lowerAverage
					weightArray.push(usableArray[i]);
					b = b * (1 / weightStrength);	//decrease the b value
				}
			}
			if(sArray[(usableArray[i])].volume < (lowerAverage)){ //eliminate all spaces below average
				removeArray.push(usableArray[i]);
			}
		}
		var c1 = usableArray.length;
		usableArray = usableArray.concat(weightArray);
		
		var c2 = usableArray.length;
		for(var i=0;i<removeArray.length;i++){
			for(var ii=0;ii<usableArray.length;ii++){
				if(removeArray[i] == usableArray[ii]){
					//usableArray[ii].pop()
					usableArray.splice(ii,1);
				}
			}
		}/**/
		var c3 = usableArray.length;
		//alert("usableArray unweighted:"+c1+";\nusableArray after weight:"+c2+";\nusableArray after remove:"+c3+";\nremove"+removeArray.length);
		var cUSpace = Math.floor(usableArray.length*Math.random());
		var chosenSpace = usableArray[cUSpace];
		return chosenSpace;
	}
	
	function createNewSpaces(num,c){
		/* This is where you subdivide an existing space. Calculating new h,w,t,l values based on the container and the kind of subdivision being done.
		*///  [c] = chosen space number in spaceArray/sArray
		//alert("space0:"+spaceSize[0]+";space1:"+spaceSize[1]+";space2:"+spaceSize[2]+";");
		var o = sArray[c];//o for original space
		if(!sArray[c]){
			c--;
			o = sArray[c];
		}/**/
		//alert("create spaces - current itemNumber:"+c);
		
		var splitType = "vertical";
		//Simplified choose on splitType, find the bigger dimension, then split that. TEMPORARY!!!!!!
		if(o.width>o.height){
		}else{
			splitType = "horizontal";	
		}
		//alert("owidth:"+o.width+";oheight:"+o.height+";");
		spaceSize = new Array();
		if(num==2){
			spaceSize[0] = Math.round(30 + 40*Math.random())/100 ;
			spaceSize[1] = 1-spaceSize[0];//(100-spaceSize[0])/100;	
		}else{
			spaceSize[0] = (1/3);
			spaceSize[1] = (1/3);
			spaceSize[2] = (1/3);
		}
		
		if(spaceSize.length==2){
			if(splitType == "horizontal"){ //height and top changes
				var fh = o.height*spaceSize[0] - (gutterH/2);
				sArray.push( new space(o.width,fh,o.leftP,o.topP));
				sArray.push( new space(o.width,o.height*spaceSize[1] - (gutterH/2),o.leftP,o.topP+fh + (gutterH)));
			}else{ //width and left changes
				var fw = o.width*spaceSize[0] - (gutterW/2);
				sArray.push( new space(fw,o.height,o.leftP,o.topP));
				sArray.push( new space(o.width*spaceSize[1] - (gutterW/2),o.height,o.leftP+fw + (gutterW),o.topP));
			}
			TSP+= 1;
		}else{
			if(splitType == "horizontal"){ //height and top changes
				var eh = o.height*spaceSize[0] - (gutterH*2/3);
				sArray.push( new space(o.width,eh,o.leftP,o.topP));
				sArray.push( new space(o.width,eh,o.leftP,o.topP+eh + (gutterH)));
				sArray.push( new space(o.width,eh,o.leftP,o.topP+(eh*2) + (gutterH*2)));
			}else{ //width and left changes
				var ew = o.width*spaceSize[0] - (gutterW*2/3);
				sArray.push( new space(ew,o.height,o.leftP,o.topP));
				sArray.push( new space(ew,o.height,o.leftP+ew + (gutterW),o.topP));
				sArray.push( new space(ew,o.height,o.leftP+(ew*2) + (gutterW*2),o.topP));
			}
			TSP+= 2;	
		}
		sArray[c].uspace=false;
	}
	
	function get2or3(){
		var rn = Math.random()*10;
		if(rn>5){ return 2;
		}else{	  return 3;	}
	}
	
	//Presentation
	function displaySpaces(){
		b= 0;
		var elem = "a";
		for(var i=0;i<sArray.length;i++){
			if(sArray[i].uspace == true){
			  if($(theList+":eq("+b+") img")[0]){
				   var addclass="us_space";
				   if(sArray[i].width < (sArray[0].width/4)){
					   addclass+=" us_smaller";
				   }
				   
				  if(sArray[i].width > (sArray[i].height *2)){
					addclass+=" us_biggerW";
				  }else if(sArray[i].width > (sArray[i].height*1.3)){
				  	addclass+=" us_bigW";  
				  }else{
					addclass+=" us_bigH"; 
				  }
				  
				  $(theBox).append("<"+elem+" id='num"+i+"' href='"+$(theList+":eq("+b+") .purl")[0].innerHTML+"' class='"+addclass+"' style='position:absolute;top:"+sArray[i].topP+"px;left:"+sArray[i].leftP+"px;width:"+sArray[i].width+"px;height:"+sArray[i].height+"px;'><div class='us_content' style='opacity:0;'><div class='window'></div><img src='"+$(theList+":eq("+b+") img")[0].src+"' alt='' /></div></"+elem+">");//background-image:url("+$(theList+":eq("+b+") img")[0].src+");
				  b++;
			  }
			}
			if(i==(sArray.length-1)){
				revealSpaces();	
			}
		}
	}
	function revealSpaces(){
		for(var i=0;i<$(theBox+" .us_space").length;i++){
			$(theBox+" .us_space:eq("+i+") .us_content").animate({"opacity":1},400);
		}
		
	}
	
	/////////////////////////////////////
	//    EVENT LISTENERS AFTER LOAD   //
	/////////////////////////////////////
	$(theBox+" .us_space").click(function(){
		var cw=$(this).css("width");
		var ch=$(this).css("height");
		var ct=$(this).css("top");
		var cl=$(this).css("left");
		$(theBox).append("<div class='featuredImage us_space usp_cw_"+cw+"-ch_"+ch+"-ct_"+ct+"-cl_"+cl+"-'><span class='arrow'></span><img src='"+$(this).children(".us_content").children("img")[0].src+"' alt='' /></div>" );
		$(theBox+" .featuredImage").css({"height":ch,"width":cw,"top":ct,"left":cl});
		//$(theBox+" .us_space").css({"z-index":"21"});
		//$(this).css({"z-index":"22"});
		
		$(theBox+" .featuredImage").animate({"height":"100%","width":"100%","top":"0","left":"0"},400,"easeOutQuad");				
		$(theBox+" .featuredImage").click(function(){
			$(theBox+" .featuredImage").animate({"height":ch,"width":cw,"top":ct,"left":cl},400,function(){
				$(this).remove();																							 
			});//									   
		});
		return false;
	});/**/
};
























