var fadeSpeed = 600;
var $f;

// serializeObject is good for 'associative' structures.
// DEPRECATED, REPLACED BY serializeFullArray, which behaves properly.
(function($){
	$.fn.serializeObject = function()
	{
	    var o = {};
	    var a = this.serializeArray();
	    $.each(a, function() {
	        if (o[this.name]) {
	            if (!o[this.name].push) {
	                o[this.name] = [o[this.name]];
	            }
	            o[this.name].push(this.value || '');
	        } else {
	            o[this.name] = this.value || '';
	        }
	    });
	    return o;
	};
})(jQuery);

(function($){
	'$:nomunge'; // Used by YUI compressor.

	$.fn.serializeFullArray = function () {
		// Grab a set of name:value pairs from the form dom.
		var set = $(this).serializeArray();
		var output = {};

		for (var field in set)
		{
			if(!set.hasOwnProperty(field)) continue;

			// Split up the field names into array tiers
			var parts = set[field].name;
				parts = parts.split(/\]|\[/);

			// Start ref out at the root of the output object
			var ref = output;

			for (var segment in parts)
			{
				if(parts[segment] == '') continue;
				if(!parts.hasOwnProperty(segment)) continue;

				// set key for ease of use.
				var key = parts[segment];
				var value = {};

				// If we're at the last part, the value comes from the original array.
				if (segment == parts.length - 1)
				{
					var value = set[field].value;
				}

				// Create a throwaway object to merge into output.
				var objNew = {};
				objNew[key] = value;

				// Extend output with our temp object at the depth specified by ref.
				$.extend(true, ref, objNew);

				// Reassign ref to point to this tier, so the next loop can extend it.
				ref = ref[key];
			}
		}

		return output;
	};
})(jQuery);

function hideVideo(){
	return;
	if( $.isFunction( $f ) )
	{
		$f().stop();
	}
	$("#video").fadeOut( fadeSpeed, showForm );
}

function showForm( values ) {
	if( typeof values == 'undefined' )
		values = {};

	values.pagename = pagename;

	$.ajax( {
		url: 'site/pagepick/process',
		data: values,
		success: function( data ) {
				switch ($('#resultstate', data).val())
				{
					default:
						$('#form').html( data );
						$('#formwrapper').fadeIn( fadeSpeed );

						$('#submit').click( function( event ) {
							event.preventDefault();
							
							var formDetails = $('#form').find( 'form' ).serializeFullArray();

							showForm( formDetails )

							_gaq.push( [ '_trackEvent', 'Form submitted - ' + pagename ] );
						});
						break;
					case 'thanks':
						$("#formwrapper").fadeOut( fadeSpeed );
						$("#thanks").fadeIn( fadeSpeed );
						break;
					case 'sorry':
						$("#formwrapper").fadeOut( fadeSpeed );
						$("#sorry").fadeIn( fadeSpeed );
						break;
				}
				
				
				
		},
		dataType: 'html',
		type: 'post'
	});
}




$(document).ready( function() {
	$('#skipvideolink').show().click( function() { hideVideo(); } );
	
	$('#prizelink').click( function(){  $('#prizes').fadeIn(200); return; });
	$('#prizesclose').click( function(){  $('#prizes').fadeOut(200); return; });


	/* Fix for IE. For some reason $f doesn't get set in the flowplayer JS file */
	if( ( typeof $f == 'undefined' ) &&
		( typeof flowplayer != 'undefined' ) )
	{
		$f = flowplayer;
	}

	if( ( typeof flowplayer != 'undefined' ) && ( $.isFunction( flowplayer ) ) && showvideo )
	{
		flowplayer("player", "flash/flowplayer.commercial-3.2.2.swf", {
			key: "#$e599ac080fbb3e99905",
			clip: {

			   urlResolvers: 'bwcheck',
			   provider: 'rtmp',
			   autoPlay: true,
			   scaling: 'fit',

			   // available bitrates and the corresponding files. We specify also the video width
			   // here, so that the player does not use a too large file. It switches to a
			   // file/stream with larger dimensions when going fullscreen if the available bandwidth permits.
			   bitrates: [
				 {
					url: "cemaus/CemAus-650-2", width: 640, height:360, bitrate: 650, isDefault:true
					// this is the default bitrate, the playback kicks off with this and after that
					// Quality Of Service monitoring adjusts to the most appropriate bitrate
				 },
				 { url: "cemaus/CemAus-350-2", width: 512, height:288, bitrate: 350 },
				 { url: "cemaus/CemAus-1000-2", width: 800, height:450, bitrate: 1000 }
			   ],

				onStart: function( clip ) {
					_gaq.push( [ '_trackEvent', 'Play - ' + pagename, clip.url] );
				},
				onStop: function( clip ) {
					_gaq.push( [ '_trackEvent', 'Stop - ' + pagename, clip.url, parseInt( this.getTime() ) ] );
				},
				onPause: function( clip ) {
					_gaq.push( [ '_trackEvent', 'Pause - ' + pagename, clip.url, parseInt( this.getTime() ) ] );
				},
				onFinish: function( clip ) {
					_gaq.push( [ '_trackEvent', 'Finish - ' + pagename, clip.url ] );
				}
			},
			onFinish: function() {
				//location.href = "/forum";
				//alert("on finish");
				hideVideo();
			},
			onBeforeFinish: function() {
				//hideVideo();
				//return false;
			},

			plugins:  {

				controls: ( jQuery.inArray(pagename, ['default']) === -1) ? null : true,

				// bandwidth check plugin
				bwcheck: {
					url: 'flash/flowplayer.bwcheck-3.2.1.swf',

					// HDDN uses Wowza servers
					serverType: 'fms',

					// we use dynamic switching, the appropriate bitrate is switched on the fly
					dynamic: true,

					netConnectionUrl: 'rtmp://fms.bcm.com.au/cfx/st'
				},



				// RTMP streaming plugin
				rtmp: {
					url: 'flash/flowplayer.rtmp-3.2.1.swf',
					netConnectionUrl: 'rtmp://fms.bcm.com.au/cfx/st'
				}
			}
		});
	}
	
	if (!showvideo)
	{
		$('#player').css('display', 'none');
	}

	if (jQuery.url.attr("anchor") == 'showform')
	{
		hideVideo();
	}
})

