/*
 * Global Javascript File for www.rivercitystudio.com
 * created 12/30/2010
 * by Jay Pilgreen
 * copyright River City Studio
 * 
 * */


	// Start the Global Object in all of it's glory .. haha
	
var GLOBAL = {};

	// Defined Variables that need Global Scope

GLOBAL.shotgunTimer = 100;

	// Initialize all Event listeners

GLOBAL.init = function() {
	
	// Portfolio Section

	jQuery(".portGalleryThumbs img").live("click", function() {
		GLOBAL.swapMainPortGalleryImage(this);
	});
	
	jQuery(".portGalleryProject").click(function() {
		GLOBAL.swapPortGallery(this);
		
		// Swap the active status
		jQuery(".portGalleryProject").removeClass("active");
		jQuery(this).addClass("active");
		return false;
	});
	
		// New Accordian Functions
	jQuery(".portGalleryCategory").click(function() {
		jQuery(this).parents("#clientList").find(".portGalleryProjectNav").slideUp(225);
		jQuery(this).parent().find("ul").slideDown(225);
	});
	
	// Globally Scoped Variable
	var notIndex = false;
	
	// Mark the first link active since we are not going to the actual page where php can
	jQuery(".portGalleryExampleLink").each(function() {
		if(jQuery(this).hasClass("active")) {
			notIndex = true
		}
	});
	
	if(notIndex == false) {
		jQuery(".portGalleryExampleLink:first").addClass("active");
	};
	
	
	// FancyBox Listener
	jQuery("a.iframe").fancybox({
		"width": "90%",
		"height": "90%",
		"type": "iframe"
	});
	
	// Movement setter for all the different decorative boxes
	jQuery(".navyBox, .greenBox, .aquaBox, .whiteBox, .orangeBox, .yellowBox").each(function() {
		if(!jQuery(this).hasClass("static")) {
			var finishOpacity = jQuery(this).css("opacity");
			
			jQuery(this).animate({
				"opacity": 8
			}, 0);
			
			GLOBAL.shotgunAnimation(this, finishOpacity);
		}
	});
	
	
	
	// Email Signup Form
	jQuery( "#emailSignupForm input" ).focus( function() {
		if( jQuery( this ).attr('value') == "enter email address" ) jQuery( this ).attr( "value", "");
	}).blur( function() {
		if( jQuery( this ).attr('value') == "" ) jQuery( this ).attr( "value", "enter email address");
	});
	
	jQuery("#emailSignupForm").submit(function(event) {
		event.preventDefault();
		
		jQuery.ajax({
			url: "emailSignup.php",
			type: "POST",
			data: jQuery(this).serialize(),
			success: function() {
				jQuery("#emailSignupForm").html("<p>Thank you.<br />We have added your email address.</p>");
			}
		})
	});
	
}

	// Main Portfolio Image Swap
	
GLOBAL.swapMainPortGalleryImage = function(e) {
	// Reset the Active Toggle
	jQuery(".portGalleryThumbs img").removeClass("active");
	jQuery(e).addClass("active");
	
	// Swap the Image
	jQuery(".portGalleryMainImage").fadeOut(
		200, 
		function() {
			jQuery(this).attr("src", jQuery(e).attr("src")).fadeIn("200");
		}
	);
	
	var imageNumber = (jQuery(e).index()) + 1;
	var currentGallery = jQuery(".portGalleryDescription h2").text();
	
	_gaq.push(["_trackEvent", "Portfolio Gallery", "Changed Image", currentGallery + " - Image #" + imageNumber]);
}

	// Full Portfolio Gallery Swap
	
GLOBAL.swapPortGallery = function(e) {
	
	// Initiate the Ajax call
	
	var targetUrl = jQuery(e).attr("href");

	jQuery.ajax({
		url: targetUrl,
		beforeSend: GLOBAL.slideGalleryOut(),
		success: function(result) {
			jQuery("#portGallery")
				.html(jQuery(result).find("#portGallery").html())
				.children().hide();
				
			GLOBAL.slideGalleryIn();
		}
	});
	
	_gaq.push(["_trackEvent", "Portfolio Gallery", "Changed Gallery", jQuery(e).text()]);
}

GLOBAL.slideGalleryOut = function() {
	jQuery("#portGallery").children().fadeOut(200, "easeInExpo");
	
	jQuery("#portGallery")
		.animate(
			{
				"opacity": 0
			},
			400,
			"easeInCirc"
		)
		.animate(
			{
				"left": "+=" + jQuery(window).width()
			}, 
			400, 
			"easeInCirc"
		);
}

GLOBAL.slideGalleryIn = function() {
	// Functionality Here
	jQuery("#portGallery")
		.animate(
			{
				"opacity": 1,
				"left": 0
			}, 
			400, 
			"easeOutCirc",
			function() {
				jQuery("#portGallery").children().fadeIn(400, "easeOutExpo");
			}
		)
}

	// This fucntionality os for the box slide .. we need to move them out first .. then back in

GLOBAL.slideInBoxes = function(e, finishOpacity) {
	var position = jQuery(e).offset();
	var animationLength = 700;
	
	// Top Position is Easy Breezy Beautiful
	if(position.top < 0) {
		jQuery(e)
			.show()
			.animate({
				"top": "-=" + jQuery(document).height()
			}, 0)
			.animate({
				"top": position.top,
				"opacity": finishOpacity
			}, animationLength)
	}
	
	// Bottom Position, not so much
	else if((position.top + jQuery(e).height()) > jQuery(document).height()) {
		var bottomPosition = jQuery(e).css('bottom');
		jQuery(e)
			.show()
			.animate({
				"bottom": "-=" + jQuery(document).height()
			}, 0)
			.animate({
				"bottom": bottomPosition,
				"opacity": finishOpacity
			}, animationLength)
	}
	
	// Left Position is Cover Girl
	else if(position.left < 0) {
		jQuery(e)
			.show()
			.animate({
				"left": "-=" + jQuery(document).width()/2
			})
			.animate({
				"left": position.left,
				"opacity": finishOpacity
			}, animationLength)
	}
	
	// Right Side Like the Bottom
	else if((position.left + jQuery(e).width()) > jQuery(document).width()) {
		var rightPosition = jQuery(e).css('right');
		jQuery(e)
			.show()
			.animate({
				"right": "-=" + jQuery(document).width()
			}, 0)
			.animate({
				"right": rightPosition,
				"opacity": finishOpacity
			}, animationLength)
	}
}

GLOBAL.shotgunAnimation = function(e, finishOpacity) {
	// This little guy sets different delays on each animation to create a shotgun effect
	setTimeout(function(){ GLOBAL.slideInBoxes(e, finishOpacity) }, GLOBAL.shotgunTimer);
	GLOBAL.shotgunTimer += 100;
}

