var	IMG_SHOW_JS = true;
var	THM_OPT_COOKIE_NAME = 'thmOpt';
var 	THM_OPT_PPG, THM_OPT_F_PREVIEW;

function catKeywords(fObj)
{
	var kw = fObj.KW;
	var dsc = '';
	
	if ((getElementLength(kw) == 1) && kw.checked) {
		dsc += kw.value;
	} else {
		for (i = 0; i < kw.length; i++) {
			if (kw[i].checked)
				dsc += kw[i].value + ' ';
		}
	}
	
	if (dsc == '') {
		alert("You must select at least one keyword to perform a search.");
		return false;
	}
	
	fObj.I_DSC.value = dsc;
	return true;
}


// ##################### imgUpdShow #####################

function imgUpdShow(type, data, args)
{
	var		url = '/ajax/imgUpdShow/';

	if (empty(data)) return false;

	url += data;

	_bsBsapi.get(url, _bsMm.ceNew('bsapi'));
}

function imgUpdShowCb(result)
{
	// do nothing - for debugging purposes only
	return;
}


// ##################### gallery comments #####################

var		GC_IMG_INDEX = null;
var 		GC_IMAGES_DIR = '/img/act/';
var		GC_IMAGES = new Array('thumbsdown', 'neutral', 'thumbsup');

function galCmtRating(index, fObj, rating)
{
	var		url = location.protocol + '//' + location.host + '/ajax/galCmtRating?';

	GC_IMG_INDEX = index;
	
	try {
		url += "GC_GALLERY_ID=" + escape(fObj.GC_GALLERY_ID.value);
		url += "&GC_IMAGE_ID=" + escape(fObj.GC_IMAGE_ID.value);
		url += "&GC_RATING=" + rating;
		url += "&CALLBACK=galCmtRatingCb";

		getXml(url);
	} catch (e) {
		alert('Sorry, your rating could not be saved at this time.');
	}
}

function galCmtRatingCb(result, err)
{
	if (result) {
		gcRatingImgReset();
		imgObj = eval("document.images['gcRatingImg" + GC_IMG_INDEX + "']");
		imgObj.src = GC_IMAGES_DIR + GC_IMAGES[GC_IMG_INDEX] + '-over.gif';
	} else {
		switch (err) {
			case 'NO_SESSION':
				alert('You must be logged in to rate this image');
				location.reload();
				break;
			default:
				alert('Sorry, an error occurred: ' + err);
		}
	}
}

function gcRatingImgReset()
{
	
	for (i = 0; i < 3; i++) {
		imgObj = eval("document.images['gcRatingImg" + i + "']");
		imgObj.src = GC_IMAGES_DIR + GC_IMAGES[i] + '.gif';
	}
}

function galCmtToggle(flag)
{
	divToggle('galCmt');
	divToggle('galCmtForm');

	if (flag)
		document.gcForm.GC_COMMENT.focus();
}

function galCmtCancel(fObj)
{
	galCmtToggle();
	fObj.GC_COMMENT.value = fObj.GC_COMMENT_ORIGINAL.value;
}

var IMG_SHOW_LBX = null;

function lightboxAdd(id)
{
	try {
		if (!BS.utl.dom.isLoaded) {
			alert("Please wait until the page has finished loading.");
			return
		}
		if (!IMG_SHOW_LBX)
			IMG_SHOW_LBX = BS.utl.mm.oNew(PS.com.Lightbox.AddPrompt, true);

		IMG_SHOW_LBX.show(true, id);
	} catch (e) { alert(e); }
}

function thmOptCookieSet(ppg, opt)
{
	var t = [];
	t[0] = (!empty(ppg)) ? ppg : '0';
	t[1] = (!empty(opt)) ? opt : '0';

	var exp = new Date();
	exp.setDate(exp.getDate()+30);
	document.cookie = THM_OPT_COOKIE_NAME + '=' + escape(t.join('|')) + '; expires=' + exp.toGMTString() + '; path=/';
}

