/*========================================================

 LEGACY LAYER - updated to use MooTools 1.2+ API.
                Further additions are to be implemented in
				Common.js or a new Class.

 @site		Iperbole
 @company	Keepthinking LTD
 @address	http://keepthinking.it
 @author	Oskar Krawczyk (o.krawczyk@keepthinking.it)
 @rev		25/07/2007
 ========================================================*/

var sendForm;

window.addEvent('domready', function(){
	$$('.ajaxForm').each(function(formEl){
		formEl.addEvents({
			submit: function(e){
				e.stop();
			}
		});
	});
	
	sendForm = function(method, form_id, controller, statusContainer, status) {
		controller = $pick(controller, 'common');
		statusContainer = $pick(statusContainer, 'status-container');
		status = $pick(status, 'status');

		new Request.HTML({
			url: absoluteUrl+controller+'/'+method+'/',
			data: $($pick(form_id, 'theform')), 
			onActive: $(statusContainer).innerHTML = '<p id="status" class="loading"><img src="'+templateUrl+'img/ajax-loader.gif" alt="Loader." /> Trasmissione del messaggio in corso ...</p>',
			update: statusContainer
		}).send();
	};
	
});

subscription = function(postBody, section) {
	new Request({
		url: absoluteUrl+section+'/send_subscription/',
		data: $(postBody), 
		onActive: $('status-container-'+postBody).innerHTML = '<p id="status" class="loading"><img src="'+templateUrl+'img/ajax-loader.gif" alt="Loader." /> Trasmissione del messaggio in corso ...</p>',
		update: 'status-container-'+postBody,
		onFailure: (function() {
			$('status-container-'+postBody).innerHTML = '<p id="status" class="oops">Inserire un valido indirizzo email e selezionare almeno una newsletter.</p>';
			Notifier.warning('Inserire un valido indirizzo email e selezionare almeno una newsletter.');
		})
	}).send();
};

toggleOc = function(elToggled, elToToggle, elHighlight) {
	
	var toggled = $(elToggled);
	
	if(toggled.className == 'opened') {
		toggled.className = 'closed';
		toggled.set('html', 'Vedi');
		$(elToToggle).removeClass('show');
		$(elToToggle).addClass('hide');
	} else {
		toggled.className = 'opened';
		toggled.set('html', 'Chiudi');
		$(elToToggle).removeClass('hide');
		$(elToToggle).addClass('show');
	}
	
	//$(elToToggle).toggleClass('show');
	
	if(elHighlight) {
		if(toggled.className == 'opened') {
			$(elHighlight).addClass('highlighted');
		} else {
			$(elHighlight).removeClass('highlighted');
		}
	}
};

window.onload = function() {
	
	$$('img.imgEl').each(function(el) {
		el.getParent().getParent().setStyle('width', el.getSize().x);
	});
			
	if($('theform')) {
		var addAjaxSubmit = $('theform');
		addAjaxSubmit.set('action', 'psc/send_mail/');
	}
	
	var popups = $$("#popup .popup");
	popups.each(function(popup) {
		popup.addEvent("click", function(e) {
			var newWindow = window.open(popup.get('href'), '_blank');
			newWindow.focus();
			e.stop();
		});
	});
	
	var slideshowImage = $("slideshow-image");
	var slideshowDesc = $("slideshow-desc");
	var slideshow = $$("#slideshow .slide");
	slideshow.each(function(slide) {
		slide.addEvent("click", function(e) {			
			var newImageSrc = slide.get('href');
			var newImageTitle = slide.get('title');
			slideshowImage.set('src', newImageSrc);
			slideshowDesc.set('text', newImageTitle);
			
			e.stop();
		});
	});
	
	if($('theform')) {
		$('contactName').onfocus = function(el) {
			if(this.value == 'Nome') {
				this.value = '';
			}
		};
	
		$('contactName').onblur = function(el) {
			if(this.value == '') {
				this.value = 'Nome';
			}
		};
	
		$('contactMail').onfocus = function(el) {
			if(this.value == 'E-mail') {
				this.value = '';
			}
		};
	
		$('contactMail').onblur = function(el) {
			if(this.value == '') {
				this.value = 'E-mail';
			}
		};
	
		$('contactSubject').onfocus = function(el) {
			if(this.value == 'Oggetto') {
				this.value = '';
			}
		};
	
		$('contactSubject').onblur = function(el) {
			if(this.value == '') {
				this.value = 'Oggetto';
			}
		};
	
		$('contactMessage').onfocus = function(el) {
			if(this.value == 'Messaggio') {
				this.value = '';
			}
		};
	
		$('contactMessage').onblur = function(el) {
			if(this.value == '') {
				this.value = 'Messaggio';
			}
		};
	}
	
	if($('login')) {
		var loginForm = $('login');
		
		$('show-login').addEvent('click', function(e) {
			if(loginForm.hasClass('display')) {
				loginForm.setStyle('display', 'none');
				loginForm.removeClass('display');
				loginForm.addClass('hide');
			} else {
				loginForm.setStyle('display', 'block');
				loginForm.removeClass('hide');
				loginForm.addClass('display');
			}
		});
	}
	
	
	/* BUILT THE TOC */
	$$('.tocItem').each(function(el) {
		try {
			new Element('li', {'class': 'tocItemCont'}).set('html', '<a href="#'+el.id+'">'+el.get('text')+'</a>').inject($('content-toc'), 'inside');
		} catch(d) {}

	});
};
