/*
	Slimbox v1.41 - The ultimate lightweight Lightbox clone
	by Christophe Beyls (http://www.digitalia.be) - MIT-style license.
	Inspired by the original Lightbox v2 by Lokesh Dhakar.
*/

var Lightbox = {

	init: function(options){
		this.options = $extend({
			resizeDuration: 400,
			resizeTransition: false,	// default transition
			initialWidth: 250,
			initialHeight: 250,
			animateCaption: true,
			showCounter: true
		}, options || {});

		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^lightbox/i)){
				el.onclick = this.click.pass(el, this);
				this.anchors.push(el);
			}
		}, this);
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);

		this.overlay = new Element('div', {'id': 'lbOverlay'}).injectInside(document.body);

		this.center = new Element('div', {'id': 'lbCenter', 'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight, 'marginLeft': -(this.options.initialWidth/2), 'display': 'none'}}).injectInside(document.body);
		this.image = new Element('div', {'id': 'lbImage'}).injectInside(this.center);
		this.prevLink = new Element('a', {'id': 'lbPrevLink', 'href': '#', 'styles': {'display': 'none'}}).injectInside(this.image);
		this.nextLink = this.prevLink.clone().setProperty('id', 'lbNextLink').injectInside(this.image);
		this.prevLink.onclick = this.previous.bind(this);
		this.nextLink.onclick = this.next.bind(this);

		this.bottomContainer = new Element('div', {'id': 'lbBottomContainer', 'styles': {'display': 'none'}}).injectInside(document.body);
		this.bottom = new Element('div', {'id': 'lbBottom'}).injectInside(this.bottomContainer);
		new Element('a', {'id': 'lbCloseLink', 'href': '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		this.caption = new Element('div', {'id': 'lbCaption'}).injectInside(this.bottom);
		this.number = new Element('div', {'id': 'lbNumber'}).injectInside(this.bottom);
		new Element('div', {'styles': {'clear': 'both'}}).injectInside(this.bottom);

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			resize: this.center.effects($extend({duration: this.options.resizeDuration, onComplete: nextEffect}, this.options.resizeTransition ? {transition: this.options.resizeTransition} : {})),
			image: this.image.effect('opacity', {duration: 500, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 400, onComplete: nextEffect})
		};

		this.preloadPrev = new Image();
		this.preloadNext = new Image();
	},

	click: function(link){
		if (link.rel.length == 8) return this.show(link.href, link.title);

		var j, imageNum, images = [];
		this.anchors.each(function(el){
			if (el.rel == link.rel){
				for (j = 0; j < images.length; j++) if(images[j][0] == el.href) break;
				if (j == images.length){
					images.push([el.href, el.title]);
					if (el.href == link.href) imageNum = j;
				}
			}
		}, this);
		return this.open(images, imageNum);
	},

	show: function(url, title){
		return this.open([[url, title]], 0);
	},

	open: function(images, imageNum){
		this.images = images;
		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + (window.getHeight() / 15);
		this.center.setStyles({top: this.top, display: ''});
		this.fx.overlay.start(0.8);
		return this.changeImage(imageNum);
	},

	position: function(){
		this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getHeight()});
	},

	setup: function(open){
		var elements = $A(document.getElementsByTagName('object'));
		elements.extend(document.getElementsByTagName(window.ie ? 'select' : 'embed'));
		elements.each(function(el){
			if (open) el.lbBackupStyle = el.style.visibility;
			el.style.visibility = open ? 'hidden' : el.lbBackupStyle;
		});
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
		this.step = 0;
	},

	keyboardListener: function(event){
		switch (event.keyCode){
			case 27: case 88: case 67: this.close(); break;
			case 37: case 80: this.previous(); break;	
			case 39: case 78: this.next();
		}
	},

	previous: function(){
		return this.changeImage(this.activeImage-1);
	},

	next: function(){
		return this.changeImage(this.activeImage+1);
	},

	changeImage: function(imageNum){
		if (this.step || (imageNum < 0) || (imageNum >= this.images.length)) return false;
		this.step = 1;
		this.activeImage = imageNum;

		this.bottomContainer.style.display = this.prevLink.style.display = this.nextLink.style.display = 'none';
		this.fx.image.hide();
		this.center.className = 'lbLoading';

		this.preload = new Image();
		this.preload.onload = this.nextEffect.bind(this);
		this.preload.src = this.images[imageNum][0];
		return false;
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.center.className = '';
			this.image.style.backgroundImage = 'url('+this.images[this.activeImage][0]+')';
			this.image.style.width = this.bottom.style.width = this.preload.width+'px';
			this.image.style.height = this.prevLink.style.height = this.nextLink.style.height = this.preload.height+'px';

			this.caption.setHTML(this.images[this.activeImage][1] || '');
			this.number.setHTML((!this.options.showCounter || (this.images.length == 1)) ? '' : 'Image '+(this.activeImage+1)+' of '+this.images.length);

			if (this.activeImage) this.preloadPrev.src = this.images[this.activeImage-1][0];
			if (this.activeImage != (this.images.length - 1)) this.preloadNext.src = this.images[this.activeImage+1][0];
			if (this.center.clientHeight != this.image.offsetHeight){
				this.fx.resize.start({height: this.image.offsetHeight});
				break;
			}
			this.step++;
		case 2:
			if (this.center.clientWidth != this.image.offsetWidth){
				this.fx.resize.start({width: this.image.offsetWidth, marginLeft: -this.image.offsetWidth/2});
				break;
			}
			this.step++;
		case 3:
			this.bottomContainer.setStyles({top: this.top + this.center.clientHeight, height: 0, marginLeft: this.center.style.marginLeft, display: ''});
			this.fx.image.start(1);
			break;
		case 4:
			if (this.options.animateCaption){
				this.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.style.height = '';
				this.fx.bottom.start(0);
				break;
			}
			this.bottomContainer.style.height = '';
		case 5:
			if (this.activeImage) this.prevLink.style.display = '';
			if (this.activeImage != (this.images.length - 1)) this.nextLink.style.display = '';
			this.step = 0;
		}
	},

	close: function(){
		if (this.step < 0) return;
		this.step = -1;
		if (this.preload){
			this.preload.onload = Class.empty;
			this.preload = null;
		}
		for (var f in this.fx) this.fx[f].stop();
		this.center.style.display = this.bottomContainer.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return false;
	}
};

