if (top.location != location)
{
  top.location.href = document.location.href;
}
var videourl;
var videoplayer;
var html5video;
var timestamp;
function videoplaying()
{
  $("#loader").hide();
  $("#player").show();
}
function loadflashvideo(url)
{
  $("#flashplayer").append('<div id="flashf4player"><object width="600" height="450" id="f4Player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="movie" value="flash/player.swf" /><param name="quality" value="high" /><param name="menu" value="false" /><param name="allowFullScreen" value="true" /><param name="scale" value="noscale" /><param name="allowScriptAccess" value="always" /><param name="swLiveConnect" value="true" /><param name="flashVars" value="skin=flash/default.swf&thumbnail=img/unitypreloader.png&video='+videourl+'&autoplay=1"/><!--[if !IE]><--><object width="600" height="450" data="flash/player.swf" type="application/x-shockwave-flash" id="f4Player"><param name="quality" value="high" /><param name="menu" value="false" /><param name="allowFullScreen" value="true" /><param name="scale" value="noscale" /><param name="allowScriptAccess" value="always" /><param name="swLiveConnect" value="true" /><param name="flashVars" value="skin=flash/default.swf&thumbnail=img/unitypreloader.png&video='+videourl+'&autoplay=1"/></object><![endif]--></object></div>');
}
function returnfalse()
{
  return false;
}
var tandc = false;
var tandchover = false;
function returntrue()
{
  if(tandchover)
  {
    return true;
  }
  else
  {
    return false;
  }
}
var unityurl;
var flashurl;

