var DomainName="zatup.ru";
//var EditorType='HTML';


function open_win(doc, w, h)
{
    window.open(doc, '', 'width='+w+',height='+h+', copyhistory=no,directories=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
}
function del_win(com,item_id,message,other)
{
    if (!message) message='';
    if (!other) other='';
    open_win('?com=del_window&ccom='+com+'&item_id='+item_id+'&message='+message+'&other='+other ,500,200);
}
function FindToPhoneImages()
{
    var c=document.getElementById('ContentPlace');
    if (c)
    {
        var is=c.getElementsByTagName('img');
        for(var i=0;i<is.length; i++)
        {
            img=is.item(i);
            //alert(img.style.pixelWidth);
            if (img.width>=128 && img.height>=60)
            {
                coo=getXY(img);
                showToPhoneIco(coo.x + img.width, coo.y, img.src);
                /*
                img.onmouseover=function (event)
                {
                    //alert(coo.x);
                }
                */
            }
        }
    }
}

var tooltips=new Array();
function showToPhoneIco(x, y, img_path)
{
    x+=5;
    //alert(img_path);
    var c=document.getElementById('LayerContainer');
    if (c)
    {
        c.innerHTML+= "<div style='position: absolute; top: "+y+"; left:"+x+"'>"
            +"<a href=\"javascript:;\"  title=\"Закачать картинку на телефон\"  onClick=\"javascript:window.open('http://www.smsonline.ru/s/s.cgi?id=738&f="+img_path+"','smsonline'+parseInt(Math.random()*10000),'width=780,height=560,status=1,resize=0'); return false;\"><img src='http://www.smsonline.ru/i/s17.gif' width='24' height='24' border='0' />"
            +"<br/><span style='font-size: 10px'>На тел.</span></a>"
            +"</div>";
        //tooltips[tooltips.length] = new YAHOO.widget.Tooltip("tt", { context:img_path } );
    }
}

function getXY(o) {
  var c = { x:o.offsetLeft, y:o.offsetTop }, p = o.offsetParent;
  while (p) {
    c.x += p.offsetLeft;
    c.y += p.offsetTop;
    p    = p.offsetParent;
  }
  return c;
}





function CreateHTMLEditor(dataName)
{
    SetCookie('EditorType','HTML');
    fe(dataName+"_EditorType").value='HTML';
    var oEd=FCKeditorAPI.GetInstance( dataName+'_Visual' ) ;

    var tArea=fe(dataName+'_HTML');
    tArea.value=oEd.GetXHTML();


    fe('editorContainer_'+dataName+'_HTML').style.display='';
    fe('editorContainer_'+dataName+'_Visual').style.display='none';
    fe("EditorTypeButton_"+dataName+"_HTML").className='EditorTypeButton_Active';
    fe("EditorTypeButton_"+dataName+"_Visual").className='EditorTypeButton_Passive';
    tArea.focus();
}


function CreateVisualEditor(dataName)
{
    SetCookie('EditorType','Visual');
    fe(dataName+"_EditorType").value='Visual';
    var oEd=FCKeditorAPI.GetInstance( dataName+'_Visual' ) ;

    var tArea=fe(dataName+'_HTML');
    //var txt=tArea.value.replace(/\r\n/,'<br />');
    oEd.SetHTML(tArea.value.replace(/\n/g,'<br />'));

    fe('editorContainer_'+dataName+'_HTML').style.display='none';
    fe('editorContainer_'+dataName+'_Visual').style.display='';
    fe("EditorTypeButton_"+dataName+"_HTML").className='EditorTypeButton_Passive';
    fe("EditorTypeButton_"+dataName+"_Visual").className='EditorTypeButton_Active';

}


function CreateBlogPostEditor(dataName, width, height, tmpContainerId)
{
    var isVisual=GetCookie('EditorType')=='Visual';

    document.write('<input type="hidden" id="'+dataName+'_EditorType" name="'+dataName+'_EditorType" value="'+(isVisual?'Visual':'HTML')+'"/>');

    document.write('<div align="right"><a id="EditorTypeButton_'+dataName+'_Visual" class="EditorTypeButton_'+(isVisual?'Active':'Passive')+'" href="javascript:CreateVisualEditor(\''+dataName+'\');">[Визуальный редактор]</a>&#xA0;&#xA0;&#xA0;<a class="EditorTypeButton_'+(!isVisual?'Active':'Passive')+'"  href="javascript:CreateHTMLEditor(\''+dataName+'\');" id="EditorTypeButton_'+dataName+'_HTML">[HTML]</a></div>');

    document.write('<div id="editorContainer_'+dataName+'_HTML" style="display:'+(isVisual?'none':'')+'">');
    document.write('<textarea name="'+dataName+'_HTML" id="'+dataName+'_HTML" style="width: '+width+'; height: '+height+';"></textarea>');
    document.write('</div>');

    document.write('<div id="editorContainer_'+dataName+'_Visual" style="display:'+(isVisual?'':'none')+'">');
    document.write('<textarea name="'+dataName+'_Visual" id="'+dataName+'_Visual" style="width: '+width+'; height: '+height+';"></textarea>');
    document.write('</div>');


//  if (!isVisual) return;

    var oFCKeditor = new FCKeditor( dataName + '_Visual', width, height) ;
    oFCKeditor.BasePath = '/editor/' ;

    var tmpcont=document.getElementById(tmpContainerId);
    if (tmpcont)
    {
        //oFCKeditor.Value = tmpcont.value ;
        fe(dataName+"_Visual").value=tmpcont.value;
        fe(dataName+"_HTML").value=tmpcont.value;
        tmpcont.value='';
    }

    oFCKeditor.Config['AutoDetectLanguage']=false;
    oFCKeditor.Config['DefaultLanguage']='ru';
    oFCKeditor.Config['CustomConfigurationsPath'] = '/js/blogeditor.js?v=3';
    oFCKeditor.ToolbarSet='BLOG';

    //oFCKeditor.Create() ;
    oFCKeditor.ReplaceTextarea();
    //oFCKeditor.MakeEditable();

}

/**
* document.getElementById
*
*/
function fe(id)
{
    return document.getElementById(id);
}


function FLVPlayer(videoUrl, imageUrl, containerId, reqCount)
{
        var uid = Math.round(100000*Math.random());
        var vId;
        var timeOut = 500;

        if (!reqCount)
        {
            reqCount=0;
        }

        if (reqCount>3)
        {
            return;
        }

        if (!imageUrl || imageUrl=="")
        {
                imageUrl="/img/zatup_ru_rolik3.jpg";
        }

        if (!containerId)
        {
            vId="VideoPlayer_"+videoUrl.replace(/[\/\.]/g,'_')+"_"+uid;
            var vurlMatch = videoUrl.match(/\/(\d+)\/(\d+)_(\w+)\.flv$/);
            var imgNum = imageUrl.match(/_(\d)\.jpg$/)[1];
            swfUrl = 'http://www.zatup.ru/video/'+vurlMatch[1]+"/"+vurlMatch[2]+"/"+imgNum+"/"+vurlMatch[3]+".swf";

            liRuCode = "[flash=450,350,"+swfUrl+"]";

            swfPlainCode='<OBJECT width="450" height="350">';
            swfPlainCode+='<PARAM name="movie" value="'+swfUrl+'" />';
            swfPlainCode+='<PARAM name="wmode" value="window" />';
            swfPlainCode+='<PARAM name="allowFullScreen" value="true"></PARAM>';
            swfPlainCode+='<EMBED src="'+swfUrl+'" type="application/x-shockwave-flash" wmode="window" width="450" height="350" allowFullScreen="true" /></OBJECT>';

            ljCode = "<lj-embed>"+swfPlainCode+"</lj-embed>";


            document.write(
            '<div class="VideoPlayer" style="width: 450; height: 350; background: url(/img/zatup-kino.gif) no-repeat #DDDDDD; background-position: center center; text-align: center;" id="'+vId+'"><b><a href="javascript:FLVPlayer(\''+videoUrl+'\',\''+imageUrl+'\', \''+vId+'\')">Показать</a></b></div>'
            + '<div class="download_block"><img src="/img/applications-internet.png" width="16" height="16" alt="В блоги" class="png" align="absmiddle" /> <a href="javascript:void(0);" onClick="document.getElementById(\''+vId+'_vcodes\').style.display=\'\';">Код для блогов</a> | <a href="'+videoUrl+'">скачать (flv)</a></div>'
            + '<div style="display:none;" id="'+vId+'_vcodes" class="blogcodes_block">'
            +   '<table>'
            +   '<tr><td>Код для LiveInternet:</td><td width="100%"><input type="text" onfocus="this.select()" value="'+liRuCode+'"></td></tr>'
            +   '<tr><td>Код Flash:</td><td width="100%"><input type="text" onfocus="this.select()" value="'+swfPlainCode.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")+'"></td></tr>'
            +   '<tr><td>Код ЖЖ:</td><td width="100%"><input type="text" onfocus="this.select()" value="'+ljCode.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")+'"></td></tr>'
            +   '</table>'
            + '</div>'
            );
//            alert(swfPlainCode.replace("<","&lt;").replace(">","&gt;").replace('"',"&quot;"));
        }
        else
        {
            vId=containerId;
            timeOut=1;
        }

        window.setTimeout(
                function (){
                    var w = 450;
                    var h = 350;
                    var so = new SWFObject("/scripts/flv/flvplayer.swf?v1", vId+"_obj", w, h, "7", "#FFFFFF");
                    so.addParam("wmode","transparent");
                    so.addParam("allowfullscreen","true");
                    so.addVariable("image", imageUrl);
                    so.addVariable("file", videoUrl);
                    so.addVariable("autostart","false");
                    so.addVariable("logo", "/img/videologo2.png");
                    so.addVariable("link", "http://www.zatup.ru/");
                    so.addVariable("showdigits", "false");
                    so.addVariable("showicons", "true");
                    so.addVariable("height", h);
                    so.addVariable("width", w);
		    so.addVariable("streamscript", "lighttpd");
                    so.addVariable("displayheight", h);
                    so.addVariable("usefullscreen", "true");

                    var ret = so.write(vId);
                    var el = document.getElementById(vId);
                    if (el.innerHTML=='')
                    {
                        window.setTimeout(so.write(vId), 1000);
                    }

                    if (false)
                    {
                        window.setTimeout(function(){ FLVPlayer(videoUrl, imageUrl,  containerId, ++reqCount)},  100);
                    }

            }, timeOut);

}

function CreateFLVPlayer(videoFile, widthMovie, heightMovie, isSMSDownload, isDownloadUrl)
{

//    if (!widthMovie) widthMovie=320;
//    if (!heightMovie) heightMovie=240;
    if (!widthMovie) widthMovie=450;
    if (!heightMovie) heightMovie=350;
    if (!isDownloadUrl) isDownloadUrl=true;
    document.write('<object type="application/x-shockwave-flash" width="'+widthMovie+'" height="'+heightMovie+'"'
                    +'wmode="transparent" data="/scripts/flv/flvplayer.swf?autostart=true&image=/img/zatup_ru_rolik3.jpg&file='+videoFile+'">'
                    +'<param name="movie" value="/scripts/flv/flvplayer.swf?autostart=true&image=/img/zatup_ru_rolik3.jpg&file='+videoFile+'" />'
                    +'<param name="wmode" value="transparent" />'
                    +'</object>');

    if (isSMSDownload==null) isSMSDownload=true;

    var videoURL='http://www.zatup.ru/'+videoFile;

    if (isDownloadUrl)
    {
        document.write("<br /><br /><div><a href='"+videoURL+"'>Скачать ролик</a></div>");
    }

    if (false && isSMSDownload)
    {
            document.write('<br /><div class="SMSVideoDownload"><sc' + 'ript language=JavaScript src="http://video.smsonline.ru/jv.cgi?id=738&f=' +
            escape(videoURL) + '&rnd=' + parseInt(Math.random()*1000000) + '"></sc' + 'ript></div>');
    }
}


//var Cookies= new Array();
//alert(document.cookie);


/**
* @param String name
* @param String value
* @param String expires example: Thu, 01-Jan-70 00:00:01 GMT
*/
function SetCookie(name, value,expires)
{
     document.cookie = name + "=" + escape(value) +
//                "; expires=" +
                "; path=/" +
                "; domain=."+DomainName;
}

function GetCookie(name)
{
    var beg = document.cookie.indexOf(name+"=");
    if(beg==-1)
        return false;
    var end = document.cookie.indexOf(";", beg + name.length);
    if(end==-1)
        end = document.cookie.length;
    return unescape(document.cookie.substring(beg + name.length + 1, end));
}
