// video_player stuff
var flashvars = {};
flashvars.autostart = "false";
flashvars.showbutton = "false";
var params = {};
params.menu = "false";
params.quality = "high";
params.scale = "noScale";
params.allowfullscreen = "true";
params.allowscriptaccess = "always";
params.wmode = "opaque";

function videoStatus(status) {
  // statuses: done_loading, done_playing, clicked
} 

jQuery.noConflict();

jQuery(document).ready(function($) {

function log_error(error_message) {
  /* off for production... */
  // $('div#header').after("<div id='pagemessages'> <div id='message_id-xxx' class='message'> <div class='messageClass-xxx'> <a href=''>[X]</a> <span>"+error_message+"</span> </div> </div> </div>");
}
  
function controlVideo(action, id) {   
  // actions: play, pause, reset
  try {
    // TODO: This will sometimes fail.
    var movie = swfobject.getObjectById(id);
    if (movie) {
      movie.controlVideo(action);
    } else {
      log_error('failed to '+action+' '+id);
    }
  } catch (e) {
    log_error('failed to '+action+' '+id+' because of '+e);
  }
}

// override cycle plugin hiding
$.fn.cycle.promocommonReset = function(curr,next,opts,w,h,rev) {
	//$(opts.elements).not(curr).hide();
	$(opts.elements).not(curr).css('overflow', 'hidden');
	$(opts.elements).not(curr).css('height', '0px');


	opts.cssBefore.height = 'auto';

	opts.cssBefore.opacity = 1;
  /*
	opts.cssBefore.display = 'block';
	opts.cssBefore.position = 'static';
	opts.cssBefore.left = '0px';
  */

  /*
  var test = '';
  $(opts.elements).not(curr).each(function() {
    test += $(this).find(".promo_title").html()
  });
  //alert(test);
  */

  /*
	if (w !== false && next.cycleW > 0)
		opts.cssBefore.width = next.cycleW;
	if (h !== false && next.cycleH > 0)
		opts.cssBefore.height = next.cycleH;
    */
	opts.cssBefore.height = '304px';
	opts.cssBefore.width = '440px';

	opts.cssAfter = opts.cssAfter || {};
	//opts.cssAfter.display = 'none';
	opts.cssAfter.overflow = 'hidden';
	opts.cssAfter.height = '0';
	$(curr).css('zIndex',opts.slideCount + (rev === true ? 1 : 0));
	$(next).css('zIndex',opts.slideCount + (rev === true ? 0 : 1));
  if ($(curr).find(".video_data").html()) {
    //alert("youtube video");
  }
};

// the actual fn for effecting a transition (modified to have a slideTime delay)
$.fn.cycle.custom = function(curr, next, opts, cb, speedOverride) {
	var $l = $(curr), $n = $(next);
	var speedIn = opts.speedIn, speedOut = opts.speedOut, easeIn = opts.easeIn, easeOut = opts.easeOut;
	$n.css(opts.cssBefore);
	if (speedOverride) {
		if (typeof speedOverride == 'number')
			speedIn = speedOut = speedOverride;
		else
			speedIn = speedOut = 1;
		easeIn = easeOut = null;
	}
	var fn = function() {
      $n.animate(opts.animIn, speedIn, easeIn, cb);
  };
  setTimeout(function(w) {
    $l.animate(opts.animOut, speedOut, easeOut, function() {
      if (opts.cssAfter) $l.css(opts.cssAfter);
      if (!opts.sync) fn();
    });
    if (opts.sync) fn();
  }, slideTime);
};

// uncover_and_play - curr slide moves off next slide and then next slide plays if it's a video
$.fn.cycle.transitions.uncover_and_play = function($cont, $slides, opts) {
	var d = opts.direction || 'left';
	var w = $cont.css('overflow','hidden').width();
	var h = $cont.height();


	opts.before.push(function(curr, next, opts) {
    $(curr).find(".promo_text").slideUp(slideTime); // hide ( this doesn't sync well with the other transitions )
  });
  opts.before.push(function(curr, next, opts) {
    $(curr).css('height', '304px');
    if ($(curr).find(".imavideo").html()) {
      vp_id = $(curr).find(".video_data").attr("vp");
      controlVideo('reset', 'video-'+vp_id);
    }
  });
  opts.before.push(function(curr, next, opts) {
    $.fn.cycle.promocommonReset(curr,next,opts,true,true,true);
    if (d == 'right')
      opts.animOut.width = 0;
    else if (d == 'up')
      opts.animOut.top = -h;
    else if (d == 'down')
      opts.animOut.top = h;
    else
      opts.animOut.left = -w;
  });
  opts.after.push(function(curr, next, opts) {
    //$(curr).find(".promo_text").slideUp(slideTime); // hide
    setTimeout(function(w){
      $(next).find(".promo_text").slideDown(slideTime); // show
      }, beforeSlideTime);
    if ($(next).find(".imavideo").html()) {
      vp_id = $(next).find(".video_data").attr("vp");
      controlVideo('play', 'video-'+vp_id);
    }
  });
  opts.animIn = { left: 0, top: 0 };
  opts.animOut = { opacity: 1 };
  opts.cssBefore = { top: 0, left: 0 };
};

  var beforeSlideTime = 1000;
  var slideTime = 1000;
  var fxTime = 1000;
  var timeoutTime = (beforeSlideTime + slideTime + 5000 + slideTime) - fxTime;
  first_duration = $("#super_promo_block").find(".video_data:first").attr("duration");
  if (first_duration > 0) {
    timeoutTime = first_duration*1000;
  }

  function promo_pager(index, DOMelement) {
    var promo_id = $("#super_promo_block").children().slice(index, index+1).attr('id');
    var promo_title = $("#super_promo_block").children().slice(index, index+1).find('h2.promo_title').html();
    var promo_jump = $("<a class='super_promo_jump' title='"+promo_title+"' href='#"+promo_id+"'><span>&bull;</span></a>");

    return(promo_jump);
  }

  function promo_on_before(currSlideElement, nextSlideElement, options, forwardFlag) {
      
  }
  function promo_on_after(currSlideElement, nextSlideElement, options, forwardFlag) {
  }
  function timeoutSlide(currSlideElement, nextSlideElement, options, forwardFlag) {
    if ($(nextSlideElement).find(".video_data").html()) {
      // movie's real duration is only available when video is playing.
      duration = $(nextSlideElement).find(".video_data").attr("duration");
      if (duration > 0) {
        return duration*1000;
      }
    }
    return timeoutTime;
  }

  var super_promo_playing = true;
  $("#super_promo_block").bind('click', function(e) {
    if (super_promo_playing) {
      $("#super_promo_block").cycle('pause');
      super_promo_playing = false;
    } else {
      $("#super_promo_block").cycle('resume');
      super_promo_playing = true;
    }
  });

  $(".promo_text").hide();
  $("div.first_promo").find(".promo_text").slideDown(slideTime); // show


  // start cycling
  // http://malsup.com/jquery/cycle/options.html
  $("#super_promo_block").after("<div id='super_promo_controls'><a id='super_promo_prev' href='#super_promo_prev'><span>&larr;</span></a><span id='super_promo_pager'></span><a id='super_promo_next' href='#super_promo_next'><span>&rarr;</span></a></div>")
    .cycle({
      fx: 'uncover_and_play',
      speed: fxTime,
      timeout: timeoutTime,
      timeoutFn: timeoutSlide,
      pause: 1, // pause on mouse hover
      pager: '#super_promo_pager',
      prev: '#super_promo_prev',
      next: '#super_promo_next',
      pagerAnchorBuilder: promo_pager,
      fastOnEvent: 0,
      sync: 1,
      before: promo_on_before,
      after: promo_on_after,
      autostop: 25,
      autostopCount: 25,
      direction: 'right'
    });

});




