$(function() {
	var links = $('a.confirm');
	if (!links.length)
		return;
	links.click(function() {
		return confirm('Are you sure?');
	});
});

$(function() {
	var slideshow = $('#home-slideshow');
	if (!slideshow.length)
		return;
	slideshow.cycle({ fx: 'fade', timeout: 5000 }).css('visibility', 'visible');
});

var lightboxSettings = {
	fixedNavigation: true,
	imageLoading:'/lib/jquery-lightbox/images/lightbox-ico-loading.gif',
	imageBtnPrev:'/lib/jquery-lightbox/images/lightbox-btn-prev.gif',
	imageBtnNext:'/lib/jquery-lightbox/images/lightbox-btn-next.gif',
	imageBtnClose:'/lib/jquery-lightbox/images/lightbox-btn-close.gif',
	imageBlank:'/lib/jquery-lightbox/images/lightbox-blank.gif'
};

$(function() {
	var businessViewThumbs = $('.business-view .thumb');
	if (!businessViewThumbs.length)
		return;
	businessViewThumbs.lightBox(lightboxSettings);
});

$(function() {
	$('#account .user_nav').each(function() {
		$(this).tvgUserMenu();
	});
});

$(function() {
	var mapView = $('.dynamic-legacymap .lightbox');
	if (!mapView.length)
		return;
	mapView.lightBox(lightboxSettings);
});

$(function() {
	var selectors = ['#navigation li', '#bottom-navigation li',
		'#footer .items li'];
	for (i = 0; i < selectors.length; i++)
		$(selectors[i]).clickyMenu();
});

$(function() {
	var form = $('.ajaxform');
	if (!form.length)
		return;
	form.submit(function() {

		// FCKeditor is designed to fill in the hidden textarea with HTML
		// before the form submits, but since we are not submitting the form
		// in this case, we need to use the UpdateLinkedField() method instead.
		form.find('textarea').each(function() {
			var textarea = $(this);
			var iframe = textarea.prev('iframe');
			if (!iframe.length) {
				return true;
			}

			var textareaId = textarea.attr('id');
			if (iframe.attr('id') !== textareaId + '___Frame') {
				return true;
			}
			var oEditor = FCKeditorAPI.GetInstance(textareaId);
			oEditor.UpdateLinkedField();
		});

		$.ajax({
			type: 'POST',
			url: form.attr('action'),
			data: form.serialize(),
			success: function(data) {
				form.find('.message').html(data);
			}
		});
		return false;
	});
});

$(function() {
	$('.link-select').change(function() {
		var url = $(this).val();
		if (url)
			window.location.href = url;
	});
});

$(function() {
	var fck = new Array();
	var i = 0;
	$('textarea.html').each(function() {
		var textarea = $(this);
		fck[i] = new FCKeditor(textarea.attr('id'));
		fck[i].BasePath = '/lib/fckeditor-custom/';
		fck[i].Config['CustomConfigurationsPath'] = '/fck/front/default.js';
		if (textarea.attr('data-config'))
			fck[i].Config['CustomConfigurationsPath'] = textarea.attr('data-config');
		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 = '/lib/fckeditor-custom/';
		fck[i].Config['CustomConfigurationsPath'] = '/fck/front/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 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;
}
(function($){
	$.wrapAll2 = function(sel, wrapSel) {
		var wrap = $(wrapSel);
		$(sel).each(function() {
			var more = $(this).next(sel).length;
			$(this).replaceWith(wrap).appendTo(wrap);
			if (!more) wrap = $(wrapSel);
		});
	};
})(jQuery);

$(function() {
	$.wrapAll2('.feature', '<div class="feature-group"/>');
	$('.feature-inner').removeClass('flash');
});

$(function() {
	var links = $('#main-content, #down-content').find('a');
	links.each(function() {
		var link = $(this);
		var href = link.attr('href');

		if (!/\.(?:pdf|mov|jpg|jpeg|gif|wmv|mpg|avi)$/.test(href))
			return false;

		if (link.find('img').length)
			return false;

		$.ajax({
			type: 'get',
			data: { href: href },
			url: '/pages/fileinfo',
			success: function (data) {
				if (!data.success) {
					return;
				}

				link.html(link.html()+' ('+data.infoString+')');
			}
		});
	});
});

$(function() {
	var container = $('.jcarousel-skin-custom');
	if (!container.length)
		return;
	container.jcarousel({
		easing: 'swing',
		start: $('li', container).index($('.current', container)) + 1,
		scroll: 2
	});
});

$(function() {
	$('input[placeholder], textarea[placeholder]').placeholder();
});

