//
// Wait for the DOM to be ready and then load some on-demand content.
//
$(document).ready(function() {
    //
    // Setup button handlers.
    //
    $("#playButton").click(function() {
        var VideoUrl = $('#VideoUrl').text();

	stopSnowing();
        clearTimeout(SeriesTimeout);
        $('#currentSeries').show();
        $('#nextSeries').hide();
        $('#seriesPlaceholder').html('');

	_gaq.push(['_trackEvent', 'Videos', 'Play', VideoUrl]);

        if (navigator.mimeTypes['application/x-shockwave-flash'] || navigator.appName == 'Microsoft Internet Explorer')
        {
            var CloudFront = (VideoUrl.indexOf('/aws.') > 0 ? true : false);

            if (CloudFront)
            {
                VideoUrl = VideoUrl.substring(VideoUrl.indexOf('/', 8) + 1);
                VideoUrl = "mp4:" + VideoUrl;
            }

            flowplayer("seriesPlaceholder", "/Custom/Website/Support/flowplayer.commercial-3.2.3.swf", {
                    key: '#$ebe1ee548bc0ac4e393',
                    canvas: { background: '#000000', backgroundGradient: 'none' },
                    clip: {
                            url: VideoUrl,
                            provider: (CloudFront ? 'rtmp' : 'http'),
                            autoPlay: true,
                            scaling: 'fit'
                    },
                    plugins: {
                            rtmp: {
                                    url: '/Custom/Website/Support/flowplayer.rtmp-3.2.3.swf',
                                    netConnectionUrl: 'rtmp://s3khk9u4zq102y.cloudfront.net/cfx/st'
                            }
                    }
            });
        }
        else
        {
            flowplayer("seriesPlaceholder", "/Custom/Website/Support/flowplayer.commercial-3.2.3.swf", {
                    key: '#$ebe1ee548bc0ac4e393',
                    canvas: { background: '#000000', backgroundGradient: 'none' },
                    clip: {
                            url: VideoUrl,
                            provider: 'http',
                            autoPlay: true,
                            scaling: 'fit'
                    }
            }).ipad();
        }

	$('#playButton').hide();
    });

    //
    // Load the latest series graphic.
    //
    var img = new Image();
    $(img).load(function() {
        $("#currentSeries").append(this);
            $("#playButton").fadeIn(200);
    });
    $(img).attr('src', $('#SeriesImage').text());
});

//
// Prepare the rotater if we need it.
//
$(document).ready(function() {
    if (NextSeriesImage != '')
    {
        var div = $('<div id="nextSeries" class="absolute"></div>');
        var link = $('<a id="nextSeriesLink" href="default.aspx?page=3473"></a>');
	var img = new Image();

        $(div).hide();
        $(div).appendTo($('#seriesPlaceholder'));
        $(link).appendTo($(div));
        $(img).load(function() {
            $('#nextSeriesLink').append(this);
            SeriesTimeout = setTimeout("ShowNextSeries()", 5000);
        });
        $(img).attr('src', NextSeriesImage);
    }
    if (0)
    {
        var div = $('<div class="absolute"></div>');
        var link = $('<a href="default.aspx?page=3582"></a>');
        var img = new Image();

        $(div).hide();
        $(div).appendTo('#seriesPlaceholder');
        $(link).appendTo($(div));
        $(img).appendTo($(link));
        $(img).attr('src', 'http://www.highdesertchurch.com/Custom/Website/Images/easter2011.jpg');
        SeriesTimeout = setTimeout("ShowNextSeries()", 5000);
    }
});

var SeriesTimeout = null;
var SeriesIndex = 0;

function ShowNextSeries()
{
    var kids, last, current;

    kids = $('#seriesPlaceholder').children();
    last = kids[SeriesIndex];
    if (++SeriesIndex >= $(kids).length)
        SeriesIndex = 0;
    current = kids[SeriesIndex];

    if (last != current)
    {
        $(current).fadeIn(1000);
        $(last).fadeOut(1000);
    }

    SeriesTimeout = setTimeout("ShowNextSeries()", 5000);
}


function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function createSnowFlake()
{
  var deltaY = (Math.random() * 2.0) + 0.25;
  var deltaX = (Math.random() * 1) - 0.5;

  var div = $(document.createElement("div"));
  $(div).attr({'class': 'snowflake'});
  $(div).css('background-image', 'url(/Custom/Website/Images/snow.png)');
  $(div).css('position', 'absolute');
  $(div).css('width', '32px').css('height', '30px');
  $(div).css('top', '-30px');
  $(div).css('left', Math.floor(Math.random() * $(document).width()) + 'px');
  $(div).css('z-index', '5');
  $(div).data('snowX', parseInt($(div).css('left')));
  $(div).data('snowY', parseInt($(div).css('top')));
  $(div).data('snowDeltaY', deltaY);
  $(div).data('snowDeltaX', deltaX);

  return div;
}

function updateSnowFlakes()
{
  $('.snowflake').each(function() {
    var top = parseFloat($(this).data('snowY'));
    var left = parseFloat($(this).data('snowX'));

    top += parseFloat($(this).data('snowDeltaY'));
    left += parseFloat($(this).data('snowDeltaX'));

    $(this).css('top', top + 'px').css('left', left + 'px');
    $(this).data('snowX', left).data('snowY', top);

    if (top >= 270 && navigator.appName != 'Microsoft Internet Explorer')
    {
      var opac = (1 - ((top - 270) / 100));

      $(this).css('opacity', opac);
    }
    if (top >= 370 || left + 35 >= $(document).width())
    {
      $(this).remove();
      $('#header').append(createSnowFlake());
    }
  });

  _snowTimer = setTimeout('updateSnowFlakes();', 33);
}

function stopSnowing()
{
  clearTimeout(_snowTimer);
  _snowTimer = null;
  $('.snowflake').remove();
}

var _snowTimer;
if (false)
{
  $(document).ready(function() {
    for (i = 0; i < 15; i++)
    {
      $('#header').append(createSnowFlake());
    }

    _snowtimer = setTimeout('updateSnowFlakes();', 33);
  });
}


