function sendEvent(swf, typ, prm) { 
  var movie = swfobject.getObjectById(swf);
  if (movie && movie.sendEvent) {
    movie.sendEvent(typ, prm);
    return true;
  } else {
    return false;
  }
};

function getUpdate(typ, pr1, pr2, swf) {
  //console.log(typ + " " + pr1 + " " + pr2 + " " + swf);
  window.teaserCtrl.update(typ, pr1, pr2);
};

window.teaserCtrl = {
  time: null,
  time_remaining: null,
  update: function(typ, pr1, pr2) {
    if ("time" == typ) {
      this.time = pr1;
      this.time_remaining = pr2;
    }
    
    if ("state" == typ) {
      if (3 == pr1 && (0 == this.time_remaining || 0 == this.time)) {
        jQuery(".teaser-stop").fadeIn("slow");
      }
    }
  }
};

jQuery(function($) {
	$(".teaser-stop").click(function(event) {
    sendEvent("teaser", "playpause");
	  $(this).fadeOut("slow");
	});
	
	$("a[rel=video]").click(function(event) {
    event.preventDefault();
    $("object#teaser").after($("<div>", { id: "teaser" }));
    $("object#teaser").remove();

    swfobject.embedSWF(
      "/templates/scripts/mediaplayer.swf",
      "teaser",
      "630",
      "354",
      "9.0.0",
      null,
      {
        enablejs: "true",
        javascriptid: "teaser",
        file: $(this).attr("href"),
        autostart: "true",
        width: "630",
        height: "354",
        displaywidth: "630",
        displayheight: "334",
        lightcolor: "0x5dbfda",
        backcolor: "0x21596a",
        frontcolor: "0xffffff"
      },
      {
        play: "true",
        menu: "false",
        wmode: "transparent",
        allowfullscreen: "true"
      }
    );
    
    if ($(".teaser-stop:visible")) {
      $(".teaser-stop").fadeOut("slow");
    }
	});
});
