function Player(p)
{
	/* ee vars */
	this.entry_id = p.entry_id;
	this.unique_id = p.unique_id;
	
	/* js vars */
	this.anchor = false;
	this.watched = false;
	
	/* swf vars */
	this.flashvars = {
		 entry_id: this.entry_id
		,unique_id: this.unique_id
		,graceperiod: (typeof(p.graceperiod)!='undefined'&&p.graceperiod!='')?p.graceperiod:'3'
		,source: ''
		,url_title: p.url_title
	}
	this.params = {
		 wmode:'opaque'
		,bgcolor:'#eef1f1'
		,allowscriptaccess: 'always'
	}
	this.attr = {
		 id:'player_object'
		,name:'player_object'
	}
	
	/* show init? */
	if(typeof(p.init)!='undefined'&&p.init=='yes')
	{
		this.flashvars.init = 'true';
	}
	
	/* make it accessible */
	playerObject = this;
	
	/* get the xhtml */
	new Ajax.Request('/site/player/'+this.entry_id + '/', {
		onSuccess: this._embed.bind(this)
	});
	
	/* update the thumbs? */
	if(typeof(p.anchor) != 'undefined')
	{
		this.flashvars.autoStart = 'true';
		this.anchor = p.anchor;
		this._selectThumb(p.anchor);
		
		var href = this.anchor.getAttribute('href', 2);
		href = href.replace(/\/$/, '');
		href = href.replace(/^.*\//, '');
		
		window.location = '#'+href;
	}
	else if(typeof(p.url_title) != 'undefined' && !window.location.toString().match(/ad\//))
	{
		// window.location = '#'+p.url_title;
	}
	
	/* are we on an ad detail? */
	if(window.location.toString().match(/\/ad\//))
	{
		this.flashvars.autoStart = 'true';
	}
}

Player.prototype.view = function(p)
{
	this.flashvars.entry_id = ""+this.entry_id;
	this.flashvars.source = p.path;
	this.flashvars.thumb = p.thumb;
	
	swfobject.embedSWF("/s/player.swf", "player", "443", "400", "8.0.0", false, this.flashvars, this.params, this.attr);
}
// Created by Rajmohan to get the video seconds and store in the database
var statictime = 0.00;
Player.prototype.setUpdate = function(varTime)
{

 var videotime =  varTime.split('/');
 var Current_time = videotime[0].replace(/:/, ".");
 var Total_time = videotime[1].replace(/:/, ".");
 if(statictime != Current_time)
 { 
	statictime = Current_time;
	//alert('Current_time'+statictime);
	//alert('Total_time'+Total_time);
	
	//alert(temp_time);
	var temp =Current_time % 0.05;
	var num = new Number(temp);
	num = num.toFixed(3); 
	if(num == 0.000 || num == 0.050 || Current_time == Total_time)
	 {
		//alert('Current_time'+statictime);
		var viewed_time = this.entry_id+"-"+Current_time+"-"+Total_time;
		//alert(viewed_time);
	//	new Ajax.Request('/site/video_viewed/'+viewed_time+'/');
		
		new Ajax.Request('/?ACT=42&view_time='+viewed_time);
	 }
	 

 }
 
}
// End created by rajmohan

Player.prototype._embed = function(response)
{
	var playerXhtml = response.responseText;

	var div = $('player-holder');
	var h2 = div.previous('h2');

	h2.remove();
	div.replace(playerXhtml);

	this._load_comments();
	this._sifr();
}

Player.prototype._load_comments = function()
{
	new Ajax.Request('/site/comments/'+this.entry_id + '/', {
		onComplete: this._embed_comments.bind(this)
	});
}

Player.prototype._embed_comments = function(response)
{

	$('comment-holder').update(response.responseText);
	comment_formx = $("comment_form");
	if(comment_formx)
	{
		comment_formx.observe('submit', function(evt) {
			evt.stop();
			comment_formx.request({
				onSuccess: this._embed_comments.bind(this)
			});
		}.bindAsEventListener(this));
	}

	login_form = $('login-inline');
	if(login_form)
	{
		login_form.observe('submit', function(evt) {
			evt.stop();
			login_form.request({
				onSuccess: function(url, req)
				{
					if(req.responseText.match(/Error<\/title>/))
					{
						alert('There was a problem logging you in, please check your username and password.');
					}
					else
					{
						new Ajax.Request(url, {
							method:'get', 
							onSuccess: this._embed_comments.bind(this)
						});
					}
				}.bind(this, login_form['comment_url'].value)
			});
		}.bindAsEventListener(this));
	}
}


Player.prototype._sifr = function()
{
	sIFR.replace(gotham_medium, {
		 selector: 'h2.sifr'
		,wmode: 'transparent'
		,css: [
			 '.sIFR-root { font-size:24px; }'
			,'a { color:#000000; text-decoration:none; }'
			,'a:hover { color:#609031; }'
		]
	});
	hijackSave();
}

Player.prototype.setWatched = function()
{
	var watching = $$('li.watching');
	watching.each(function(li){
		// li.removeClassName('watching');
		// li.getElementsBySelector('div a em').first().remove();
		if(! li.hasClassName('watched'))
		{
			li.watched = true;
			li.addClassName('watched');
			li.getElementsBySelector('div a').first().insert({bottom:'<em class="watched">You have watched this ad</em>'});
		}
	}.bind(this));
}

Player.prototype._selectThumb = function()
{
	var li = $(this.anchor).up('li');
	if(li)
	{
		var watching = $$('li.watching');
		watching.each(function(li){
			li.removeClassName('watching');
			li.getElementsBySelector('div a em').first().remove();
		}.bind(this));
		li.addClassName('watching');
		li.getElementsBySelector('div a').first().insert({top:'<em class="now-playing">Now Playing</em>'});
	}
}

Player.prototype.newNumber = function(num, save)
{
	if(save)
	{
		new Ajax.Request('/lottery/generate/'+num+'/');
	}
	thisMovie('numbersFlash').newNumber(num);
}


function nowValid()
{
	// alert('Now a valid watch');
}

function debug(str)
{
	alert(str);
}