var honeypot = function () {
	return {
		setup: function () {
			honeypot.contact.init();
			honeypot.videos.bind_events();
			honeypot.misc.bind_testimonial_events();
		}
	}
} ();

honeypot.videos = function () {
	return {
		bind_events : function () {
			$('#video-close').unbind().bind('click', honeypot.videos.close_popup);
			$('.video-link').unbind().bind('click', honeypot.videos.open_popup);
		}, //bind_events
		close_popup : function () {
			honeypot.slideshow.activate();
			if(jQuery.support.opacity)
			{
				$('#video-close').unbind();
				$('#video-container').animate({opacity: 0}, 400, function () { 
					$(this).html('');
					$('#video-close').fadeOut(400, function () {
						$('#video-title').fadeOut(300, function () {
							$('#video-overlay').fadeOut(200);
							honeypot.videos.bind_events();
						 });
					});
				});
			}
			else
			{
				$('#video-overlay, #video-overlay *').hide();
				$('#video-container').html('');
			}
		}, // close_popup
		open_popup : function (e) {
			var _el = $(e.target);
			var _href;

			if(typeof(_el.parent('a').attr('href')) == 'undefined')
			{
				_href = _el.attr('href');
			}
			else
			{
				_href = _el.parent('a').attr('href');
			}
			honeypot.slideshow.deactivate();
			$('#video-container').html('');
			if(jQuery.support.opacity)
			{
				$('#video-overlay, #video-overlay *').css({display: 'block', opacity: 0});
				$('#video-titles').css({opacity: 1});
				$('#video-overlay').animate({opacity: 1}, 300, function () { 
					$('#video-title').animate({opacity: 1}, 400, function () {
						$('#video-close').animate({opacity: 1}, 500, function () {
							$('#video-container').animate({opacity: 1}, 600, function () {
								$('#video-container').html('<object width="640" height="340"><param name="movie" value="' + _href + '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="' + _href + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="340"></embed></object>');						
							});
						 });
					});
				});
			}
			else
			{
				$('#video-overlay, #video-overlay *').show();
				$('#video-container').html('<object width="640" height="505"><param name="movie" value="' + _href + '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="' + _href + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object>');						
			}
			return false;
		} // open_popup
	}
}();

honeypot.misc = function () {
	return {
		bind_testimonial_events: function () {
			$('.readfull').unbind().bind('click', honeypot.misc.show_testimonial);
			$('#testimonial-close').unbind().bind('click', honeypot.misc.close_testimonial);
		}, //bind_testimonial_events
		show_testimonial: function (e) {
			honeypot.slideshow.deactivate();
			var fulltext = $(e.target).parent('.testimonials-small');
			$(fulltext).children('.text');
			
			if(jQuery.support.opacity)
			{
				$('#testimonial-overlay, #testimonial-overlay *').css({display: 'block', opacity: 0});
				$('#testimonial-overlay').animate({opacity: 1}, 300, function () { 
					$('#testimonial-titles, #testimonial-title').animate({opacity: 1}, 350, function () {
						$('#testimonial-close').animate({opacity: 1}, 400, function () {
							$('#testimonial-container').html('<p class="bigwho"><strong>' + $(fulltext).children('.who').eq(0).html() + ' said...</strong></p><p>' + $(fulltext).children('.full').eq(0).html() + '</p>').animate({opacity: 1}, 450);
						 });
					});
				});
			}
			else
			{
				$('#testimonial-overlay, #testimonial-overlay *').show();
				$('#testimonial-overlay').show();
				$('#testimonial-titles, #testimonial-title').show();
				$('#testimonial-close').show();
				$('#testimonial-container').html('<p class="bigwho"><strong>' + $(fulltext).children('.who').eq(0).html() + ' said...</strong></p><p>' + $(fulltext).children('.full').eq(0).html() + '</p>').show();
			}
		}, // show_testimonial()
		close_testimonial: function (e) {
			honeypot.slideshow.activate();
			$('#testimonial-close').unbind();
			if(jQuery.support.opacity)
			{
				$('#testimonial-container').animate({opacity: 0}, 300, function () { 
					$('#testimonial-close').fadeOut(250, function () {
						$('#testimonial-title').fadeOut(200, function () {
							$('#testimonial-overlay').fadeOut(150);
							honeypot.misc.bind_testimonial_events();
						 });
					});
				});
			}
			else
			{
				$('#testimonial-overlay, #testimonial-overlay *').hide();
				honeypot.misc.bind_testimonial_events();
			}
		} // close_testimonial()
	}
} ();

honeypot.contact = function () {
	return {
		init: function () {
			$('#contact-tab, a[rel=contactus]').unbind().bind('click', honeypot.contact.open_form);
			$('#contact-close').unbind().bind('click', honeypot.contact.close_form);
		}, // init()
		open_form : function () {
			honeypot.slideshow.deactivate();
			if(jQuery.support.opacity)
			{
				$('body').animate({backgroundPosition: '50% 316px'}, 1400, 'easeOutQuint');
				$('#contact-slide-shadow').fadeIn(1400);
			}
			else
			{
				$('body').animate({backgroundPositionY: 316}, 1400, 'easeOutQuint');
				$('#contact-slide-shadow').show();
			}
			$('#whole-thing').animate({top: 316}, 1400, 'easeOutQuint');
			$('#contact-slidedown').animate({top: 0}, 1400, 'easeOutQuint');
			$('#flash-background').animate({top: 316}, 1400, 'easeOutQuint');
		}, // open_form
		close_form : function () {
			honeypot.slideshow.activate();
			if(jQuery.support.opacity)
			{
				$('body').animate({backgroundPosition: '50% 0px'}, 1400, 'easeOutQuint');
				$('#contact-slide-shadow').fadeOut(1400);
			}
			else
			{
				$('body').animate({backgroundPositionY: 0}, 1400, 'easeOutQuint');
				$('#contact-slide-shadow').hide();
			}
			$('#whole-thing').animate({top: 0}, 1400, 'easeOutQuint');
			$('#contact-slidedown').animate({top: -316}, 1400, 'easeOutQuint');
			$('#flash-background').animate({top: 0}, 1400, 'easeOutQuint');
		} // close_form
	}
} ();

honeypot.slideshow = function () {
	var image_array = new Array();
	var image_path = 'res/photographs/mini-slideshow/';
	var use_image_path;
	var current_pos = 0;
	var automatic = true;
	return {
		activate: function () {
			automatic = true;
		},
		deactivate: function () {
			automatic = false;
		},
		init: function (array, custom_image_path) {						
			image_array = jQuery.makeArray(array);
			
			if(typeof(custom_image_path) == 'undefined')
			{
				use_image_path = image_path;
			}
			else
			{
				use_image_path = custom_image_path;
			}

			for (var i = 0; i < image_array.length; i++) 
			{
				$('#slideshow_placeholder').append($('<div>').attr('id', 'placeholder_' + (i)).attr('class', 'loading slideshow-slide'));
				image_array[i].image = new Image();
				$(image_array[i].image)
					.load(function(){
							for (var c = 0; c < image_array.length; c++) 
							{
								if($('#placeholder_' + c).attr('title') == '')
								{
									$('#placeholder_' + c).removeClass()
													.addClass('slideshow-slide')
													.attr('style', 'background-image:url(' + $(this).attr('src') + ')')
													.attr('title', $(this).attr('title').substr(0,1));
									break;
								}
							}
							$('#placeholder_0').addClass('slideshow-slide').show().css({left: -645, display: 'block'});
						})
					.attr('src', use_image_path + image_array[i].src)
					.attr('title', image_array[i].title);
			}
			

			honeypot.slideshow.proceed();
			honeypot.slideshow.init_controls();
		}, //init()
		init_controls: function () {
			$('.slideshow-slide').hide();
			
			$('#placeholder_0').addClass('slideshow-slide loading').show().css({left: -645, display: 'block'});
			
			$('#placeholder_' + (current_pos)).show();
			
			$('#slideshow_placeholder, #slideshow-tools').unbind().bind('mouseover', function () {
				automatic = false;
			}).bind('mouseout', function () {
				automatic = true;
			});
			
			
			$('#slideshow-leftarrow').removeClass('no-pointer').bind('click', function () {
				honeypot.slideshow.go_prev();		
			});
			
			$('#slideshow-rightarrow').removeClass('no-pointer').bind('click', function () {
				honeypot.slideshow.go_next(false);																 
			});
			
			if(jQuery.support.opacity)
				$('#slideshow-tools span').fadeTo(150, 1);
		}, //init_controls()
		kill_controls : function () {
			//alert(current_pos);		
			$('.slideshow-slide').css('zIndex', 1);		
			$('#placeholder_' + current_pos).css({zIndex: 2}).show();
			if(jQuery.support.opacity)
				$('#slideshow-tools span').unbind().fadeTo(150, 0.2);
			else
				$('#slideshow-tools span').unbind();
		}, // kill_controls()
		go_next : function (is_automatic) {
			if(is_automatic && automatic || is_automatic == false)
			{
				$('.slideshow-slide').hide();	
				$('#slideshow-rightarrow').addClass('no-pointer');																		 
				honeypot.slideshow.kill_controls();	
				current_pos--;
				if(current_pos < 0)
				{
					current_pos = (image_array.length - 1);
				}
				$('#placeholder_' + (current_pos)).show().css({zIndex: 3, left: -1290, opacity: 0}).animate({left: -645, opacity: 1}, 1500, 'easeOutQuint', honeypot.slideshow.init_controls);
			}
		}, // go_next()
		go_prev : function () {
			$('.slideshow-slide').hide();	
			$('#slideshow-leftarrow').addClass('no-pointer');
			honeypot.slideshow.kill_controls();											  
			current_pos++;
			if(current_pos > (image_array.length - 1))
			{
				current_pos = 0;
			}
			$('#placeholder_' + (current_pos)).show().css({zIndex: 3, left: 645, opacity: 0}).animate({left: -645, opacity: 1}, 1500, 'easeOutQuint', honeypot.slideshow.init_controls);
		}, // go_prev()
		proceed : function () {
			setTimeout('honeypot.slideshow.go_next(true); honeypot.slideshow.proceed();', 5000);
		}
	}
} ();

$(function () {
	honeypot.setup()
});
