//Global configuration settings
var global = {
				lbWidth: null,
				lbContWidth: null
			 };


//SIFR Config
var ffdCorpBold = {
				  	src: './flash/ffd-corporate-bold.swf'
				  };


//Active sifr to use later
sIFR.activate(ffdCorpBold);


//Functions to run when the document first loads
//Also includes event listeners to add
$(document).ready(function()
{
	//Functions to run when the window resizes
	$(window).resize(function()
	{
		$(this).windowHeight('#wrapper');
		$('#lightBox').centerScale(50, 40);
	});
	
	
	//Make sure the wrapper is as tall as the viewport
	$(window).windowHeight('#wrapper');
	
	
	//Resize the lightbox
	$('#lightBox').centerScale(50, 40);
	
	
	//Add load functions for the tool tips
	$('#main .thumb').tooltip(
	{ 
		track: true, 
		delay: 0, 
		showURL: false, 
		showBody: " - ", 
		fade: 250,
		top: -40,
		left: -16,
		fixPNG: true,
		bodyHandler: function()
		{
			return "<div class='left'></div><div class='content'>" + $(this).children('img:first').attr('alt') + "</div><div class='right'></div><div class='bot'></div>";
		}
	});
	
	
	//Add listeners to the thumbs
	$('#main .thumb').click(function()
	{
		//Get the content needed from the controller and loombox it in.
		$(this).getThumbData($(this).attr('name'), 'left', false);
		
		return false;
	});
	
	
	//Add listeners to the previous and next project buttons
	$('#lightBox .content .nav .next').click(function()
	{
		$(this).getNextThumb($('#lightBox .content .holder .idHolder').val());		
		return false;
	});
	$('#lightBox .content .nav .prev').click(function()
	{
		$(this).getPrevThumb($('#lightBox .content .holder .idHolder').val());		
		return false;
	});
	
	
	//Initialize the loombox
	$(this).loomboxInit();
	
	
	//Add listeners to the scroll buttons
	$('#lightBox .content .scroll a.left').click(function()
	{
		$('#lightBox .content .holder .html').slideThis(200);
	});
	
	$('#lightBox .content .scroll a.right').click(function()
	{
		$('#lightBox .content .holder .html').slideThis(-200);
	});
	
											
	//When everything is loaded, if there is a hash set, try to load the requested thumb
	if (window.location.hash)
	{
		$('#main .thumb').each(function()
		{
			if ('#' + $(this).attr('name') == window.location.hash)
				$(this).getThumbData($(this).attr('name'), 'left', false);
		});
		
		if (window.location.hash == '#contact')
			$('#nav .contact').getContact();
	}
	
	
	//Add listener to drag the html box in the lightbox
	$('#lightBox .content .holder .html').draggable({
														axis: 'x',
														stop: function(event, ui)
														{
															//alert(event.timeStamp - event.originalEvent.timeStamp);
															if ($(this).css('left').replace('px', '') > 0)
																$(this).animate({'left': '0'});
															
															if ($(this).css('left').replace('px', '') < -($(this).width() - $('#lightBox .content .holder').width()) + 13)
																$(this).animate({'left': -($(this).width() - $('#lightBox .content .holder').width()) + 13});
																								
														}
														
													});
													
													
	//Add a listener to the about button
	$('#nav .about').mouseover(function()
	{
		$('#about').fadeIn(500);
	});
	$('#nav .about').mouseout(function()
	{
		$('#about').fadeOut(500);
	});
	
	
	//Add a listener to the contact button
	$('#nav .contact').click(function()
	{
		$(this).getContact();
	});
});




//Get the contact form
(function($)
{
	$.fn.getContact = function()
	{
		return $.ajax({
						url: "./php/controller.php",
						type: 'POST',
						data: "ajax=getContactForm",
						dataType: 'string',
						success: function(data)
						{
							//Set the contact form
							$(this).setContactForm(data);
							$(this).loombox('left');
							
							//Add a listener to the submit of the contact form
							$('#contact').submit(function()
							{
								$(this).setContactData();
								return false;
							});							

							//Set the hash
							window.location.hash = 'contact';
						}
					 });
	};
})(jQuery);




