var flagTeamExpanded = false;
//document ready
$(function(){
	
	// Blinking Fields
	$('.blink')
		.focus(function() { if(this.title==this.value) { this.value = ''; } })
		.blur(function(){ if(this.value=='') {this.value = this.title; } });
	
		
	//customize messages
	$('.customize-box input[type="button"]').click(function(){
		var uName = $('.customize-box input[type="text"]').val();
		
		//clear default value 
		if (uName === $('.customize-box input[type="text"]').attr('title')) {
			uName = '';	
		}
		
		//hide homepage box and show message
		$('.customize-box').fadeOut(function(){
			$('.customize-message h3 span, #page-gallery .modal-box h3 span, #page-redesign .modal-box p span, #page-now-playing .badge span span, #page-team .customize strong span, #page-now-playing .appBox h3 span').text(uName);
			$('.customize-message').fadeIn();
		});
		
		//changes name fields
		$('#contact-dd-name, #contact-name').attr('value', uName);
		
	});
		
	//contact tabs
	$('.contact-tabs .links a').click(function(){
		var idx = $(this).parent().index();
		$(this).parents('.contact-tabs').find('.tab').hide();
		$(this).parents('.contact-tabs').find('.tab').eq(idx).show();
		if (idx == 1) {
			$(this).parents('.contact-tabs').find('.tab').eq(idx).html('<iframe width="272" height="231" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=Showcase+Marketing+Inc,+Woodruff+Road,+Greenville,+SC,+United+States&amp;aq=3&amp;sll=37.0625,-95.677068&amp;sspn=59.119059,135.263672&amp;ie=UTF8&amp;hq=Showcase+Marketing+Inc,&amp;hnear=Woodruff+Rd,+Greenville,+South+Carolina&amp;ll=34.823316,-82.288198&amp;spn=0.016276,0.02326&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>');
		}
		return false;
	});
	
	//contact dropdown
	$('li.contact a').click(function(){
		$('#contact-dd').slideToggle();
		return false;
	});
	
	//modal box action
	$('.modal-box .arrow-trigger').click(function(){
		$(this).toggleClass('arrow-trigger-expand');
		$(this).parents('.modal-box').find('.inner').slideToggle();
		return false;
	})
	
	$('#page-redesign .slideshow .item').simpleFade({
		speed: 500,
		duration: 3000,
		init: function(){
			fader = this;
		},
		onBeforeFade: function(index){
			$('.item p').hide();
			$('.item .view-site').hide();
			
		},
		onAfterFade: function(index){
			$('.item p').hide();
			$('.item .view-site').hide();
			if( $('.item').eq(index).find('.view-site').length ){
				$('.item').eq(index).find('.view-site').fadeIn('fast');
			}
			$('.item').eq(index).find('p').fadeIn('fast');
		}
	});
	$('#page-redesign .slideshow .next').click(function(){
		fader.next();
		return false;
	});
	$('#page-redesign .slideshow .prev').click(function(){
		fader.prev();
		return false;
	});
	
	$('#page-we-did a.comingSoon').click(function(){
		return false;
	});
	
	
	$('#page-team .picture').each(function(){
		$(this).attr('initTop', parseInt($(this).css('top')))
		$(this).attr('initLeft', parseInt($(this).css('left')))
		$(this).attr('initzIndex', parseInt($(this).css('z-index')))
	})
	
	if ($.client.os != "iPad" && $.client.os != "iPhone/iPod") {
		$('#page-team .fanOut').bind('inview', function(event, visible){
			if (visible) {
				var dir = getScrollDirection();
				if ( dir === "down" ) {
					$('#page-team .picture').each(function(){
						var x = parseInt($(this).find('.x').text());
						var y = parseInt($(this).find('.y').text())
						$(this).animate({
							top: y,
							left: x
						});
						if ($(this).find('.zidx').length) {
							var z = parseInt($(this).find('.zidx').text());	
							$(this).css({
								zIndex: z
							});
						}
					});
					flagTeamExpanded = true;
				}
			}
		});
		
		$('#page-team a.ribbon').bind('inview', function(event, visible){
			if (!visible) {
				var dir = getScrollDirection();
				if ( dir === "up" ) {
					$('#page-team .picture').each(function(){
						var x = parseInt($(this).attr('initLeft'));
						var y = parseInt($(this).attr('initTop'));
						var z = parseInt($(this).attr('initzIndex'));	
						$(this).animate({
							top: y,
							left: x,
							zIndex: z
						});
						
					});
					flagTeamExpanded = false;
				}
			}
		})
	}
	
	var galleryUpNone = true;
	var galleryUp = false;
	$('#page-gallery').bind('inview', function(event, visible){
		if (visible && galleryUpNone && !galleryUp) {
			window.setTimeout(function(){
				$('#page-gallery').find('.modal-box').fadeIn(800);
				galleryUpNone = false;
				galleryUp = true;
			}, 2000);
		} else if (visible && !galleryUp) {
			$(this).find('.modal-box').fadeIn(800);
			galleryUp = true;
		} else if (!visible) {
			galleryUp = false;
		};
	});
	var monitorUpNone = true;
	var monitorUp = false;
	var mobileUpNone = true;
	var mobileUp = false;
	var blackboxUpNone = true;
	var blackboxUp = false;
	$('#page-redesign .slideshow').bind('inview', function(event, visible){
		if (visible && monitorUpNone && !monitorUp) {
			$(this).find('.modal-box').fadeIn(800);
			monitorUpNone = false;
			monitorUp = true;
		} else if (visible && !galleryUp) {
			$(this).find('.modal-box').fadeIn(800);
			monitorUp = true;
		} else if (!visible) {
			monitorUp = false;
		};
	});
	$('#page-now-playing').bind('inview', function(event, visible){
		if (visible && monitorUpNone && !monitorUp) {
			$(this).find('.modal-box').fadeIn(800);
			mobileUpNone = false;
			mobileUp = true;
		} else if (visible && !galleryUp) {
			$(this).find('.modal-box').fadeIn(800);
			mobileUp = true;
		} else if (!visible) {
			mobileUp = false;
		};
	});
	$('#page-magazine').bind('inview', function(event, visible){
		if (visible && monitorUpNone && !monitorUp) {
			$(this).find('.modal-box').fadeIn(800);
			blackboxUpNone = false;
			blackboxUp = true;
		} else if (visible && !galleryUp) {
			$(this).find('.modal-box').fadeIn(800);
			blackboxUp = true;
		} else if (!visible) {
			blackboxUp = false;
		};
	});
	
	
	
	
	$("#page-team .picture").mouseenter(function(){
		$(this).mousemove(function(e){
			if (flagTeamExpanded && $(this).hasClass('team-member')) {
				var x = e.pageX;
				var y = e.pageY;
				var name = $(this).find('.name').text();
				var pos = $(this).find('.position').text();
				$('.team-tip h3').text(name);
				$('.team-tip p').text(pos);
				if (x < $(window).width()*.15) {
					$('.team-tip .arrow').addClass('arrow-left');
					x = x + $('.team-tip').outerWidth(true)/2 - 50;
				} else if (x > $(window).width()*.85) {
					$('.team-tip .arrow').removeClass('arrow-left');
					x = x - $('.team-tip').outerWidth(true) - 20;
				} else if (x > $(window).width()/2) {
					$('.team-tip .arrow').addClass('arrow-left');
					x = x + $('.team-tip').outerWidth(true)/2 - 50;
				}else{
					$('.team-tip .arrow').removeClass('arrow-left');
					x = x - $('.team-tip').outerWidth(true) - 20;
				}
				$('.team-tip').css({
					top: y - $(this).outerHeight(true)/6,
					left: x
				}).show();
			}
		})
	}).mouseleave(function(){
		$('.team-tip h3').text('');
		$('.team-tip p').text('');
		$('.team-tip').hide();
	});
	
	$("#page-we-did a").mouseenter(function(){
		$(this).mousemove(function(e){
			var x = e.pageX;
			var y = e.pageY;
			var title = $(this).find('.title').text();
			$('.team-tip h3').text(title);
			if (x < $(window).width()*.15) {
				$('.team-tip .arrow').addClass('arrow-left');
				x = x + $('.team-tip').outerWidth(true)/2 - 50;
			} else if (x > $(window).width()*.85) {
				$('.team-tip .arrow').removeClass('arrow-left');
				x = x - $('.team-tip').outerWidth(true) - 20;
			} else if (x > $(window).width()/2) {
				$('.team-tip .arrow').addClass('arrow-left');
				x = x + $('.team-tip').outerWidth(true)/2 - 50;
			}else{
				$('.team-tip .arrow').removeClass('arrow-left');
				x = x - $('.team-tip').outerWidth(true) - 20;
			}
			$('.team-tip').css({
				top: y - $(this).outerHeight(true)/6,
				left: x
			}).show();
		})
	}).mouseleave(function(){
		$('.team-tip h3').text('');
		$('.team-tip p').text('');
		$('.team-tip').hide();
	});
	
	$('#page-we-did .picture').colorbox({
		opacity: 0.7,
		onLoad: function(){
			$('#cboxClose').hide();
		},
		onComplete: function(){
			$('#cboxClose').fadeIn();
			$('.scroll-area').jScrollPane({
				verticalDragMinHeight: 60,
				verticalDragMaxHeight: 60
			});
		}
	});
	
	$('#page-team .picture').click(function(){
		if(!$(this).hasClass('team-member')){
			return false;
		}
	})
	
	var endX, endY;
	var mouseFlag = false;
	$('.mouse-pad').mouseover(function(){
		mouseFlag = true;
		$('.mouse-pad').mousemove(function(e){
			if (mouseFlag) {
				var pX = e.pageX - 45;
				var pY = e.pageY - 40;
				var ofT = $(this).offset().top;
				var ofL = $(this).offset().left;
				endX = Math.round(parseInt(pX)) - Math.round(parseInt(ofL));
				endY = Math.round(parseInt(pY)) - Math.round(parseInt(ofT));
				if (endX > 0 && endX < 190 && endY > 0 && endY < 190) {
					$('.mouse').css({
						top: endY,
						left: endX
					});
				}
			};
			
		});
	}).mouseout(function(){
		mouseFlag = false;
	});
	
	$('.monitor .item').simpleFade({
		auto: false,
		speed: 600,
		init: function(){
			monitorFader = this;
			$('.mouse').click(function(){
				monitorFader.next();
				$('#beep-two')[0].play();
			})
		},
		onAfterFade: function(index){
			var href = $('.monitor .item').eq(index).find('a').attr('href');
			$('#page-contact .monitor .note .trigger').attr('href', href);
		}
	})
	
	$('.monitor .item a, .monitor .trigger, .tab-info .all, .team-member').colorbox({
		opacity: 0.7,
		onLoad: function(){
			$('#cboxClose').hide();
		},
		onComplete: function(){
			$('#cboxClose').fadeIn();
			if ($('#phone-dir select').length) {
				$('#phone-dir select').selectbox();
				$.colorbox.resize();
				$('.directorySelect').change(function(){
					var result = $(this).find('option:selected').val();
					$('ul.results').fadeOut(500, function(){
						$('ul.results li').hide().filter('.'+result).show();
						$('ul.results').fadeIn();
						$.colorbox.resize();
					});
				});
			}
			if ($('#project-popup.preview').length){
				$('#project-popup.preview').mousedown(function(){
					$.colorbox.close();
				});
			};
		}
	});
	
	$('.keyboard').draggable({
		containment: '#page-contact'
	});
	
	$('.scroll-back').click(function(){
		$('html, body').animate({
			scrollTop: 0
		}, 1000);
		return false;
	});
	
	$('form').submit(function(){
		if ($(this).hasClass('customize-box')) {
			$('.customize-box .button').trigger('click');
			return false;
		}
		
	})
	
	$('.yellow-note, #note-popup .close').click(function(){
		$('#note-popup').fadeToggle();
		return false;
	});
	
	if ($.client.os !== "iPad" && $.client.os !== "iPhone/iPod" ) {
		var sOff = $('#page-magazine .shell').offset().left + 74;
		$('.magazine').css('background-position', sOff + 'px -15px');
	}
	
	$('form.contact-form').submit(function(){
		$(this).find('input.button').val('Sending...');
		$(this).addClass('submitted');
		$(this).bind('finished', function(){
			$(this).fadeOut(500, function(){
				$(this).next('.formSuccess').fadeIn(500);
			});
		});
	});
	
	
	if ($.client.os === "iPad" || $.client.os === "iPhone/iPod" ) {
		
		$('#page-team .picture').each(function(){
				var x = parseInt($(this).find('.x').text());
				var y = parseInt($(this).find('.y').text())
				$(this).animate({
					top: y,
					left: x
				});
				if ($(this).find('.zidx').length) {
					var z = parseInt($(this).find('.zidx').text());	
					$(this).css({
						zIndex: z
					});
				}
			});
			flagTeamExpanded = true;
	};
	
	$('.modal-box .closeBtn').click(function(){
		$(this).parent().fadeOut();
		return false;
	});
	
	$('#didThatHolder .scrollWrapper').horizontal_scroll()
	
});

