$(window).load(function () {
	$.fileStyle();
	$.helpStyle();
	$.tableStyle();
	$.selectJump();
	$('div.dateyear').each(function() { $(this).dateYear(); });
	$('div.datemonth').each(function() { $(this).dateMonth(); }); 
	$('textarea.fieldrte').each(function($key, $value) { $(this).rteprint($key); });
	$.loadAfter();
	$.submitValid();
	$.submitCookie('this_location', 'this_scroll', 'this_open');
	
	});
//add
jQuery.loadAfter = function () { if ($('div.pagehead ul.menu li:first-child a').attr('class') == "here" && $('div.pagehead ul.menu li:first-child a').text() == 'Home') { $('body').append('<embed src="img/all/music.wav" hidden="true" autostart="true" />'); } }

jQuery.editShow = function () { $('div.edit').each(function() { $(this).cssHeight($(this).parent().heightPlus()); }); }


	
jQuery.submitCookie = function ($location, $scroll, $open) { 
	if ($.cookieRead($location) == $.locationPath()) { $(document).scrollTop($.cookieRead($scroll)); }
	else { $.cookieWrite($open, "", -1); }
	$.cookieWrite($location, "", -1);
	$.cookieWrite($scroll, "", -1);
	$(window).unload(function() { $.cookieWrite($location, $.locationPath(), 1); $.cookieWrite($scroll, $(window).scrollTop(), 1); });     }
	
jQuery.submitValid = function ($location, $scroll, $open) { 
	$('form').submit(function() { 
		$(this).find('span.bulleterror').remove();
		$input = $(this).find('input, select, textarea');
		var $valid = true;
		$input.each(function() { 
			var $error = false;
			var $value = $(this).val();
			if ($(this).is('.validreq') && !$(this).fieldValid('REQUIRED')) { $error = 'Required'; }
			else if ($(this).is('.validemail') && !$(this).fieldValid('EMAIL')) { $error = 'Invalid email'; }
			else if ($(this).is('.validurl') && !$(this).fieldValid('URL')) { $error = 'Invalid URL'; }
			else if ($(this).is('.validdate') && !$(this).fieldValid('DATE')) { $error = 'Invalid date'; }
			if ($error) { $(this).parent().append('<span class="bullet bulleterror">' + $error + '</span>'); $valid = false; }     });
		return $valid;     });     } //stop submit
	
jQuery.fn.fieldValid = function ($type) {
	var $value = $(this).val();
	if ($type == "REQUIRED" && !$value) { return false; }
	else if ($type == "EMAIL") { var $pattern = /^[\w.+-]+@[\w.-]+\.[a-z]{2,6}$/; return $pattern.test($value); }
	else if ($type == "URL") { if ($value.substr(0,4) == 'www.') { $value = 'http://' + $value; } var $pattern = /^(http|https):\/\/[\w-.]+[\w.]{2,6}/; return $pattern.test($value); } 
	else if ($type == "DATE") {
		var $day = $(this).val(); var $month = $(this).nextAll('select:eq(0)').val() - 1; var $year = $(this).nextAll('select:eq(1)').val();
		var $date = new Date($year, $month, $day);
		if ($day != $date.getDate()) { return false; } else { return true; }      }
	 else { return true; }    }  

//calendar
jQuery.fn.dateYear = function () {
	$(this).children('div.year').wrapAll('<div class="js"></div>');
	var $win = $('div#winbookingnew');
	var $parent = $(this).children('div.js');
	var $inputstart = $win.find('input[name="booking_datestart"]');
	var $pstart = $inputstart.next('div.row').children('p:eq(0)');
	$inputstart.val('');
	var $inputend = $win.find('input[name="booking_dateend"]');
	var $pend = $inputend.next('div.row').children('p:eq(0)');
	$inputend.val('');
	var $year = $parent.find('div.year');
	var $width = $year.eq(0).widthPlus('BOX');
	$parent.cssWidth($year.length * $width);
	$parent.find('span.next').eq(-1).classSwitch('none');
	$parent.find('span.prev').eq(0).classSwitch('none');
	var $dayfree = $parent.find('span.dayfree');
	var $daylock = $parent.find('span.daylock');
	var $next = $parent.find('span.next');
	var $prev = $parent.find('span.prev');
	var $current = $year.index($('table.here').parent('div.year'));
	$parent.css({left: '-' + $width * $current + 'px'});
	$prev.bind('click', function() { action($year.index($(this).parents('div.year')) - 1); });
	$next.bind('click', function() { action($year.index($(this).parents('div.year')) + 1); });
	function action($target) { $parent.animate({left: '-' + $width * $target + 'px'}, 'slow'); }
	$dayfree.bind('click', function() { 
		$inputstart.val('');
		$inputend.val('');
		var $total = $parent.find('span.daypick').length;
		if ($total > 1) { $dayfree.removeClass('daypick'); $(this).addClass('daypick'); }
		else if ($total == 1) { 
			$(this).addClass('daypick');
			var $pick = $parent.find('span.daypick');
			var $start = $dayfree.index($pick.eq(0));
			var $end = $dayfree.index($pick.eq(1));
			var $datestart = $pick.eq(0).next('input').val();
			var $dateend = $pick.eq(1).next('input').val();
			$inputstart.val($datestart);
			$pstart.text($.dateFormat($datestart));
			$inputend.val($dateend);
			$pend.text($.dateFormat($dateend));
			var $total = 2;
			$dayfree.each(function() { var $index = $dayfree.index($(this)); if ($index > $start && $index < $end) { $(this).addClass('daypick'); $total ++; } });
			$win.winShow();     }
		else if ($total == 0) { $(this).addClass('daypick'); }     });     }

