/*

 * deVideoPlayer - jQuery plugin 1.0.0

 *

 * Copyright (c) 2010 Cristian-Ionut Colceriu

 *

 * www.de.net

 * contact@de.net

 *

 */



(function($) {

    // plugin definition

    $.fn.deVideo = function(options) {       

        // build main options before element iteration      
 
        var defaults = {

            name: '',

            theme: 'simpledark',

            childtheme: ''
            
            

        };

        var options = $.extend(defaults, options);

        // iterate and reformat each matched element

        return this.each(function() {

            var $deVideo = $(this);
        
            //create html structure

            //main wrapper

            var $video_wrap = $('<div></div>').addClass('de-video-player').addClass(options.theme).addClass(options.childtheme);

            //controls wraper

            var video_controls = '<div class="de-video-title cufon">'+options.name+'</div><a class="de-closeVideo" title="close">close</a><div class="de-video-controls"><a class="de-video-play" title="Play/Pause"><span class="cufon">Play/Pause</span></a><div class="de-video-seek">';
             
            for(i=1;i<=30;i++){
                   video_controls += '<div class="seekButtonBack" id="'+i+'" ><div>'+i+'</div></div>';
                
            }
            
            video_controls += '</span></div>';
      
            var $video_controls = $(video_controls);                      

            $deVideo.wrap($video_wrap);

            $deVideo.after($video_controls);

            
            //get new elements

            var $video_container = $deVideo.parent('.de-video-player');          

            var $video_controls = $('.de-video-controls', $video_container);

            var $de_title = $('.de-video-title', $video_container);
                                
            var $de_play_btn = $('.de-video-play', $video_container);

            var $de_video_seek = $('.de-video-seek', $video_container);
            
            var $de_close = $('.de-closeVideo', $video_container);
            
            
            var widthVid = 768;
            var heightVid = 576;
            var ratioVid = widthVid/heightVid;
            
             
            var windowHeight = $(window).height();
            var windowWidth = $(window).width();
           
            if(windowHeight<600){
                heightVid = windowHeight-200;
                widthVid = Math.ceil(heightVid*ratioVid);
            }else{
                heightVid =500;
                widthVid = Math.ceil(heightVid*ratioVid);
            }
          
            $deVideo.attr('width',widthVid);       
            $deVideo.attr('height',heightVid);       
             
            //$video_controls.hide(); // keep the controls hidden
            $video_container.css('position','absolute');
            $video_container.css('width',heightVid+'px');
     
            
            $video_controls.css('position','absolute');
            $video_controls.css('top',heightVid+'px');
            $video_controls.css('width',widthVid+'px');
            $de_play_btn.css('float','left');
            $de_play_btn.css('text-align','center');
            $de_play_btn.css('clear','both');
            $de_play_btn.css('width','100%');
            $de_video_seek.css('float','left');
            
             $de_title.css('position','absolute');
             $de_title.css('width',widthVid+'px');
             $de_title.css('text-align','center');
             $de_title.css('top','-20px');
            
            $de_video_seek.css('width','100%');
            
            var padding = (widthVid-240)/2;
            
            
            
            $de_video_seek.css('padding-left',padding+'px');
            
            $de_play_btn.css('padding','0 0 4px 0');
           
           
            $video_container.css('left',((windowWidth-widthVid)/2)+'px');
            $video_container.css('top',((windowHeight-(heightVid+40))/2)+'px');
            
            $de_close.css('left',(widthVid-2)+'px');
        
            var dePlay = function() {
                
                if($deVideo.attr('paused') == false) {

                    $deVideo[0].pause();                 

                } else {                    

                    $deVideo[0].play();              

                }

            };
   

            $de_play_btn.click(dePlay);

            $deVideo.click(dePlay);

            

            $deVideo.bind('play', function() {

                $de_play_btn.addClass('de-paused-button');

            });

            

            $deVideo.bind('pause', function() {

                $de_play_btn.removeClass('de-paused-button');

            });

            

            $deVideo.bind('ended', function() {

                $de_play_btn.removeClass('de-paused-button');

            });

            
            var deClose = function() {
                
                $video_container.remove();
                
            };
           

            $de_close.click(deClose);

           // createSeek();

            var seekTime=function(){
              var id= this.id;
              var video_duration = $deVideo.attr('duration');
              var currentTime = (video_duration/30)*id;
              $deVideo.attr("currentTime",currentTime);
              seekUpdate();

            };
        
            $('.seekButtonBack').bind('click',seekTime);

            var gTimeFormat=function(seconds){

                var m=Math.floor(seconds/60)<10?"0"+Math.floor(seconds/60):Math.floor(seconds/60);

                var s=Math.floor(seconds-(m*60))<10?"0"+Math.floor(seconds-(m*60)):Math.floor(seconds-(m*60));

                return m+":"+s;

            };

            var seekBar = function(currenttime){
            
                var video_duration = $deVideo.attr('duration');
            
                var seekID = Math.ceil((currenttime*30)/video_duration);
            
                $('.seekButtonBack').each(function(){
                var id = this.id;
                    
                    if(id<seekID){
                          $(this).find('div').addClass('seekButtonOver');
                    }else{
                          $(this).find('div').removeClass('seekButtonOver');
                    }
                
                });
            
            };

            var seekUpdate = function() {

                var currenttime = $deVideo.attr('currentTime');
                seekBar(currenttime);
                //$de_video_timer.text(gTimeFormat(currenttime));                         

            };

            

           $deVideo.bind('timeupdate', seekUpdate);
            

           $deVideo.removeAttr('controls');

            

        });

    };



    //

    // plugin defaults

    //

    $.fn.deVideo.defaults = {        

    };



})(jQuery);
