var video =  {
	init: function() {
		video.setMainRatingStars.delay(1);
		video.setSideStarRatings.delay(1);
		video.setCodeFields.delay(1);
		video.setToolTips.delay(1);
	},
	
	// setters
	playerInit: function() {
		/*
		var s1 = new SWFObject('/bin/vid-bin/octPlayer.swf?','player','608','382','9','##FFFFFF');
		s1.addParam('allowfullscreen','true');
		s1.addParam('allowscriptaccess','always');
		s1.addParam('wmode','opaque');
		s1.write('vidPlayer');
		*/
	},
	voteLock: false,
	setMainRatingStars: function() {
		if ($$('.video-rating-main').length == 0){return;}
		var starTarget = $$('.video-rating-main')[0];
		var avgScore = starTarget.id.split("_")[1];
		var totalVotes = starTarget.id.split("_")[2];
		var vidID = starTarget.id.split("-")[1].split("_")[0];
		new Ajax.Request('/rating/ajax/check_video_vote/', {
			method: 'post',
			parameters: {
				vidID: vidID
			},
			onSuccess: function(t) {
				if (t.responseText.strip() == "true") {
					video.voteLock = true;
				}
				new Starbox(starTarget, avgScore, {
					overlay: 'big.png',
					locked: false,
					rated: false,
					buttons: 5,
					total: totalVotes,
					locked: video.voteLock,
					rated: video.voteLock,
					indicator: '#{average} average rating from #{total} votes',
					onRate: catchVideoRate
				});
			},
			onFailure: function(t){
				_tools.ajaxBug(t);
			}
		});
	},
	setSideStarRatings: function() {
		var compList = $$('.comp-score');
		if (compList.length == 0) {return;}
		compList.each(function(compAt) {
			var avgScore = compAt.id.split("_")[1];
			var totalVotes = compAt.id.split("_")[2];
			new Starbox(compAt, avgScore, {
				locked: true,
				total: totalVotes,
				buttons: 5,
				locked: true
			});
		});
	},
	setCodeFields: function() {
		var fields = [];
		fields.push($('url_field'));
		fields.push($('embed_field'));
		fields.each(function(fieldAt) {
			if(!fieldAt) {return;}
			fieldAt.onfocus = function() {
				fieldAt.select();
			}
		});
	},
	
	// video voting
	videoRate: function(el,info) {
		new Ajax.Request('/rating/ajax/video_rate/', {
			method: 'post',
			parameters: {
				score: info.rated,
				vidID: el.id.split("-")[1].split("_")[0]
			},
			onSuccess: function(t) {
				var h = $H(t.responseText.evalJSON());
				if (!h._object.success) {
					login.promptLogin('content');
					/*
					util.talkbox(t.responseText);
					util.removebox = util.removebox.wrap(
						function(proceed) {
							video.setMainRatingStars();
							return proceed();
						}
					);
					*/
				} else {
					new Effect.Pulsate(el, {
						from: .5,
						pulses: 3
					});
				}
			},
			onFailure: function(t) {
				_tools.ajaxBug(t);
			}
		});
	},
	setToolTips: function() {
		if(document.getElementsByClassName('digg').length <= 0){ return false;}
		$A(document.getElementsByClassName('digg')).each(function(itemAt){
			new Tip($(itemAt),'Share this video on Digg', {
				style: 'gpblue',
				hook: {target: 'topMiddle', tip: 'bottomMiddle'},
				stem: 'bottomMiddle',
				width: 'auto'
			});
		});
		$A(document.getElementsByClassName('facebook')).each(function(itemAt){
			new Tip($(itemAt),'Share this video on Facebook', {
				style: 'gpblue',
				hook: {target: 'topMiddle', tip: 'bottomMiddle'},
				stem: 'bottomMiddle',
				width: 'auto'
			});
		});
		$A(document.getElementsByClassName('stumble')).each(function(itemAt){
			new Tip($(itemAt),'Share this video on StumbleUpon', {
				style: 'gpblue',
				hook: {target: 'topMiddle', tip: 'bottomMiddle'},
				stem: 'bottomMiddle',
				width: 'auto'
			});
		});
		$A(document.getElementsByClassName('delicious')).each(function(itemAt){
			new Tip($(itemAt),'Share this video on Delicious', {
				style: 'gpblue',
				hook: {target: 'topMiddle', tip: 'bottomMiddle'},
				stem: 'bottomMiddle',
				width: 'auto'
			});
		});
		$A(document.getElementsByClassName('gamekicker')).each(function(itemAt){
			new Tip($(itemAt),'Share this video on GameKicker', {
				style: 'gpblue',
				hook: {target: 'topMiddle', tip: 'bottomMiddle'},
				stem: 'bottomMiddle',
				width: 'auto'
			});
		});
		$A(document.getElementsByClassName('n4g')).each(function(itemAt){
			new Tip($(itemAt),'Share this video on N4G', {
				style: 'gpblue',
				hook: {target: 'topMiddle', tip: 'bottomMiddle'},
				stem: 'bottomMiddle',
				width: 'auto'
			});
		});
	},
	// share function object
	share : {
		shareSite: '',
		init: function() {
			dataReturn = {
				"title": $('video-title').innerHTML.replace(/<span( class="?posted_on"?|)>/gi,'').replace(/<\/span>/gi,''),
				"blurb": $('video-blurb').innerHTML,
				"url": window.location.href.replace('#','')
			}
			return dataReturn;
		},
digg: function() {
			var dataObject = this.init();
			this.shareSite = 'Digg';
			this.goURL("http://digg.com/submit?phase=2&url=" + encodeURIComponent(dataObject.url)+ "&title=" + encodeURIComponent(dataObject.title)+ "&bodytext=" + encodeURIComponent(dataObject.blurb),(window.sharewindow)?window.sharewindow:"none");
		},
		yahoo: function() {
			var dataObject = this.init();
			this.shareSite = 'Yahoo';
			this.goURL("http://buzz.yahoo.com/article/gameprocom109/" + dataObject.url,(window.sharewindow)?window.sharewindow:"none");
		},
		facebook: function() {
			var dataObject = this.init();
			this.shareSite = 'Facebook';
			this.goURL("http://www.facebook.com/share.php?u=" + dataObject.url,(window.sharewindow)?window.sharewindow:"none");
		},
		delicious: function() {
			var dataObject = this.init();
			this.shareSite = 'Del.icio.us';
			this.goURL("https://secure.delicious.com/login?url=" + encodeURIComponent(dataObject.url) + "&title=" + encodeURIComponent(dataObject.title) + "&jump=close&partner=addthis&v=4",(window.sharewindow)?window.sharewindow:"none");
		},
		stumbleupon: function() {
			var dataObject = this.init();
			this.shareSite = 'StumbleUpon';
			this.goURL("http://www.stumbleupon.com/submit?url=" + dataObject.url+ "&title=" + dataObject.title,(window.sharewindow)?window.sharewindow:"none");
		},
		gamekicker: function() {
			var dataObject = this.init();
			this.shareSite = 'GameKicker';
			this.goURL("http://www.gamekicker.com/node/add/drigg?url=" + encodeURIComponent(dataObject.url) + "&title=" + encodeURIComponent(dataObject.title),(window.sharewindow)?window.sharewindow:"none");
		},
		n4g: function() {
			var dataObject = this.init();
			this.shareSite = 'N4G';
			this.goURL("http://n4g.com/tips?url=" + encodeURIComponent(dataObject.url) + "&title=" + encodeURIComponent(dataObject.title),(window.sharewindow)?window.sharewindow:"none");
		},
		goURL: function(newURL,newWindow) {
			var obj = {
				"cat": 'share-this',
				"action": this.shareSite,
				"label": window.location.href
			}
			goog.generalTracker(obj);
			(function(){
				if(newWindow != "none"){
					newWindow.document.location = newURL;
				} else {
					var sharewindow = window.open(newURL);
				}
			}).delay(1);
		}
	}
}

function catchVideoRate (el,info) {
	video.videoRate(el,info)
}

var vSwap = {
	swapPop:function(tF,sec){
		var gUrl='/lib/includes/components/flush/rail_video_popular_'+sec+'_'+tF+'.html';
		new Ajax.Updater($('vSwap'), gUrl,
		{
		onComplete:function(){
			video.setSideStarRatings();
			}	
		});
	}
}

Event.observe(window,'load',video.init);
//document.observe('dom:loaded',video.init);