$(function()
{
  timestamp = $("#timestamp").attr("value");
  videoplayer = document.getElementById("player");
  $("#videodialog").dialog(
  {
    autoOpen: false,
    resizable: false,
    width: 800,
    height: 660,
    modal: true,
    position: "top",
    draggable: false,
    open: function(event, ui) 
    {
      _gaq.push(['_trackEvent', 'VideoDialog', 'Open', videourl]);
      //$('body').css('overflow','hidden');
      //$(window).bind("mousewheel", returnfalse);
      $("#loader").show();
      if(html5video)
      {
        videoplayer.src = videourl;
        videoplayer.play();
      }
      else
      {
        $("#loader").hide();
      }
    },
    close: function(event, ui)
    {
      _gaq.push(['_trackEvent', 'VideoDialog', 'Close', videourl]);
      $(window).unbind("mousewheel", returnfalse);
      if(html5video)
      {
        videoplayer.pause();
        videoplayer.src = "";
      }
      $('body').css('overflow','visible');
      $("#flashplayer").empty();
    }
  });
  $("#unitydialog").dialog(
  {
    autoOpen: false,
    resizable: false,
    width: 800,
    height: 660,
    modal: true,
    position: "top",
    draggable: false,
    open: function(event, ui) 
    {
      _gaq.push(['_trackEvent', 'UnityDialog', 'Open', unityurl]);
      //$('body').css('overflow','hidden');
      //$(window).bind("mousewheel", returnfalse);
      $("#unity").unity(unityurl,
      {
          width: 600,
          height: 500,
          bordercolor: "000000",
          backgroundcolor: "ffffff",
          textcolor: "00000",
          logoimage: "img/unitypreloader.png",
          progressbarimage: "img/unitypreloaderframe.png",
          progressframeimage: "img/unitypreloaderbar.png"
      });
    },
    close: function(event, ui)
    {
      _gaq.push(['_trackEvent', 'UnityDialog', 'Close', unityurl]);
      $('body').css('overflow','visible');
      $(window).unbind("mousewheel", returnfalse);
      $("#unity").unity("destroy");
    }
  });
  $("#flashdialog").dialog(
  {
    autoOpen: false,
    resizable: false,
    width: 800,
    height: 660,
    modal: true,
    position: "top",
    draggable: false,
    open: function(event, ui) 
    {
      _gaq.push(['_trackEvent', 'FlashDialog', 'Open', flashurl]);
      swfobject.embedSWF(flashurl, "flash", "600", "500", "9.0.0", "flash/expressInstall.swf");
      //$('body').css('overflow','hidden');
      //$(window).bind("mousewheel", returnfalse);
    },
    close: function(event, ui)
    {
      _gaq.push(['_trackEvent', 'FlashDialog', 'Close', flashurl]);
      $('body').css('overflow','visible');
      $(window).unbind("mousewheel", returnfalse);
      $("#flash").empty();
    }
  });
  $("#tandcdialog").dialog(
  {
    autoOpen: false,
    resizable: false,
    width: 800,
    height: 700,
    modal: true,
    position: "top",
    draggable: false,
    open: function(event, ui) 
    {
      _gaq.push(['_trackEvent', 'TandCDialog', 'Open']);
      //$('body').css('overflow','hidden');
      //$(window).bind("mousewheel", returntrue);
      if(!tandc)
      {
        tandc = true;
        $("#tandccontainer").hover(function()
        {
          tandchover = true;
        }, 
        function() 
        {
  	  tandchover = false;
        });
        $("#tandccontainer").load("tc.html");
      }
    },
    close: function(event, ui)
    {
      _gaq.push(['_trackEvent', 'TandCDialog', 'Close']);
      $('body').css('overflow','visible');
      $(window).unbind("mousewheel", returntrue);
    }
  });
  $("#androiddialog").dialog(
  {
    autoOpen: false,
    resizable: false,
    width: 800,
    height: 660,
    modal: true,
    position: "top",
    draggable: false,
    open: function(event, ui) 
    {
      _gaq.push(['_trackEvent', 'AndroidDialog', 'Open']);
      //$('body').css('overflow','hidden');
      //$(window).bind("mousewheel", returntrue);
      $("#androidcontainer").load("droid.html");
    },
    close: function(event, ui)
    {
      _gaq.push(['_trackEvent', 'AndroidDialog', 'Close']);
      $('body').css('overflow','visible');
      $(window).unbind("mousewheel", returntrue);
    }
  });
  $("#iphonedialog").dialog(
  {
    autoOpen: false,
    resizable: false,
    width: 800,
    height: 360,
    modal: true,
    position: "top",
    draggable: false,
    open: function(event, ui) 
    {
      _gaq.push(['_trackEvent', 'AndroidDialog', 'Open']);
      //$('body').css('overflow','hidden');
      //$(window).bind("mousewheel", returntrue);
      $("#iphonecontainer").load("iphone.html");
    },
    close: function(event, ui)
    {
      _gaq.push(['_trackEvent', 'AndroidDialog', 'Close']);
      $('body').css('overflow','visible');
      $(window).unbind("mousewheel", returntrue);
    }
  });
  $(".preview").click(function()
  {
    videourl = $(this).attr("href");
    html5video = false;
    if(!!document.createElement('video').canPlayType)
    {
      if(document.createElement('video').canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"'))
      {
        //html5video = true;
        //videourl += ".ogv";
      }
      else if(document.createElement('video').canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"'))
      {
        //html5video = true;
        //videourl += ".mp4"
      }
      else
      {
        //videourl += ".flv";
        //loadflashvideo();
      }
      videourl += ".flv";
      loadflashvideo();
    }
    else
    {
      videourl += ".flv";
      loadflashvideo();
    }
    scroll(0,0);
    $("#videodialog").dialog("open");
    return false;
  });
  $(".playGame").click(function()
  {
    unityurl = $(this).attr("href");
    if(unityurl == "http://www.optathlon.com/unity/EP.unity3d")
    {
      $("#unitydialog").children(".dialogheader").empty().append('<a href="http://www.united.com" ><img alt="" class="dialogleft" src="img/dialog/unitedLogo.png"/></a><a href="http://www.optathlon.com" ><img alt="" class="dialogmiddle" src="img/dialog/optathlon.png"/></a><a href="http://adfarm.mediaplex.com/ad/ck/1091-107466-3840-0?mpt='+timestamp+'" ><img class="hidden" src="http://adfarm.mediaplex.com/ad/tr/1091-107466-3840-0?mpt='+timestamp+'" border="0"><img alt="" class="dialogright" src="img/dialog/optfortraveloptions.png"/></a><a href="#" class="unityclose dialogclose"></a>');
    }
    else
    {
      $("#unitydialog").children(".dialogheader").empty().append('<a href="http://www.united.com" ><img alt="" class="dialogleft" src="img/dialog/unitedLogo.png"/></a><a href="http://www.optathlon.com" ><img alt="" class="dialogmiddle" src="img/dialog/optathlon.png"/></a><a href="http://adfarm.mediaplex.com/ad/ck/1091-107466-3840-1?mpt='+timestamp+'" ><img class="hidden" src="http://adfarm.mediaplex.com/ad/tr/1091-107466-3840-1?mpt='+timestamp+'" border="0"><img alt="" class="dialogright" src="img/dialog/optfortraveloptions.png"/></a><a href="#" class="unityclose dialogclose"></a>');
	  	/*if($(".flash").attr("data")=="http://www.optathlon.com/flash/MA.swf"){
			$("#unitydialog").children(".dialogheader").empty().append('<a href="http://www.united.com" ><img alt="" class="dialogleft" src="img/dialog/unitedLogo.png"/></a><a href="http://www.optathlon.com" ><img alt="" class="dialogmiddle" src="img/dialog/optathlon.png"/></a><a href="http://adfarm.mediaplex.com/ad/ck/1091-107466-3840-2?mpt='+timestamp+'" ><img class="hidden" src="http://adfarm.mediaplex.com/ad/tr/1091-107466-3840-2?mpt='+timestamp+'" border="0"><img alt="" class="dialogright" src="img/dialog/optfortraveloptions.png"/></a><a href="#" class="unityclose dialogclose"></a>');
		}else if($(".flash").attr("data")=="http://www.optathlon.com/flash/SS.swf"){
			$("#unitydialog").children(".dialogheader").empty().append('<a href="http://www.united.com" ><img alt="" class="dialogleft" src="img/dialog/unitedLogo.png"/></a><a href="http://www.optathlon.com" ><img alt="" class="dialogmiddle" src="img/dialog/optathlon.png"/></a><a href="http://adfarm.mediaplex.com/ad/ck/1091-107466-3840-4?mpt='+timestamp+'" ><img class="hidden" src="http://adfarm.mediaplex.com/ad/tr/1091-107466-3840-4?mpt='+timestamp+'" border="0"><img alt="" class="dialogright" src="img/dialog/optfortraveloptions.png"/></a><a href="#" class="unityclose dialogclose"></a>');*/
    }
    $(".unityclose").click(function()
    {
      $("#unitydialog").dialog("close");
      return false;
    });
    scroll(0,0);
    $("#unitydialog").dialog("open");
    return false;
  });
  $(".playFlashGame").click(function()
  {
    flashurl = $(this).attr("href");
    if(flashurl == "http://www.optathlon.com/flash/MA.swf")
    {
      $("#flashdialog").children(".dialogheader").empty().append('<a href="http://www.united.com" ><img alt="" class="dialogleft" src="img/dialog/unitedLogo.png"/></a><a href="http://www.optathlon.com" ><img alt="" class="dialogmiddle" src="img/dialog/optathlon.png"/></a><a href="http://adfarm.mediaplex.com/ad/ck/1091-107466-3840-0?mpt='+timestamp+'" ><img class="hidden" src="http://adfarm.mediaplex.com/ad/tr/1091-107466-3840-0?mpt='+timestamp+'" border="0"><img alt="" class="dialogright" src="img/dialog/optfortraveloptions.png"/></a><a href="#" class="unityclose dialogclose"></a>');
    }
    else
    {
      $("#flashdialog").children(".dialogheader").empty().append('<a href="http://www.united.com" ><img alt="" class="dialogleft" src="img/dialog/unitedLogo.png"/></a><a href="http://www.optathlon.com" ><img alt="" class="dialogmiddle" src="img/dialog/optathlon.png"/></a><a href="http://adfarm.mediaplex.com/ad/ck/1091-107466-3840-1?mpt='+timestamp+'" ><img class="hidden" src="http://adfarm.mediaplex.com/ad/tr/1091-107466-3840-1?mpt='+timestamp+'" border="0"><img alt="" class="dialogright" src="img/dialog/optfortraveloptions.png"/></a><a href="#" class="unityclose dialogclose"></a>');
    }
    $(".flashclose").click(function()
    {
      $("#flashdialog").dialog("close");
      return false;
    });
    scroll(0,0);
    $("#flashdialog").dialog("open");
    return false;
  });
  $("#tandcbtn").click(function()
  {
    scroll(0,0);
    $("#tandcdialog").dialog("open");
    return false;
  });
  $(".videoclose").click(function()
  {
    $("#videodialog").dialog("close");
    return false;
  });
  $(".unityclose").click(function()
  {
    $("#unitydialog").dialog("close");
    return false;
  });
  $(".flashclose").click(function()
  {
    $("#flashdialog").dialog("close");
    return false;
  });
  $(".tandcclose").click(function()
  {
    $("#tandcdialog").dialog("close");
    return false;
  });
  $(".androidclose").click(function()
  {
    $("#androiddialog").dialog("close");
    return false;
  });
  $("#androidbtn").click(function()
  {
    $("#androiddialog").dialog("open");
    return false;
  });
  $(".iphoneclose").click(function()
  {
    $("#iphonedialog").dialog("close");
    return false;
  });
  $("#iphonebtn").click(function()
  {
    $("#iphonedialog").dialog("open");
    return false;
  });
  $(".ui-dialog-titlebar").hide();
  $(".addthis_button_facebook").click(function()
  {
    _gaq.push(['_trackEvent', 'Share', 'Facebook']);
  });
  $(".addthis_button_myspace").click(function()
  {
    _gaq.push(['_trackEvent', 'Share', 'MySpace']);
  });
  $(".addthis_button_google").click(function()
  {
    _gaq.push(['_trackEvent', 'Share', 'Google']);
  });
  $(".addthis_button_twitter").click(function()
  {
    _gaq.push(['_trackEvent', 'Share', 'Twitter']);
  });
  $(".addthis_button_compact").click(function()
  {
    _gaq.push(['_trackEvent', 'Share', 'Extended']);
  });
});
