var postData = '';

function getMixoData()
{
	var params = Form.serialize($('add_mixo'));
	new Ajax.Request('/article/get-data/',
		{
			method:'post',
			parameters:params,
			onCreate:ajaxCreate,
			onFailure:function (ex){
				setTimeout("$('loader').hide()",1000);
				//alert(ex);
			},
			onException:function (transport, ex){
				setTimeout("$('loader').hide()",1000);
				//alert(ex.message);
			},
			on404:function(){
				alert('Pagina nu exista, verifica adresa introdusa!');
			},
			onSuccess:function (transport){
				postData = transport.responseJSON.p;
				if (transport.responseJSON.found==1)
				{
					alert('Link-ul introdus de dvs exista deja in baza de date MiXo.');
				}
				else
				{
					$('article_title').value = transport.responseJSON.title;
					for (i=0;i<transport.responseJSON.images.length;i++)
					{
						inp = new Element('input', {type: "radio", id: "imageslist", name: "article_image", className: 'radio_button_images', value:transport.responseJSON.images[i] });
						$('article_image_div').appendChild(inp);
						inp = new Element('img', {src: transport.responseJSON.images[i], className:"radio_images"});
						$('article_image_div').appendChild(inp);
						
						br = new Element('br', { className:"radio_br_img" });
						$('article_image_div').appendChild(br);
					}
					for (i=0;i<transport.responseJSON.p.length;i++)
					{
						//diuv
						inp = new Element('input', {type: "radio", id: "textlist", name: "article_text", className: 'radio_button_p', value:transport.responseJSON.p[i], onclick:"selectparagraph(" + i + ")"});
						$('article_text_div').appendChild(inp);
						inp = new Element('p', { className:"radio_p"}).update(transport.responseJSON.p[i]);
						$('article_text_div').appendChild(inp);
						
						br = new Element('br', { className:"radio_br_p" });
						$('article_text_div').appendChild(br);
					}
				}
				setTimeout("$('loader').hide()",1000);
			}
		}
	);
}

function selectparagraph(p)
{
	$('article_text').value = postData[p];
}

function ajaxCreate()
{	
	$$('.radio_button_images, .radio_images, .radio_button_p, .radio_p, .radio_br_p, .radio_br_img').each(function(id, position) { $(id).remove(); } );
	
	if (window.innerHeight && window.scrollMaxY) {
		h = window.innerHeight + window.scrollMaxY;
		w = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ 
		h = document.body.scrollHeight;
		w = document.body.scrollWidth;
	} else { 
		h = document.body.offsetHeight;
		w = document.body.offsetWidth;
  	}
	
	if (typeof window.innerWidth != 'undefined')
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}
	else
	{
		w = document.getElementsByTagName('body')[0].clientWidth;
		h = document.getElementsByTagName('body')[0].clientHeight;
	}
	
	$('loader_img').style.top = (h-128)/2 + 'px';
	$('loader_img').style.left = (w-15)/2 + 'px';
	
	
	$('loader').style.width = w + 'px';
	$('loader').style.height = h + 'px';
	
	$('loader').show();
	$('loader').setOpacity(0.5);
}

var browser = {
		fullHeight: function()
		{
			if (window.innerHeight && window.scrollMaxY) {
				return window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ 
				return document.body.scrollHeight;
			} else { 
				return document.body.offsetHeight;
		  	}
		},
		
		fullWidth: function()
		{
			if (window.innerWidth && window.scrollMaxY) {
				return window.innerWidth + window.scrollMaxX;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ 
				return document.body.scrollWidth;
			} else { 
				return document.body.offsetWidth;
		  	}
		},
		
		  viewportWidth: function() {
		    return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
		  },
		
		  viewportHeight: function() {
		    return self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
		  },
		  
		  viewportSize: function() {
		    return { width: this.viewportWidth(), height: this.viewportHeight() };
		  }
	};

function submitSearchForm(form)
{
	var url = '/cauta/' + form.termen.value;
	window.location = url;
	return false;
}
