$(document).ready(function() {
	
	$(function () {
		var tabContainers = $('div.tabs > div');
		tabContainers.hide().filter(':first').show();
		
		$('div.tabs ul.tabNavigation a').click(function () {
			tabContainers.hide();
			tabContainers.filter(this.hash).show();
			$('div.tabs ul.tabNavigation a').removeClass('selected');
			$(this).addClass('selected');
			return false;
		}).filter(':first').click();
	});
	
	/* Event Album Tweeking */
	$('.event-album ul li:nth-child(5n)').addClass('end-row');
	$('.event-album ul li:nth-child(5n+1)').addClass('first-row');
	$('.event-album ul li a.img img').verticalAlignEvent();
	$('.event-album ul li span.img img').verticalAlignEvent();
	
	/* Photo Album Tweeking */
	$('.photo-album ul li:nth-child(5n)').addClass('end-row');
	$('.photo-album ul li:nth-child(5n+1)').addClass('first-row');
	$('.photo-album ul li a img').verticalAlignPhoto();
	
	$('.photo-zoom-photo p img').verticalAlignPhoto();
	
	Date.format = 'dd/mm/yyyy';
	
	$('.date-pick').datePicker({startDate:'01/01/1996', clickInput:true });
	
	$('.search-cal').datePicker({startDate:'01/01/1996' }).bind('dateSelected', function () {
		$('#nav_search form').submit();
	});
	
	$('.tooltip').tipsy({ gravity:'n', fade:true });
	
	$('a[rel*=facebox]').facebox({
		loading_image:'loading.gif',
		close_image:'closelabel.gif'
	});

});

/* ----------------------
   Scripts
---------------------- */

function sfHover() {
	if (document.all&&document.getElementById) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" over";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" over\\b"), "");
			}
		}
	}
}

/* Popup:
popup(URL, Width, Height, Left, Top, AutoCenter[0=False, 1=True], FullScreen[0=False, 1=True])
<a href="javascript:popUp('domain', 640, 480, 50, 50, 1, 0)">Popup!</a>
*/
function popup(URL, popWidth, popHeight, popLeft, popTop, autoCenter, fullScreen) {
	day = new Date();
	id = day.getTime();
	if ( autoCenter == 1 ) {
		var popLeft = (screen.width - popWidth) / 2;
		var popTop = (screen.height - popHeight) / 2;
	}
	if ( fullScreen == 1 ) {
		// Open in Full Screen window!
		eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+(screen.width-10)+",height="+(screen.height-26)+",left=0,top=0');");
	} else {
		// Open in normal window!
		eval("page"+id+" = window.open(URL, '"+id+"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+popWidth+",height="+popHeight+",left="+popLeft+",top="+popTop+"');");
	}
}

/* Function for modal window */

function showPopUp(el) {
	var cvr = document.getElementById("cover")
	var dlg = document.getElementById(el)
	cvr.style.display = "block"
	dlg.style.display = "block"
	dlg.style.left = (screen.width / 2) - 200 
	dlg.style.top = (screen.height / 2) - 150
	if (document.body.style.overflow = "hidden") {
		cvr.style.width = screen.width
		cvr.style.height = screen.height
	}
}

function closePopUp(el) {
	var cvr = document.getElementById("cover")
	var dlg = document.getElementById(el)
	cvr.style.display = "none"
	dlg.style.display = "none"
	document.body.style.overflowY = "scroll"
}

/* Function for Event Album */
(function($) {
	$.fn.verticalAlignEvent = function(opts) {
		return this.each(function() {
			if ( $(this).parent().height() != $(this).height()) {
				var top = (($(this).parent().height() - $(this).height()) / 2);
				if ( top > 2 ) {
					$(this).css('margin-top', top);
				}
			}
		});
	};
})(jQuery);

/* Function for Photo Album */
(function($) {
	$.fn.verticalAlignPhoto = function(opts) {
		return this.each(function() {
			if ( $(this).parent().parent().height() != $(this).height() ) {
				var top = (($(this).parent().parent().height() - $(this).height()) / 2);
				if ( top > 2 ) {
					$(this).css('margin-top', top);
				}
			}
		});
	};
})(jQuery);


/* ----------------------
   Date
---------------------- */

Date.dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
Date.abbrDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
Date.monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
Date.abbrMonthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
Date.firstDayOfWeek = 1;
Date.format = 'dd/mm/yyyy';
Date.fullYearStart = '20';

