/*

!! REQUIRE:
/_js/gui/gf.js (for creating ghost frame to form-post)
/_js/dom/setattribute.js (for setting create cheer <td>)
/_js/dom/select.js (for swst())
/_js/var/path.js
/_js/data/numberdirectory.js (for write photo url, gtpu())

PJ=Project
PS.ack = anti-cach key
PS.pop(ac) = pop photo(anti-cache[boolean])
PS.gtpu(t, ac) = get project photo url(type [p|t|s|m|pht|phs|phm] ('ph*' for place holder), anti-cache[boolean])

PS.poc() = pop cheer
PS.snc() = send a cheer
PS.pom(m) = pop comment (milestoneId [0 if comment for project] )
PS.posi(i) = pop sales item (itemId)
PS.acc(cc) = add cheer count (countryCode)
PS.cch(cc) = create cheer icon & count box (countryCode)

PS.swst(id) = switch status when editing (<select> dom id)
PS.swst_oc() = swst() for <select> onChange

PS.wt() = watch this project
PS.ctvs() = count visit

PS.buit(i) = but item (itemId)
PS.acit(i) = add to add - item (itemId)
PS.vwsc() = view shopping cart

*/

function PJ(u,p) {

var _PB = photoRootUrl;

// for prototype access
this.u = u; this.p = p;

this.ack = Math.random();

this.pop = function(ac) {
	var ok = false;
	var pcon = ge('pjphoto');
	if (pcon) {
		var img = pcon.firstChild;
		if (img && img.tagName=='IMG') {
			if (!img.src.match(/projectph/i)) ok = true;
		}
	}
	if (ok) {
		var w = window.open(this.gtpu('p', ac), 'p'+p, 'width=840,height=640,status=yes,toolbar=no,resizable=yes,scrollbars=yes');
		w.focus();
	}
}

this.gtpu = function(t, ac) {
	var t = (t==null)?'p':t;
	var pstr = NumberDirectory.prototype.write(p);
	if (t.match(/^ph/)) { var u = _PB+'/ph/p'+t.substr(2,1)+'.gif'; }
	else { var u = _PB+'/p/'+pstr+'/p/'+t+'.jpg'; }
	return (ac)?u+='?'+this.ack:u;
}


this.poc = function() {
	var w = window.open('/'+u+'/projects/'+p+'/cheers', 'c'+p, 'width=500,height=500,status=yes,toolbar=no,resizable=yes,scrollbars=yes');
	w.focus();
}

this.snc = function() {
	// create gf
	chgf = new GF('chgf');
	chgf.url = '/'+u+'/projects/'+p+'/cheers/send';
	chgf.c();
	// hide cheer button
	if (ge('sncbutt')) ge('sncbutt').style.display = 'none';
}

this.pom = function(m) {
	var mm = (m>0)?'/milestones/'+m:'';
	var w = window.open('/'+u+'/projects/'+p+mm+'/comments', 'm'+p+m, 'width=500,height=500,status=yes,toolbar=no,resizable=yes,scrollbars=yes');
	w.focus();
}

this.posi = function(i) {
	var w = window.open('/'+u+'/projects/'+p+'/sales/'+i, 'si'+p, 'width=500,height=500,status=yes,toolbar=no,resizable=yes,scrollbars=yes');
	w.focus();
}

this.acc = function(cc) {
	// get count con
	var con = ge('chc'+cc);
	// if exist, add number
	if (con) {
		var ct = parseInt(con.innerHTML);
		if (!isNaN(ct)) con.innerHTML = ct+1;
	// if not, create new cheer	
	} else {
		this.cch(cc);
	}
}

this.cch = function(cc) {
	// get cheer <table> & <tr>'s
	var chtb = ge('chtb'+p);
	var trs = gt(chtb, 'TR');
	// create cheer icon in td
	var td0 = ce('TD');
	var td1 = ce('TD');
	var tdsp = ce('TD');
	setAttribute(td0, 'align', 'center');
	setAttribute(td1, 'align', 'center');
	setAttribute(tdsp, 'width', '10'); // spacer td
	td1.className = 'smaller';
	td0.innerHTML = '<a href="#"><img src="/images/cheer_1.gif" border="0"></a>';
	td1.innerHTML = '<a href="#">'+cc+' <span id="chc'+cc+'">1</span></a>';
	// put <td>'s into <tr>'s
	trs[0].appendChild(td0);
	trs[0].appendChild(tdsp);
	trs[1].appendChild(td1);
	trs[1].appendChild(tdsp);
}

this.wt = function() {
	wtgf = new GF('wtgf');
	wtgf.url = '/'+u+'/projects/'+p+'/save/watchedby';
	wtgf.c();
	ge('wlstatus').innerHTML = 'it\'s on my watch list';
}

this.ctvs = function() {
	cvgf = new GF('cvgf');
	cvgf.url = '/'+u+'/projects/'+p+'/save/visit';
	cvgf.c();
}

this.buit = function(i) {
	bugf = new GF('bugf');
	bugf.url = '/'+u+'/projects/'+p+'/sales/'+i+'/save/buynow';
	bugf.c();
}
this.acit = function(i) {
	acgf = new GF('acgf');
	acgf.url = '/'+u+'/projects/'+p+'/sales/'+i+'/save/addtocart';
	acgf.c();
}
this.vwsc = function(i) {
	vsgf = new GF('vsgf');
	vsgf.url = '/'+u+'/projects/'+p+'/sales/'+i+'/save/viewcart';
	vsgf.c();
}

}
