﻿window.addEvent('domready', function(){
	if(window.ie6){
		if(Cookie.get('ie6a')===false){
			var eng = 'What are you doing with such an old browser?!<br />Please, update to Internet Explorer 7 or simply download a better browser like <a href="http://getfirefox.com/" title="Mozilla Firefox Homepage" target="_blank">Firefox</a> or Opera.<br />You still can visit this website with this old browser, but some things may not work. Click OK to continue.';
			var cat = 'On vas amb un navegador tan antic?!<br />Siusplau, actualitza a Internet Explorer 7 o simplement descarrega un navegador millor, com el <a href="http://getfirefox.com/" title="Mozilla Firefox Homepage" target="_blank">Firefox</a> o l\'Opera.<br />Així i tot, encara podràs veure aquesta pàgina web, encara que potser trobaràs alguna cosa que no acabi de funcionar. Fes clic al botó d\'OK per a continuar.';
			var al = new Element('div').setStyles({
				'background-color':'#fff8c1',
				'background-image':'url(imgs/ie6-alert-bg.png)',
				'background-repeat':'repeat-x',
				'border':'10px solid red',
				'margin':'20px',
				'padding':'20px',
				'width':'650px',
				'position':'absolute',
				'color':'#000'
			}).setHTML('<div style="float:right;margin-left:20px;margin-bottom:20px;"><img width="134" height="182" src="imgs/ie6-alert.png" title="Alert!" alt="Download Firefox!" /></div><p style="font-size:25px; font-weight:bold;">Old browser!</p><p>'+eng+'</p><p style="font-weight:bold;">'+cat+'</p><p><input type="button" value="OK" onclick="Cookie.set(\'ie6a\',\'sip\',false); $(this.parentNode.parentNode).setStyle(\'display\',\'none\');" /></p>').injectInside(document.body).center(true).id='ie6-alert';
		}
		new Asset.javascript('js/pngie.js', {id: 'ie6-png'});
	}
	var menus =	['home','blog','work','contact','aboutme'];

	menus.each(function(seccio){
		if($(seccio) && (!($(seccio).hasClass('sel')))){
			new Asset.image('imgs/menu_'+seccio+'_over.png');
			$(seccio).getElement('img').addEvent('mouseover', function(){
				this.src = 'imgs/menu_'+seccio+'_over.png';
			}).addEvent('mouseout', function(){
				this.src = 'imgs/menu_'+seccio+'.png';
			});
		}
	});

	var cercabotoLink = new Element('a').setProperty('href','javascript:void(0);').setProperty('title','Search!').addEvent('click', function(){

		var val = $('cerca-text').value;
		$('cerca-barra-contingut').setHTML('Searching... <a href="javascript:void(0);" onclick="$(\'cerca-barra\').toogle(\'amaga\');" title="Cancel">Cancel</a>');
		//cerca ajax
		new Ajax('blog/_ajax.php', {
			onComplete: function(response){
				response = Json.evaluate(response);
				var tw =	'<div>'+(response.length)+' results for &quot;'+val.htmlEntities()+'&quot; (<a href="javascript:void(0);" title="Hide" onclick="$(\'cerca-barra\').toogle();">hide</a>)</div>' +
								'<ul>';
				response.each(function(res){
					tw +=			'<li><a href="blog/post.php?id='+res.id+'" title="'+res.title.htmlEntities()+'">'+res.title.htmlEntities()+'</a></li>';
				});
				tw +=			'</ul>';
				$('cerca-barra-contingut').setHTML(tw);
			},
			postBody: Object.toQueryString({
				's': val
			})
		}).request();
		$('cerca-barra').toogle('mostra');
	});
	var cercabotoImg = new Element('img').setProperty('src','imgs/cerca-boto-blau.png').setProperty('title','Search!').setProperty('alt','Search!').setProperty('id','cerca-boto');
	$('cerca-boto').replaceWith(cercabotoLink).adopt(cercabotoImg);
	$('cerca-img').addEvent('click', function(){
		$('cerca-boto').focus();
	});
	$('cerca').onsubmit = function(){
		$('cerca-boto').getParent().fireEvent('click');
		return false;
	};
	$('cerca-text').addEvent('focus', function(){
		if(this.value=='Search') this.value = '';
	}).addEvent('blur', function(){
		if(this.value.replaceAll(' ','')=='') this.value = 'Search';
	}).value = 'Search';
	
	var barraCerca = new Element('div').setProperty('id','cerca-barra').setHTML('<div id="cerca-barra-contingut"></div>').injectInside(document.body).setOpacity(0.95);
	barraCerca.efecte = $('cerca-barra').effect('width', {duration: 1000});
	barraCerca.toogle = function(accio){
		var ample = 252;
		var This = $('cerca-barra');
		var accio2 = (This.getStyle('width') == '0px') ? 'mostra' : 'amaga';

		if(!accio) var accio = accio2;
		if(accio!=accio2) return false;

		var ampleInicial = (accio=='mostra') ? 0 : ample;
		var ampleFinal = (accio=='mostra') ? ample : 0;

		var setStylePosition = function(que, qui){
			if($type(qui)!='array') qui = [qui];
			qui.each(function(el){
				$(el).setStyle('position',que);
			});
		}
		setStylePosition((accio=='mostra') ? 'fixed' : 'absolute', ['cerca-boto', 'cerca-text', 'cerca-img']);

		This.efecte.start(ampleInicial, ampleFinal);
	};

});
