/**************************************
*   (c) Andrew Zaitsew, 2005 - 2009   *
*       Aranea web-design studio      *
*           www.aranea.com.ua         *
*               custom.js             *
*      front-end custom javascript    *
***************************************/

function PopupImage(imgPath, title) {
	var win = window.open('','araneapreview',
	'width=50,height=50,left=0,top=0,screenX=0,screenY=0,resizable=1');

	if (title == undefined) title = VIEW_IMAGE;
	var content = '<html><head><title>' + title + '</title>' +
					'<style>body { margin:0px; padding:0px; } #div-image { padding:2px; overflow:auto; } img { border:0; text-align:center; } form { margin:0px; padding:0px; } #header { text-align:center; margin:0px; padding:5px; background:#E6E6E6; color:navy; font-size:14px; font-weight: bold; }</style>' +
					'</head><body><div id="all"><div id="header">' + title + '</div><div id="div-image" align="center"><a href="javascript:self.close()">' +
					'<img alt="' + CLOSE_WINDOW + '" id="image" src="' + imgPath + '" /></a></div></div></body></html>'
	win.document.write(content);

	win.document.body.onload = function() {
    	var obj = win.document.getElementById('image');
    	var w = obj.width, h = obj.height;
    	var iHeight= document.body.clientHeight, iWidth = self.innerWidth;

    	var left = (self.opera ? iWidth : screen.availWidth)/2 - w/2;
    	var top =  (self.opera ? iHeight : screen.availHeight)/2 - h/2;

		if (w + 25 > screen.availWidth) {
			left = 0;
			w = screen.availWidth;
		} else {
			w += 25;
		}

		if (h + 120 > screen.availHeight) {
			top = 0;
			h = screen.availHeight;
		} else {
			h += 120;
		}
	
		win.resizeTo(w, h);	
		win.moveTo(left, top);
    }
    win.onload = win.document.body.onload; // special for Mozilla
    
    win.document.close();
    win.focus();
}

function showAJAXicon(_item, _mode) {
	if ( document.getElementById(_item) ) {
		if ( _item == 'ajax-icon') {
			AJAXFade('content-wrapper');
		}
		if ( _mode == 0 ) {
			document.getElementById(_item).innerHTML = '<div style="clear:both; margin:5px 0 10px 0;"><img src="' + site_url + 'images/icon-ajax.gif" width="31" height="31" alt="-" /></div>';
		}
		if ( _mode == 1 ) {
			document.getElementById(_item).innerHTML = document.getElementById(_item).innerHTML + ' ';
			setSelected(_item);
		}
		if ( _mode == 2 ) {
			document.getElementById(_item).innerHTML = '<div align="center" style="clear:both; margin:0px;"><img src="' + site_url + 'images/icon-ajax-bg.gif" width="31" height="31" alt="-" /></div>';
		}
	}
}

function hideAJAXicon(_item, _mode) {
	if ( document.getElementById(_item) ) {
		if ( _mode == 1 ) {
			unsetSelected(_item, 'SPAN');
		} else {
			document.getElementById(_item).innerHTML = '';
		}
	}
}

function updateAccountForm(req) {
	Spry.Utils.setInnerHTML('user-content', req.xhRequest.responseText);
}

function updateMessageForm(req) {
	Spry.Utils.setInnerHTML('message-content', req.xhRequest.responseText);
}

var tooltips = new Array();
function SetToolTip(_id, _name, _text) {
	_field = _name.replace('hint_', 'span_');
	delete(tooltips[_id]);
	if ( document.getElementById(_name) && document.getElementById(_field) ) {
		setText(_name, _text);
		tooltips[_id] = new Spry.Widget.Tooltip(_name, '#' + _field, {followMouse: true});
		return true;
	} else {
		return false;
	}
}

function autoRefresh(orderId, urlkey) {
	if ( getObj('autorefresh').checked ) {
		RefreshCycle++;
		if ( RefreshCycle == RefreshCycleMax ) {
			doRefresh(orderId, urlkey);
		} else {
			tmp = String(RefreshCycleMax - RefreshCycle);
			tmp2 = tmp.substr((tmp.length - 1), 1);
			if ( (tmp > 4) && (tmp < 21) ) {
				tmp = RefreshHint3.replace("{$cycle}", tmp);
			} else if ( tmp2 == 1 ) {
				tmp = RefreshHint1.replace("{$cycle}", tmp);
			} else if ( ( tmp2 > 1 ) && ( tmp2 < 5 ) ) {
				tmp = RefreshHint2.replace("{$cycle}", tmp);
			} else {
				tmp = RefreshHint3.replace("{$cycle}", tmp);
			}
			setText('autoRefreshHint', tmp);
		}
	} else {
		setText('autoRefreshHint', '');
	}
}

function doRefresh(orderId, urlkey) {
	setText('autoRefreshHint', RefreshHint0);
	RefreshCycle = 0;
	showAJAXicon('exchange-ajax-' + orderId, 1);
	Spry.Utils.updateContent('account_' + orderId, site_url + 'module/ajax/action_exchangectrl.php?urlkey=' + urlkey);
}

function cancelOrder(orderId, urlkey) {
	var string = prompt(EXCHANGE_CANCEL_PROMPT, '');
	if ( (string != null) && (string.length > 0) ) {
		showAJAXicon('exchange-ajax-' + orderId, 1);
		if ( urlkey.length > 0 ) {
			Spry.Utils.updateContent('account_' + orderId, site_url + 'module/ajax/action_exchangectrl.php?act=cancel&urlkey=' + urlkey + '&custom=' + string);
		} else {
			Spry.Utils.updateContent('account_' + orderId, site_url + 'module/ajax/action_user.php?act=cancel&orderId=' + orderId + '&custom=' + string);
		}
	}
}