//Set the content for the contact form
(function($)
{
	$.fn.setContactForm = function(data, callback)
	{
		return this.each(function()
		{
			//Disable the previous and next buttons
			$('#lightBox .content .nav .next').css('display', 'none');
			$('#lightBox .content .nav .prev').css('display', 'none');
			
			//Set the title
			$('#lightBox .content .nav h1').html('Contact');
			
			//Set the link
			$('#lightBox .content .nav .visit').attr('href', '#');
			$('#lightBox .content .nav .visit').text('Visit Site');
			$('#lightBox .content .nav .visit').css('display', 'none');
			
			//Set the html
			$('#lightBox .content .holder .html').html(data);
			
			//Set the height of the box
			var mTop = $('#lightBox .body .content .holder').css('margin-top').replace('px', '');
			var mBot = $('#lightBox .body .content .holder').css('margin-bottom').replace('px', '');
			var height = 360 - mTop - mBot;
			$('#lightBox .body td.content').css('height', height);
			$('#lightBox .body td.left').css('height', height);
			$('#lightBox .body td.right').css('height', height);
			
			//Set the fixed width
			global.lbWidth = 500;
			global.lbContWidth = 500;
			$('#lightBox .content .holder .html').css('width', global.lbContWidth + 'px');
			$('#lightBox .content .scroll').css('display', 'none');
			$('#lightBox .content .holder .html').draggable('disable');
			
			//Center the lightbox
			$('#lightBox').centerScale(50, 40);			
		});
	};
})(jQuery);




//Send the contact data back to the server
(function($)
{
	$.fn.setContactData = function()
	{
		//Run form checks
		if (!$('#contact').formChecks())
		{
			return $.ajax({
							url: "./php/controller.php",
							type: 'POST',
							data: $(this).serialize() + "&ajax=setContactData",
							dataType: 'string',
							success: function(data)
							{
								//Set the contact form with the response
								$('#lightBox .content .holder .html').html(data);													
							}
						 });
		}
		else
		{
			return false;
		}
	};
})(jQuery);




//Check the fields of the given form to make sure they're not blank
(function($)
{
	$.fn.formChecks = function()
	{
		var err = false;
			
		$(this).children('input.text, textarea.textarea').each(function()
		{
			if ($(this).val() == '')
			{
				$(this).addClass('error');
				err = true;
				return;
			}
			else if ($(this).attr('name') == 'email' && !$(this).emailCheck($(this).val()))
			{
				$(this).addClass('error');
				err = true;
				return;
			}
			else
				$(this).removeClass('error');
		});
		
		return err;
	};
})(jQuery);




//Check for a valid email
(function($)
{
	$.fn.emailCheck = function(email)
	{
		return (email.indexOf(".") > 2) && (email.indexOf("@") > 0);
	};
})(jQuery);



//Get the content for a thumbnail
(function($)
{
	$.fn.getThumbData = function(id, slide, clearPrev)
	{
		return $.ajax({
						url: "./php/controller.php",
						type: 'POST',
						data: "ajax=getThumbData&id=" + id,
						dataType: 'json',
						success: function(data)
						{
							//If we're supposed to be sliding the old data out...
							if (clearPrev)
							{
								$(this).loomboxOut(clearPrev, function()
								{
									$(this).setThumbData(data);
									$(this).loombox(slide);
								});
							}
							else
							{
								$(this).setThumbData(data);
								$(this).loombox(slide);
							}
							
							//Set the hash
							window.location.hash = id;
						}
					 });
	};
})(jQuery);



//Get the data for the next thumbnail in the gallery
(function($)
{
	$.fn.getNextThumb = function(id)
	{
		return this.each(function()
		{
			var tId = $('#main a[name=' + id + ']').next().attr('name');
			if(!tId)
				tId = $('#main a.thumb:first').attr('name');
			$(this).getThumbData(tId, 'left', 'right');
			return false;
		});
	};
})(jQuery);



//Get the data for the previous thumbnail in the gallery
(function($)
{
	$.fn.getPrevThumb = function(id)
	{
		return this.each(function()
		{
			var tId = $('#main a[name=' + id + ']').prev().attr('name');
			if(!tId)
				tId = $('#main a.thumb:last').attr('name');
			$(this).getThumbData(tId, 'right', 'left');
			return false;
		});
	};
})(jQuery);



