// JavaScript Document

var tempVars;
function getURLVars(url, query){
	 var test = 'yep';
	$.getJSON(url+query, function(json){  
			return json;	 
	});   	
	
	return test;
} 

function alignTo(copyObj, srcObj, dir){
	  
		   switch(dir){
			case 'l':
			$(copyObj).css('position','absolute');     
			var left = $(srcObj).offset().left - $(copyObj).width();
 
			$(copyObj).css('left', left +"px"); 
			
			break;
			
			case 'r':
			$(copyObj).css('position','absolute');     
			var right = $(srcObj).offset().left + $(srcObj).width();
 
			$(copyObj).css('left', right +"px"); 
			break;
			
			case 't':
			break;
			
			case 'b':
			break;
			   
		   }
	
	// alert("$padding " + $(srcObj).css('margin'));
}

function copyProps(copyObj, srcObj){
	  
		   
	$(copyObj).css('position','absolute');  
	$(copyObj).css('height', $(srcObj).css('height'));  
	$(copyObj).css('width', $(srcObj).css('width'));   
	
	$(copyObj).css('top', $(srcObj).offset().top +"px");  
	$(copyObj).css('left', $(srcObj).offset().left +"px");  
	// alert("$padding " + $(srcObj).css('margin'));
}

function copyPos(copyObj, srcObj){
	  
		   
	$(copyObj).css('position','absolute'); 
	$(copyObj).css('top', $(srcObj).offset().top +"px");  
	$(copyObj).css('left', $(srcObj).offset().left +"px");  
	// alert("$padding " + $(srcObj).css('margin'));
}


//pass an array of items to be clickable, items need label, href, and className
function createSubMenu(subDiv, parentObj, items){
 
 	
	$(subDiv).css('visibility','visible');   
	$(subDiv).css('marginTop' , -1 * $(subDiv).height() + "px"); 
	$(subDiv).html('')	
	 
		
		for(var i=0; i< items.length; i++){
		//$(subDiv).append(
			//EXCEPTION 
			if(items[i].label != ''){
				 $(subDiv).append( "<a href="+ items[i].href  +" id="+ items[i].label+" class=" + items[i].className +" rel='internal'>" + removeUnderscore(items[i].label)+"</a>");
			}
		  
		}
	 
	 
	 
	 
	$(subDiv).animate({ 
		marginTop:"-7px"
	},400, function () { dispatchEvent('SubNavDone') });

	
	$(subDiv).css('z-index', $(parentObj).css('z-index') - 1);
	
}

function dispatchEvent(e){
	 
	copyPos($('#shine'), $('#contentLeft'));
	 
	
}
//pass in an object dim with props heght and width
function constrain(dim, object) {
			
		 
			if ($(object).height() > $(object).width()) {
				var orgHeight = $(object).height();
				$(object).height() = dim.height;
				
				 
				var newWidth = ($(object).width() * $(object).height()) / orgHeight;
				$(object).width() = newWidth;
				
			}else {
				var orgWidth = $(object).width();
				$(object).width() = dim.width;
				var newHeight= ($(object).width() * $(object).height()) / orgWidth;
				$(object).height() = newHeight;
			}
		 
		}
		
function removeUnderscore(txtFile) {
			 var myPattern = /_/g;   
			return txtFile.replace(myPattern, " "); 
}


function addUnderscore(txtFile) {
			 var myPattern = / /g;   
			return txtFile.replace(myPattern, "_"); 
}
		
//TODO THIS FUNC RETURN THE ARRAY TO ORIGINAL CALLING FUNCTION		
function varsLoaded(vars){
	
	  
	vars =vars.replace(/[\r\n]+/g, ",");
 	tempVars = vars.split(',');
	
 	//EXCEPTION no thumbs for some of the swfs
	if(tempVars[0]  == "0" || tempVars[0]  == "1"){
		loadThumbs(tempVars, thumbContainer);		
	}else{
		layoutBoxes(tempVars, thumbContainer);					
	}
	
	 

}

