// load video from a URL variable
function loadVideo(playerId,flvURL,introImgURL) {
	var player = document.getElementById(playerId); 
	if (player) {
		player.loadFLV( flvURL, introImgURL );
		player.play();
	}
}

function loadCarousel(story) {
  // set default carousel state
  //  Define target flash object to reference
  // IE uses outer object, everyone else uses the inner object
  // var carouselSwf = document.getElementById("carousel");
  var carousel_object = swfobject.getObjectById("carousel");
  switch(story) {
    case "jont":
      carousel_object.gotoPanel(1);
      break;
    case "everyone":
      carousel_object.gotoPanel(2);
      break;  
  }
}

// load a story from a URL variable
// this function is called onLoad of <body>
function loadStory(story) {
  switch(story) {
    case "#story1":
      window.location = "i-am-jont.php";
//window.location = "i-am-jont.php";
      // POKE.orange.iam.openJont();
      // $('div#middle').animate({opacity: 1.0}, 1500).show("blind", {direction: "vertical"}, 750);
      break;
    case "#story2":
      window.location = "i-am-everyone.html";
      // POKE.orange.iam.openStory1();
      break;
    case "#story3":
      window.location = "i-am-rose-tremain.html";
      // POKE.orange.iam.openStory2();
      break;
    case "#story4":
      window.location = "i-am-rob-law.html";
      // POKE.orange.iam.openStory3();
      break;
    case "#story5":
      window.location = "i-am-james-averdieck.html";
      // POKE.orange.iam.openStory5();
      break;
	case "#story6":
      window.location = "index.php";
      break;
	case "#story7":
      window.location = "i-am-orange-dna.php";
      break;
    case "#mark":
      window.location = "i-am-markbeaumont.php";
//window.location = "index.php";
      // POKE.orange.iam.openStory5();
      break;
    default:
      window.location = "index.php";
  }
}

// reset page
function resetPage() {
	// if page is currently active, don't do anything on click
	if ($('#mark.active').length == 1) {
		return false;
	}
}

// reveal "i am everyone"
function story1() {
	// if page is currently active, don't do anything on click
	if ($('#everyman.active').length == 1) {
		return false;
	}
}

// reveal "orange prize"
function story2() {
	
	if ($('#orange_prize.active').length == 1) {
		return false;
	}
}

// reveal "rob law"
function story3() {
	
	if ($('#trunki.active').length == 1) {
		return false;
	}
}

// reveal "gü puddings"
function story5() {
	
	if ($('#gu.active').length == 1) {
		return false;
	}
}

// reveal "jont"
function story4() {
  // if page is currently active, don't do anything on click
	if ($('#jont.active').length == 1) {
		return false;
	}
}

function panelChange (direction) {
	panelCurrPage = currPage -= direction;
}

POKE = {};
POKE.orange = {};

POKE.orange.iam = {
	
	openMark: function(){
    // resetPage();
    // setTimeout("document.title = 'I am Everyone : Orange : I am Mark Beaumont'", 850);
	},
	
	openJont: function(){
    // $('html').animate({scrollTop: -9999}, 0, '', story4());
    // setTimeout("document.title = 'I am Everyone : Orange : I am Jont'", 850);
	},
	
	openStory1: function(){
		// scroll to the top of the page and transition to story 1
    // $('html').animate({scrollTop: -9999}, 0, '', story1());
    // setTimeout("document.title = 'I am Everyone : Orange'", 850);
	},
	
	openStory2: function(){
		// scroll to the top of the page and transition to story 2
    // $('html').animate({scrollTop: -9999}, 0, '', story2())
    // setTimeout("document.title = 'I am Everyone : Orange : I am Rose Tremain'", 850);
	},
	
	openStory3: function(){
		// scroll to the top of the page and transition to story 3
    // $('html').animate({scrollTop: -9999}, 0, '', story3());
    // setTimeout("document.title = 'I am Everyone : Orange : I am Rob Law'", 850);
	},
	
	openStory5: function(){
		// scroll to the top of the page and transition to story 3
    // $('html').animate({scrollTop: -9999}, 0, '', story5());
    // setTimeout("document.title = 'I am Everyone : Orange : I am James Averdieck'", 850);
	},
	
	hideVideoObjects: function(){
    // $('object#iam_markb, object#iam_jont, object#iam_everyone').hide();
	},
	
	setUpMiddle: function(){
	  // when top action is clicked, reveal middle section
  	$('#jont_get_involved').click(function(){
  		$('#middle').show("blind", {direction: "vertical"}, 750);
  		$('#middle a.close').show();
  		$('a.get_involved').hide();
  	});
	}

}

