﻿function flashPlayer(id, flashvars){

    //private properties / varibles
    
    //
    
    //private functions
    var setCookie = function (name, value) {
        var expires = new Date();     //set new date object
        expires.setTime(expires.getTime() + (1000 * 60 * 60 * 24 * 30));     //set it 30 days ahead 
        document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString())
    }
    var readCookie = function (name) {
	    var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0;i < ca.length;i++) {
		    var c = ca[i];
		    while (c.charAt(0)==' ') c = c.substring(1,c.length);
		    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    }
	    return null;
    }
    var stringIsNullOrEmpty = function (string) {
        if (string == null) return true;
        if (string == "null") return true;
        if (string == "undefined") return true;
        if (string == "") return true;
        if (string == '') return true;
        return false;
    }
    //
    
    
    //public properties / varibles
    this.subs = false;
    this.lang = readCookie('lang');
    this.highRes = readCookie('highres');
    this.ads = false;
    this.video = new Object();
    this.autoplay = true;
    this.playListPosition = -1;
    //
  
    this.callback_GetRandomMovie = function (res){
        fPlayer.video = res.value;
        if (fPlayer.autoplay)
            fPlayer.playVideo();
        //VideoDetailsAjax.GenerateVideoDetailsById(fPlayer.video.VideoId, this.callback_GenerateVideoDetailsById);
    }
    
    this.callback_GenerateVideoDetailsById = function (res) {
        $j('#VideoDetailsContent').html(res.value);
        
    }

    
    this.callback_GetNextMovie = function (res){
        fPlayer.video = res.value.Video;
        fPlayer.playListPosition = res.value.Position;
        if (fPlayer.autoplay)
            fPlayer.playVideo();
        ChangeTabPlayList('0');
        //VideoDetailsAjax.GenerateVideoDetailsById(fPlayer.video.VideoId, this.callback_GenerateVideoDetailsById);
    }
    
    this.playPlsPosition = function (pos) {
        if (this.subs)
            __doPostBack('Subs:PlayPlsPosition', pos);
        else
            PlayerAjax.GetNextMovie(pos, this.callback_GetNextMovie);
    }
    
    this.playCurrPlsPosition = function (pos){
        if (this.subs)
            __doPostBack('Subs:PlayCurrPlsPosition', pos);
        else
            PlayerAjax.GetCurrMovie(pos, this.callback_GetNextMovie);
    }
    
    this.playerStateChange = function (obj){
        if (obj.newstate == 'COMPLETED') {
            if (this.subs){
                switch (this.playListPosition){
                    case -1: __doPostBack('Subs:GetRandomMovie', this.video.VideoId); break;
                    case -2: __doPostBack('Subs:GetRecomendedMovie', this.playListPosition); break;
                    case -3: __doPostBack('Subs:GetNextSearched', this.video.VideoId); break;
                    default: __doPostBack('Subs:GetNextMovie', this.playListPosition); break;
                }
            }else{
                switch (this.playListPosition){
                    case -1: PlayerAjax.GetRandomMovie(this.video.VideoId, this.callback_GetRandomMovie); break;
                    case -2: PlayerAjax.GetRecomendedMovie(this.playListPosition, this.callback_GetRandomMovie); break;
                    case -3: PlayerAjax.GetNextSearched(this.video.VideoId, this.callback_GetRandomMovie); break;
                    default: PlayerAjax.GetNextMovie(this.playListPosition, this.callback_GetNextMovie); break;
                }
            }
        }
    }
    
    this.playHighRes = function (highres){
        this.highRes = highres;
        setCookie('highres', highres);
        if (this.autoplay)
            this.playVideo();
    }
    
    this.changeLang = function (){
        switch (this.lang){
            case 'en': this.lang = 'pl'; break;
            default: this.lang = 'en'; break;
        }
        setCookie('lang', this.lang);
        if (this.autoplay)
            this.playVideo();
    }
    
    //creates flash player object with specified flashvars
    var p = new SWFObject('/player.swf', 'flashPlayer', '490', '315', '9');
    p.addParam('allowfullscreen', 'true');
    p.addParam('allowscriptaccess', 'always');
    p.addParam('flashvars', flashvars);
    p.write(id);
    //
    
    //public functions
    this.loadVideo = function (play){
        
        if (this.video == null) return;
        
        
        //highRes handling
        $j('#LowRes').hide();
        $j('#HighRes').hide();
        if (!stringIsNullOrEmpty(this.video.VideoUrlHigh)){
            if (this.highRes)
                $j('#LowRes').show();
            else
                $j('#HighRes').show();
        } 
        //
        
        //lang handling
        var tmpLang = this.lang;
        if (!stringIsNullOrEmpty(this.video.VideoUrlEn) || !stringIsNullOrEmpty(this.video.VideoUrlHighEn)){
            $j('#LangChange').show();
            switch (this.lang){
                case 'en':
                    $j('#LowRes').html('Watch in low resolution');
                    $j('#HighRes').html('Watch in heigh resolution');
                    $j('#LangChangeImg').attr({ src: '/img/pl_flag.png'});
                    break;
                case 'pl':
                    $j('#LowRes').html('Oglądaj w niskiej rozdzielczości');
                    $j('#HighRes').html('Oglądaj w wysokiej rodzielczości');
                    $j('#LangChangeImg').attr({ src: '/img/uk_flag.png'});
                    break;
            }
        }else{
            tmpLang = 'pl';
            $j('#LangChange').hide();
            $j('#LowRes').html('Oglądaj w niskiej rozdzielczości');
            $j('#HighRes').html('Oglądaj w wysokiej rodzielczości');
        }
        
        $j('#VideoDetailsContent').html(this.video.Description);
        
        //Export playListPosition to code behind using asp:HiddenField value
        $j('input[id$=PlsPos]').val(this.playListPosition);
        //
        
        //subs handling
        if (!stringIsNullOrEmpty(this.video.SubtitlesRbl)){
            $j('#SubtitlesDiv').show();
            $j('#LangDiv').html(this.video.SubtitlesRbl);
        }else{
            $j('#SubtitlesDiv').hide();
        }
        //

        //playlist handling
        if (this.playListPosition > 0){
            $j('.dvPlayListGray').attr('class', 'dvPlayListWhite');
            $j('.dvPlayListWhite').eq(this.playListPosition - 1).attr('class', 'dvPlayListGray');
        }
        //
        
        //play handling
            if (this.highRes)
                switch (tmpLang){
                    case 'en': 
                        this.player.sendEvent('LOAD', {type:'video', image:this.video.ImageUrl, file:this.video.VideoUrlHighEn}); break;
                    default:
                        this.player.sendEvent('LOAD', {type:'video', image:this.video.ImageUrl, file:this.video.VideoUrlHigh}); break;
                }
                else
                switch (tmpLang){
                    case 'en': 
                        this.player.sendEvent('LOAD', {type:'video', image:this.video.ImageUrl, file:this.video.VideoUrlEn}); break;
                    default:
                        this.player.sendEvent('LOAD', {type:'video', image:this.video.ImageUrl, file:this.video.VideoUrl}); break;
                }
			if (play)
				this.player.sendEvent('PLAY', true);
        //
    }
    
	this.playVideo = function (){
		this.loadVideo(true);
		
	}
	//
} 
        

