﻿
function Go(link) {
    document.location=link;
	return true;
}

function CGo(link, mesg) {
    if (confirm(mesg))
	{
		document.location=link;
		return true;
	}
}

function _v(id) {
    return document.getElementById(id);
}

function SHBl( id ) {
    _v( id ).style.display = ('none' == _v( id ).style.display) ? 'block' : 'none';
}

function Show(img, num, w, h) {
    if (!w) {
	    w = 100;	
	}
	if (!h) {
	    h = 100;	
	}
	window.open( '/im.php?im='+img,'popup','resizable=1,left=0,top=0, width='+w+', height='+h+', scrollbars=yes'); 	
}


function GetInf( mod, what, ar, block, rblock, wait, after_id ) {
    _v(wait).style.display = 'block';   
	var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
		    if ( null != req.responseJS.q ) {
			    _v( block ).innerHTML =  req.responseJS.q;
			}
			if ( null != req.responseJS.eq ) {
			    _v( rblock).innerHTML = req.responseJS.eq; 
			}	
			if (null != _v(wait)) {
			    _v(wait).style.display = 'none';
			}
			
			if (after_id) {
			    ShowIm( after_id );
			}
        }
    }
    req.open('get', mod + what, true);
	req.send( { ar: ar } );
}

function ShowIm( photo_id ) { 
	/** check page */ 
    if (ii[photo_id][1] < (gpage-1)*pcnt) {
        /** prev */ 
    	$npage = parseInt( ii[photo_id][1] / pcnt ) + 1;    	
		ShowPage(item_id, $npage/**gpage-1*/, photo_id);
  	} else if (ii[photo_id][1] >= gpage*pcnt) {
	    /** next */
  		$npage = parseInt( ii[photo_id][1] / pcnt ) + 1;  
  		ShowPage(item_id, $npage/**gpage+1*/, photo_id);
	}
	else {
    	if (null != _v('itm'+jj[num]))
		    _v('itm'+jj[num]).className = '';
		
    	if (null != _v('itm'+photo_id)) {		
	        _v('itm'+photo_id).className = 'act';
    	    if (!bresize) {
			    _v('mim').src = '/files/images/mresize/' + ii[photo_id][0];
			    _v('mih').href = '/base/photo/show'+photo_id;
			    GetCom('/base/dogs/', 'getcom/', {id: photo_id, id_dog: item_id}, 'comml', 'ititle', 'wait', 'ph_hdr' );
			} else {
			    _v('mim').src = '/files/images/bresize/' + ii[photo_id][0];
				GetCom('/base/photo/', 'getcom/', {id: photo_id}, 'comml', 'ititle', 'wait', 'ph_hdr' );
			}
            num = ii[photo_id][1]; 	
	        _v('prev').href = (1*num > 0) ? 'javascript:ShowIm(\''+jj[1*num-1]+'\');' : 'javascript://;'; 
            _v('previ').src = (1*num > 0) ? '/includes/templates/images/ar_left.gif' : '/includes/templates/images/ar_left_n.gif';
	        _v('next').href = (null != jj[1*num+1]) ? 'javascript:ShowIm(\''+jj[1*num+1]+'\');' : 'javascript://;'; 
            _v('nexti').src = (null != jj[1*num+1]) ? '/includes/templates/images/ar_right.gif' : '/includes/templates/images/ar_right_n.gif';
        }
	}
}


function GetCom( mod, what, ar, block, rblock, wait, tblock ) {
    _v(wait).style.display = 'block';   
	var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
		    if ( null != req.responseJS.q ) {
			    _v( block ).innerHTML =  req.responseJS.q;
			}
			if ( null != req.responseJS.eq && null != _v( rblock) ) {
			    _v( rblock).innerHTML = req.responseJS.eq; 
			}
			if ( null != req.responseJS.sq && null != _v(tblock) ) { 
			    _v( tblock).innerHTML = req.responseJS.sq;  
			}
			if (null != _v(wait)) {
			    _v(wait).style.display = 'none';
			}
        }
    }
    req.open('get', mod + what, true);
	req.send( { ar: ar } );
}