//CONVERTS A STRING SEPERATED BY LINKE BREAKS TO AN ARRAY
function textToArray(vars){
	
	  
	vars =vars.replace(/[\r\n]+/g, ",");
 	tempVars = vars.split(',');
	
return tempVars;
	 

}

function loadThumbs(images, parentObj){ 
 
	$(images).each(function(index,value){
			 // alert('projects/Websites/' + value + '_t.jpg');
			  // create the LI programatically
			 var thumb = $('<a id="portpic_'+index+'" href="'+curSecLinks[index]+'" rel="external" ></a>').attr('class','loading');
			// append the new LI to UL
			$(parentObj).append(thumb); 
			 
			 // new image
			var img = new Image();
			 // load image
			$(img).css('border', '0px');
			 $(img).load(function () {
				
				$(thumb).animate({ 
						opacity:'0' 
						 
						},100); // since .hide() failed in safari
				
				 $(thumb).removeClass('loading').append(this);
				  $(thumb).attr('class' , 'thumbnail');
				  $(thumb).attr('id' , 'thumb_'+index); 
				$(thumb).animate({ 
						opacity:'.5' 
						 
						},700);
				 
				 }).error(function () {
				 	$(thumb).remove();
			 	}).attr('src', curSec +'/'+curSubSec+'/' + value + '_t.jpg');
	}); 
}

 


 
 
 
 //EXCEPTION for no thumb sections
 
 function layoutBoxes(images, parentObj){ 
 

	$(images).each(function(index,value){
			 // alert('projects/Websites/' + value + '_t.jpg');
			  // create the LI programatically
			  //href="'+curSubSec+"/"+index+'" 
			 var thumb = $('<a id=thumb_'+index+' href="'+curSubSec+'" rel="internal" ></a>').attr('class','swfthumbnail');
			  $(thumb).css('background-color', String(colorsArr[rand(0, (colorsArr.length - 1))]));
			  $(thumb).attr('mediaUrl', value); 
			 $(parentObj).append(thumb);
			 
	}); 
}

function rand(mini, maxi){
	
return rand_no = Math.floor((maxi - (mini - 1))*Math.random()) + mini;
	
	
}

function centerObject(obj, objWidth, objHeight, dir){
			
		if(dir == "h"){
			var newXLoc = (document.body.clientWidth/2) - (objWidth/2); 
			document.getElementById(obj).style.left = newXLoc; 	 
		}else if(dir == "v"){ 
			var newYLoc = (document.body.clientHeight/2) - (objHeight/2); 	
			document.getElementById(obj).style.top = newYLoc; 
		}else if(dir!= "h" && dir != "v"){
			
			var newXLoc = (document.body.clientWidth/2) - (objWidth/2);
			var newYLoc = (document.body.clientHeight/2) - (objHeight/2); 
			$(obj).css('left', newXLoc +'px'); 	
			$(obj).css('top', newYLoc +'px');
					
							 
			}	
}	

function winLoc(newloc){
 
	
}
function indexNum(obj){
	return $(obj).attr("id").split("_")[1];
}

function scrollHeight() {
    var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
    // Append our div, do our calculation and then remove it
    $('body').append(div);
    var h1 = $('div', div).innerHeight();
    div.css('overflow-x', 'scroll');
    var h2 = $('div', div).innerHeight();
    $(div).remove();
    return (h1 - h2);
}

//IE Bitches about this so commented out
/*
screen.getDocumentHeight = function() {
var body = screen.getBody();
var innerHeight = (defined(self.innerHeight)&&!isNaN(self.innerHeight))?self.innerHeight:0;
if (!document.compatMode || document.compatMode=="CSS1Compat") {
var topMargin = parseInt(CSS.get(body,'marginTop'),10) || 0;
var bottomMargin = parseInt(CSS.get(body,'marginBottom'), 10) || 0;
return Math.max(body.offsetHeight + topMargin + bottomMargin,
document.documentElement.clientHeight,
document.documentElement.scrollHeight, screen.zero(self.innerHeight));
}
return Math.max(body.scrollHeight, body.clientHeight,
screen.zero(self.innerHeight));
};
*/
function isValidEmail(email) {
			var emailExpression = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
			return emailExpression.test(email);
}
