 /*-----------------------------------------------
'	Company: Community Engine (www.communityengine.com)
'	Copyright (c) 2009, All rights reserved.
'	Date Created: April 2009
'
'	Last Modified Date: 21st April, 2009
'	Last Modified By: Benjamin -> ben.ruhe@communityengine.com
'
'	DO NOT MODIFY THIS DOCUMENT WITHOUT
'	NOTIFYING THE AUTHOR FIRST
'
------------------------------------------------*/

// array for equal heights. Safari compatible
$(window).load(function() {
    var classArr = ['div.equal'];
    for (var i = 0; i < classArr.length; i++) {
        $(classArr[i]).equalHeights();
    }
});

$(function() {

    // 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;
    };
}