window.addEvent('domready', Lightbox.init.bind(Lightbox));
/* Copyright (c) 2006 Patrick Fitzgerald */

function tabberObj(argsObj)
{var arg;this.div=null;this.classMain="tabber";this.classMainLive="tabberlive";this.classTab="tabbertab";this.classTabDefault="tabbertabdefault";this.classNav="tabbernav";this.classTabHide="tabbertabhide";this.classNavActive="tabberactive";this.titleElements=['h2','h3','h4','h5','h6'];this.titleElementsStripHTML=true;this.removeTitle=true;this.addLinkId=false;this.linkIdFormat='<tabberid>nav<tabnumberone>';for(arg in argsObj){this[arg]=argsObj[arg];}
this.REclassMain=new RegExp('\\b'+this.classMain+'\\b','gi');this.REclassMainLive=new RegExp('\\b'+this.classMainLive+'\\b','gi');this.REclassTab=new RegExp('\\b'+this.classTab+'\\b','gi');this.REclassTabDefault=new RegExp('\\b'+this.classTabDefault+'\\b','gi');this.REclassTabHide=new RegExp('\\b'+this.classTabHide+'\\b','gi');this.tabs=new Array();if(this.div){this.init(this.div);this.div=null;}}
tabberObj.prototype.init=function(e)
{var
childNodes,i,i2,t,defaultTab=0,DOM_ul,DOM_li,DOM_a,aId,headingElement;if(!document.getElementsByTagName){return false;}
if(e.id){this.id=e.id;}
this.tabs.length=0;childNodes=e.childNodes;for(i=0;i<childNodes.length;i++){if(childNodes[i].className&&childNodes[i].className.match(this.REclassTab)){t=new Object();t.div=childNodes[i];this.tabs[this.tabs.length]=t;if(childNodes[i].className.match(this.REclassTabDefault)){defaultTab=this.tabs.length-1;}}}
DOM_ul=document.createElement("ul");DOM_ul.className=this.classNav;for(i=0;i<this.tabs.length;i++){t=this.tabs[i];t.headingText=t.div.title;if(this.removeTitle){t.div.title='';}
if(!t.headingText){for(i2=0;i2<this.titleElements.length;i2++){headingElement=t.div.getElementsByTagName(this.titleElements[i2])[0];if(headingElement){t.headingText=headingElement.innerHTML;if(this.titleElementsStripHTML){t.headingText.replace(/<br>/gi," ");t.headingText=t.headingText.replace(/<[^>]+>/g,"");}
break;}}}
if(!t.headingText){t.headingText=i+1;}
DOM_li=document.createElement("li");t.li=DOM_li;DOM_a=document.createElement("a");DOM_a.appendChild(document.createTextNode(t.headingText));DOM_a.href="javascript:void(null);";DOM_a.title=t.headingText;DOM_a.onclick=this.navClick;DOM_a.tabber=this;DOM_a.tabberIndex=i;if(this.addLinkId&&this.linkIdFormat){aId=this.linkIdFormat;aId=aId.replace(/<tabberid>/gi,this.id);aId=aId.replace(/<tabnumberzero>/gi,i);aId=aId.replace(/<tabnumberone>/gi,i+1);aId=aId.replace(/<tabtitle>/gi,t.headingText.replace(/[^a-zA-Z0-9\-]/gi,''));DOM_a.id=aId;}
DOM_li.appendChild(DOM_a);DOM_ul.appendChild(DOM_li);}
e.insertBefore(DOM_ul,e.firstChild);e.className=e.className.replace(this.REclassMain,this.classMainLive);this.tabShow(defaultTab);if(typeof this.onLoad=='function'){this.onLoad({tabber:this});}
return this;};tabberObj.prototype.navClick=function(event)
{var
rVal,a,self,tabberIndex,onClickArgs;a=this;if(!a.tabber){return false;}
self=a.tabber;tabberIndex=a.tabberIndex;a.blur();if(typeof self.onClick=='function'){onClickArgs={'tabber':self,'index':tabberIndex,'event':event};if(!event){onClickArgs.event=window.event;}
rVal=self.onClick(onClickArgs);if(rVal===false){return false;}}
self.tabShow(tabberIndex);return false;};tabberObj.prototype.tabHideAll=function()
{var i;for(i=0;i<this.tabs.length;i++){this.tabHide(i);}};tabberObj.prototype.tabHide=function(tabberIndex)
{var div;if(!this.tabs[tabberIndex]){return false;}
div=this.tabs[tabberIndex].div;if(!div.className.match(this.REclassTabHide)){div.className+=' '+this.classTabHide;}
this.navClearActive(tabberIndex);return this;};tabberObj.prototype.tabShow=function(tabberIndex)
{var div;if(!this.tabs[tabberIndex]){return false;}
this.tabHideAll();div=this.tabs[tabberIndex].div;div.className=div.className.replace(this.REclassTabHide,'');this.navSetActive(tabberIndex);if(typeof this.onTabDisplay=='function'){this.onTabDisplay({'tabber':this,'index':tabberIndex});}
return this;};tabberObj.prototype.navSetActive=function(tabberIndex)
{this.tabs[tabberIndex].li.className=this.classNavActive;return this;};tabberObj.prototype.navClearActive=function(tabberIndex)
{this.tabs[tabberIndex].li.className='';return this;};function tabberAutomatic(tabberArgs)
{var
tempObj,divs,i;if(!tabberArgs){tabberArgs={};}
tempObj=new tabberObj(tabberArgs);divs=document.getElementsByTagName("div");for(i=0;i<divs.length;i++){if(divs[i].className&&divs[i].className.match(tempObj.REclassMain)){tabberArgs.div=divs[i];divs[i].tabber=new tabberObj(tabberArgs);}}
return this;}
function tabberAutomaticOnLoad(tabberArgs)
{var oldOnLoad;if(!tabberArgs){tabberArgs={};}
oldOnLoad=window.onload;if(typeof window.onload!='function'){window.onload=function(){tabberAutomatic(tabberArgs);};}else{window.onload=function(){oldOnLoad();tabberAutomatic(tabberArgs);};}}
if(typeof tabberOptions=='undefined'){tabberAutomaticOnLoad();}else{if(!tabberOptions['manualStartup']){tabberAutomaticOnLoad(tabberOptions);}}



