var video =  {
	init: function() {
		video.setMainRatingStars.delay(1);
		video.setSideStarRatings.delay(1);
		video.setCodeFields.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) {
					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);
			}
		});
	}
}

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

var vSwap = {
	swapPop:function(tF,sec){
		var gUrl='/lib/includes/components/flush/vid_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);