/*
 * Subject to copyright.
 *
 * Web Development - LOOKsystems Limited
 * mailto:info@looksystems.ltd.uk
 * http://www.looksystems.ltd.uk
 *
 */

function label2value(){

	var inactive = "inactive";
	var active = "active";
	var focused = "focused";

	$("label").each(function(){
		obj = document.getElementById($(this).attr("for"));
		if(($(obj).attr("type") == "text") || (obj.tagName.toLowerCase() == "textarea") || (obj.tagName.toLowerCase() == "select")){
			var text = $(this).text();
			$(this).css("display","none");
			if ($(obj).val() != "" && $(obj).val() != text) $(obj).addClass(active);
			else {
				$(obj).val(text);
				$(obj).addClass(inactive);
			}
			$(obj).focus(function(){
				$(this).addClass(focused);
				$(this).removeClass(inactive);
				$(this).removeClass(active);
				if($(this).val() == text) $(this).val("");
			});
			$(obj).blur(function(){
				$(this).removeClass(focused);
				if($(this).val() == "") {
					$(this).val(text);
					$(this).addClass(inactive);
				} else {
					$(this).addClass(active);
				};
			});
		};
	});

	$("form").submit(function() {
		$(this).children('label').each(function() {
			if ($(this).css('display') != 'none') return;
			var text = $(this).text();
			var obj = document.getElementById($(this).attr("for"));
			if ($(obj).val() == text) $(obj).val('');
		});
	});

};

function resizeWindow() {
	var winHeight = $(window).height() - 40;
	if ($('#main').height() < winHeight) {
		$('#main').height(winHeight);
	}
}


$(document).ready(function(){

	// set focus
	var $input = $("#content input[type='text'],#content input[type='password'],#content textarea");
	if ($input.length) $input.get(0).focus();

	// hide form labels
	label2value();

	// maximise page height
	var resizeTimer = null;
	$(window).bind('resize', function() {
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(resizeWindow, 50);
	});
	resizeWindow();

	// initialise legacy script
	if (typeof(init) == 'function') init();

	// initialise lightbox
	$("a[rel^='lightbox']").prettyPhoto();
	$(".lightbox-start").click(function() {
		$("a[rel^='lightbox']:first").click();
		return false;
	});

	// initialise lightwindow
	var height_ = jQuery.fn.height;
	jQuery.fn.height = function() {
		if ( this[0] == window && jQuery.browser.opera && jQuery.browser.version >= 9.50) return window.innerHeight;
		else return height_.apply(this,arguments);
	};

	$('.lightwindow').click(function (e) {

		e.preventDefault();

		$('<div><h1>Creekside Cottages<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 - 303;
					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() });

	});

	// flash text

	if (!$.fn.flash.hasFlash(7.0)) return;

	$('body').addClass('hasFlash');

	var swffile = (typeof(fontfile) == 'undefined') ? false : fontfile;
	var swfcol = (typeof(fontcol) == 'undefined') ? '#ffffff' : fontcol;
	function swfreplace(htmlOptions) {
			htmlOptions.flashvars.txt = this.innerHTML.replace('+', '%2B');
			this.innerHTML = '<div>'+this.innerHTML+'</div>';
			var $alt = $(this.firstChild);
			htmlOptions.height = $alt.height();
			htmlOptions.width = $alt.width();
			$alt.addClass('alt');
			$(this)
				.addClass('flash-replaced')
				.prepend($.fn.flash.transform(htmlOptions));	
	}
	$('#bodytext h1').flash(
		{
			src: swffile,
			wmode: 'opaque',
			flashvars: {
				css: '* { color: '+swfcol+'; }'
			}
		},
		{ version: 7 },
		swfreplace
	);
	$('#bodytext h2 span.cottage_features').flash(
		{
			src: swffile,
			wmode: 'opaque',
			flashvars: {
				css: '* { color: #ffffff; text-align: right; }'
			}
		},
		{ version: 7 },
		swfreplace
	);
	$('#bodytext h2 span.cottage_name').flash(
		{
			src: swffile,
			wmode: 'opaque',
			flashvars: {
				css: '* { color: #ffffff; }'
			}
		},
		{ version: 7 },
		swfreplace
	);

});