// JavaScript Document
function basename (path, suffix) {
    var b = path.replace(/^.*[\/\\]/g, '');
    
    if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
        b = b.substr(0, b.length-suffix.length);
    }
    
    return b;
}
//Adds active class to navigation for arrow
$(function() { 
		  /* $("#port_nav li a").click(function(){
					$("#port_nav li a.active").removeClass('active');				   
											   });*/
		   	//This gets the page name
			var thispage = basename(document.location.href);
			//convert pagename to string
			var newstr = thispage.toString();
			//set reg ex 1
			var myreg = ".php?";
			//set reg ex 2
			var myreg2 = "=";
			//get starting pos for reg 1
			var pos = newstr.search(myreg);
			//if reg 1 is found
			if(pos > 0) {
				//get pos of reg 2
				var pos2 = newstr.search(myreg2);
				//if reg 2 is found (which means there is a query string on url)
				if(pos2 > 0) { 
					//get links
					var subnav = $("a");
					//loop through links
					subnav.each(function() {
								//if link href == current page		
								if(newstr == $(this).attr('href')) {
									 //add active class
									$(this).addClass('active');
									//disable link
									$(this).click(function() { return false;});
								}
										 });
				}
				//removes querys from url
				newstr = newstr.slice(0,pos);
				thispage = newstr+'.php';
				} 
			var thelist = $("#header ul li a");
			thelist.each( function() {
				if(thispage == $(this).attr('href')) {
					$(this).addClass('active');	
				}
								   });
			
			
			//set up contact links and buttons to trigger form script on click 
			var formlink = $("a.conform");
			formlink.click(function(){
								formset();
								return false;
									});
			
})
//Builds form and fades it into document
function formset() {
	//Find out the window's width and set backdrop to match
	var thewidth = $(window).width();
	thewidth = thewidth/2-160;
	//Find out the document's height and set backdrop to match
	var theheight =($(document).height());
	theheight = theheight+'px';
	//Get top scrolling amount
	var winheight = $(window).height();
	winheight = winheight/2-225;
	var toppos = $(document).scrollTop();
	//if there is any scroll add it to the window calc
	if(toppos > 0) {
		winheight = winheight + toppos;	
	} 
	toppos = toppos + 75;
	toppos = toppos + 'px';
	//get element to insert backdrop and form into
	var thebody = $("#outerwrap");
	//build shadowbox
	var backdrop = $("<div id='formback'>").css({'height':theheight});
	//insert and shadowbox into document
	thebody.prepend(backdrop);
	//create form element div holder
	//var closelink = $("<a id='contactclose' href='#'>").text('close');
	//create form holding div
	var formbox = $("<div id='contactform'>").css({'left':thewidth,'top':winheight+'px'});
	//form markup
	var formmarkup = "<h1 class='boldh1'>Contact Riser Design</h1> \
		<div id='theform'> \
		<a id='contactclose' href='#'>Close</a> \
		<form id='conform' action='formprocess.php' name='conform1' method='post'> \
		<table>\
			<tr><td>Your Name:</td></tr> \
			<tr><td><input type='text' name='name' value='' class='required'/></td></tr> \
			<tr><td>Your Company:</td></tr> \
			<tr><td><input type='text' name='company' value=''/></td></tr> \
			<tr><td>Your Email:</td></tr> \
			<tr><td><input type='text' name='email' value='' class='required email'/></td></tr> \
			<tr><td>Brief Project Description:</td></tr> \
			<tr><td><textarea name='description' cols='36' rows='5'></textarea></td></tr> \
			<tr class='hidden_field'><td><input type='text' name='bots' value=''/></td></tr> \
			<tr><td><input id='submit' type='submit' name='contactsubmit' value='Submit'/></td></tr> \
			</table> \
			<input type='hidden' name='contactsubmit' value='submit'/> \
			</form> \
			<h2 class='boldh1 conformh2'>Or Call 561-889-4169</h2>\
		</div>";
	//insert markup into div
	formbox.prepend(formmarkup);
	//insert close link into div
	//formbox.prepend(closelink);
	//insert form into document
	thebody.prepend(formbox);
	//build loader div
	var loader = $("<div id='loader'>");
	//begin fade in's
	backdrop.fadeTo(300,.90, function(){
								formbox.fadeTo(300,1.0);	  
									  });
	//set close box link for form pop-up
	$('a#contactclose').click(function(){
				//fade out form and box
				formbox.fadeTo(100,0.0, function(){formbox.remove();
				//fade out shadowbox
				backdrop.fadeTo(300,0.0, function(){backdrop.remove();});
										});//end function
					 });//end click
	
	//Setup form submit event handler
	$("form#conform").submit(function() {
				//if validation passes
				if($("form#conform").validate().form()) {
					//initiate loader bar
					$("#theform form").replaceWith(loader);	
					//Send post data to php file
					$.post('../formprocess.php',$(this).serialize(),function(data) {
					//Insert php response
					loader.replaceWith(data);});
					//cancel default behavior
					return false;
				//if validation fails
				} else {
				return false;	
				}
					  
									  
	
									 });//End click input setup
}