function thmOptSetPpg(ppg, url)
{
	thmOptCookieSet(ppg);
	location = url;
}

// ##################### PS.com.ThmOptBalloon #####################

PS.com.ThmOptBalloon = function (qry)
{
	this.mm = new BS.com.Delete;
	var mm = this.mm;
	this.gvStr = BS.utl.mkGvStr(this);
	this.qry = qry;

	this.balloon = this.mm.oNew(PS.com.Balloon);
	this.mask = mm.oNew(BS.com.Mask, null, 0.1, '5999');
	_bsEvt.attach(this.mask.obj, 'click', this._clickCB, this);

	this.balloon.setHeader('<h3>Display Options</h3>');
	this.form = mm.domNew(this.balloon.content, 'form', 'thmOptForm');

	var t, div;
	div = mm.domNew(this.form, 'div', 'thmOptBlock');
	div.innerHTML = '<b>Thumbnails per page:</b>';

	t = mm.oNew(BS.com.Form.Select, div, 'ppg');
	t.load(this.PPG_A);

	/*div = mm.domNew(this.form, 'div', 'thmOptBlock');
	div.innerHTML = '<div class="f_left" style="height:2em;width:22px"><input type="checkbox" name="F_PREVIEW" value="1"></div>' + 
		'<div style="font-weight:bold;line-height:2em">Enable Image Preview</div>' + 
		'<div style="padding: 0 0 10px 22px; font-size:11px; color: #666;">' + 
		'Displays larger image and details when mouse pointer is over a thumbnail.</div>';

	mm.domNew(this.form, 'div', 'clear');
	*/

	t = '<div class="thmOptFooter">' +
		'<a href="javascript:' + this.gvStr + '._submit();">' +
		'<img src="/img/button/save.gif" border="0" width="68" height="23" alt="Save Settings"></a>' + 
		'<a href="javascript:' + this.gvStr + '.show(0);">' +
		'<img src="/img/button/close.gif" border="0" width="68" height="23" alt="Close"></a>' + 
		'</div>';
	this.balloon.setFooter(t);

	this.form.onsubmit = function () { return false; };
};

PS.com.ThmOptBalloon.prototype = {
	PPG_A:	[
		[10,'10 Images'],
		[25,'25 Images'],
		[50,'50 Images'],
		[100,'100 Images']
	],

	_delete: function () 
	{
		_bsEvt.detach(this.mask.obj, 'click', this._clickCB);
		this.mm._delete();
	},

	_clickCB: function (type, data, args)
	{
		this.show(false);
	},

	_cookieGet: function ()
	{
		var t = _bsCookie.get(THM_OPT_COOKIE_NAME);
		if (!empty(t)) return t.split('|');
	},

	_submit: function ()
	{
		var p = _bsForm.toArray(this.form);
		//thmOptCookieSet(p.ppg, p.F_PREVIEW);
		thmOptCookieSet(p.ppg, 1);
		location = this.qry;
	},

	show: function (f, obj)
	{
		if (f) {
			var t = this._cookieGet();
			var ppg, f_preview;
			ppg = (t && !empty(t[0])) ? t[0] : THM_OPT_PPG;
			//f_preview = (t && !empty(t[1])) ? t[1] : THM_OPT_F_PREVIEW;

			_bsForm.setValue(this.form.ppg, ppg);
			//this.form.F_PREVIEW.checked = parseInt(f_preview);
		}

		this.mask.show(f);
		this.balloon.show(f, obj);
	}
};

var _thmOptBalloon = null;

function thmOptBalloon (qry, obj)
{
	if (!_thmOptBalloon)
		_thmOptBalloon = _bsMm.oNew(PS.com.ThmOptBalloon, qry);

	// a3 doesn't want the tail
	obj = null;

	_thmOptBalloon.show(true, obj);
};

