var tagpos=0;
var tagcount=0;
var oldimg = "";
var descr_target = "descr";
function PopupPic(sPicURL) {
    window.open("screenshots.php?"+sPicURL, "", "resizable=1,HEIGHT=200,WIDTH=200");
} 

$(function() {
    $('#gallery a').lightBox(); // Select all links in object with gallery ID
});


function add_tag(tid,tagname) {
    //httpGETRequest("http://www.patup.com/torrent/"+tid+"/?action=addnewtag&tagname="+urlencode(tagname),"torrenttaglist");
    httpGETRequest("?action=addnewtag&tagname="+urlencode(tagname),"torrenttaglist");
}
function add_tag_post(tid) {
    var tagname = document.getElementById("tagname").value;
    if (tagname) {
	//httpGETRequest("http://www.patup.com/torrent/"+tid+"/?action=addnewtag&tagname="+urlencode(tagname),"torrenttaglist");
	httpGETRequest("?action=addnewtag&tagname="+urlencode(tagname),"torrenttaglist");
	document.getElementById("tagname").value = "";
    }
}

function display_target(which) {
    descr_target = which;
    var content = document.getElementById('finaldescr');
    if (content) {
	if (document.getElementById('screenshotsadmin')) {
	    switchMenu('screenshotsadmin',0);
	}
	switchMenu('screenshotsdiv',0);
	var fromcontent = document.getElementById(which)
	if (fromcontent)
	    content.innerHTML = fromcontent.innerHTML;
    }
}    
function switch_target(obj,id) {
    var tmpobj = obj;
    if (obj == 'editdesc') {
	if (descr_target == 'descr') {
	    tmpobj = 'editdesc';
	} else {
	    tmpobj = 'editchangelog';
	}
    }
    switchMenu(tmpobj,id);
}
function textCounter(field,cntfield,maxlimit) {
    if (document.getElementById(field).value.length > maxlimit) // if too long...trim it!
	document.getElementById(field).value = document.getElementById(field).value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
    else
	document.getElementById(cntfield).value = maxlimit - document.getElementById(field).value.length;
}

function closeAll(obj) {
    var tmpobj = obj.split("?");
    var i=0;
    for(i=0;i<tmpobj.length;i++) {
	el = document.getElementById(tmpobj[i]);
	el.style.display = 'none';
    }
}

function switchMenu(obj,id) {
    var el = document.getElementById(obj);
    if ( el.style.display != 'none' ) {
	el.style.display = 'none';
    }
    else {
	el.style.display = '';
	if (obj == 'torrentfilelist') {
	    el.innerHTML = "<img src=/templates/default/images/loading.gif>";
	    currobj=obj;
	    done=0;
	    //httpGETRequest("http://www.patup.com/torrent/"+id+"/?action=listfiles",obj);
	    httpGETRequest("?action=listfiles",obj);

	}
	if (obj == 'addnewtag') {
	    el.innerHTML = "<img src=/templates/default/images/loading.gif>";
	    currobj=obj;
	    done=0;
	    //httpGETRequest("http://www.patup.com/torrent/"+id+"/?action=addnewtag",obj);
	    httpGETRequest("?action=addnewtag",obj);

	}
    }
}

function editTag(torrid,tagid) {
    switchMenu("edittag"+tagid, torrid);
}

function tag_suggest(key, query) {
    if (key == 38) {
	tag_goPrev();
    } else if (key == 40) {
	tag_goNext();
    } else if (key != 13) {
	if (query.length > 0) {
	    done = 0;
	    currobj = "tagsuggestions";
	    //httpGETRequest("http://www.patup.com/details.php?id=1&action=suggesttag&query="+query,"tagsuggestions");
	    httpGETRequest("?action=suggesttag&query="+query,"tagsuggestions");

	} else {
	    tag_update('');
	}
    }

}
function tag_noenter(key) {
    suggcont = document.getElementById("tagsuggcont");
    if (suggcont.style.display = "block") {
	if (key == 13) {
	    tag_choiceclick(document.getElementById("stag-"+tagpos));
	    return false;
	} else {
	    return true;
	}
    } else {
	return true;
    }
}
function tag_choiceclick(obj) {
    document.getElementById("tagname").value = obj.innerHTML;
    tagcount = 0;
    tagpos = 0;
    suggcont = document.getElementById("tagsuggcont");
    suggcont.style.display = "none";
    document.getElementById("tagname").focus();
}
function tag_closechoices() {
    suggcont = document.getElementById("tagsuggcont");
    if (suggcont) {
	if (suggcont.style.display == "block") {
    	    tagcount = 0;
    	    tagpos = 0;
    	    suggcont.style.display = "none";
	}
    }
}
                                                    
