
function setupShowAboutMe() {
    var showHide = $('.profileAboutMe .showHide');
    showHide.children('a').text('More');
    showHide.children('a').unbind('click');
    showHide.children('a').click(function() {
        $('.profileAboutMe .wrapper').animate({
            height: $('.profileAboutMe .content').height()
        }, 500, setupHideAboutMe);
    });            
};
function setupHideAboutMe() {
	var showHide = $('.profileAboutMe .showHide');
	showHide.children('a').text('Less');
    showHide.children('a').unbind('click');
    showHide.children('a').click(function() {
        $('.profileAboutMe .wrapper').animate({
            height: originalAboutMeHeight
        }, 500, setupShowAboutMe);
    });            
};
function setupShowTestimonials() {
    var showHide = $('.profileTestimonials .showHide');
    showHide.children('a').text('More');
    showHide.children('a').unbind('click');
    showHide.children('a').click(function() {
        $('.profileTestimonials .wrapper').animate({
            height: $('.profileTestimonials .content').height()
        }, 500, setupHideTestimonials);
    });            
};
function setupHideTestimonials() {
    var showHide = $('.profileTestimonials .showHide');
    showHide.children('a').text('Less');
    showHide.children('a').unbind('click');
    showHide.children('a').click(function() {
        $('.profileTestimonials .wrapper').animate({
            height: originalTestimonialHeight
        }, 500, setupShowTestimonials);
    });            
};

hare.site.modal.initDialogButtons = function(modalId) {	
	$('.close-dialog').click(function closeDialog(e){
		$.modal.close();
	});
	hare.site.eventRegistry.bindEvent('modal', modalId + " .buttonbar .submit", 'click', function(){
		hare.site.modal.submitContact();
	});	
}

hare.site.modal.submitContact = function() {
	if (hare.site.tinymce.numWords['emailMessage'] > 500) {
		$('#tooManyWords').show();
		return false;
	}
	$('#tooManyWords').hide();

    var data = $('.contactDialog form').serialize();
    var ed = tinyMCE.get('emailMessage');
    data = data.replace(/emailMessage=.*?&/, 'emailMessage='+escape(ed.getContent())+'&');
    $.ajax({
          type:"POST",
          url: $('.contactDialog form').attr('action'),
          data: data,
          success: function(responseText) {
			if ($(responseText).parent().find('.contactDialog form').length > 0) {
				tinyMCE.execCommand('mceRemoveControl', false, 'emailMessage');
				$('.contactDialog form').replaceWith($(responseText).find('form'));
				tinyMCE.execCommand('mceAddControl', false, 'emailMessage');
				$('.mceToolbar').hide();
			} else {
				tinyMCE.execCommand('mceRemoveControl', false, 'emailMessage');
				$('.formButton').hide();
				$('.closeButton').show();
				$('.contactDialog form').replaceWith($('#emailSent').html());
			}
			// omniture tracking
			try {
    			s = s_gi(s_account);
    			s.linkTrackVars = "events";
    			s.linkTrackEvents = "event46";
    			s.events = 'event46';
    			s.tl(null, 'o', 'Profile Inquiry');
			} catch (err) {
				// ignore
			}
    	  }
    });
	return false;	
}

hare.site.modal.initContact = function () {
	$('.close-dialog').click(function closeDialog(e){
		tinyMCE.execCommand('mceRemoveControl', false, 'emailMessage');
	});
	hare.site.modal.initDialogButtons('#modalContact');

	tinyMCE.execCommand('mceAddControl', false, 'emailMessage');
	$('.mceToolbar').hide();
	
	if ($.browser.msie && $.browser.version == '6.0') {
		setTimeout(function() {
			$('.buttonbar').css('bottom', '12px');
		}, 100);
	}
}

hare.site.modal.initPhone = function () {
	$('#modalPhone').css({'display':'block'});
	hare.site.modal.initDialogButtons('#modalPhone');
	
	$.get("/statistic", {'id': $('#profile').text(), 'key': 'ProfilePhoneClick'});
}

hare.site.modal.initIM = function () {
	$('#modalIM').css({'display':'block'});
	hare.site.modal.initDialogButtons('#modalIM');
	
	$.get("/statistic", {'id': $('#profile').text(), 'key': 'ProfileInstantMessageClick'});
}

hare.site.modal.initReportThis = function () {
	$('form.reportThisForm:input').val('');
	$('#modalReport').css({'display':'block'});
	$('.close-dialog').click(function closeDialog(e){
		$.modal.close();
	});
	hare.site.eventRegistry.bindEvent('modal', "#modalReport .buttonbar .submit", 'click', function(){
	    var data = $('form.reportThisForm').serialize();
	    $.ajax({
	          type:"POST",
	          url: $('form.reportThisForm').attr('action'),
	          data: data,
	          success: function(responseText) {
	    			if ($(responseText).parent().find('form.reportThisForm').length > 0) {
		    			$('form.reportThisForm').replaceWith($(responseText).find('form'));
		    		} else {
		    			$.modal.close();
		    		}
	    	  }
	    });
	});	
	
}

hare.site.tinymce.setupTinyMCE(500, 100, 415);
$(function() {
	if ($('.profileAboutMe .content').height() > 150) {
		$('.profileAboutMe .wrapper').height(150);
	}
    originalAboutMeHeight = $('.profileAboutMe .wrapper').height();       
    if (originalAboutMeHeight < $('.profileAboutMe .content').height()) {
    	var showHide = $('.profileAboutMe .showHide');
        showHide.css('display', 'block');
        setupShowAboutMe();
    }
    originalTestimonialHeight = $('.profileTestimonials .wrapper').height();       
    if (originalTestimonialHeight < $('.profileTestimonials .content').height()) {
        var showHide = $('.profileTestimonials .showHide');
        showHide.css('display', 'block');
        setupShowTestimonials();
    }
    $('.testimonialText').find('p:first').prepend('<img src="/resources/2807/img/left_quote.png" />');
    $('.testimonialText').find('p:not(:first)').css('padding-left', '10px');
    $('.testimonialText').find('p:last').append('<img src="/resources/2807/img/right_quote.png" />');
    
    var ajaxUrl = '/profileEvent/contact/' + $('#screenName').text();
	hare.site.modal.configureModal('.emailIcon', '#modalContact', ajaxUrl, hare.site.modal.initContact);
	hare.site.modal.configureModal('.phoneIcon', '#modalPhone', null, hare.site.modal.initPhone);
	hare.site.modal.configureModal('.imIcon', '#modalIM', null, hare.site.modal.initIM);
	hare.site.modal.configureModal('.reportThis', '#modalReport', null, hare.site.modal.initReportThis);
	
    $('#profileName').qtip({
    	position: {corner: {target: 'topLeft', tooltip: 'bottomLeft'}},
    	style: {background:"#ffffcc"}
    });

});
