/* vim: set ft=javascript expandtab tabstop=4 shiftwidth=4: */
/*

  예제:
   <script type="text/javascript" src="http://img.malltb.com/player/player.js"></script>
   <script type="text/javascript">
   new MTBPlayer('128735302', {autoplay:false} );
   </script>
  설명:
   new MTBPlayer(string video_id, [object options]);

   string video_id : 비디오 고유번호. 앞에 !를 붙이면 'temp'모드로 동작한다. !를 붙일땐 반드시 문자열로 써야한다.
   object options  : 옵션. 현재 skin, autoplay값을 지정할 수 있다.
                     skin은 default만 작동되고
                     autoplay는 bool로 true/false를 지정하면 된다. (문자열이 아님을 유의)

 */
var HOST_STATISTICS = 'http://upload.malltb.com/';

if (typeof HOST_STREAM == 'undefined') {
    var HOST_STREAM = 'http://stream.malltb.com/';
}
if (typeof HOST_WWW == 'undefined') {
    var HOST_WWW = 'http://www.malltb.com';
}

if (HOST_STREAM.slice(-1) != '/') {
    HOST_STREAM += '/';
}


if (typeof MTBPlayer != 'function') {
    var MTBPlayer = function(video_id, options) {
        this.options = {
          loc      : 'static',
          autoplay : false,   // or true
          skin     : 'default',
          size     : {width: 410, height: 342},
          volume   : 70,
          container: ''
        }

        // simulate Object.extend() in prototype.js
        options = options || {};
        for (var property in options) {
          this.options[property] = options[property];
        }
        HOST_STREAM = HOST_STREAM.replace('http://', '');
        HOST_STREAM = 'http://' + Math.random() + '.' + HOST_STREAM;

        if (video_id.toString().indexOf('!') != -1) {
            this.video_id = video_id.toString().substr(1);
            var url = HOST_STREAM + "data1/tmp/" + this.video_id + "/";
        } else {
            this.video_id = video_id.toString();
            var url = HOST_STREAM + "data" + parseInt(this.video_id.slice(-2)) + '/' + ('00000' + this.video_id).slice(-5, -2) + '/' + this.video_id.slice(0, -2) + '/';
        }

        this.pid = 'mtb_v' + this.video_id;

        // for shop main player. old version.
        if (this.options.skin.indexOf('main_') >= 0) {
            var url = 'http://img.malltb.com/player/' + this.options.skin + '.swf' + '?r=' + Math.random()
                    + '&url=' + url
                    + '&upload=' + HOST_STATISTICS
                    + '&uid=' + this.video_id
                    + '&autoPlay=' + this.options.autoplay.toString()
                    + '&volume=' + this.options.volume.toString();
            var wmode = ['<param name="wmode" value="transparent" />', ' wmode="transparent" '];
        }
        // for brand new players.
        else {
            var url = HOST_WWW + '/video/player/type/' + this.options.skin
                    + '/id/' + this.video_id
                    + '/loc/' + this.options.loc.toString()
                    + '/autoplay/' + this.options.autoplay.toString()
                    + '/volume/' + this.options.volume.toString();
            var wmode = ['', ''];
        }

        switch (this.options.skin) {
        case 'bbong':
        case 'bbong2':
        case 'pdove':
        case 'pdove2':
            this.options.size = {width: 420, height: 458};
            break;
        case 'shopdetail':
        case 'shopdetail2':
            this.options.size = {width: 400, height: 342};
            break;
        case 'nude':
        case 'prshop':
            this.options.size = {width: 400, height: 300};
            break;
        case 'dev' :
        case 'default':
        case 'default2':
        case 'malltb_black':
        case 'malltb_black2':
            this.options.size = {width: 400, height: 342};
            break;
        case 'malltb':
        case 'malltb2':
            this.options.size = {width: 400, height: 342};
            break;
        case 'detailimage':
            this.options.size = {width: 290, height: 285};
            break;
        case 'suyar':
            this.options.size = {width: 420, height: 410};
            break;
        
        }


        var html = 
            '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"'
          + ' width="' + this.options.size.width + '" height="' + this.options.size.height + '"'
          + ' id="' + this.pid + '">'
          + ' <param name="allowScriptAccess" value="always" />'
          + ' <param name="allowFullScreen" value="true" />'
          + ' <param name="movie" value="' + url + '" />'
          + ' <param name="quality" value="high" />'
          + wmode[0]
          + ' <embed src="' + url + '" allowFullScreen="true" allowScriptAccess="always" type="application/x-shockwave-flash"'
          + ' width="' + this.options.size.width + '" height="' + this.options.size.height + '"'
          + ' name="' + this.pid + '" id="' + this.pid + '"'
          + wmode[1]
          + ' pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';

        if (this.options.container.length) {
            document.getElementById(this.options.container).innerHTML = html;
        } else {
            document.write(html);
        }

        if (navigator.appName.indexOf("Microsoft") != -1) {
            return window[this.pid]
        }
        else {
            return document[this.pid]
        }
    }

    var MTBPlayerPopup = function(video_id) {
        window.open('http://img.malltb.com/player/popup.html?video_id=' + video_id, '', 'width=400,height=342');
    }
}
var openURL = function(url, width, height, scrollbars) {
    window.open(url, "", "width=" + width + ", height=" + height + ", scrollbars=" + scrollbars + "");
}


