
var stories;
var pressRelease = null;

function initAndStartPopShow() {
    initPopBulletList();

    $('loading').style.display='none';
    $('web_site_container').style.display='block';

    move();
}
		

var maxStories = 5;
var currentStory = 0;

var autoCycleTimeOutHandle;

var firstTransition = true;
var comingFromHappyEnding = false;

function initPopBulletList() {
	var html = "";
	
        if (stories == null) {
            return;
        }

	for (var i = 0; i < stories.length; i++) {
		var story = stories[i];
		html = html + "<li><a href=\"javascript: doShowStory(" + i + ")\">" + story.bulletListCopy + "</a></li>";
	}
	
	var ulElement = $('pop_bottom_right_list');
	ulElement.innerHTML = html;
	
}

function cycleStoriesNext() {
	currentStory++;
	if (currentStory > maxStories) {
		currentStory = 0;
	}
}

function cycleStoriesBack() {
	currentStory--;
	if (currentStory < 0) {
		currentStory = maxStories - 1;
	}
}

function loadStory(index) {
	var story = stories[index];
	$('pop_top_left_image').src = story.topLeftImage;
	$('pop_bottom_left_image').src = story.bottomLeftImage;
	$('pop_bottom_right_image').src = story.bottomRightImage;
	$('pop_top_right').innerHTML = story.headingCopyHtml;
	$('pop_bottom_left_copy').innerHTML = story.bottomLeftCopyHtml;
	$('pop_bottom_right_copy').innerHTML = story.bottomRightCopyHtml;
	updateStoryList(index);
}

function updateStoryList(index) {
	  var ultags=$('pop_bottom_right_links').getElementsByTagName("li");
    for (var t=0; t<ultags.length; t++){
    	if (t == index) {
    		ultags[t].className = 'active';
    	}
    	else {
    		ultags[t].className = '';
    	}
	  }  // end for t
	
}

function doHappyEnding3() {

      new Effect.multiple(
        ['pop_happy_ending_tagline', 'pop_happy_ending_logo'], 
        Effect.Appear,
        {duration: 0.5, from: 1.0, to: 0.0});

      comingFromHappyEnding = true;
      autoCycleTimeOutHandle = window.setTimeout("autoCycle()", 1000);
}

function doHappyEnding2() {

      new Effect.multiple(
        ['pop_happy_ending_tagline'], 
        Effect.Appear,
        {duration: 1.0, from: 0.0, to: 1.0});

      autoCycleTimeOutHandle = window.setTimeout("doHappyEnding3()", 4000);
}

function doHappyEnding() {

      new Effect.multiple(
        ['pop_happy_ending_logo'], 
        Effect.Appear,
        {duration: 1.0, from: 0.0, to: 1.0});

      autoCycleTimeOutHandle = window.setTimeout("doHappyEnding2()", 3000);
}

function move(){
        if (currentStory == maxStories) {
            doHappyEnding();
            return;
        }
        
	loadStory(currentStory);
	
	new Effect.Move('pop_line_v', {x:147, y:105, mode:'absolute', duration: 0});
	new Effect.Move('pop_line_v2', {x:800, y:400, mode:'absolute', duration: 0});
	new Effect.Move('pop_line_h', {x:147, y:105, mode:'absolute', duration: 0});
	new Effect.Move('pop_top_left', {x:220, y:600, mode:'absolute', duration: 0});
	new Effect.Move('pop_top_right', {x:600, y:400, mode:'absolute', duration: 0});
	new Effect.Move('pop_bottom_left', {x:200, y:500, mode:'absolute', duration: 0});
	new Effect.Move('pop_bottom_right', {x:0, y:0, mode:'absolute', duration: 0});

	if (firstTransition) {
	new Effect.multiple(['pop_line_v','pop_line_h', 'pop_line_v2', 'pop_top_left','pop_top_right','pop_bottom_left','pop_bottom_right', 'pop_bottom_right_links'], Effect.Appear, {duration: 0.5, from: 0.0, to: 1.0});
		new Effect.Move('pop_bottom_right_links', {x:840, y:300, mode:'absolute'});
		firstTransition = false;
	}
	else {
	  new Effect.multiple(['pop_line_v','pop_line_h', 'pop_line_v2', 'pop_top_left','pop_top_right','pop_bottom_left','pop_bottom_right'], Effect.Appear, {duration: 0.5, from: 0.0, to: 1.0});
	}
  
	new Effect.Move('pop_line_v', {x:360, y:105, mode:'absolute'});
	new Effect.Move('pop_line_v2', {x:800, y:300, mode:'absolute'});
	new Effect.Move('pop_line_h', {x:147, y:263, mode:'absolute'});
	new Effect.Move('pop_top_left', {x:233, y:144, mode:'absolute'});
	new Effect.Move('pop_top_right', {x:370, y:226, mode:'absolute'});
	new Effect.Move('pop_bottom_left', {x:168, y:280, mode:'absolute'});
	new Effect.Move('pop_bottom_right', {x:374, y:276, mode:'absolute'});

  autoCycleTimeOutHandle = window.setTimeout("autoCycle()", 8000);

}

function fadeOut(){
    if (currentStory == maxStories) {
      new Effect.multiple(['pop_line_v','pop_line_h', 'pop_line_v2', 'pop_top_left','pop_top_right','pop_bottom_left','pop_bottom_right', 'pop_bottom_right_links', 'pop_top_right_controls'], Effect.Appear, {duration: 0.5, from: 1.0, to: 0.0});
    }
    else {
      if (comingFromHappyEnding) {
          comingFromHappyEnding = false;
          new Effect.multiple(['pop_bottom_right_links', 'pop_top_right_controls'], Effect.Appear, {duration: 0.5, from: 0.0, to: 1.0});
      }
      else {
        new Effect.multiple(['pop_line_v','pop_line_h', 'pop_line_v2', 'pop_top_left','pop_top_right','pop_bottom_left','pop_bottom_right'], Effect.Appear, {duration: 0.5, from: 1.0, to: 0.0});
      }
    }
    

    window.setTimeout("move()", 1000);
}

function autoCycle() {
	cycleStoriesNext();
	fadeOut();
}

function doButtonBack() {
	window.clearTimeout(autoCycleTimeOutHandle);
	cycleStoriesBack();
	fadeOut();
}

function doButtonNext() {
	window.clearTimeout(autoCycleTimeOutHandle);
	cycleStoriesNext();
	fadeOut();
}

function doButtonPause() {
	window.clearTimeout(autoCycleTimeOutHandle);
}

function doShowStory(index) {
	window.clearTimeout(autoCycleTimeOutHandle);
	currentStory = index;
	fadeOut();
}


// Initialization

Event.observe(
	window,
	'load',
	function() {
		
          Event.observe('button_back', 'mouseover', function() { $('button_back').src="/slices/pop/button_back_active.png";	}	);
          Event.observe('button_back', 'mouseout', function() {	$('button_back').src="/slices/pop/button_back.png"; }	);

          Event.observe('button_pause', 'mouseover', function() { $('button_pause').src="/slices/pop/button_pause_active.png";	}	);
          Event.observe('button_pause', 'mouseout', function() {	$('button_pause').src="/slices/pop/button_pause.png"; }	);

          Event.observe('button_next', 'mouseover', function() { $('button_next').src="/slices/pop/button_next_active.png";	}	);
          Event.observe('button_next', 'mouseout', function() {	$('button_next').src="/slices/pop/button_next.png"; }	);

          PopStoryService.getAvailablePopStories(function (data) { stories = data; if ((stories != null) && (stories.length > 0)) { initAndStartPopShow(); } } );
	}
	
);


