﻿(function(){
	//shortcuts
	var T = reNative.lang.Type, Sys = reNative.framework.Sys, reUriQs = /^.*\?(.*)$/i;
	reNative.namespace( "neg.VideoContest" ).def( {
		Config: {
			video:	"http://www.newegg.com/product/showvideo.aspx",
			item:	"http://www.newegg.com/product/product.aspx",
			image:	"http://c1.neweggimages.com/onlinevideo/snapshot/",
			flash:	"images/player.swf",
			egg:	"http://c1.neweggimages.com/webresource/themes/2005/nest/video_egg2.png"
		},
		getItemInfo: function( item ) {
			try { return Product[ item ]; } catch( e ) {
				return undefined;
			}
		},
		getVideoInfo: function( url ) {
			try {
				var d = new Sys.Web.HttpNameValueCollection( url.replace( reUriQs, "$1" ) );
				return {
					file: d.get( "videoFileName" ),
					size: d.get( "videoFileSize" ),
					title: d.get( "title" ),
					duration: d.get( "videoDuration" )
				};
			} catch( e ) { return undefined; }
		},
		listing: function( idx, video ) {
			try {
				var htmls = [];




			} catch( e ) {
				alert( e );
			}
			//document.write( html );
		},
		play: function( video, autoplay, idx, count ) {
			var p = neg.VideoContest.player, c = neg.VideoContest.Config, l,
			d = video.info;
			p.addVariable( "videofilename", d.file );
			p.addVariable( "videoSize", d.size );
			p.addVariable( "videoDuration", d.duration );
			p.addVariable( "autoStart", autoplay || "false" );
			if (autoplay) {
				location.hash = "playing";
			}
			if ( !p.write("playerContainer") ) {
				//todo: display non-support info;
			}
			jQuery("#playing-title").text( T.String.encodeHtml( T.String.decodeUri( video.title ) ) );
			jQuery("#playing-author").text( T.String.encodeHtml( T.String.decodeUri( video.name ) ) );

		},
		init: function() {
			try {
				var videos = neg.VideoContest.Data.videos, l = videos.length, i = 0, c = neg.VideoContest.Config;
				for ( ; i < l; i++ ) {
					videos[i].info = neg.VideoContest.getVideoInfo( videos[i].url );
				}

				//apply jquery links

				jQuery.noConflict();
				jQuery.fn.innerDisplay = function() {
					var display = function() {
						var ins = this;
						var url = ins.getAttribute( "href" );
						var qsb = new Sys.Web.HttpNameValueCollection( url.replace( reUriQs, "$1" ) );
						var idx = (qsb.get( "di" )-0);
						try {
							neg.VideoContest.play( neg.VideoContest.Data.videos[ idx ], true, idx, neg.VideoContest.Data.videos.length );
							return false;
						} catch( e ) {}
					};

					return this.unbind( "click" ).click( display );
				};
				jQuery(document).ready(function(){
					//active first video;
					try {
						neg.VideoContest.play( videos[0], false, 0, videos.length );
					} catch( e ) {
						//todo: display error msg;
						//alert( "play: "+ e );
					}

					var rImage = function( videos, i ) {
						var h = [];
						for ( var j = 0; j < 3 && (i+j < l); j++ ) {
							var url = "";
							if ( i+j < l ) {
								var video = videos[i+j];
								url = '<a href="'+ T.String.encodeHtml( video.url +'&di='+ (i+j) ) +'" class="noline neg-video-link" title="play">'+ neg.utils.png( T.String.encodeHtml( c.image + video.info.file ) +'.png', {
									alt: T.String.encodeHtml( video.title ),
									style: "width:80px;height:60px;"
								} ) + neg.utils.png( c.egg, {
									alt: "play",
									style: "position:absolute;top:20px;left:26px;width:30px;height:23px;border:0;margin:auto;z-index:999;"
								} ) + '</a>';
							}
							h.push('<td width="200" height="70"><div style="position:relative;width:80px;margin:auto;">' );
							h.push( url );
							h.push( '</div></td>' );
						}
						return h.join("");
					};
					var rTitle = function( videos, i ) {
						var th = [], ah = [];
						th.push( '<tr align="center" valign="top">' );
						ah.push( '<tr align="center" valign="top">' );
						for ( var j = 0; j < 3; j++ ) {
							var title = "", name = "";
							if ( (i+j < l) ) {
								var video = videos[i+j];
								title = T.String.encodeHtml( T.String.decodeUri( video.title ) );
								name = 'Submitted by ' + T.String.encodeHtml( video.name );
							}
							th.push( '<td width="200"><h1>'+ title +'</h1></td>' );
							ah.push( '<td width="200"><h2>'+ name +' </h2></td>' );
						}
						th.push( '</tr>' );
						ah.push( '</tr>' );
						return th.join( "" ) + ah.join( "" );
					};

					try {
						var htmls = [];
						if ( l > 0 ) {
							htmls.push( '<table width="600" border="0" cellpadding="0" cellspacing="0" id="vidthumb">' );
							for ( var i = 0; i < l; i+=3 ) {
								if ( i !== 0 ) {
									htmls.push( '<tr align="center">' );
									htmls.push( '<td colspan="3" valign="bottom" style="padding:0 0 10px 0;"><img src="images/line.gif" width="580" height="10"></td>' );
									htmls.push( '</tr>' );
								}
								htmls.push( '<tr align="center" valign="top">' );
								htmls.push( rImage( videos, i ) );
								htmls.push( '</tr>' );
								htmls.push( '<tr align="center"><td colspan="3">' );
								htmls.push( '<table width="600" border="0" cellspacing="0" cellpadding="5">' );
								htmls.push( rTitle( videos, i ) );
								htmls.push( '</table></td></tr>' );
							}
							htmls.push( '</table>' );
							var htmlstring = htmls.join( "" );
							jQuery( "#neg-video-list" ).html( htmlstring );
						}
					} catch( e ) {
						alert( e );
					}

					jQuery(".neg-video-link").innerDisplay();
				});
				return true;
			} catch( e ) {
				jQuery(document).ready(function(){
					jQuery( "#neg-video-list" ).html(
						'<p style="font-weight:normal;font-size:0.9em;line-height:1.3em;color:#a9232d;">We\'re sorry the page you are trying to view is not available. We are working quickly to resolve this error, please try again later.</p>'
					);
				});
				return false;
			}
		},
		Data: {
			videos:null
		}
	} );

	neg.VideoContest.player = (function(){
		var so = new SWFObject( neg.VideoContest.Config.flash, "videoPlayer", "330", "308","9" );
		so.addParam("quality", "high");
		so.addParam("allowFullScreen", "true");
		so.addVariable("videoFileSite","http://c1.neweggimages.com/");
		so.addVariable("videoFileRootPath","OnlineVideo/Encoded/");
		return so;
	})();
})();