var Player = function (settings) {
   
   this.videoObjs = [];
   this. currentIndex = 1;
   this.init();
   this.currentStatus = false;
   this.timeout=null;
}

Player.prototype = {

		init: function() {
			 // create the html containers for the site
			 var self = this;
			 			 
			 $('body').append("<div id='joebox'><div id='joebox_data'></div><div id='joebox_controls'><div id='title'></div><img class='close' src='/other_files/img/closelabel.gif'/></div><div class='clear'></div></div>");
			 $('#joebox #joebox_controls img').click(function() { self.close(); });
			 // find all videos on the site and put them in an array
			 
			 $('body a.joebox').each(
			    function(intIndex) {		        
              jObj = $(this);
			        self.addVideo(this.href, jObj.attr("j_width"), jObj.attr("j_height"), jObj.attr("j_title"), jObj.attr("j_duration"), intIndex);  
			        jObj.click( function() {
										    self.play_video(intIndex);
										    return false;
			         });

			    }
			 );
			 

		},

		play_video: function(index) {
       
         this.open(this.videoObjs[index].height, this.videoObjs[index].width);
         if (this.videoObjs[index].duration!="") {
             var _duration = '<span class="type">['+this.videoObjs[index].duration+']</span>';	
         } else { var _duration = ""; }
         $('#joebox_controls #title').html('<h2>'+this.videoObjs[index].title + _duration +' </h2>');
         var self = this;
         
         this.timeout = setTimeout(function (){
         	self.placeVideo(self.videoObjs[index]);
         },1500);
                   
		},
		
		placeVideo: function(videoObj) {
		   if (videoObj.type =="wmv") {
		   
		      // code for wmv player
		      
		     $ ('#joebox_data').html('<object id="video_box" bufferlength="10" smoothing="true" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"'
+'type="application/x-oleobject" width="'+videoObj.width+'" height="'+videoObj.height+'">'
+'<param name="showControls" value="true"><param name="ShowStatusBar" value="1"/><param name="autoStart" value="TRUE" />'
+'<param name="url" value="'+videoObj.href+'"><param value="0" name="AutoSize" />'
+'<embed type="application/x-mplayer2" width="'+videoObj.width+'" height="'+videoObj.height+'"'
+'showcontrols="true" showstatusbar="1" autostart="true" name="MediaPlayer1" src="'+videoObj.href+'"><\/embed><\/object>');

		   
		   
		   } else if (videoObj.type =="flv") {
		      //code for flash video
		   
		   }

		   this.showControls()
		   
		},
		
		
		
    sTop : function() {
        return window.pageYOffset || document.documentElement && document.documentElement.scrollTop ||	document.body.scrollTop;
      },
    
    wHeight : function() { 
        return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
    },
    

		open: function(height, width) {
		
		   this.rePosition((parseInt(height)+30), width); 
		
			 if (!this.currentStatus) {
         
         /**var topPos = this.sTop() + (this.wHeight() / 2) - (height/ 2);
         alert(topPos);

         $('#joebox').css({'position':'absolute'},{'top':topPos});**/
				    
			 } 
			 
			   var _width = "0 0 0 -"+width / 2+"px"; 
			 
				 $.dimScreen(250, 0.7, function() {
						$('#joebox').css({'margin': _width});
				 });
				 
		},
		
		rePosition: function(height, width) {

		  $('#joebox').height(height);
		  $('#joebox').width(width);
		  $('#joebox').vCenter();
		  $('#joebox').height(0);
		  $('#joebox').width(0);		  
		  $('#joebox').fadeIn(500);
		  $('#joebox').animate({width: width, height: height},"slow","swing");
		},
		
		showControls: function() {
		
		 $('#joebox_controls').slideDown('slow');
		  
		},

		close: function() {
		
				           
                       location.reload();
                       
                       try { window['video_box'].controls.stop(); } catch(e) { };
   
            
		   $('#joebox_data').html('');
			// handle the style closing
			  this.currentStatus = false;
				// use dim screen to reverse
				$.dimScreenStop();
				// hide hidden div
				$("#joebox").fadeOut("medium");
				$('#joebox_controls').hide();
				
		},

    addVideo: function(href, width, height, title, duration, index) {
       var height =  parseInt(height)  + 72;
       if (!duration) { duration = ""};
       this.videoObjs[index] = {href: href, type: "wmv", width:width, height: height, title:title, duration:duration};
    }

}
