// AJAX FUNCTIONS

var checkTimer;
var editing = false;

function ajaxInitObj() {
	try	{
		var x = new XMLHttpRequest();
	}
	catch (error) {
		try {
			var x = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (error) {
			try {
				var x = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (error) {
				return false;
			}
		}
	}
	return x;
}

function ajaxRequest(url, args, callback) {
	var request = ajaxInitObj();
	request.open('POST', url, true);
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.setRequestHeader('Connection','close');
	request.onreadystatechange = function() {
		if (request.readyState != 4) 
			return;
		callback(request.responseText);
		if(!window.ActiveXObject) delete request;
		else request = null;
	}
	if ( $type (args) == 'object' )
	{
		var argArr = [];
		for (var key in args)
			argArr.push ( key + "=" + args[key] );
		args = argArr.join ( "&" );
	}
	request.send(args);
}

function isInt(x) {
	var y=parseInt(x);
	if (isNaN(y)) return false;
	return x==y && x.toString()==y.toString();
} 

window.addEvent ( "domready", function () {
	// Admin
	
	ajaxRequest ( "/resourcecenter/remote.php?action=check_login", {}, adminShowControls );
} );

function toggleShade ()
{
	if ( $( 'shade' ) == null )
	{
		var shade = document.createElement ( 'DIV' );
		shade.id = 'shade';
		if ( Browser.Engine.trident )
			shade.style.backgroundColor = '#000000';
		else
			shade.className = 'shade';
		shade.style.position = 'absolute';
		shade.style.top = '0px';
		shade.style.opacity = 0;
		shade.style.left = '0px';
		shade.style.height = Window.getScrollHeight() + 'px';
		shade.style.width = ( Window.getScrollWidth() - 1 ) + 'px';
		document.body.appendChild ( shade );
		if ( Browser.Engine.trident  )
			$(shade).tween ( "opacity", [0, 0.2] );
		else
			$(shade).tween ( "opacity", [0, 1] );
		shade.style.zIndex = '100';
	}
	else
	{
		document.body.removeChild ( $( 'shade' ) );
	}
	
}

function toggleMBox ( windowType, content, buttons, customHeader, customWidth )
{
	if ( windowType != null && $ ( 'mbox' ) != null )
		document.body.removeChild ( $ ( 'mbox' ) );
	if ( windowType != false )
	if ( $ ( 'mbox' ) == null )
	{
		var mbox = document.createElement ( 'DIV' );
		mbox.id = 'mbox';
		mbox.style.width = '300px;';
		if ( windowType == "applet" )
			if ( customWidth != undefined )
				mbox.style.width = customWidth + 'px;';
			else
				mbox.style.width = '350px;';
		mbox.style.zIndex = '1000';
		mbox.style.position = 'absolute';
		mbox.style.visibility = 'hidden';
		document.body.appendChild ( mbox );
		var contentDiv = document.createElement ( 'DIV' );
		contentDiv.style.position = 'relative';
		if ( windowType == 'info' )
			contentDiv.innerHTML = "<div id='mbox_header'>Notice</div>";
		else if ( windowType == 'warning' )
			contentDiv.innerHTML = "<div id='mbox_header'>Warning</div>";
		else if ( windowType == 'error' )
			contentDiv.innerHTML = "<div id='mbox_header'>Error!</div>";
		else if ( customHeader != null )
			contentDiv.innerHTML = "<div id='mbox_header'>" + customHeader + "</div>";
		contentDiv.innerHTML += "<div id='mbox_content'>" + content + "</div>";
		mbox.appendChild ( contentDiv );
		if ( buttons && buttons.length > 0 )
		{
			var buttonDiv = document.createElement ( 'DIV' );
			buttonDiv.id = 'mbox_buttons';
			for ( var i = 0; i < buttons.length; i++ )
			{
				var button = document.createElement ( 'INPUT' );
				button.className = 'mbox_button';
				button.type = 'button';
				button.value = buttons[i][0];
				button.onclick = buttons[i][1];
				if ( buttons[i][2] )
					button.setAttribute ( 'style', buttons[i][2] );
				buttonDiv.appendChild ( button );
			}
			contentDiv.appendChild ( buttonDiv );
		}
		mbox.style.left = ( ( Window.getWidth() / 2 ) - ( $(mbox).getSize().x / 2 ) ) + 'px';
		mbox.style.top = ( Window.getHeight() / 2 ) - ( $(mbox).getSize().y / 2 ) + Window.getScrollTop() + 'px';
		$(mbox).tween ( "opacity", [0, 1] );
	}
	else
	{
		$("mbox").tween ( "opacity", [1, 0] );
		if ( windowType == null )
			window.setTimeout ( "toggleShade ();", 300 );
	}
}

// ADMIN

function adminLogin () {
	toggleShade ();
	toggleMBox ( "applet", "<div class='window_form'><div class='window_form_left'>Login:</div><div class='window_form_right'><input type='text' id='admin_login' name='admin_login' /></div><br /><div class='window_form_left'>Password:</div><div class='window_form_right'><input type='password' id='admin_password' name='admin_password' /></div><br /></div>", [ [ "Anuluj", function () { toggleMBox () } ], [ "Zaloguj", adminLoginDo ] ], "Logowanie" );
}

function adminLoginDo () {
	ajaxRequest ( "/resourcecenter/remote.php?action=login", { login: $('admin_login').value, password: $('admin_password').value }, adminLoginDoCheck );
	toggleMBox ( false );
}

function adminLoginDoCheck ( response )
{
	if ( response == "error" )
		toggleMBox ( "error", "Login or/and password you provided are incorrect!", [ [ "Ok", function () { toggleMBox () } ] ] );
	else if ( response == "ok" )
		toggleMBox ( "info", "You are now logged in!", [ [ "Ok", function () { toggleMBox (); top.location.href = top.location.href } ] ] );
	else
		toggleShade ();
}

function adminShowControls ( response )
{
	if ( response != "ok" ) return false;
	
	var btnEdit = new Element ( "img", {
		"class": "admin_button",
		"src": "/resourcecenter/image/tag_blue_edit.png",
		"events": {
			"click": function () {
				adminEditDescription();
				editing = true;
			}
		}
	} );

	btnEdit.injectBefore ( $('product_tags') );
}

function adminEditDescription ( obj )
{
	if ( editing ) return false;
	var url = top.location.href;
	var productID = url.substr ( url.lastIndexOf ( "/" ) + 1, url.indexOf ( "_", url.lastIndexOf ( "/" ) ) - url.lastIndexOf ( "/" ) - 1 );
	var txtDesc = new Element ( "textarea", {
		"id": "new_description",
		"value": $('product_description').get('html'),
		"styles": {
			"width": "480px",
			"height": "200px"
		},
		"events": {
			"keyup": function ( e ) {
				window.clearTimeout ( checkTimer );
				checkTimer = window.setTimeout ( "adminCheckTypes();", 750 );
			}
		}
	} );
	var btnSend = new Element ( "input", {
		"id": "edit_button",
		"type": "button",
		"value": "Save description",
		"events": {
			"click": function () {
				adminSaveDescription ();
			}
		}
	} );
	$('product_description').set('html', '');
	txtDesc.injectInside ( $('product_description') );
	btnSend.injectInside ( $('product_edit_controls') );
	adminCheckTypes ();
}

function adminCheckTypes ()
{
	var url = top.location.href;
	var productID = url.substr ( url.lastIndexOf ( "/" ) + 1, url.indexOf ( "_", url.lastIndexOf ( "/" ) ) - url.lastIndexOf ( "/" ) - 1 );
	ajaxRequest ( "/resourcecenter/remote.php?action=check_tags", { product: productID, description: escape ( $('new_description').value ) }, function ( response ) {
		$('product_edit_controls').getFirst().set('html', '<strong>Matching types:</strong> ' + response);
	} );
}

function adminSaveDescription () {
	var url = top.location.href;
	var productID = url.substr ( url.lastIndexOf ( "/" ) + 1, url.indexOf ( "_", url.lastIndexOf ( "/" ) ) - url.lastIndexOf ( "/" ) - 1 );
	ajaxRequest ( "/resourcecenter/remote.php?action=edit_description", { product: productID, description: escape ( $('new_description').value ) }, function ( response ) {
		$('product_description').set('html', $('new_description').get('value'));
		$('edit_button').dispose();
		editing = false;
	} );
}