function doClipboard(txt) {
	if(window.clipboardData) { 
        window.clipboardData.setData("Text", txt);
    }
    else {
        var fc = document.getElementById("flashCopier");
        if(!fc) { 
            fc = document.createElement("div"); 
            fc.id = "flashCopier"; 
            document.body.appendChild(fc); 
        }
		fc.innerHTML = '<embed src="/includes/templates/js/clipboard.swf" FlashVars="clipboard=' + 
            encodeURIComponent(txt) +
            '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    }
}


function moveCaretToEnd(e, obj) { 
    var characterCode;
    if(e && e.which) { 
        e = e
        characterCode = e.which;
    } else {
        e = event;
        characterCode = e.keyCode;
    }

	if (characterCode==13) { 
	    /** wee need only for opera :) */ 
	    if(typeof(obj.selectionStart) == "number") { 
		    obj.selectionStart = obj.selectionEnd = obj.value.length; 
		} 
	}
} 


//**************************************************************//
//          Comments
//**************************************************************//
function AddCom(module, tblname, item_id, item_uid, story, err_list, res_list, waitb, id ) {
	_v(waitb).innerHTML = 'Подождите...';
	var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {  
			_v(waitb).innerHTML = 'Оставить комментарий';
			if (null != req.responseJS.q && req.responseJS.q) {
                _v(err_list).style.color = 'green';	
                _v( err_list ).innerHTML = (null == id || !id) ? 'Комментарий добавлен' : 'Комментарий изменен'; 				
			    _v(res_list).innerHTML = req.responseJS.q;
			}
			if (null != req.responseJS.eq && req.responseJS.eq) {
			    _v(err_list).style.color = 'red';		
                _v( err_list ).innerHTML = req.responseJS.eq;  
            }			
        }
    }        
    req.caching = false;
    req.open('GET', '/base/blog/ajax/', true);
	var id = (null == id) ? 0 : id;
    req.send( {act: 'addcom', module:module, tblname: tblname, fm: {item_id: item_id, item_uid: item_uid, story: story, id: id}} );
}

function AddComMulti(module, tblname, item_id, item_uid, parent, story, err_list, res_list, waitb, id ) {

	var req = new JsHttpRequest(); 
    req.onreadystatechange = function() {
        if (req.readyState == 4) {  
			if (null != req.responseJS.q && req.responseJS.q) {
                _v(err_list).style.color = 'green';	
                _v( err_list ).innerHTML = (null == id || !id) ? 'Комментарий добавлен' : 'Комментарий изменен'; 				
			    _v(res_list).innerHTML = req.responseJS.q;
			}
			if (null != req.responseJS.eq && req.responseJS.eq) {
			    _v(err_list).style.color = 'red';		
                _v( err_list ).innerHTML = req.responseJS.eq;  
            }			
        }
    }	
    req.caching = false;
    req.open('GET', '/base/blog/ajax/', true);
	var id = (null == id) ? 0 : id;
    req.send( {act: 'addcom', module:module, tblname: tblname, fm: {is_multi: '1', item_id: item_id, item_uid: item_uid, parent: parent, story: story, id: id}} );
}


function ShowComEdit(module, tblname, item_id, res_list, id) {
    alert(id);
}

function ShowComDel(module, tblname, item_id, res_list, id) {
    if (confirm('Удалить комментарий?')) {
	    var req = new JsHttpRequest();
        req.onreadystatechange = function() {
            if (req.readyState == 4) {  
			    if (null != req.responseJS.q && req.responseJS.q) {  	
			        _v(res_list).innerHTML = req.responseJS.q;
			    }		
            }
        }        
        req.caching = false;
        req.open('GET', '/base/blog/ajax/', true);
	    var id = (null == id) ? 0 : id;
        req.send( {act: 'delcom', module:module, tblname: tblname, fm: {item_id: item_id, id: id}} );
	}
}

function ShowComDelMulti(module, tblname, item_id, res_list, id) {
	if (confirm('Удалить комментарий?')) {
	    var req = new JsHttpRequest();
        req.onreadystatechange = function() {
            if (req.readyState == 4) {  
			    if (null != req.responseJS.q && req.responseJS.q) {  	
			        _v(res_list).innerHTML = req.responseJS.q;
			    }		
            }
        }        
        req.caching = false;
        req.open('GET', '/base/blog/ajax/', true);
	    var id = (null == id) ? 0 : id;
        req.send( {act: 'delcom', module:module, tblname: tblname, fm: {item_id: item_id, is_multi: '1', id: id}} );
	}
}
