if (Function.prototype.bind == undefined) {
    Function.prototype.bind = function() {
        if (arguments.length < 2 && !arguments[0]) {
            return this;
        }
        var __method = this;
        var arg      = [];
        var length = arguments.length;
        while(length--) {
            arg[length] = arguments[length];
        } 
        var object = arg.shift();
        return function() { return __method.apply(object, arg.concat(arg)); };
    }
};

var WMPlayer = function(){
    this.controller             = {};
    this.controller.playButton  = null;
    this.controller.stopButton  = null;
    this.controller.track       = null;
    this.controller.currentTime = null;
    this.WMP                    = null;
    this.music                  = {};
    this.music.duration         = null;
    this.music.currentTime      = null;
    this.timer                  = null;
    this.isPlaying              = false;
    this.playComplete           = false;

};
WMPlayer.prototype.onPlayStateChange = function() {
};
WMPlayer.prototype.onReadyStateChange = function(e) {
    //alert ("e=======" + e + " this.WMP.Duration = " + this.WMP.Duration);
	if(e == 4) {
        this.music.duration = this.WMP.Duration;
		//alert ("in=======" + e);
    	// This.WMP.Stop(); 
    }
    this.WMP.Stop(); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
};
WMPlayer.prototype.onEndOfStream = function() {
    this.stop();
};
WMPlayer.prototype.setOption = function(options) {
    this.controller.playButton  = options.playButton;
    this.controller.stopButton  = options.stopButton;
    this.controller.track       = options.track;
    this.controller.currentTime = options.time;
    this.controller.loading     = options.loading;
    this.WMP          = options.player;
    this.setupController();
};
WMPlayer.prototype.setupController = function() {
    this.controller.playButton.style.cursor = 'pointer';
    this.controller.stopButton.style.cursor = 'pointer';
    this.controller.playButton.onclick = function() { this.onClickPlay(); }.bind(this);
    this.controller.stopButton.onclick = function() { this.onClickStop(); }.bind(this);
    //this.controller.loading.style.display = 'none';
	this.controller.track.style.display = 'none';
	this.controller.track.style.width = "1%";
};
WMPlayer.prototype.setFileURL = function(url) {
    this.WMP.FileName = url;
    this.setupMusic();
    
    // alert(this.WMP.PlayState);
};

WMPlayer.prototype.setFileMainURL = function(url) {
    this.WMP.FileName = "http://211.234.232.84:9000/"+url;
    this.setupMusic();
    
    // alert(this.WMP.PlayState);
};

WMPlayer.prototype.setupMusic = function() {
    if (this.WMP == null) { 
        return false;
    }
};

WMPlayer.prototype.onClickPlay = function(e) {
	if(this.music.duration == null || this.music.duration == 0) {
		if(e == undefined) {
			e = 0;
		}
		
		if(e < 500) {
			e += 100;
			var _self = this;
			setTimeout(function(e){_self.onClickPlay();}, 100);
		}
		
		return false;
	}
	
		this.controller.track.style.display = 'block';
	this.controller.playButton.style.display = 'none';
    this.controller.stopButton.style.display = 'block';
    
    this.WMP.Play();
    this.isPlaying = true;
	// alert("du" + this.music.duration);
    this.initTimer();
    
    
    
    //alert("play:" + this.WMP.PlayState + "(" + this.WMP.CurrentPosition + "/" + this.music.duration + ")");
};

WMPlayer.prototype.onClickStop = function(e) {
    this.controller.playButton.style.display = 'block';
    this.controller.stopButton.style.display = 'none';
    this.WMP.Pause();
    this.isPlaying = false;
    this.stopTimer();
    
    // alert("pause:" + this.WMP.PlayState);
};
WMPlayer.prototype.stop = function() {
    this.controller.track.style.display = 'none';
	this.controller.track.style.width = "1%";
	this.onClickStop();
    this.playComplete = true;
    this.isPlaying = false;
    this.WMP.Stop();

   //this.WMP.CurrentPosition = 0;
	this.controller.currentTime.innerHTML = '00:00';
    //this.controller.track.style.width = '0px';
	
    return false;
};

WMPlayer.prototype.convertTimeToPrint = function(time) {
    var min = Math.floor(time / 60);
    var sec = time - (min * 60);
    if (min < 10) {
        min = '0' + min;
    }
    if (sec < 10) {
        sec = '0' + sec;
    }
    return min + ':' + sec;
};

WMPlayer.prototype.initTimer = function() {
    this.music.currentTime = this.WMP.CurrentPosition;

    if (this.music.currentTime >= this.music.duration) {
        this.playComplete = true;
        this.onClickStop();
        return false;
    }
    
    if (this.isPlaying == true) {
        
		//OLD
		//var currentTimeToPrint = this.convertTimeToPrint(Math.ceil(this.music.currentTime));
        //this.controller.currentTime.innerHTML = currentTimeToPrint;
        //this.controller.track.style.width = Math.ceil(this.music.currentTime / this.music.duration * 100) + '%';
        //this.timer = setTimeout(function() { this.initTimer(); }.bind(this), 500);



		//NEW
		var currentTimeToPrint = this.convertTimeToPrint(Math.ceil(this.music.currentTime));
		if (currentTimeToPrint == "00:00") {
		
		} else {
			this.controller.currentTime.innerHTML = currentTimeToPrint;
		}

		var trackWidth = Math.ceil(this.music.currentTime / this.music.duration * 100) + '%';
	  
		if (trackWidth == "0%") {
			
		} else {
			this.controller.track.style.display = 'block';
			this.controller.track.style.width = trackWidth;
		}
        this.timer = setTimeout(function() { this.initTimer(); }.bind(this), 500);
		
		/**
		
		
		this.timer = setTimeout(function() { this.initTimer(); }.bind(this), 500);**/


    }
};

WMPlayer.prototype.stopTimer = function() {
    clearTimeout(this.timer);
};