//Set the content for a thumbnail
(function($)
{
	$.fn.setThumbData = function(data, callback)
	{
		return this.each(function()
		{
			//Enable the previous and next buttons
			$('#lightBox .content .nav .next').css('display', 'inline');
			$('#lightBox .content .nav .prev').css('display', 'inline');
			
			//Set the title
			$('#lightBox .content .nav h1').html(data.title);
			
			//Set the link
			if (data.link)
			{
				$('#lightBox .content .nav .visit').attr('href', data.link);
				$('#lightBox .content .nav .visit').text(data.linkDesc);
				$('#lightBox .content .nav .visit').css('display', 'inline');
			}
			else
			{
				$('#lightBox .content .nav .visit').attr('href', '#');
				$('#lightBox .content .nav .visit').text('Visit Site');
				$('#lightBox .content .nav .visit').css('display', 'none');
			}
			
			//Set the html
			$('#lightBox .content .holder .html').html(data.string);
			
			//Set the hidden ID input
			$('#lightBox .content .holder .idHolder').val(data.id);
			
			//Set the height of the box
			var mTop = $('#lightBox .body .content .holder').css('margin-top').replace('px', '');
			var mBot = $('#lightBox .body .content .holder').css('margin-bottom').replace('px', '');
			var height = data.contentHeight - mTop - mBot;
			$('#lightBox .body td.content').css('height', height);
			$('#lightBox .body td.left').css('height', height);
			$('#lightBox .body td.right').css('height', height);
			
			//Set the fixed width or unset it
			if (data.width)
				global.lbWidth = data.width;
			else
				global.lbWidth = null;
								
			//Set the width of the html box
			if (data.contentWidth)
			{
				$('#lightBox .content .holder .html').css('width', data.contentWidth + 'px');
				global.lbContWidth = data.contentWidth;
				
				//Set the scroll buttons
				if (data.contentWidth > $('#lightBox .content .holder').width())
				{
					//Set the top-margin of the buttons
					var mT = height / 2;
					$('#lightBox .content .scroll a').css('margin-top', mT + 'px');
					$('#lightBox .content .scroll').css('display', 'block');
				}
				else
				{
					$('#lightBox .content .scroll').css('display', 'none');
				}
			}
			else
			{
				global.lbContWidth = null;
				$('#lightBox .content .scroll').css('display', 'none');
			}
			
			
			//Add a draggable listener if we're supposed to
			if (data.draggable == 'TRUE')
			{
				$('#lightBox .content .holder .html').draggable('enable');
			}
			else
			{
				$('#lightBox .content .holder .html').draggable('disable');
			}
				

			//Center the lightbox
			$('#lightBox').centerScale(50, 40);			
		});
	};
})(jQuery);



//Transform all of the SIFR elements
(function($)
{
	$.fn.sifrReload = function(tag, color, fontSize)
	{
		sIFR.replace(ffdCorpBold, 
		{
		  selector: tag,
		  css: '.sIFR-root { color: ' + color + '; text-transform: uppercase; font-size: ' + fontSize + '}; }',
		  wmode: 'transparent'
		});
	};
})(jQuery);




//Resize the given element
(function($)
{
	$.fn.windowHeight = function (element)
	{
		//Need a conditional here about element height!!!
			$(element).height($(window).height() + 'px');
	};
})(jQuery);




//Center the element in the frame and scale it
(function($)
{
	$.fn.centerScale = function(marginLeft, marginRight)
	{
		return this.each(function()
		{
			//If the box is hidden, we need to make it invisible so we can get the size
			if ($(this).css('display') == 'none')
			{
				//Make element visibility: hidden to get it's height.
				$(this).css({'display': 'inline',
									'visibility': 'hidden'});
									
				//Hide the element again.
				$(this).css({'display': 'none',
									'visibility': 'visible'});
			}
								
			//Set the width of the box if we need to
			if (!global.lbWidth)
			{
				$(this).width($(window).width() - (marginLeft + marginRight) + 'px');
				$(this).css('left', marginLeft + 'px');
			}
			else
			{
				$(this).width(global.lbWidth);
				$(this).css('left', ($(window).width() - $(this).width()) / 2 + 'px');
			}
			
			
			//Set the size of the table body content
			$('#lightBox .body .content .holder').width($(this).width() - 70 + 'px');
			
			
			//If the content width isn't set
			if (!global.lbContWidth)
			{
				var conWidth = $('#lightBox .content .holder').width();
				if (conWidth != 0)
					$('#lightBox .content .holder .html').width(conWidth);
				else
					$('#lightBox .content .holder .html').css('width', '100%');
			}
			else
			{
				$('#lightBox .content .holder .html').width(global.lbContWidth + 'px');
			}
			
			
			//Set the top value
			var top = ($(window).height() - $(this).height()) / 2;
			$(this).css('top', top + 9 + 'px');
			
			
			//Set the width of the top and bottom of the box center elements
			var w = $(this).width();
			$('#lightBox .top .loop').width(w - 82);
			$('#lightBox .bot .loop').width(w - 82);
		});
	};
})(jQuery);




//Slide the given element in the given direction as long as it doesn't exceed the width or go to a negative value
(function($)
{
	$.fn.slideThis = function(amount)
	{
		return this.each(function()
		{
			var left = $(this).css('left').replace('px', '');
			var frame = $('#lightBox .content .holder').width();
			var width = $(this).width();
			var val =  left - -(amount);
			if (val < 0)
			{
				if (val < -(width - frame - 13))
				{
					$(this).animate({'left': -(width - frame)});
				}
				else
					$(this).animate({'left': val});
			}
			else
				$(this).animate({'left': 0});
				
			return false;
		});
	};
})(jQuery);

