$(function() {
	
	// Pretty main navigation
	$('.navigation > ul').tvgMenu();
	
	// Pretty user navigation
	$('.user_navigation > ul').each(function() {
		$(this).tvgUserMenu();
	});
	
	// Pretty fonts
	//Cufon.replace($('.user_controls .identification, .pane .more a, .page_feature .more a, .events .more a'), { hover: true });
	Cufon.replace($('.page_content .more a, .pane .more a, .page_feature .more a, .events .more a'), { hover: true });
	
	// Widget onchange submit
	$('.find_a_hotel select').change(function() {
		$(this).closest('form').submit();
	});
	
	// Certain links open in a new window (target attribute not valid XHTML)
	$('a.new, a.external').attr('target', '_blank');
	
	// DIVs into clickable links
	$('.pane, .page_feature, .lodger_type li').each(function() {
		var $a = $(this).find('a');
		if ($a.length !== 1) {
			return;
		}
		$(this).click(function() {
			window.location.href = $a.attr('href');
		});
		$(this).hover(
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
		);
	});
	
	// Autocomplete off
	$.each($('.no_autocomplete'), function () {
		this.setAttribute('autocomplete', 'off');
	});
	
	// Press release jumper
	$('.edit_jumper input').hide();
	$('.edit_jumper select').change(function() {
		$(this).closest('form').submit();
	});
	
	// FCKeditor
	var fck = new Array();
	var i = 0;
	$('textarea.html').each(function() {
		fck[i] = new FCKeditor($(this).attr('id'));
		fck[i].BasePath = '/packages/fckeditor/';
		fck[i].Config['CustomConfigurationsPath'] = '/js/fckconfig_basic_user_edit.js';
		fck[i].Config['AutoDetectLanguage'] = true;
		fck[i].Height = '312';
		fck[i].ReplaceTextarea();
		i++;
	});
	$('textarea.html-public').each(function() {
		fck[i] = new FCKeditor($(this).attr('id'));
		fck[i].BasePath = '/packages/fckeditor/';
		fck[i].Config['CustomConfigurationsPath'] = '/js/fckconfig_basic_user_edit_public.js';
		fck[i].Config['AutoDetectLanguage'] = true;
		fck[i].Height = '312';
		fck[i].ReplaceTextarea();
		i++;
	});
});
	
function FCKeditor_OnComplete(editorInstance) {
	var matches = editorInstance.LinkedField.className.match(/\bwordcount-(\d+)\b/);
	if (matches !== null) {
		editorInstance.wordLimit = parseInt(matches[1]);
		fckeditorWordCount(editorInstance);
		editorInstance.Events.AttachEvent('OnSelectionChange', fckeditorWordCount);
	}
}

function fckeditorWordCount(editorInstance) {
	var data = editorInstance.GetData();
	data = data.replace('&nbsp;', ' ');
    var matches = strip_tags(data).match(/([\w']+)/g);
    var count = 0;
    if (matches) {
		count = matches.length;
	}
    if (document.getElementById(editorInstance.Name+'_word_count')) {
		document.getElementById(editorInstance.Name+'_word_count').innerHTML = count;
	}
    if (document.getElementById(editorInstance.Name+'_words_remaining')) {
		document.getElementById(editorInstance.Name+'_words_remaining').innerHTML = editorInstance.wordLimit - count;
	}
}

function deleteAlert() {
	return confirm('Are you absolutely sure? You cannot undelete.');
}

function strip_tags(str, allowed_tags) {
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';
    var replacer = function (search, replace, str) {
        return str.split(search).join(replace);
    };
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z0-9]+)/gi);
    }
    str += '';
    matches = str.match(/(<\/?[\S][^>]*>)/gi);
    for (key in matches) {
        if (isNaN(key)) {
            continue;
        }
        html = matches[key].toString();
        allowed = false;
        for (k in allowed_array) {
            allowed_tag = allowed_array[k];
            i = -1;
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
            if (i == 0) {
                allowed = true;
                break;
            }
        }
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
    return str;
}
