/**********************************************************************/
/***             Quotes Collection                                  ***/
/**********************************************************************/

var quotcoll_requrl, quotcoll_nexquote, quotcoll_loading, quotcoll_errortext;

function quotescollection_init(requrl, nextquote, loading, errortext)
{
	quotcoll_requrl = requrl;
	quotcoll_nextquote = nextquote;
	quotcoll_loading = loading;
	quotcoll_errortext = errortext;
}

function quotescollection_refresh(instance, exclude, show_author, show_source)
{
    // function body defined below
	var mysack = new sack( quotcoll_requrl );

	mysack.execute = 1;

	mysack.method = 'POST';
	mysack.setVar("refresh", instance);
	mysack.setVar("exclude", exclude);
	mysack.setVar("show_author", show_author);
	mysack.setVar("show_source", show_source);

	mysack.onError = function() { document.getElementById('quotescollection_randomquote-'+instance).innerHTML = quotcoll_errortext; };
	mysack.onLoading = function() { document.getElementById('quotescollection_nextquote-'+instance).innerHTML = quotcoll_loading; };
	mysack.onLoaded = function() { document.getElementById('quotescollection_nextquote-'+instance).innerHTML = '<a style="cursor:pointer" onclick="quotescollection_refresh('+instance+','+exclude+','+show_author+','+show_source+');">' + quotcoll_nextquote + ' &raquo</a>'; };
//	mysack.onInteractive = function() { document.getElementById('quotescollection_nextquote-'+instance).innerHTML += '...'; };
//	mysack.onCompletion = function() { document.getElementById('quotescollection_randomquote-'+instance).innerHTML = mysack.response; };
	mysack.runAJAX();
	return true;
}


/**********************************************************************/
/***             Bookmarks                                          ***/
/**********************************************************************/