var tempScrollTop, currentScrollTop = 0;
var dir = "down";
function getScrollDirection(){
	$(window).scroll(function(){
		currentScrollTop = $(window).scrollTop();
		if (tempScrollTop < currentScrollTop ) dir = "down"
		else if (tempScrollTop > currentScrollTop ) dir = "up"
		tempScrollTop = currentScrollTop;
	});
	return dir;
}

$.fn.horizontal_scroll = function(settings){  
    var clip = $(this);
    if(clip.length == 0 )
        return;
    var el = clip.find('.scrollableArea');
    var direction = 'right';
    var el_w = el.width(); 
    var is_fb = typeof(settings) != 'undefined' && typeof(settings.is_fb) != 'undefined' ? settings['is_fb'] : false;
    var clip_w = clip.width();
    var clip_x = clip.position().left;
    if( el_w < clip_w && !is_fb )
        return;
    var animated = false;
    var left_position;
    var direct;
    var space_not_enabled = Math.ceil(clip_w / 8);
    clip.mousemove(function(e){
        var x = e.pageX - clip.offset().left;
        var left_x = clip_w / 2 - space_not_enabled;
        var right_x = clip_w / 2 + space_not_enabled;
        if( x > clip_w / 2 ) {
            direct = 'right';
        }else {
            direct = 'left';
        }
        if( direction != direct || !animated ){
            direction = direct;
            _animate_horizontal();
        }
        if( x > left_x && x < right_x ) {
            _stop_h();
        }
    });
    clip.hover(function(){}, function(){
        _stop_h();  
    });
    function _stop_h(){
        el.stop();
        animated = false;
    }
    function _animate_horizontal(){
        var el_w = el.width();
        if( el_w < clip_w ) {
            return;
        }
        if( direction == 'left' ) {
            left_position = 0;
        }else {
            left_position = clip_w - el_w;
        }
        var space_left = Math.abs( left_position - el.position().left );
        time = Math.ceil(space_left / clip_w * 3000);
        el.stop();
        el.animate({ 'left' : left_position + 'px' }, time, 'linear');
        animated = true;
    }
}
