function replaceValues(el) {
	var $this = el;
	var $name = $this.attr('name');
	$('.replace-' + $name).each(function() {
		$(this).text($this.val());
	});
	var $address_line_1 = $('#edit-address-line-1').val();
	var $city           = $('#edit-city').val();
	var $post_code      = $('#edit-post-code').val();
	var $country        = $('#edit-country').val();
	var delimiter       = ', ';

	var $address        = '';
	if ($address_line_1 != '' && $city != '' && $post_code != '' && $country != '') {
		var $address        = $address_line_1 + delimiter + $city + delimiter + $post_code + delimiter + $country;
	}
	$('.replace-address').text($address);
}

$(document).ready(function() {
  
  // Media Handler
  var videoID = 0;
  $('.media').each(function(){
    $this = $(this);
    videoID++;
    $this.attr('id', 'media' + videoID);
    
    jwplayer('media' + videoID).setup({
      flashplayer: "/sites/default/themes/transgaming/js/player.swf",
      file: $this.attr('href'),
      width: $this.attr('data-width'),
      height: $this.attr('data-height'),
      'controlbar': 'bottom'
    });
    
    var played = false;
    jwplayer('media' + videoID).onPlay(function(){
      if ( ! played){
        virtualAnalytics('media/play/' + $this.attr('data-name'));
        played = true;
      }
    });
    
  });
  
  // Media Analytics Handling
  function virtualAnalytics(event){
    if (_gaq){
      _gaq.push(['_trackPageview', '/virtual/' + event + '/']);
    }    
  }
  
	$('h2').each(function() {
		var content = $(this).html();
		$(this).html('<span>' + content + '</span>');
	});
	
	$('.secondary-menu a').each(function() {
		var content = $(this).html();
		$(this).html('<span></span>' + content);
	});
	
	if ($('#content form').length) {
		$('#content input:first').focus();
	}

	if ($.browser.msie) {
		$('.form-required').remove()
		$('<span class="ie-required">*</span>').insertAfter('.required');
	}

	if ($('.page-contact-us .messages.status').length) {
		$('#tg-contactus-form').remove();
	}

	if ($('.page-business .messages.status').length) {
		$('#swiftshader-registration-form, #node-167, #tg-game-submission-form').remove();
	}

	if ($('#distro').length) {
		function setupPage() {
			var distro = $('#distro');

			var visibleHeight = distro.height();
			var scrollableHeight = distro[0].scrollHeight;
			if (scrollableHeight > visibleHeight) {
				$('#edit-distribution-checkbox').attr({'disabled': true, 'checked': false});

				distro.bind('scroll', function handleScroll() {
					var distro = $("#distro");
					var visibleHeight = distro.outerHeight();
					var scrollableHeight = distro[0].scrollHeight;
					var position = distro.scrollTop();
					if (position + visibleHeight >= scrollableHeight) {
						$('#edit-distribution-checkbox').attr('disabled', false);
					}
				});
			}

			$('#edit-company, #edit-representative, #edit-job-title, #edit-email, #edit-phone, #edit-address-line-1, #edit-city, #edit-post-code, #edit-country').each(function() {
				replaceValues($(this));
			}).bind('change blur focus', function() {
				replaceValues($(this));
			});

		}
		setupPage();

	}
	
	var $prettyphotos = $("a[rel^='prettyPhoto']");
	if ($prettyphotos.length > 0){
		$prettyphotos.each(function(){
			$(this).prettyPhoto();
		});
	}
	

	

});

