// array for equal heights. Safari compatible
$(window).load(function() {
    var classArr = ['div.equal-heights'];
    for (var i = 0; i < classArr.length; i++) {
        $(classArr[i]).equalHeights();
    }
});

$(function () {
    // Setup content slider
    $('.module-carousel').nivoSlider({
      	effect: 'fade',
      	animSpeed: 800,
      	pauseTime: 7000,
      	slices: 1,
      	captionOpacity: 1,
      	directionNav: false
    });

	//cufon
    Cufon.replace('.popup .lbl, div.header ul li a, div.footer dl dt, .cufon, .follow-text, .carousel-caption p, h1, h2, div.eventDetail h3, .heading, .barDark, .barLight, .barLightBlue, .view-link, .barPink, .bar-theme, .bar-promo, .bar-login .eventType, .breadCrumbs, .tabular, .region-description', { hover: true });

	// Share Button (Multichannel)
	stLight.options({ publisher: '8d400154-4af6-4ab8-9c75-9cf4dfeb6d1c' });

	// fancy box
	$(".ajax-window").fancybox({
		padding: 5,
		titleShow: false,
		overlayOpacity: "0.9",
		overlayColor: "#000"
	});


	// Facebook Logged In Options
	$(".facebook-logged-in-popup").hoverIntent({
		sensitivity: 3,
		interval: 100,
		over: function() { $(".facebook-logged-in").slideDown(); },
		timeout: 300,
		out: function() { $(".facebook-logged-in").slideUp(); }
	});

	// Swaps out the default value of textfields
	$(".defaultVal").defaultVal();

	$('.closeBtn').dialog('close');

	// Tool tip function
	$(document.body).append('<div style="display:none;" id="thumbTooltipContainer"><div class="content"></div></div>');

	//Share this
	$('span.stbuttontext').each(function() {
		if ($(this).text().toLowerCase() == 'sharethis') {
			$(this).text('Share');
		}
		$(this).closest('a').removeClass();
		$(this).closest('a').addClass('shareThis');
	});

	$('a.tn img').tooltip({
		tip: '#thumbTooltipContainer',
		offset: [-18, 50],
		onBeforeShow: function() {
			var myID = this.getTrigger().attr('id');
			var descCont = $('#' + myID + 'Desc');
			var myHeight = $(descCont).height();
			if (descCont.length > 0) {
				if ($('#thumbTooltipContainer').attr('rel') != (myID + 'Desc')) {
					$('#thumbTooltipContainer').attr('rel', (myID + 'Desc'));
					$('#thumbTooltipContainer').html($(descCont).html());
					$('#thumbTooltipContainer').attr('class', $(descCont).attr('class'));
					$('#thumbTooltipContainer .content').css('height', myHeight + 'px');
				}
			}
			else {
				return false;
			}
		}
	});

	//to track external links
	$('a[target*="_blank"]').click(function() {
		pageTracker._trackPageview('/outbound/' + $(this).attr('href'));
	});
	$('#uservoice-feedback a#uservoice-feedback-tab').attr('rel', 'external');
	$('a[rel*="external"]').click(function() {
		pageTracker._trackPageview('/outbound/' + $(this).attr('href'));
	});

	// end function
	$('A[rel="external"]').click(function() {
		window.open($(this).attr('href'));
		return false;
	});

	// controls character input/counter
	$('textarea.countChars').each(function() {
		ShowValueLength(this);
	});
	$('textarea.countChars').keyup(function() {
		ShowValueLength(this);
	});

});

function ShowValueLength(srcEl) {
    var charLength = $(srcEl).val().length;
    var charCount = $(srcEl).closest('tr').find('.charCount');
    var maxChars = $(srcEl).closest('tr').find('.maxChars').text();
    $(charCount).html(charLength);
    if (charLength > maxChars)
        $(charCount).html('<strong>You have exceeded the maximum </strong>');
}


$.fn.defaultVal = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};


// Equal height columns
$.fn.equalHeights = function(minHeight, maxHeight) {
	tallest = (minHeight) ? minHeight : 0;
	this.each(function() {
		if($(this).height() > tallest) {
			tallest = $(this).height();
		}
	});
	if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
	return this.each(function() {
		$(this).height(tallest);
	});
};

// jQuery Randomiser to be used wherever you need randomization
jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],{
    random: function(a, i, m, r) {
        if (i === 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        }
        return i == jQuery.jQueryRandom;
    }
});

//Added by DW:
//24th July 2009
//For a lot of the autocomplete adding/removing objects to other objects javascript, I have been using the indexOf function on arrays which
//IE doesn't like very much. Code below fixes the problem.
if (!Array.indexOf) {
    Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    };
}

