var ReflexionsData = new function() {

	window.onload = function() {		
		$('#homepage-carousel').jcarousel({
			visible: 1,
			scroll: 1,
			auto: 6,
			wrap: 'last',
			initCallback: homepage_initCallback,
			itemFirstInCallback: {
				onBeforeAnimation: homepage_itemBeforeInCallback,
				onAfterAnimation: homepage_itemAfterInCallback
			},
			itemFirstOutCallback: {
				onBeforeAnimation: homepage_itemBeforeOutCallback,
				onAfterAnimation: homepage_itemAfterOutCallback
			},
			itemVisibleInCallback: {
				onAfterAnimation: homepage_itemVisibleInCallback
			},
			itemVisibleOutCallback: {
				onBeforeAnimation: homepage_itemVisibleOutCallback
			}
		});

		function homepage_itemVisibleInCallback(c, li) {
			$(li).animate({
				opacity: 1
			}, 'normal');
		}

		function homepage_itemVisibleOutCallback(c, li) {
			$(li).animate({
				opacity: 0
			}, 'normal');
		}

		function homepage_initCallback(carousel) {
            $('#homepage-carousel > li').css('opacity', 0);
			$('#homepage-carousel').show();
			var caption = $('#home-banner-caption');
			caption.animate({
				width: '406'
			}, 800, function() {
				caption.find('h1').hide();
				caption.animate({
					height: '120'
				}, 600, function() {
					caption.css('width', '356px');
					caption.css('height', '84px');
					caption.css('padding', '18px 25px');
					caption.find('h1').fadeIn('normal');
				});
			});

			$($('#home-banner-utility > img')[0]).attr('src', '/images/icon_home_on.png');
			$('#home-banner-utility > img').bind('click', function() {

				var new_banner_index = $(this).attr('banner_id');
				var new_banner = $('#homepage-carousel').find('img[rel='+new_banner_index+']').parents('li');
				carousel.scroll(new_banner.index() + 1);
			});
			return false;
		}

		function homepage_itemBeforeInCallback(c,li,i) {
			var banner_index = $(li).find('img').attr('rel');
			var indicator = $('#home-banner-utility  > img[banner_id='+banner_index+']');
			$(indicator).attr('src', '/images/icon_home_on.png');

			var condition1 = ($(li).attr('caption') != $('#home-banner-caption > h1').text());
			var condition2 = ($(li).is(':last-child') && ($(li).attr('caption') != $(li).first().attr('caption')));

			if (condition1 || condition2) {
				$('#home-banner-caption > h1').fadeOut('fast', function() {
					$('#home-banner-caption > h1').text($(li).attr('caption'));
					if ($(li).children('a').length) {
						var url = $(li).children('a').attr('href');
						$('#home-banner-caption').parent('a').attr('href', url);
					} else {
						$('#home-banner-caption').parent('a').removeAttr('href');
					}
				});
			}
		}

		function homepage_itemAfterInCallback(c,li,i) {
			var caption = $('#home-banner').find('#home-banner-caption');
			var timeoutId = setTimeout(function() {

				$('#home-banner-caption > h1').fadeIn('normal');

			}, 300);
			$('#homepage-carousel').data('timeoutId', timeoutId);

		}

		function homepage_itemBeforeOutCallback(c,li,i) {
			var banner_index = $(li).find('img').attr('rel');
			var indicator = $('#home-banner-utility > img[banner_id='+banner_index+']');
			$(indicator).attr('src', '/images/icon_home_off.png');
			clearTimeout($('#homepage-carousel').data('timeoutId'))

		}

		function homepage_itemAfterOutCallback(c,li,i) {


		}

		$('#home-feed-items').vTicker({
			speed: 300,
			pause: 5000,
			showItems: 1,
			mousePause: true,
			height: 28,
			direction: 'down'
		});
		$('#home-feed').show();

		$('#home-feed-title').mouseenter(function() {
			clearTimeout($(this).data('timeoutId'));

			var clip_height = $('#feed-carousel').height();

			$(this).addClass('on');
			$('#home-feed-arrow').attr('src', '/images/divider_arrow_grey.png');
			$('#home-feed-more').addClass('on');
			$('#home-feed-items').addClass('on').animate({
				height: clip_height
			}, 800);
		}).mouseleave(function() {
			var timeoutId = setTimeout(function() {

				var item_height = $('#home-feed').css('line-height');

				$('#home-feed-title').removeClass('on');
				$('#home-feed-arrow').attr('src', '/images/divider_arrow_white.png');
				$('#home-feed-more').removeClass('on');
				$('#home-feed-items').removeClass('on').animate({
					height: item_height
				}, 800);
			}, 300);
			$(this).data('timeoutId', timeoutId);
		});

		$('#home-feed-items').mouseenter(function() {
			clearTimeout($('#home-feed-title').data('timeoutId'));
		}).mouseleave(function() {
			var timeoutId = setTimeout(function() {

				var item_height = $('#home-feed').css('line-height');

				$('#home-feed-arrow').attr('src', '/images/divider_arrow_white.png');
				$('#home-feed-title').removeClass('on');
				$('#home-feed-more').removeClass('on');
				$('#home-feed-items').removeClass('on').animate({
					height: item_height
				}, 800);
			}, 300);
			$('#home-feed-title').data('timeoutId', timeoutId);
		});

		$('#client-carousel').jcarousel({
			visible: 5,
			scroll: 5,
			wrap: 'circular',
			animation: 'slow',
			initCallback: client_initCallback
		});

		function client_initCallback(carousel) {
			$('#client-carousel').show();
		}

		$('#client-carousel > li > img').mouseenter(function() {
			var client_id = $(this).parent('li').data('client');
			var client_popup = $('div#'+client_id);
			
			clearTimeout($(this).data('timeoutId'));

			/* Positioning popup */
			var offset = $(this).offset(); 
			var width = $(this).width();
			var popup_height = client_popup.height();
			var left = true;
			var left_offset = $(this).parent('li').offset().left;
			
			if (left_offset > 556) {
				left = false;
			}

			if (!left) {
				client_popup.css('background-image','url("../images/bg_client_popup_right.png")');
				client_popup.css('left', offset.left - 530);
			} else {
				client_popup.css('background-image', 'url("../images/bg_client_popup_left.png")');
				client_popup.css('left', offset.left - 65);
			}
			client_popup.css('top', offset.top - popup_height - parseInt(client_popup.css('padding-bottom')));
			
			$(this).parent('li').addClass('current');
			
			var logo_color = $(this).attr('src').replace('_bw','_color');
			$(this).removeClass('logo_bw').attr('src', logo_color);
			
			/* Show popup */
			client_popup.fadeIn();
		}).mouseleave(function() {
			var client_id = $(this).parent('li').data('client');
			var client_popup = $('div#'+client_id);
			var client_logo = $(this);
			
			var timeoutId = setTimeout(function() {
				/* Hide popup */
				$(client_popup).fadeOut();
				
				var logo_bw = client_logo.attr('src').replace('_color','_bw');
				client_logo.addClass('logo_bw').attr('src', logo_bw);
			}, 300);

			clearTimeout(client_logo.data('timeoutId'));
			$(this).data('timeoutId', timeoutId);
		});

		$('div.client-popup').mouseenter(function() {			
			var client_id = $(this).attr('id');
			var client_logo = $('#client-carousel > li[data-client='+client_id+'].current > img');
            var client_popup = $(this);

			clearTimeout(client_logo.data('timeoutId'));
		}).mouseleave(function() {
 			var client_id = $(this).attr('id');
			var client_logo = $('#client-carousel > li[data-client='+client_id+'].current > img');
			var client_popup = $(this);

			var timeoutId = setTimeout(function() {				
				/* Hide popup */
				client_popup.fadeOut();
				
				var logo_bw = client_logo.attr('src').replace('_color','_bw');
				client_logo.addClass('logo_bw').attr('src', logo_bw);
			}, 300);
			
			clearTimeout(client_logo.data('timeoutId'));
			client_logo.data('timeoutId', timeoutId);
		});

        $('.gallery').height($('img.image-loaded').height()+30);

		$('a.gallery-select').hover(function() {

			if (!$(this).hasClass('selected')) {
				var new_image_url = $(this).attr('image_url');
				var new_image = $('<img src="'+new_image_url+'" style="display:none;" class="border" />');
				var old_image = $(this).parent('div').siblings('img');

				if ($('span.gallery-caption').length) {
					var new_caption = $(this).attr('caption');
					var caption = $('span.gallery-caption');
					caption.fadeOut(function() {
						caption.text(new_caption);
						caption.fadeIn();
					});
				}

				old_image.fadeOut(function() {

					old_image.replaceWith(new_image);
                                        $('.gallery').height(new_image.height()+30);
                                        new_image.fadeIn();

				});


				$(this).siblings('a.gallery-select').removeClass('selected');
				$(this).addClass('selected');
			}

                    });

		$('a.gallery-select').click(function() {
			if (!$(this).hasClass('selected')) {
				var new_image_url = $(this).attr('image_url');
				var new_image = $('<img src="'+new_image_url+'" style="display:none;" class="border" />');
				var old_image = $(this).parent('div').siblings('img');
				old_image.fadeOut(function() {
					old_image.replaceWith(new_image);
					new_image.fadeIn();
				})

				if ($('span.gallery-caption').length) {
					var new_caption = $(this).attr('caption');
					var caption = $('span.gallery-caption');
					caption.fadeOut(function() {
						caption.text(new_caption);
						caption.fadeIn();
					});
				}

				$(this).siblings('a.gallery-select').removeClass('selected');
				$(this).addClass('selected');
			}
		});
	}

	this.submitForm = function(type) {
		var errors = $('div#errors');
		var form = $('form#'+type+'-form');
		var data = form.serialize();
		$.post('/ajax/'+type+'-submit', { data: data }, function(resp) {
			if (resp.success) {
				if (type == 'job') {
					$('input[name=id]','form#resume_upload').val(resp.id);
					$('form#resume_upload').submit();
				} else {
					form.parent('div').prev('div').remove();
					form.parent('div').html('<p><strong>Thank you for your inquiry. We will be contacting you shortly.</strong></p>');
				}
			} else {
				errors.empty();
				$.each(resp.errors, function(i, e) {
					errors.append(e+'<br />');
				});
			}
		}, 'json');
	}

}