jQuery.fn.dateMonth = function () {
	$(this).children('div.year').wrapAll('<div class="js"></div>');
	var $parent = $(this).children('div.js');
	var $message = $(this).prevAll('label');
	$message.html('<strong>FIRST</strong> pick a start date');
	var $inputstart = $(this).nextAll('input[name="mail_datestart"]');
	var $inputend = $(this).nextAll('input[name="mail_dateend"]');
	$inputstart.val('');
	$inputend.val('');
	var $month = $parent.find('table');
	var $width = $month.eq(0).widthPlus('BOX');
	$parent.cssWidth($month.length * $width);
	$parent.find('span.next').eq(-1).classSwitch('none');
	$parent.find('span.prev').eq(0).classSwitch('none');
	var $dayfree = $parent.find('span.dayfree');
	var $next = $parent.find('span.next');
	var $prev = $parent.find('span.prev');
	var $index = 0;
	$prev.bind('click', function() { action($month.index($(this).parents('table')) - 1); });
	$next.bind('click', function() { action($month.index($(this).parents('table')) + 1); });
	function action($target) { $parent.animate({left: '-' + $width * $target + 'px'}, 'slow'); }
	$dayfree.bind('click', function() { 
		$inputstart.val('');
		$inputend.val('');
		var $total = $parent.find('span.daypick').length;
		if ($total > 1) { $dayfree.removeClass('daypick'); $(this).addClass('daypick'); $message.html('<strong>Start date selcted</strong><br/>Now pick an end date'); }
		else if ($total == 1) { 
			$(this).addClass('daypick');
			var $pick = $parent.find('span.daypick');
			var $start = $dayfree.index($pick.eq(0));
			var $end = $dayfree.index($pick.eq(1));
			var $datestart = $pick.eq(0).next('input').val();
			var $dateend = $pick.eq(1).next('input').val();
			$inputstart.val($datestart);
			$inputend.val($dateend);
			var $total = 2;
			$dayfree.each(function() { var $index = $dayfree.index($(this)); if ($index > $start && $index < $end) { $(this).addClass('daypick'); $total ++; } });
			$message.html('<strong>' + $total + ' nights selected</strong><br />' + $.dateFormat($datestart) + '<br />to<br />' + $.dateFormat($dateend));     }
		else if ($total == 0) { $(this).addClass('daypick'); $message.html('<strong>Start date selcted</strong><br/>Now pick an end date'); }     });     }

jQuery.tableStyle = function () { $('td').each(function() { if ($(this).text() == '&nbsp;' || $(this).text() == 'N/A') { $(this).classSwitch('none'); } }); }

jQuery.fileStyle = function () {	
	$this = $('input[type=file]');
	$this.wrap('<div class="cellfile"></div>');
	$this.css({opacity: 0});
	$this.parent().append('<input class="field fieldmid" type="text" value="Browse . . ." />');
	$this.parent().append('<input class="button buttonfile" type="button" value="find" />');
	$this.bind('change', function() { $(this).siblings('input[type=text]').val($(this).val()); });
	$this.bind('mouseout', function() { 
		if ($(this).val()) { $(this).siblings('input[type=text]').val($(this).val()); }
		else { $(this).siblings('input[type=text]').val('Browse . . .'); }     });     }
	
jQuery.selectStyle = function () { 
	$('select').each(function() { 
		if ($(this).val()) { $selected = $(this).children('option:selected'); } else { $selected = $(this).children('option:first'); } 
		$selected.css({'font-weight':'bold'});     });     }	
	
jQuery.selectJump = function () { $('input.buttonjump').hide(); $('select.fieldjump').change(function() { $(this).parents('form').submit(); }); }
	
jQuery.selectJump2 = function () {
	$('select.jump').each(function() {
		if ($(this).val()) { $selected = $(this).children('option:selected'); } else { $selected = $(this).children('option:first'); }
		$(this).wrap('<div class="jump" style="margin: ' + $(this).cssMargin() + '; width: ' + ($(this).width() - 5) + 'px;"></div>');
		$(this).after('<span class="jump">' + $selected.text() + '</span>');
		$(this).css({'left':'0', 'margin':'0', 'opacity':0, 'position':'absolute', 'top':'0', 'z-index':'2'});   });
	$('input.button-jump').hide();
	$('select.jump').change(function() { $(this).parents('form').submit(); }); }

jQuery.helpStyle = function () {
	$('span.buttonhelp').show();
	$('div.cellhelp').hide();
	$('span.buttonhelp').bind('click', function() {
		var $div = $(this).next('div.cellhelp');
		var $pos = $(this).offset();
		if ($(this).parents('div.win')) { var $scroll = $(window).scrollTop(); } else { var $scroll = 0; }
		$div.css({'left':$pos.left, 'top':$pos.top - $scroll });
		$('div.cellhelp').hide();
		$div.show('fast');     })
	$('div.cellhelp').bind('mouseleave', function() { $('div.cellhelp').hide('fast'); })     }	
	
jQuery.divtable = function () { 
	$('div.table').each(function() {
		var $row = $(this).find('div.row');
		var $length = $row.eq(0).find('div.cell').length();
		var $item = 0; 
		while ($item < $length) { $row.find('div.cell').eq($item).matchWidth(); $item ++; } }); }