function NewBookmarker() {
    this.domain=location.href+'/';
    this.domain=this.domain.substr(this.domain.indexOf('://')+3);
    this.domain=this.domain.substr(0,this.domain.indexOf('/'));
    this.location=false;

    this.selection=function() {
        var sel;
        if (window.getSelection) sel=window.getSelection();
        else if (document.selection) sel=document.selection.createRange();
        else sel='';
        if (sel.text) sel=sel.text;
        return encodeURIComponent(sel);
    }

    this.redirect=function() {
        if (this.location) location.href=this.location;
        this.location=false;
    }

    this.log=function(i, document_url, product_id) {
        /* отслеживаем популярность сервисов закладок и страниц попадающих в закладки */
        this.location=this.url(i, document_url, '');
        setTimeout('bookmarker2.redirect()', 1000); //2000
        log_url = '/pop/bookmarker/save_bm/?d='+document_url+'&s='+this.system_alias(i) + '&p='+product_id;
        //window.open(log_url, '', '');
        //return;
        var scr=document.createElement('script');
        scr.type='text/javascript';
        scr.src=log_url;
        document.body.appendChild(scr);
    }

    this.url=function(system, document_url, document_title) {
        var url = '';
        var title = '';
        if (document_url>""){
            url=document_url;
        } else {
            url=location.href;
        }
        if (document_title>""){
            title = document_title;
        } else {
            title = document.title;
        }
        url=encodeURIComponent(url);
        title = encodeURIComponent(title);
        switch (system) {
            case 1: return 'http://bobrdobr.ru/addext.html?url='+url+'&title='+title;
            case 2: return 'http://memori.ru/link/?sm=1&u_data[url]='+url+'&u_data[name]='+title;
            case 3: return 'http://www.mister-wong.ru/index.php?action=addurl&bm_url='+url+'&bm_description='+title;
            case 4: return 'http://del.icio.us/post?v=4&noui&jump=close&url='+url+'&title='+title;
            case 5: return 'http://zakladki.yandex.ru/userarea/links/addfromfav.asp?bAddLink_x=1&lurl='+url+'&lname='+title;
            case 6: return 'http://www.google.com/bookmarks/mark?op=add&bkmk='+url+'&title='+title;
            case 7: return 'http://text20.ru/add/?source='+url+'&title='+title+'&text='+this.selection();
            case 8: return 'http://pisali.ru/load_article/';
            case 9: return 'http://news2.ru/add_story.php?url='+url;
            case 10: return 'http://smi2.ru/add/';
            case 11: return 'http://myscoop.ru/add/?URL='+url+'&title='+title;
            case 12: return 'http://moemesto.ru/post.php?url='+url+'&title='+title;
            case 13: return 'http://www.ruspace.ru/index.php?link=bookmark&action=bookmarkNew&bm=1&url='+url+'&title='+title;
            case 14: return 'http://www.100zakladok.ru/save/?bmurl='+url+'&bmtitle='+title;
            case 15: return 'http://rumarkz.ru/bookmarks/?action=add&popup=1&address='+url+'&title='+title;
            case 16: return 'http://www.vaau.ru/submit/?action=step2&url='+url;
        }
    }

    this.system_alias=function(system) {
        switch (system) {
            case 1: return 'bobrdobr.ru';
            case 2: return 'memori.ru';
            case 3: return 'mister-wong.ru';
            case 4: return 'del.icio.us';
            case 5: return 'zakladki.yandex.ru';
            case 6: return 'www.google.com';
            case 7: return 'text20.ru';
            case 8: return 'pisali.ru';
            case 9: return 'news2.ru';
            case 10: return 'smi2.ru';
            case 11: return 'myscoop.ru';
            case 12: return 'moemesto.ru';
            case 13: return 'ruspace.ru';
            case 14: return '100zakladok.ru';
            case 15: return 'rumarkz.ru';
            case 16: return 'vaau.ru';
        }
    }

    this.hide=function() {
        if (this.timeout) clearTimeout(this.timeout);
        document.getElementById('bookmarker').style.visibility='hidden';
    }

    this.show=function(element) {
        if (this.timeout) clearTimeout(this.timeout);
        var left=0,top=0;
        var style=document.getElementById('bookmarker').style;
        while (element) {
            left+=element.offsetLeft;
            top+=element.offsetTop;
            element=element.offsetParent;
        }
        style.left=left+'px';
        style.top=(top+16)+'px';
        style.visibility='visible';
    }

    this.init=function(document_url, document_title, btn_format, product_id) {
        var titles=new Array(
                '&#1041;&#1086;&#1073;&#1088;&#1044;&#1086;&#1073;&#1088;',
                'Memori',
                '&#1052;&#1080;&#1089;&#1090;&#1077;&#1088; &#1042;&#1086;&#1085;&#1075;',
                'del.icio.us',
                '&#1071;&#1085;&#1076;&#1077;&#1082;&#1089;.&#1047;&#1072;&#1082;&#1083;&#1072;&#1076;&#1082;&#1080;',
                '&#1047;&#1072;&#1082;&#1083;&#1072;&#1076;&#1082;&#1080; Google',
                '&#1058;&#1077;&#1082;&#1089;&#1090; 2.0',
                '&#1055;&#1080;&#1089;a&#1083;&#1080;',
                'News2',
                '&#1057;&#1052;&#1048; 2',
                'AddScoop',
                '&#1052;&#1086;&#1105;&#1052;&#1077;&#1089;&#1090;&#1086;',
                'RuSpace',
                '&#1057;&#1090;&#1086; &#1047;&#1072;&#1082;&#1083;&#1072;&#1076;&#1086;&#1082;',
                'RUmarkz',
                '&#1042;&#1072;&#1072;&#1091;!'
            );
        if (!document.getElementById('bookmarker')) {
            var div=document.createElement('div');
            var style=div.style;
            div.id='bookmarker';
            div.innerHTML='<span style="margin:0;padding:0;width:130px;background:#eee;float:left;text-align:left;color:#666;font:normal 12px arial;line-height:20px">&nbsp;&#1044;&#1086;&#1073;&#1072;&#1074;&#1080;&#1090;&#1100; &#1074; закладки:</span>';
            div.innerHTML+='<span style="margin:0;padding:0;width:130px;background:#eee;float:left;text-align:right;color:#666;font:normal 12px arial;line-height:20px">&nbsp;</span><br />';
            for (i=0;i<8;i++) {
                div.innerHTML+='<a href="'+this.url(2*i+1, document_url, document_title)+'" onclick="bookmarker2.log('+(2*i+1)+', \''+document_url+'\', ' + product_id + ');return false"><img src="/img/px.gif" width="16" height="16" alt=" #" title="'+titles[2*i]+'" style="border:0;padding:0;margin:2px;float:left;background:url(/img/bookmarker_icons.gif) -'+(i*32)+'px"/></a><a href="'+this.url(2*i+1, document_url, document_title)+'" style="float:left;width:110px;color:#666;text-align:left;text-decoration:none;color:#666;font:normal 12px arial;line-height:20px;border:0" onmouseover="this.style.background=\'#f0f0f0\'" onmouseout="this.style.background=\'#fff\'" onclick="bookmarker2.log('+(2*i+1)+', \''+document_url+'\', ' + product_id + ');return false;">'+titles[2*i]+'</a><a href="'+this.url(2*i+2, document_url, document_title)+'" onclick="bookmarker2.log('+(2*i+2)+', \''+document_url+'\', ' + product_id + ');return false;"><img src="/img/px.gif" width="16" height="16" alt=" #" title="'+titles[2*i+1]+'" style="border:0;padding:0;margin:2px;float:left;background:url(/img/bookmarker_icons.gif) -'+(i*32+16)+'px"/></a><a href="'+this.url(2*i+2, document_url, document_title)+'" style="float:left;width:110px;color:#666;text-align:left;text-decoration:none;color:#666;font:normal 12px arial;line-height:20px;border:0" onmouseover="this.style.background=\'#f0f0f0\'" onmouseout="this.style.background=\'#fff\'" onclick="bookmarker2.log('+(2*i+2)+', \''+document_url+'\', ' + product_id + ');return false">'+titles[2*i+1]+'</a><br />';
            }
            div.onmouseover=function() {if (bookmarker2.timeout) clearTimeout(bookmarker2.timeout)}
            div.onmouseout=function() {bookmarker2.timeout=setTimeout('bookmarker2.hide()',500)};
            style.position='absolute';
            style.visibility='hidden';
            style.maxWidth='262px';
            style.border='1px solid #aaa';
            style.background='#fff';
            style.font='normal 12px arial';
            style.lineHeight='20px';
            style.margin='0';
            style.padding='0';
            style.overflow='hidden';
            style.zIndex='1000';
            document.body.insertBefore(div,document.body.firstChild);
        }
        /*
        href="/bookmarker/add/" onclick="window.open(\'/bookmarker/add/?url=\'+encodeURIComponent(location.href)+\'&title=\'+encodeURIComponent(document.title),\'bookmarker\',\'scrollbars=yes,menubar=no,width=600,height=500,left='+(document.body.clientWidth/2-300)+',top='+(document.body.clientHeight/2-250)+',resizable=yes,toolbar=no,location=no,status=no\');return false;"
        */
        if (btn_format == 1) {
            document.write('<a class="services_link" href="javascript:void(0);" onmouseover="bookmarker2.show(this);" onmouseout="bookmarker2.timeout=setTimeout(\'bookmarker2.hide()\',500);">Добавить в закладки</a>');
        } else if (btn_format==2) {
            document.write('<a href="javascript:void(0);" onmouseover="bookmarker2.show(this);" onmouseout="bookmarker2.timeout=setTimeout(\'bookmarker2.hide()\',500);"><img src="/img/bookmarker_small.gif" width="16" height="16" alt="Добавить страницу в закладки" title="Добавить страницу в закладки" style="border:0;margin:0;padding:0"></a>');
        } else {
            document.write('<a href="javascript:void(0);" onmouseover="bookmarker2.show(this);" onmouseout="bookmarker2.timeout=setTimeout(\'bookmarker2.hide()\',500);"><img src="/img/bookmarker_btn.gif" width="136" height="16" alt="Добавить страницу в закладки" title="Добавить страницу в закладки" style="border:0;margin:0;padding:0"></a>');
        }
    }
}

function bookmarker(url, title, btn_format, product_id){
    bookmarker2=new NewBookmarker();
    bookmarker2.init(url, title, btn_format, product_id);
}

/**********************************************************************/


