$(document).ready(function(){
	
	// header shadow on scroll
	$(window).scroll(function(){
		if ($(window).scrollTop() > 15){
			$('body > header').addClass('active');
		} else {
			$('body > header').removeClass('active');	
		}	
	});
	
	// language selector
	$("a#selectlanguage").click(
		function(){
			var hostname = location.protocol+'//'+location.hostname;
			var url = location.href.replace(hostname,'');
			$('body').append('<div id="fade" onclick="recordOutboundLink(\'language-close\', \''+url+'\');"></div>');
			$('#fade').css({'z-index': '88888', 'filter' : 'alpha(opacity=80)'}).fadeIn(250);
			$('#languageselect').css({'z-index': '99999'}).animate({
				left: '0'
			}, 250, function(){ $('#languageselect').css({'z-index': '99999'}); });
			return false;
		}
	);
	$('#languageselect a.no').click(function() {
	  alert('Coming soon');
	  return false;
	});
	
	// show and hide the default text on form elements
	$("input, textarea").live('focus',
		function(){
			if ($(this).attr('placeholder') != ''){
				if ($(this).val() == $(this).attr('placeholder')){
					$(this).addClass('active');
					$(this).val('');
				}
			}
		}
	);
	$("input, textarea").live('blur',
		function(){
			if ($(this).attr('placeholder') != ''){
				if ($.trim($(this).val()) == ''){
					$(this).removeClass('active');
					$(this).val($(this).attr('placeholder'));
				}
			}
		}
	);
	
	// honeypot flash video controls
	$('#honeypotvideocontrols a').click(
		function(){
			if ($(this).hasClass('active')){
				// do nothing
			} else {
				if ($(this).hasClass('play')){
					$('#honeypotvideocontrols a.play').addClass('active');
					$('#honeypotvideocontrols a.pause').removeClass('active');
					flashTogglePlaying();
				} else if ($(this).hasClass('pause')){
					$('#honeypotvideocontrols a.pause').addClass('active');
					$('#honeypotvideocontrols a.play').removeClass('active');
					flashTogglePlaying();
				} else if ($(this).hasClass('sound')){
					$('#honeypotvideocontrols a.sound').addClass('active');
					$('#honeypotvideocontrols a.mute').removeClass('active');
					flashToggleSound();
				} else if ($(this).hasClass('mute')){
					$('#honeypotvideocontrols a.mute').addClass('active');
					$('#honeypotvideocontrols a.sound').removeClass('active');
					flashToggleSound();
				}
			}
			return false;
		}
	);
	
	// launch the contract team form
	$("a#contractteamjoinlaunch").click(
		function(){
			$('body').append('<div id="fade"></div>');
			$('#fade').css({'z-index': '88888', 'filter' : 'alpha(opacity=80)'}).fadeIn(250);
			$('#contractteamsignup').css({'z-index': '99999'}).animate({
				left: '0'
			}, 250, function(){ $('#contractteamsignup').css({'z-index': '99999'}); });
			return false;
		}
	);

	// close the contract team form - via the fade layer or a close link
	$('a.close, #fade').live('click', function(){
		if ($('#contractteamsignup').length != 0){
			$('#contractteamsignup').animate({
				left: '-730px'
			}, 250, function(){ });
		}
		$('#languageselect').animate({
			left: '-880px'
		}, 250, function(){ });
	    $('#fade').fadeOut(function() {
	        $('#fade').remove();
	    });
	    return false;
	});
	
	// toggle contract team terms and conditions
	$('a.teamcontractterms').click(function(){
		$("#teamcontract-termsandconditions").slideToggle("normal");
		return false;
	});
	
	$('a.teamcontractagreetoterms').click(function(){
		// hide signup form
		$('#contractteamsignup').animate({
			left: '-730px'
		}, 250, function(){ });
	    $('#fade').fadeOut(function() {
	        $('#fade').remove();
	    });
		// open terms
		$("#teamcontract-termsandconditions").slideDown("normal");
		return false;
	});
	
	// honeypot media thumbnails
	$('ul.media a').click(function(){
		if ($(this).hasClass('video')){
			var vid = $(this).attr('id').substr(1);
			$('#media-shell').html(videoEmbed[vid]);
		} else {
			$('#media-shell').html('<img src="'+$(this).attr('href')+'" alt="">');
		}
		return false;
	});
	
	$('#contractteamsignupform select#country').change(function(){
		var country = $(this).val();
		if (country != "US" && country != "Canada"){
			//$('.contractform-intl').show();
			$('.contractform-usca').hide();
		} else {
			$('.contractform-usca').show();
			//$('.contractform-intl').hide();
			if(country == 'US') {
			  $('#state-castate').hide();
			  $('#state-usstate').show();
			  $('#state-ca').get(0).name = 'state-ca';
			  $('#state-us').get(0).name = 'state';
			} else {
			  $('#state-castate').show();
			  $('#state-usstate').hide();
			  $('#state-ca').get(0).name = 'state';
			  $('#state-us').get(0).name = 'state-us';
			}
		}
	});
	
  var submit_email = function() {
    var params = {
      url: this.action,
      type: 'POST',
      data: { email: this.email.value },
      context: this,
      dataType: 'text',
      success: function(result) {
        if(result == 'ok') {
          $(this).replaceWith('<p>Thanks for joining us.</p>');
        } else {
          alert(result);
        }
      }
    };
    $.ajax(params);
    return false;
  };
  $('form#emailsignupfooter').submit(submit_email);
  $('form#emailsignuptop').submit(submit_email);


	// image scroller
	if ($('.scroller').length > 0){
		$('.scroller').each(function(){
			$scroller = $(this);
			$list = $scroller.find('ul.scroller-list');
			$list.children('li:last-child').css({'margin-right':'0px'});
			var adNum = $list.children().length;
			var adWidth = $list.children('li:first-child').width();
			var adMargin = parseInt($list.children('li:first-child').css('margin-right'));
			var totalScrollWidth = (adNum*adWidth)+((adNum-1)*adMargin);
			$list.css({'width':totalScrollWidth+'px'});

			var type = "gallery";
			var displayMax = 7;

//			if ($scroller.hasClass('ads')){
//				var type = 'ads';
//				var displayMax = 3;
//			} else if ($scroller.hasClass('imgs')){
//				var type = 'gallery';
//				var displayMax = 4;
//			} else if ($scroller.hasClass('imgsbig')){
//				var type = 'gallery';
//				var displayMax = 7;
//			}

			if ($list.children().length > displayMax){
				$scroller.append('<div class="scroller-nav"><span class="prev disabled">Previous</span><span class="next">Next</span></div>');
				$scroller.find('.scroller-nav span').click(function(){
					if (!$(this).hasClass('working')){
						if ($(this).hasClass('next')){
							var direction = 'next';
						} else if ($(this).hasClass('prev')){
							var direction = 'prev';
						}

						var btnTarget = $(this);
						var shellTarget = btnTarget.parent().parent().find('.scroller-shell');
						var shellWidth = shellTarget.width();
						var shellLeft = 0;

						var listTarget = btnTarget.parent().parent().find('.scroller-list');
						var listWidth = listTarget.width();
						var listLeft = parseInt(listTarget.css('left'));

						var newListPos = listLeft;
						if (direction == 'next'){
							if ($(this).hasClass('disabled')){
								newListPos = 0;
							} else {
								newListPos = listLeft-(shellWidth+adMargin);
							}
							if (newListPos < 0-(listWidth-shellWidth)){ newListPos = 0-(listWidth-shellWidth); }
						} else if (direction == 'prev'){
							if ($(this).hasClass('disabled')){
								newListPos = 0-(listWidth-shellWidth);
							} else {
								newListPos = listLeft+(shellWidth+adMargin);
							}
							if (newListPos > 0){ newListPos = 0; }
						}

						btnTarget.siblings('span').addClass('working');
						btnTarget.addClass('working');

						listTarget.stop(true,false).animate({"left": newListPos+"px"}, 700, function(){
							var newListLeft = parseInt(listTarget.css('left'));

							if (direction == 'next'){
								if (newListLeft+listWidth <= shellLeft+shellWidth){
									btnTarget.addClass('disabled');
								} else {
									btnTarget.removeClass('disabled');
								}
								if (newListLeft == shellLeft){
									btnTarget.siblings('span.prev').addClass('disabled');
								} else {
									btnTarget.siblings('span.prev').removeClass('disabled');
								}
							} else if (direction == 'prev'){
								if (newListLeft < shellLeft){
									btnTarget.removeClass('disabled');
								} else {
									btnTarget.addClass('disabled');
								}
								if (newListLeft+listWidth <= shellLeft+shellWidth){
									btnTarget.siblings('span.next').addClass('disabled');
								} else {
									btnTarget.siblings('span.next').removeClass('disabled');
								}
							}
							btnTarget.siblings('span').removeClass('working');
							btnTarget.removeClass('working');
						});
					}
				});
			}
		});
	}
});