(function() {
	function add(name, method) {
		if( !Date.prototype[name] ) {
			Date.prototype[name] = method;
		}
	};
	add("isLeapYear", function() {
		var y = this.getFullYear();
		return (y%4==0 && y%100!=0) || y%400==0;
	});
	add("isWeekend", function() {
		return this.getDay()==0 || this.getDay()==6;
	});
	add("isWeekDay", function() {
		return !this.isWeekend();
	});
	add("getDaysInMonth", function() {
		return [31,(this.isLeapYear() ? 29:28),31,30,31,30,31,31,30,31,30,31][this.getMonth()];
	});
	add("getDayName", function(abbreviated) {
		return abbreviated ? Date.abbrDayNames[this.getDay()] : Date.dayNames[this.getDay()];
	});
	add("getMonthName", function(abbreviated) {
		return abbreviated ? Date.abbrMonthNames[this.getMonth()] : Date.monthNames[this.getMonth()];
	});
	add("getDayOfYear", function() {
		var tmpdtm = new Date("1/1/" + this.getFullYear());
		return Math.floor((this.getTime() - tmpdtm.getTime()) / 86400000);
	});
	add("getWeekOfYear", function() {
		return Math.ceil(this.getDayOfYear() / 7);
	});
	add("setDayOfYear", function(day) {
		this.setMonth(0);
		this.setDate(day);
		return this;
	});
	add("addYears", function(num) {
		this.setFullYear(this.getFullYear() + num);
		return this;
	});
	add("addMonths", function(num) {
		var tmpdtm = this.getDate();
		this.setMonth(this.getMonth() + num);
		if (tmpdtm > this.getDate())
			this.addDays(-this.getDate());
		return this;
	});
	add("addDays", function(num) {
		this.setTime(this.getTime() + (num*86400000) );
		return this;
	});
	add("addHours", function(num) {
		this.setHours(this.getHours() + num);
		return this;
	});
	add("addMinutes", function(num) {
		this.setMinutes(this.getMinutes() + num);
		return this;
	});
	add("addSeconds", function(num) {
		this.setSeconds(this.getSeconds() + num);
		return this;
	});
	add("zeroTime", function() {
		this.setMilliseconds(0);
		this.setSeconds(0);
		this.setMinutes(0);
		this.setHours(0);
		return this;
	});
	add("asString", function(format) {
		var r = format || Date.format;
		return r
			.split('yyyy').join(this.getFullYear())
			.split('yy').join((this.getFullYear() + '').substring(2))
			.split('mmmm').join(this.getMonthName(false))
			.split('mmm').join(this.getMonthName(true))
			.split('mm').join(_zeroPad(this.getMonth()+1))
			.split('dd').join(_zeroPad(this.getDate()));
	});
	Date.fromString = function(s)
	{
		var f = Date.format;
		var d = new Date('01/01/1977');
		
		var mLength = 0;

		var iM = f.indexOf('mmmm');
		if (iM > -1) {
			for (var i=0; i<Date.monthNames.length; i++) {
				var mStr = s.substr(iM, Date.monthNames[i].length);
				if (Date.monthNames[i] == mStr) {
					mLength = Date.monthNames[i].length - 4;
					break;
				}
			}
			d.setMonth(i);
		} else {
			iM = f.indexOf('mmm');
			if (iM > -1) {
				var mStr = s.substr(iM, 3);
				for (var i=0; i<Date.abbrMonthNames.length; i++) {
					if (Date.abbrMonthNames[i] == mStr) break;
				}
				d.setMonth(i);
			} else {
				d.setMonth(Number(s.substr(f.indexOf('mm'), 2)) - 1);
			}
		}
		
		var iY = f.indexOf('yyyy');

		if (iY > -1) {
			if (iM < iY)
			{
				iY += mLength;
			}
			d.setFullYear(Number(s.substr(iY, 4)));
		} else {
			if (iM < iY)
			{
				iY += mLength;
			}
			d.setFullYear(Number(Date.fullYearStart + s.substr(f.indexOf('yy'), 2)));
		}
		var iD = f.indexOf('dd');
		if (iM < iD)
		{
			iD += mLength;
		}
		d.setDate(Number(s.substr(iD, 2)));
		if (isNaN(d.getTime())) {
			return false;
		}
		return d;
	};
	var _zeroPad = function(num) {
		var s = '0'+num;
		return s.substring(s.length-2)
	};
})();
