// Copyright (c) 2006 Jute Softwares
// Java Script Utils for jute blog

function loadScript2(url){ url=root+"/"+url;var e = document.createElement("script"); e.src = url;e.type="text/javascript";document.getElementsByTagName("head")[0].appendChild(e);}
function loadScript(url) {
  url=root+"/"+url;
  document.writeln('<script language="JavaScript" type="text/javascript" src="'+ url + '"></script>');
}
loadScript("js/yui/yahoo.js");
loadScript("js/yui/dom.js");


function StringBuffer() {
    this.__strings__ = new Array();    
}

StringBuffer.prototype.append = function(str) {
    this.__strings__.push(str);
}

StringBuffer.prototype.toString = function() {
    return this.__strings__.join(""); 
}

function copy_url (path) {
    window.prompt('请复制下面的地址转载',path);
}

var cache = {};
function cache_put(key, value) {
	cache[key] = value;
}

function cache_get(key) {
	return cache[key];
}

function cache_remove(key) {
	var c = cache[key]; 
	cache[key] = null;
	return c;
}

function set_cookie( name, value, expires, path, domain, secure ) {
    var today = new Date();
    today.setTime( today.getTime() );
    if ( expires ){
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}

function get_cookie( name ) {
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) &&
    ( name != document.cookie.substring( 0, name.length ) ) )
    {
    return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}

function delete_cookie( name, path, domain ) {
    if ( Get_Cookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


function go(url) {
   window.location=root+url;
}

function open_album(path) {
    window.open(path,'open_album','width=600,height=500');
}

addLoadEvent(function() {
    var posts=YAHOO.util.Dom.getElementsByClassName('post','div',document.getElementById("doc"));
	for (i=0; i<posts.length; i++) {
            var imgs=posts[i].getElementsByTagName("img");
			for(j=0; j<imgs.length; j++) {
				if (imgs[j].width>400) {
                    imgs[j].height = imgs[j].height * 400 / imgs[j].width;
                    imgs[j].width = 400;
                    imgs[j].style.cursor= 'pointer';
					imgs[j].onclick = function() {window.open(this.src);}
					if (navigator.userAgent.toLowerCase().indexOf('ie') > -1) {
						imgs[j].title = '请使用Ctrl+鼠标滚轮缩放图片，点击图片可在新窗口打开';
							imgs[j].onmousewheel = function img_zoom(){
							  var zoom = parseInt(this.style.zoom, 10) || 100;
							  zoom += event.wheelDelta / 12;
							  if (zoom > 0 && event.ctrlKey) this.style.zoom = zoom + '%';
							  return false;
							}
					} else {
						imgs[j].title = '点击图片可在新窗口打开';
					}
				}
			}
    }
}
);

function insertImage(path,path2) {
    surroundHTML('body','<a href="'+path2+'" target="_blank"><img src="'+path+'"/>','</a>');
}

function addUrl(type, id) {
    if(type==null) {
        var x=document;
        var a=encodeURIComponent(x.location.href);
        var t=encodeURIComponent(x.title);
        window.open(app+'url?title='+t+'&url='+a,'add_url','width=600,height=500,scrollbars =1');
    }
    else {
        window.open(app+'url?id='+id+'&type='+type,'add_url','width=600,height=500,scrollbars =1');
    }
}

function trim(sString){
    while (sString.substring(0,1) == ' '){
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' '){
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function update_tag_text(txt,tag,add) {
    var arr = txt.value.split(",");
    for(i=0;i<arr.length;i++) {
        arr[i] = trim(arr[i]);
    }
    if(arr[0]=='')
        arr.pop();
    if(add) {
        for(i=0;i<arr.length;i++) {
            if(arr[i] == tag) {
                return;
            }
        }
        arr.push(tag);
    }
    else {
        for(i=0;i<arr.length;i++) {
            if(arr[i] == tag) {
                arr.splice(i,1);
                break;
            }
        }
    }
    txt.value = arr.join(",");
}