/*
 * Calendar Emoxion
 * with Mootools
 * Manuel Garcia (thekeeper)
 * http://www.mgarcia.info
 * Version 0.2
 *
 * Copyright (c) 2007 Manuel Garcia
 * http://www.opensource.org/licenses/mit-license.php
 */
var Calendar = new Class({
    initialize: function(el,open,Config) {
		this.input = el;
		/* configuration */
		if (!Config) this.config = {Lng: cal_lng};
		this.month_name = this.config.Lng.month;
		this.day_name =  this.config.Lng.day;
		this.create_calendar();
    },
    create_calendar: function() {
		var position = this.input.getCoordinates();
		if ($('ncalendar')) $('ncalendar').remove();
		// content div  //
		this.div = new Element('div').setStyles({'top':(position.top+position.height)+'px', 'left':(position.left)+'px','visibility':'hidden'}).setProperty('id', 'ncalendar').injectAfter($("container"));
		this.nav();
		this.input.readOnly= true;
		this.setdate(this.input.getProperty('value'));
		this.effect(this.div,'show');
	} ,
	nav: function (today) {
		// nav
		this.calendardiv = new Element('div').injectInside(this.div);
		this.titulo = new Element('strong').injectInside(this.calendardiv);
		// next month
		this.next = new Element('div');
		this.next.setProperty('class', 'cal_next')
		this.next.innerHTML='&nbsp;';
		this.next.injectAfter(this.titulo);
		// before month
		this.before = new Element('div');
		this.before.setProperty('class', 'cal_prev')
		this.before.innerHTML='&nbsp;';
		this.before.injectBefore(this.titulo);
		// close
		this.close = new Element('div').setProperty('class', 'cal_cancel').injectAfter(this.next);
		// table
		this.table = new Element('table').injectInside(this.div);
		var thead = new Element('thead').injectInside(this.table);
		var tr = new Element('tr').injectInside(thead);

		this.day_name.each(function (day) {
			var td = new Element('th').appendText(day).injectInside(tr);
		});

		var localThis = this;
		this.close.addEvent('click', function(e) {
			localThis.div.remove();
		});
	},
	setdate : function(date) {
		// reset event nav
		this.next.removeEvents('click');
		this.before.removeEvents('click');

		if (!this.validate_date(date)) {
			this.today = new Date();
			this.today.setDate(1);
		} else {
			var dateinp = date.split('/');
			this.today = new Date(dateinp[2],dateinp[1]-1,dateinp[0],0,0,0);
		}

		this.next_m = this.today.getMonth();
		this.next_m++;

		this.titulo.innerHTML = this.month_name[this.today.getMonth()]+' ' + this.today.getFullYear();
		var localThis = this;

		// event next
		this.next.addEvent('click', function(e) {
			var date = localThis.today;
			date.setMonth(localThis.next_m+1,1);
			localThis.tbody.remove();
			$$("input.ncalendar").each(function(el,i){
				el.removeEvents('click');	
			});
			localThis.setdate(date.getDate()+'/'+date.getMonth()+'/'+date.getFullYear());
			$$("input.ncalendar").each(function(el,i){
				el.addEvent('click', function(e){
					new Calendar(el);
				});	
			});
		});
		// event before
		this.before.addEvent('click', function(e) {
			var date = localThis.today;
			date.setMonth(localThis.next_m-1,1);
			localThis.tbody.remove();
			localThis.setdate(date.getDate()+'/'+date.getMonth()+'/'+date.getFullYear());
		});
		var LastMonth = new Date(this.today.getFullYear(),this.next_m-2,1,0,0,0);

		var last = LastMonth.getMonth();
		// total days the last month
		var counter = 0;
		for (var b = 1; b <= 31; b++) {
			LastMonth.setDate(b);
			if ( LastMonth.getMonth() == last) {
				counter++;
			}
		}

		this.tbody = new Element('tbody').injectInside(this.table);
		var first_day = this.today;
		var last_day = this.today;
		this.month = this.today.getMonth();
		var tr = new Element('tr').injectInside(this.tbody);

		var day=0;

		/* first day week */
		first_day.setDate(1);
		var rest = (!first_day.getDay())? 6: first_day.getDay()-1;
		counter = counter - rest;
		for (var i= this.config.Lng.first; i <= 6; i++) {
			if (first_day.getDay() == i) {
				break;
			} else {
				counter++;
				LastMonth.setDate(counter);
				if (LastMonth.getMonth() == this.today.getMonth()) LastMonth.setMonth(this.today.getMonth()-1);
				this.create_td(tr,counter,LastMonth,'noday');
			}
		}
		(this.config.Lng.first)? brea_k = 1:brea_k = 0;
		/* everydays */
		var date_s = this.today;
		var class_Css;
		var brea_k; // breaking week
		var daycounter = 0;
		for (var i = 1; i <= 31; i++) {
			date_s.setDate(i);
			if (date_s.getMonth() == this.month) {
				daycounter++;
				if (date_s.getDay() == brea_k) {
					var tr = new Element('tr').injectInside(this.tbody);
				}
				class_Css = (!date_s.getDay())? 'sunday' : '';
				this.create_td(tr,i,date_s,class_Css);
			}
		}
		this.today.setMonth(this.month);
		this.today.setDate(daycounter);
		var NextMonth = new Date(this.today.getFullYear(),this.today.getMonth()+1,1,0,0,0);
		// finish month
		var num = date_s.getDay();
		num = (brea_k)? 7 - num: 6 - num;
		var b;
		b = (brea_k)? 0 : 6 ;
		if (this.today.getDay() != b) {
			for (var i= 1; i <= (num); i++) {
				NextMonth.setDate(i);
				this.create_td(tr,i,NextMonth,'noday');
			}
		}
		if(this.div.style.visibility!="visible")
			this.effect(this.tbody,'show');
    },
	create_td: function(tr,i,date,class_Css) {
		var localThis = this;
		var td = new Element('td');
		if (date) {
			var dia = date.getDate();
			var mes = (date.getMonth()+1);
			if (dia <= 9) dia = "0"+ dia;
			if (mes <= 9) mes = "0"+ mes;
			td.setProperty('id', dia + '/'+ mes +'/'+	date.getFullYear());
		}
		td.addEvent('click', function(e) {
			localThis.input.value = this.id;
			try{
				p=this.id.split("/");
				ar_input_calendar[localThis.input.name]=new Date(p[1]+"/"+p[0]+"/"+p[2]);
			}catch(e){}
			localThis.effect(localThis.div,'fade');
			localThis.div.remove();
		});
		td.addEvent('mouseover', function(e) {
			this.addClass('dayselected');
		});
		td.addEvent('mouseout', function(e) {
			this.removeClass('dayselected');
		});

		if (class_Css) td.addClass(class_Css);
		// Today ??
		var today = new Date();
		today = today.getDate() + "/" + (today.getMonth()+1) + "/" + today.getFullYear();
		if (date) var date_td = date.getDate() + "/" + (date.getMonth()+1) + "/" + date.getFullYear();
		if (today == date_td) td.addClass('isToday');

		td.appendText(i);
		td.injectInside(tr);
	},
	effect: function(div,op) {
		var ef = new Fx.Style(div, 'opacity', {
			duration: 500,
			transition: Fx.Transitions.quartInOut
		});
		(op == 'fade')? ef.start(1,0): ef.start(0,1);
	},
	validate_date: function (date) {
		var regex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
		return date.test(regex);
	}
});
