var Monperla = Class.create();
Monperla.prototype = {
	initialize: function(links, photos) {
		this.justloaded = true;
		this.currentLink = undefined;
		this.links = links;
		this.contentEl = $('content');
		this.oe24Squares = new e24Squares( 'visor', photos,
			{ 
				base: 'images/', 
				width: 832, 
				height: 201, 
				rows: 3, 
				cols: 8, 
				hideonfinish: false, 
				autostart: false, 
				duration: 1.0, 
				interval: 1.0
			}
		);
		this.oe24Squares.start();			

		this.protoHistoryManager = new ProtoHistoryManager();
		this.reqHistory = this.protoHistoryManager.register(
			'pages',
			[this.links[0]], // default, page 0
			function(values) {
				var index = this.links.indexOf(values[0]);
				if (index == 0 && this.justloaded) { 
					$$('div.product').each(function(prod) {
							prod.observe('mouseover', showgotoshopping);
					}.bind(this));	

					return; 
				}
				
				if (index != -1) {
					this.justloaded = false;
					this.linkClick(values[0], index);
				}	
			}.bind(this)
		);			
		this.protoHistoryManager.start();
		
		this.links.each(function(linkid, index) {
			$(linkid).observe('click', function(e) {
				e.stop();
				this.linkClick(linkid, index);
				this.reqHistory.setValue(0, linkid);
			}.bind(this));
			$$('a.cls_' + linkid).each(function(link) {
				link.observe('click', function(e) {
					e.stop();
					this.linkClick(linkid, index);
					this.reqHistory.setValue(0, linkid);
				}.bind(this));
			}.bind(this));	
		}.bind(this));
		if($('fbsubmit'))
					{
						$('fbsubmit').observe('click', mail);
					}
	},
	
	linkClick: function(linkid, index) {
		var linkEl = $(linkid);
		if (this.currentLink != undefined) {
			this.currentLink.link.removeClassName(this.currentLink.linkid + 'activa');
		}	
		linkEl.addClassName(linkid + 'activa');
		this.currentLink = {link: linkEl, linkid: linkid, trans: index + 1};
		this.cleanContent();
	},
	
	cleanContent: function () {
		new Effect.Appear(this.contentEl, {from: 1.0, to: 0.0, duration: 1.0, afterFinish: this.getPage.bind(this)})
	},
	
	getPage: function () {
	    if (this.currentLink != undefined) {
			var url = this.currentLink.link.readAttribute('href');
			new Ajax.Updater('content', url + '?ajax=y',{
				encoding:'utf-8',
				onSuccess: function() {
					new Effect.Appear ( this.contentEl, {from: 0.0, to: 1.0, duration: 1.0, afterFinish: this.transition.bind(this)});
				}.bind(this),
				onComplete: function() {
					//Aņadiendo los eventos a los enlaces internos
					this.links.each(function(linkid, index) {
						$$('a.cls_' + linkid).each(function(link) {
							link.observe('click', function(e) {
								e.stop();
								this.linkClick(linkid, index);
								this.reqHistory.setValue(0, linkid);
							}.bind(this));
						}.bind(this));	
					}.bind(this));
					
					$$('div.product').each(function(prod) {
							prod.observe('mouseover', showgotoshopping);
					}.bind(this));	
						
						
					if($('fbsubmit'))
					{
						$('fbsubmit').observe('click', mail);
					}
				}.bind(this)
			});
		}	
	},
	
	transition: function (obj){
	    if (this.currentLink != undefined) {
			this.oe24Squares.transition(this.currentLink.trans);
		}	
	}
	
	
}	

Event.onDOMReady(function() {
	$('m1').observe('click', setParagraphSize);
	$('m2').observe('click', setParagraphSize);
	$('m3').observe('click', setParagraphSize);
	
	/*$('castellano').observe('click', gotocastellano);
	$('english').observe('click', gotoenglish);*/
	
	new Monperla(
		['quienes', 	 'perla', 		'freshwater',  'tahiti',      'australiana', 'mabe',        'coral',       'turquesa',    'ayuda',       'contacto', 'privacy'],		
		[ 'header1.gif', 'header2.gif', 'header4.gif', 'header3.gif','header5.gif', 'header6.gif', 'header7.gif', 'header8.gif', 'header9.gif', 'header10.gif', 'header11.gif', 'header3.gif' ]
	);	
});		

function setParagraphSize(e){
e.stop();
var size = 11;
if(this.id == 'm1') size = 11;
if(this.id == 'm2') size = 14;
if(this.id == 'm3') size = 16;

var textSize;
textSize = size + 'px';
$('leftcolone').setStyle({
  fontSize: textSize
});
$('rightcolone').setStyle({
  fontSize: textSize
});
}


function mail(){
	var name = $('name').getValue();
	//var name = document.getElementById('name').value
	var email = $('email').getValue();
	var phone = $('phone').getValue();
	var message = $('message').getValue();
	var subject = $('subject').getValue();
	var accept = $('accept').getValue();
	var answer = $('answer');
	var url = 'contact.php';
	new Ajax.Request(url, {
  method: 'post',
  parameters:  	{name: name, email: email, phone: phone, message: message, subject: subject, accept: accept},
  onSuccess: function(transport) {
	answer.update(transport.responseText);  
	answer.innerHTML;
	Effect.Appear('answer', {from:0.0,to:1.0,duration:1.0});
  }
});
	
	
}

function showgotoshopping(e){
	this.stopObserving('mouseover', showgotoshopping); 
	var curlink = this.select('p.price a').first();
	
	var prodbtn = new Element('div', { 'class': 'prodbtn' });
	prodbtn.setOpacity(0);

	var prodlink = new Element('a', { href: curlink.readAttribute('href') }).update('<img src="images/gotoshop.gif"/>');;
	prodbtn.appendChild(prodlink);
	this.appendChild(prodbtn);

	Effect.Fade(this.firstDescendant(), {from:1.0,to:0.6,duration:1.0});
	Effect.Appear(prodbtn, {from:0.0,to:1.0,duration:1.0});
}
	