function tag_goPrev() {
    if (count > 0) {
        if (document.getElementById("stag-"+tagpos)) {
            tag_unselect(document.getElementById("stag-"+tagpos));
            tagpos--;
            if (document.getElementById("stag-"+tagpos)) {
                tag_select(document.getElementById("stag-"+tagpos));
            } else {
	        tagpos = 0;
            }
        } else {
            tagpos = tagcount;
            tag_select(document.getElementById("stag-"+tagcount));
        }
    }
}

function tag_goNext() {
    if (tagpos <= tagcount && tagcount > 0) {
        if (document.getElementById("stag-"+tagpos)) {
            tag_unselect(document.getElementById("stag-"+tagpos));
        }
        tagpos++;
        if (document.getElementById("stag-"+tagpos)) {
            tag_select(document.getElementById("stag-"+tagpos));
        } else {
            tagpos = 0;
        }
    }
}

function tag_unselectAllOther(id) {
    for (i = 1; i <= tagcount; i++) {
	if (i != id) {
            document.getElementById("stag-"+i).style.backgroundColor = '#ffffff';
            document.getElementById("stag-"+i).style.color = '#000000';
        }
    }
}

function tag_select(obj,mouse) {
    obj.style.backgroundColor = '#3399ff';
    obj.style.color = '#ffffff';
    if (mouse) {
	tagposarr = obj.id;
        tagpos = obj.id.split('-')[1];
        tag_unselectAllOther(tagpos);
    }
}

function tag_unselect(obj,mouse) {
    obj.style.backgroundColor = '#ffffff';
    obj.style.color = '#000000';
    if (mouse) {
        tagpos = 0;
    }
}
function tag_update(result) {
    arr = new Array();
    arr = result.split('\r\n');
    if (arr.length > 10) {
        tagcount = 10;
    } else {
        tagcount = arr.length;
    }

    suggdiv = document.getElementById("tagsuggestions");
    suggcont = document.getElementById("tagsuggcont");
    if (arr[0].length > 0) {
        suggcont.style.display = "block";
        suggdiv.innerHTML = '';
        suggdiv.style.height = tagcount * 20;
        
        for (i = 1; i <= tagcount; i++) {
            novo = document.createElement("div");
            suggdiv.appendChild(novo);
            novo.id = "stag-"+i;
            novo.style.height = "14px";
            novo.style.padding = "3px";
            novo.onmouseover = function() { tag_select(this,true); }
            novo.onmouseout = function() { tag_unselect(this,true); }
            novo.onclick = function() { tag_choiceclick(this); }
            novo.innerHTML = arr[i-1];
        }
    } else {
        suggcont.style.display = "none";
        tagcount = 0;
    }
}

function delete_torrent(id) {
    sure = confirm("Are you sure you want to delete the torrent?");
    if (sure == true){
	httpGETRequest("?action=deletetorrent","");
	//document.location = "http://www.patup.com/browse.php";
    }
}



function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function abPos(o) { var o=(typeof(o)=='object'?o:document.getElementById(o)), z={X:0,Y:0}; while(o!=null) { z.X+=o.offsetLeft; z.Y+=o.offsetTop; o=o.offsetParent; }; return(z); }
function XY(e,v) { var o=agent('msie')?{'X':event.clientX+document.documentElement.scrollLeft,'Y':event.clientY+document.documentElement.scrollTop}:{'X':e.pageX,'Y':e.pageY}; return(v?o[v]:o); }

star={};

star.mouse=function(e,o) { if(star.stop || isNaN(star.stop)) { star.stop=0;

    document.onmousemove=function(e) { var n=star.num;
    
        var p=abPos(document.getElementById('star'+n)), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y; star.num=o.id.substr(4);

        if(oX<1 || oX>84 || oY<0 || oY>19) { star.stop=1; star.revert(); }
        
        else {

            document.getElementById('starCur'+n).style.width=oX+'px';
            document.getElementById('starUser'+n).style.color='#111';
            document.getElementById('starUser'+n).innerHTML=Math.round(oX/84*100)+'%';
        }
    };
} };

star.update=function(e,o) {
    var n=star.num, v=parseInt(document.getElementById('starUser'+n).innerHTML);

    n=o.id.substr(4); 
    document.getElementById('starCur'+n).title=v;
    httpGETRequest("?action=addvote&vote="+v, "");
    //req=new XMLHttpRequest(); req.open('GET','?action=vote&vote='+(v/100),false); req.send(null);    

};

star.revert=function() { var n=star.num, v=parseInt(document.getElementById('starCur'+n).title);

    document.getElementById('starCur'+n).style.width=Math.round(v*84/100)+'px';
    document.getElementById('starUser'+n).innerHTML=(v>0?Math.round(v)+'%':'');
    document.getElementById('starUser'+n).style.color='#888';
    
    document.onmousemove='';

};

star.num=0;

document.onclick = function() { tag_closechoices(); }