$(function() {
			  ///////////////////////////
			  //Set Variables
			  var numText = true;
			  var nav = true;			  
			  //End Variables
			  //////////////////////////	  
			  
			  //BEGIN SCRIPTING=================
			  //get container div 
			  var holder = $("#holder");
			  //get container div's divs
			  var divs = $("#holder div");
			  //get count of images
			  var eleNum = divs.size();
			  //create ul and fade it down to 75% in 1/2 sec
			  var list = $("<ul id='list1'>").fadeTo(500,.75);
			  //insert ul into holder above divs
			  holder.prepend(list);
			  //setup variable
			  var output;
			  //if nav var is true
			  if(nav) {
				//for loop to create li items and anchors and add them to ul 
			  	for(var i = 0; i < eleNum; i++) {
				  //if on the last image
				  if(i==0) {
					//if numText is true add numbers to links 
					if(numText) {
						$("#list1").append($("<li class='active'>").append($("<a href='"+i+"'>").text(i+1)));
					} else {
				  	$("#list1").append($("<li class='active'>").append($("<a href='"+i+"'>")));
				  	} } else {
						//if numText is true add numbers to links
						if(numText) {
							$("#list1").append($("<li>").append($("<a href='"+ i +"'>").text(i+1)));
						} else {
					  $("#list1").append($("<li>").append($("<a href='"+ i +"'>")));
				  	}
					}//else
				  
			  	}//for
			  }//if
			  //make anchors return false
			  $("#list1 a").bind("click",function(evt) {return false;});
			  //prepare anchors to advance image when clicked			  
			  $("#list1 a").bind("click",function(evt) {
				//get the parent li for the link that was just clicked
				var it = $(this).parent();
				//get link text to use for li index
				var url = $(this).attr("href");
				//remove active from where ever it is and add it to the li that was clicked
				$("li.active").removeClass("active");
				it.addClass("active");
				//begin image change callback function
				changeImg(url);				
				
			  });//end onload function
			  			  
			  //Start Image Switching initial behavior
			  var theInt = setInterval("switchImage()",8000);
			  
			  function changeImg(url) {
				//stop image switching for now
				clearInterval(theInt);
				//get the div of the li that was clicked
				var imgGet = $("#holder div:eq("+url+")");
				//get the current image
				var curImg2 = $("div.current");
				//set current image to previous
				curImg2.removeClass("current").addClass("previous");
				//set selected image to current and fade in then remove previous from old image
				imgGet.css({opacity: 0.0}).addClass("current").animate({opacity: 1.0},300,function() {curImg2.removeClass("previous")});				//restart image switching
				theInt = setInterval("switchImage()",8000);
			  }//callBack
			  
			  		  
			   });//end anon function
				
		function switchImage() {
		//get current image
		var curImg = $("#holder div.current");
		//get the next image
		var nxtImg = curImg.next();
		//but if this is already the last image make the first image be next
		if(nxtImg.length == 0) {
			nxtImg = $("#holder div:first");
		}//if
		//remove current from current image and add previous
		curImg.removeClass("current").addClass("previous");
		//set next image to current and fade in
		nxtImg.css({opacity: 0.0}).addClass("current").animate({opacity: 1.0},1000,function() {curImg.removeClass("previous");});
		//advance active link
		linkSwitch();
		}//function
		
		function linkSwitch() {
			//get currently active link
			var link1 = $("#list1 li.active");
			//get the next link
			var link2 = link1.next();
			//but if this is the last link then make the first link next
			if(link2.length == 0) {
				link2 = $("#list1 li:first");
			}//if
			//remove active from current
			link1.removeClass("active");
			//make next active
			link2.addClass("active");
		}//function

