﻿var swfUrl = "http://vimg.ifeng.com/swf/zuheVplayer_v1.87.swf";
function PlayItems(parentids)
{
    this.OldPlayIndex = 0;// 从第几条开始播
    var items = [];// 所有条目
    var ItemCount = GetItemCout();// 总视频数
    this.domain = "v";// 引用频道域名，ent,news,phtv,finance
    this.ShowDivId = "playerDiv";// flash播放器显示模块id
    this.swfWidth = '400';// flash播放器宽
    this.swfHeight = '300';// flash播放器高
    
    this.adUrl = "";// 定向广告地址
    this.preAdurl = "";// 定向前贴片广告地址
    this.AutoPlay = 'true';// 视频自动播放---只有设置为自动播放，this.FirstAutoPlay第一条才能设定是否自动播放
    this.FirstAutoPlay = 'true';// 第一条是否自动播放
    this.ClickPlay = 'false';// 是否为点击播放
    this.ADPlay = 'true';// 广告自动播放
    this.playitemcss = "";

    this.noRelatedPage = "";
    //this.canShare = "";
    
  	this.postUrl = "";  //海报
    this.adType = ""; // 默认为2 (专题播放，只有前贴片)，当有preAdurl有效时，会强制转换为0，即自定义广告配置.   =1时，表示内页

    this.ivbAdUrl = "";  //adType=0时  有效
    this.postAdUrl = "";//adType=0时  有效 
    this.pauseUrl = "";//adType=0时  有效 
    this.cornerAdUrl = "";  //adType=0时  有效
	  this.canShare = "true";
    
    // 获得播放条目数
    function GetItemCout()
    {
        var pid = parentids.split(",");

        for (var a = 0; a < pid.length; a++)// 计算节点数
        {
            var item = document.getElementById(pid[a]).getElementsByTagName("li");

            for (var i = 0; i < item.length; i++)
                items.push(item[i]);
        }

        var count = items.length;
        for (var i = 0; i < count; i++)
        {// 为所有item添加点击事件
            items[i].onclick = function()
            {
                for (var j = 0; j < count; j++)
                    if (this == items[j])
                        clickplay(j);
            };
        }

        return count;
    }
    // 播放
    this.ChangeStream = function(videoid, playindex, sClickPlay)
    {
		var sCp = sClickPlay;
		if(this.ClickPlay == sCp)
		{
			sCp = this.ClickPlay
		}

        if (navigator.userAgent.indexOf("iPad") != -1 || navigator.userAgent.indexOf("iPhone") != -1)// ipad,iphone
        {
            var url = "http://partner.itv.ifeng.com/IfengVideoSearch/getplayermsg.aspx?msg=" + videoid;
            getScript_cds(url, showhtml5video);
        }
        else
        {
            fo = new FlashWriter({
                        url : swfUrl,
                        width : this.swfWidth,
                        height : this.swfHeight,
                        id : 'fplay'
                    });
            fo.addVariable('guid', videoid);
            // fo.addVariable('from',this.domain);
            fo.addVariable('from', ph_get_ad_chl());
            if (sCp == 'true')// 点击播放
            {
                if (this.AutoPlay == 'true')// 点击播放自动播状态
                {
                    fo.addVariable("AutoPlay", "true");
                    fo.addVariable('forPlayNum', (parseInt(this.OldPlayIndex) + 1));// 下次播放第几条,
                }
                else
                    // 点击播放非自动播状态
                    fo.addVariable("AutoPlay", "true");
            }
            else
            // 自动播放
            {
                if (this.AutoPlay == 'true')// 是否自动播
                {
                    if (playindex == 0 && this.FirstAutoPlay == "false")// 第一条是否自动播放，除第一条外全自动播
                        fo.addVariable("AutoPlay", "false");
                    else
                    {
                        fo.addVariable("AutoPlay", "true");
                        
                    }
                    fo.addVariable('forPlayNum', (parseInt(this.OldPlayIndex) + 1));// 下次播放第几条,
                }
                else
                    fo.addVariable("AutoPlay", "false");
            }

            if (this.adUrl != "")
                fo.addVariable('ADURL', this.adUrl);// 广告定向投放用。如果没有定向广告，请用符号 //
                                                    // 注销此项。
            if(this.preAdurl !="")
			{
				fo.addVariable('preAdurl',this.preAdurl);// 前贴片广告定投地址
				fo.addVariable('adType', '0');
			}
			else
			{
				fo.addVariable('adType','2');
			}
				            
				            
				    if (this.noRelatedPage != "")
                fo.addVariable('noRelatedPage', this.noRelatedPage);//
                
            if (this.canShare != "")
                fo.addVariable('canShare', this.canShare);// 
                    
            fo.addVariable('writeby','webjs');//为了兼容以前的专题联播页面。如果source=webjs，表示是js渲染播放器，此时分享功能直接调用脚本方法；否则调用swf自身的分享方法
            fo.addVariable('width',p.swfWidth);
			fo.addVariable('height',p.swfHeight);
            fo.addVariable('ADPlay', this.ADPlay);
            fo.addVariable('postUrl', this.postUrl);// 自定义海报                
            fo.addVariable('ivbAdUrl', this.ivbAdUrl);
            fo.addVariable('postAdUrl', this.postAdUrl);
            fo.addVariable('pauseUrl', this.pauseUrl);
            fo.addVariable('cornerAdUrl', this.cornerAdUrl);
		// fo.addVariable('canShare',this.canShare);//zjt 控制是否显示分享按钮
            
            fo.addVariable("clickPlay", 'true');
            fo.addVariable("special", 'true');// 表示专题引用
				fo.addVariable('playerName', "VZTPlayer");
            fo.addParam('allowFullScreen', 'true');
            fo.addParam("wmode", "transparent");
            fo.addParam('allowScriptAccess', 'always');
	   
            fo.write(this.ShowDivId);
        }
    },

    // 播放条目轮换
    this.RenderPlay = function(swfindex)
    {
        var playindex = parseInt(swfindex) % ItemCount;
        var curritem = items[playindex];
        var olditem = items[this.OldPlayIndex];
        olditem.className = "";
        curritem.className = this.playitemcss;
        this.OldPlayIndex = playindex;
        this.ChangeStream(curritem.getAttribute("name"), playindex, this.ClickPlay); // JQ(curritem).attr("name")
    }, this.RenderPlayById = function(id)
    {
        var count = items.length;
        for (var i = 0; i < count; i++)
        {// 为所有item添加点击事件
            if (curritem.getAttribute("name") == id)
                this.RenderPlay(i);
        }
    }
}
function showhtml5video()
{
    if (typeof(playermsg) != "undefined")
    {
        var nextvideo = parseInt(p.OldPlayIndex) + 1;
        var strvideo = '';
        if (p.AutoPlay == 'true')// 是否自动播
            strvideo = '<video  src="' + playermsg.videoplayurl + '" width="' + p.swfWidth + '" height="' + p.swfHeight
                    + '" controls  id="player" onended="p.RenderPlay(' + nextvideo + ');"/>';
        else
            strvideo = '<video  src="' + playermsg.videoplayurl + '" width="' + p.swfWidth + '" height="' + p.swfHeight + '" controls  id="player" />';
        document.getElementById(p.ShowDivId).innerHTML = strvideo;
        if (p.AutoPlay == 'true' || p.ClickPlay == 'true')// 是否自动播
        {
            document.getElementById('player').load();
            document.getElementById('player').play();
        }
    }
}
function clickplay(swfindex)
{
    p.ClickPlay = 'true';// 视频自动播放
    p.RenderPlay(swfindex);
}
// swf调用的js
function swfplay(swfindex)
{
    p.RenderPlay(swfindex);
}
function getScript_cds(src, callback)
{
    var head = document.getElementsByTagName("head")[0];
    var js = document.createElement("script");
    js.setAttribute("src", src);
    js.onload = js.onreadystatechange = function()
    {
        if (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")
        {
            head.removeChild(js);
            if (callback)
                callback();
        }
    }
    head.appendChild(js);
}
function ph_get_ad_chl()
{
    var _domain = location.host;
    var _domain_array = _domain.split('.');
    if (_domain_array.length >= 3 && _domain.indexOf("ifeng.com") != -1)
        _domain = _domain_array[_domain_array.length - 3];
    return _domain;
}

function Play()
{
    this.OldPlayIndex = 0;// 从第几条开始播
    this.domain = "v";// 引用频道域名，ent,news,phtv,finance
    this.ShowDivId = "playerDiv";// flash播放器显示模块id
    this.swfWidth = '400';// flash播放器宽
    this.swfHeight = '300';// flash播放器高
  
    this.adUrl = "";// 定向广告地址
	this.preAdurl = "";// 定向前贴片广告地址
    this.AutoPlay = 'true';// 视频自动播放---只有设置为自动播放，this.FirstAutoPlay第一条才能设定是否自动播放
    this.FirstAutoPlay = 'true';// 第一条是否自动播放
    this.ClickPlay = 'false';// 是否为点击播放
    this.ADPlay = 'true';// 广告自动播放
	this.ivbAdUrl = "";  //adType=0时  有效
    this.postAdUrl = "";//adType=0时  有效 
    this.pauseUrl = "";//adType=0时  有效 
    this.cornerAdUrl = "";  //adType=0时  有效
	  this.canShare = "true";
	
	  this.noRelatedPage = "";
    this.PlayID = function(videoid)
    {
        if (navigator.userAgent.indexOf("iPad") != -1 || navigator.userAgent.indexOf("iPhone") != -1)// ipad,iphone
        {
            var url = "http://partner.itv.ifeng.com/IfengVideoSearch/getplayermsg.aspx?msg=" + videoid;
            getScript_cds(url, showhtml5video);
        }
        else
        {
            fo = new FlashWriter({
                        url : swfUrl,
                        width : this.swfWidth,
                        height : this.swfHeight,
                        id : 'fplay'
                    });
            fo.addVariable('guid', videoid);
            // fo.addVariable('from',this.domain);
            fo.addVariable('from', ph_get_ad_chl());
            if (this.ClickPlay == 'true')// 点击播放
            {
                if (this.AutoPlay == 'true')// 点击播放自动播状态
                {
                    fo.addVariable("AutoPlay", "true");
                    fo.addVariable('forPlayNum', 0);// 下次播放第几条,
                }
                else
                    // 点击播放非自动播状态
                    fo.addVariable("AutoPlay", "true");
            }
            else
            // 自动播放
            {
                if (this.AutoPlay == 'true')// 是否自动播
                {
                    fo.addVariable("AutoPlay", "true");
                    fo.addVariable('forPlayNum', (parseInt(this.OldPlayIndex) + 1));// 下次播放第几条,
                }
                else
                    fo.addVariable("AutoPlay", "false");
            }

            if (this.adUrl != "")
                fo.addVariable('ADURL', this.adUrl);// 广告定向投放用。如果没有定向广告，请用符号 //
                                                    // 注销此项。
            if(this.preAdurl !="")
			{
				fo.addVariable('preAdurl',this.preAdurl);// 前贴片广告定投地址
				fo.addVariable('adType', '0');
			}
			else
			{
				fo.addVariable('adType','2');
			}
			
			  if (this.noRelatedPage != "")
                fo.addVariable('noRelatedPage', this.noRelatedPage);//
                
        if (this.canShare != "")
                fo.addVariable('canShare', this.canShare);// 
                
                
            fo.addVariable('writeby','webjs');//为了兼容以前的专题联播页面。如果source=webjs，表示是js渲染播放器，此时分享功能直接调用脚本方法；否则调用swf自身的分享方法
            fo.addVariable('width',p.swfWidth);
			fo.addVariable('height',p.swfHeight);
            fo.addVariable('ADPlay', this.ADPlay);
            fo.addVariable('postUrl', this.postUrl);// 自定义海报
            fo.addVariable("clickPlay", 'true');           
            fo.addVariable('ivbAdUrl', this.ivbAdUrl);
            fo.addVariable('postAdUrl', this.postAdUrl);
            fo.addVariable('pauseUrl', this.pauseUrl);
            fo.addVariable('cornerAdUrl', this.cornerAdUrl);
			//fo.addVariable('canShare',this.canShare);//zjt 控制是否显示分享按钮
            
            fo.addVariable("special", 'true');// 表示专题引用
			fo.addVariable('playerName', "VZTPlayer");
            fo.addParam('allowFullScreen', 'true');
            fo.addParam("wmode", "transparent");
            fo.addParam('allowScriptAccess', 'always');
            fo.write(this.ShowDivId);
        }
    }
}

/*2011-04-15 bshare begin*/
function shareTo(site,pic,url,title,smallimg) {
	var vlink = url;//'http://v.ifeng.com/news/world/201101/57b5bddb-36b4-4178-90d3-0f96bad889af.shtml';
    var _url = encodeURIComponent(vlink);
	var vtitle = title;//'日本首相夫人直言下辈子不嫁菅直人';
    var _title = encodeURIComponent(vtitle);
    var _content = "";
    /*if (eval("_oFlv_c.Content != null")) {
        _content = encodeURIComponent(_oFlv_c.Content);
    }*/
    switch (site) {
      case "kaixin001":
        window.open("http://www.kaixin001.com/repaste/share.php?rurl=" + _url + "&rtitle=" + _title);
        break;
      case "renren":
        window.open("http://share.renren.com/share/buttonshare.do?link=" + _url + "&title=" + _title);
        break;
      case "weibo":
        var l = (screen.width - 440) / 2;
        var t = (screen.height - 430) / 2;
		//window.open("http://v.t.sina.com.cn/share/share.php?appkey=168486312&c=spr_web_bd_56_weibo&url=" + _url + "&title=" + _title + "&source=56&sourceUrl=http://www.56.com/&content=utf8&pic="+smallimg, "_blank", "toolbar=0,status=0,resizable=1,width=440,height=430,left=" + l + ",top=" + t);
        window.open("http://v.t.sina.com.cn/share/share.php?appkey=168486312&url=" + _url + "&title=" + _title + "&source=ifeng&sourceUrl=http://v.ifeng.com/&content=utf8&pic="+smallimg+"&ralateUid=1806128454", "_blank", "toolbar=0,status=0,resizable=1,width=440,height=430,left=" + l + ",top=" + t);
		break;
	 case "qqzone":
        window.open("http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=" + _url);
        break;
      case "qqweibo":
       var _appkey = encodeURI("f8ca1cd768da4529ab190fae9f1bf21d");//appkey
       var _pic = encodeURI(pic);
       var _site = "http://v.ifeng.com";
       var _u = "http://v.t.qq.com/share/share.php?title=" + _title + "&url=" + _url + "&appkey=" + _appkey + "&site=" + _site + "&pic=" + _pic;
       window.open(_u, "\u8F6C\u64AD\u5230\u817E\u8BAF\u5FAE\u535A", "width=700, height=680, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no");
        break;
      default:
        return false;
    }
}

function shareToTweet(t, z, id, p, type, s, d, e) {
    if (undefined == s) {
        s = screen;
    }
    if (undefined == d) {
        d = document;
    }
    if (undefined == e) {
        e = encodeURIComponent;
    }
    var f = "http://t.ifeng.com/interface.php?_c=share&_a=share&", u = z || d.location, pa = ["sourceUrl=", e(u), "&title=", e(t || d.title), "&pic=", e(p || ""), "&source=", e(id || ""), "&type=", e(type || 0)].join("");

    function a() {
        if (!window.open([f, pa].join(""), "", ["toolbar=0,status=0,resizable=1,width=640,height=481,left=", (s.width - 640) / 2, ",top=", (s.height - 480) / 2].join(""))) {
            u.href = [f, pa].join("");
        }
    }

    if (/Firefox/.test(navigator.userAgent)) {
        setTimeout(a, 0);
    } else {
        a();
    }
}
