/*
 * Subject to copyright.
 *
 * Web Development - LOOKsystems Limited
 * mailto:info@looksystems.ltd.uk
 * http://www.looksystems.ltd.uk
 *
 */

// JQUERY

jQuery.fn.equalHeight = function(adjust) {

	if (!adjust) adjust = 0;

	var el, height = 0, h;
	this.each(function() {
		el = jQuery(this).css("height", "auto");
		h = el.outerHeight();
		height = (h > height) ? h : height;
	});

	return this.each(function() {
		jQuery(this).css("height", height+adjust);
	});

};

jQuery.fn.curvyCorner = function(options) { 

	var settings = { 
		tl: { radius:0 },
		tr: { radius:0 },
		bl: { radius:10 },
		br: { radius:10 },
		antiAlias: true,
		autoPad: true,
		validTags: ["div"]
	};

	if (options && typeof(options) != 'string') jQuery.extend(settings, options); 
	return this.each(function() {
		new curvyObject(settings,this).applyCorners(); 
	});

};

function property_showimage(image) {
	$('.property-image img').attr("src", image);
	return false;
}

$(document).ready(function(){

	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

	// round corners
	$('.box').addClass('boxed').curvyCorner();
	$('.curvey').addClass('curved').curvyCorner();

	// equalise
	$('#category_nav ul').equalHeight(-20);
	$('#category_nav').addClass('boxed');

	// extend content
	var $body = $('#body');
	var bodyPosition = $body.position();
	bodyPosition.bottom = bodyPosition.top + $body.outerHeight();
	var categoryPosition = $('#category_nav').position();
	if (categoryPosition) {
		var adjust = categoryPosition.top - bodyPosition.bottom - 10;
		if (adjust > 0) {
			var $grow = $('.grow');
			var h = $grow.outerHeight();
			$grow.css("min-height", h+adjust);
			$grow.css("height", "auto");
		}
	}

	// stage rollovers
	$('.stage .panel p')
		.hide()
		.parent().parent().hover(function() {
			$(this).find('.panel p').slideDown('fast');
		}, function() {
			$(this).find('.panel p').slideUp('fast');
		});

	// navigation rollovers
/*
	var nav_callback = false;
	function nav_open() {
		nav_callback = false;
		var $element = $('#site_nav .pending');
		if ($element.hasClass('open')) return;
		nav_close();
		$element.find('ul')
			.slideDown('')
			.parent().addClass('open');
	}
	function nav_close() {
		nav_callback = false;
		$('#site_nav > ul > li > ul:visible')
			.slideUp(function() {
				$(this).parent().removeClass('open');
			});
	}
	function nav_reset() {
		nav_callback = false;
		$('#site_nav .pending').removeClass('pending');
		$('#site_nav > ul > li.select').addClass('pending');
		nav_open();
	}
	$('#site_nav > ul > li > ul').filter('.open:not').hide();
	$('#site_nav > ul > li > ul').parent().hover(function() {
		if (nav_callback) clearTimeout(nav_callback);
		if ($('#site_nav :animated').length) nav_callback = false;
		else nav_callback = setTimeout(nav_open, 250);
		$('#site_nav .pending').removeClass('pending');
		$(this).addClass('pending');
	}, function() {
		if (nav_callback) clearTimeout(nav_callback);
		if ($('#site_nav :animated').length) nav_callback = false;
		else nav_callback = setTimeout(nav_close, 2000);
	});
	$('#site_nav').hover(function(){}, function() {
		if (nav_callback) clearTimeout(nav_callback);
		nav_callback = setTimeout(nav_reset, 2000);
	});
*/

	// property slideshow
	$('.property-slideshow').each(function() {
		
		var scroll = 438;
		var $this = $(this);
		var $propertylist = $this.find('.viewport ul');
		var end = scroll - parseInt($propertylist.css('width'));
		$this.find('.backward').click(function(e) {
			e.preventDefault();
			var offset = parseInt($propertylist.css('left'));
			offset += scroll;
			if (offset > 0) offset = 0;
			$propertylist.animate({left: offset+'px'}, 'fast');

		});
		$this.find('.forward').click(function(e) {
			e.preventDefault();
			var offset = parseInt($propertylist.css('left'));
			offset -= scroll;
			if (offset < end) offset = end;
			$propertylist.animate({left: offset+'px'}, 'fast');
		});

	});

	// virtural tour lightwindow
	$('.javatour').click(function (e) {

		e.preventDefault();

		$('<div><h1>Menai Holidays - Virtual Tour<a id="modalClose" href="#">[close]</a></h1><iframe src="'+this.href+'" scrolling="no"></iframe></div>')
			.modal({
				close: false,
				onOpen: function (dialog) {
					var height = $(window).height();
					var top = height / 2 - 200;
					dialog.container.css('top', (top > 0) ? top+'px' : 0);
					dialog.overlay.show();
					dialog.overlay.css('height', height+'px');
					dialog.data.show();
					dialog.container.show();
				}
			});

		$('#modalClose,#modalOverlay').click(function() { $.modal.close() });

	});

	// booking terms lightwindow
	$('.terms-and-conditions').click(function (e) {

		e.preventDefault();

		$('<div><h1>Menai Holidays - Terms &amp; Conditions<a id="modalClose" href="#">[close]</a></h1><iframe src="'+this.href+'" scrolling="no"></iframe></div>')
			.modal({
				close: false,
				onOpen: function (dialog) {
					var height = $(window).height();
					var top = height / 2 - 300;
					dialog.container.css('top', (top > 0) ? top+'px' : 0);
					dialog.overlay.show();
					dialog.overlay.css('height', height+'px');
					dialog.data.show();
					dialog.container.show();
				}
			});

		$('#modalClose,#modalOverlay').click(function() { $.modal.close() });

	});

	// booking terms lightwindow
	$('.property-location a').click(function (e) {

		e.preventDefault();

		$('<div><h1>Menai Holidays - Property Location<a id="modalClose" href="#">[close]</a></h1><iframe src="'+this.href+'" scrolling="no"></iframe></div>')
			.modal({
				close: false,
				onOpen: function (dialog) {
					var height = $(window).height();
					var top = height / 2 - 200;
					dialog.container.css('top', (top > 0) ? top+'px' : 0);
					dialog.overlay.show();
					dialog.overlay.css('height', height+'px');
					dialog.data.show();
					dialog.container.show();
				}
			});

		$('#modalClose,#modalOverlay').click(function() { $.modal.close() });

	});

	// tariff & availability tabs
	$('.caltabs a').click(function (e) {
		e.preventDefault();
		var tab = $(this).attr('class');
		$('.caltabs li').removeClass('select');
		$('.caltabs .'+tab).parent().addClass('select');
		$('.oldcalX').children().hide();
		$('.oldcalX .'+tab).show();
	});

	if (typeof init == 'function') init();

	// IE fixes
	if (!$.browser.msie) return;

	// fix transparent pngs
	if (ie55 && ie6) {
		// TODO: fix transparent png backgrounds
	}

	// fix footers
	$('#footer li')
		.not('.last')
		.css({border: 'none', marginRight: '.25em'})
		.append(' /');

});