$(document).ready(function() {
	
	// set up document
	$('div#top a.get_involved').show();
	$('div#middle').hide();
	$('div#middle a.close').hide();
	$('input.submit').hide();
	$('x').attr({href: "#"});
	
	// when mark promo is clicked
	$('ul#navigation li#mark a, .mark').click(function(){
		POKE.orange.iam.openMark();
	});
	
	// when jont promo is clicked
	$('ul#navigation li#jont a, .jont').click(function(){
		POKE.orange.iam.openJont();
	});
	
	// when everyman nav is clicked 
	$('ul#navigation li#everyman a, .everyman').click(function(){
		POKE.orange.iam.openStory1();
	});

	// when orange prize nav is clicked
	$('ul#navigation li#orange_prize a, .orange_prize').click(function(){
		POKE.orange.iam.openStory2();
	});
	
	// when trunki nav is clicked
	$('ul#navigation li#trunki a').click(function(){
		POKE.orange.iam.openStory3();
	});
	
	// when trunki nav is clicked
	$('ul#navigation li#gu a').click(function(){
		POKE.orange.iam.openStory5();
	});
	
	// when top action is clicked, reveal middle section
	$('a.get_involved, #jont_get_involved').click(function(){
		$('#middle').show("blind", {direction: "vertical"}, 750);
		$('#middle a.close').show();
		$('a.get_involved').hide();
		$('#jont_wrapper ul.arrow-list li').css("background", "none");
	});
	
	// when middle close is clicked, hide middle section
	$('div#middle a.close').click(function(){
		$('div#middle').hide("blind", {direction: "vertical"}, 750);
		$('a.get_involved').show();
		$('ul.arrow-list li').css("background", "transparent url(../images/arrow_right_sq.gif) no-repeat scroll 0 50%");
	});
	
	/*** form functions ***/
	$('fieldset#left input').click(function(){
		$('input.submit').removeClass('disabled');
		if ($('fieldset#left input#house:checked').length == 1) {
			$('fieldset#right textarea').removeAttr("readonly");
			$('fieldset#right textarea').removeClass('optional');
			$('fieldset#right label.about').removeClass('optional');
			$('fieldset#right input').removeClass('optional');
			if ($('fieldset#right input.mobile').attr("value") == "optional") {
				$('fieldset#right input.mobile').attr("value", "");
			}
			if ($('fieldset#right input.postcode').attr("value") == "optional") {
				$('fieldset#right input.postcode').attr("value", "");
			}
		}
		else {
			$('fieldset#right textarea').attr("readonly", "readonly");
			$('fieldset#right textarea').addClass('optional');
			$('fieldset#right label.about').addClass('optional');
			$('fieldset#right input.mobile').addClass('optional');
			if ($('fieldset#right input.mobile').attr("value").length == 0) {
				$('fieldset#right input.mobile').attr("value", "optional");
			}
			$('fieldset#right input.postcode').addClass('optional');
			if ($('fieldset#right input.postcode').attr("value").length == 0) {
				$('fieldset#right input.postcode').attr("value", "optional");
			}
		} 
		if ($('fieldset#left input:checked').length == 0) {
			$('input.submit').addClass('disabled');
			$('input.submit').attr('disabled', 'disabled');
		}
	});
	
	// post the form
	$("form[@action*='form.php']").submit(function(event){
		$('input.submit').hide();
		event.preventDefault();
			$.post("form.php",{
				house_opt: $('#house').attr("checked"),
				startnotify_opt: $('#newsletter').attr("checked"),
				feed_opt: $('#feed').attr("checked"),
				name: $('.name').val(),
				email: $('#email').val(),
				mobile: $('.mobile').val(),
				postcode: $('.postcode').val(),
				why: $('.why').val(),
				submit: 'submit'
			},
			function(data){
		$('div#the_form').html(data);
		// I am not DRY
		$('input.proxy').each( function() {
			var input = $(this);
			$('<p><img class="submit" alt="sign up now" src="images/sign_up.jpg" /></p>')
				.attr( 'class', this.className )
				.insertBefore(this)
				.click( function() {
						$('#get_involved').submit();
						return false;
					});
			input.hide();
			});
		});
	});
	
	// When the link is pressed it "clicks" the button.
	// The initial HTML looks like this:
	//
	//		<input class="proxy" type="submit" value="Save">
	//
	// After jQuery has done it's thing it look like this:
	//
	//		<input type="submit" class="proxy" value="Save" style="display:none">
	//		<a href="#" class="proxy">Save</a>
	$('input.proxy').each( function() {
		var input = $(this);
		$('<p><img class="submit" alt="sign up now" src="images/sign_up.jpg" /></p>')
			.attr( 'class', this.className )
			.insertBefore(this)
			.click( function() {
					$('#get_involved').submit();
					return false;
		});
		input.hide();
	});
	
});
