// messages
var hideMessage = function () {
	$("#jsNoticeMessage").slideUp();
	$(".bwWidth").animate({
		marginTop: 0
	});
}

var showMessage = function (sMessage, sType, bPersist) {

	if (sType == 'notices') {
		$("#jsNoticeMessage").find('.jsContent').html(sMessage);
		$("#jsNoticeMessage").slideDown();
		$(".bwWidth").animate({
			marginTop: 41
		});

		if (!bPersist) {
			setTimeout(hideMessage, 10000);
		}

	} else {
		var $dialog = $('#jsDialogMessage');
		$dialog.find('.jsDialogText').html(sMessage);
		$dialog.show();
	}
}

var showError = function (sMessage) {
	showMessage(sMessage, 'errors');
}

/**
 * Show info message at the top of page
 *
 * @param string sMessage text of message
 * @param bool bPersist keep message open or close it after 10 sec
 */
var showNotice = function (sMessage, bPersist) {
	showMessage(sMessage, 'notices', bPersist);
}

var showModal = function (sMessage, oLinks) {
	var $dialog = $('#jsDialogMessage');
	$dialog.find('.jsDialogText').html(sMessage);

	$('.bwModalButtons').find('div.bwTemp').remove();

	for (var index in oLinks) {
		$('.bwModalButtons').prepend(
			'<div class="bwButton bwTemp">'
			+ '<a class="bwButtonLink" href="' + oLinks[index].sLink + '">' + oLinks[index].sLabel + '</a>'
			+'</div>'
		);
	}
	$dialog.show();
}

var displayLoading = function($ele, darkBackground)
{
	if (!darkBackground) {
		$ele.html('<img class="bwLoading" src="/bgs/loader1.gif" />');

	} else {
		$ele.html('<img class="bwLoading" src="/bgs/loader2.gif" />');
	}
}

$(document).ready(function() {

// set the vertical margin
$(function(){
	var widthTotal = $(".container_6").width();
	var setMargin = widthTotal * 0.04;
	$(".bwBlock").css({
		marginBottom: setMargin
	});
	var teaser = $(".bwTeaser").width();
	var teaserHeight = teaser*0.82
	$(".bwTeaser").height(teaserHeight).css("margin-top",-(295+setMargin));
	window.onresize = function(event) {
		var widthTotal = $(".container_6").width();
		var setMargin = widthTotal*0.04
		$(".bwBlock").css({
			marginBottom: setMargin
		});
		var teaser = $(".bwTeaser").width();
		var teaserHeight = teaser*0.82
		$(".bwTeaser").height(teaserHeight).css("margin-top",-(295+setMargin));
	}
});

// context menu active item
$('.bwContextActive:visible:last').livequery(function () {
	$(this).addClass('bwContextActiveFake');
});

// data filter
$(".jsDataFiltersButton").click(function(){
	$(".bwDataFilters").slideToggle();
});

// table
$(".bwBlock:has('.bwTable')").livequery(function () {
	$(this).addClass("bwBlockTable");
});

// blink "add a tag" buttons
$("#jsPilesBlink").livequery(function () {
	$(this).hover(function(){
		//$(".bwPilesAdd").effect("highlight", {color: '#f78600'}, 3000);
		$(".bwPilesAdd").slice(0,5).effect("pulsate", {times: 3}, 1000);
	},function () {
        $(".bwPilesAdd").stop();
      }
	);
});

// message
$(".bwMessageClose").click(function() {
	$(".bwMessage").slideUp();
	$(".bwWidth").animate({
		marginTop: 0
	});
});

// modal
$("body").delegate('.bwModalShow', 'click', function(e) {
	e.preventDefault();
	$(".bwModal").show();
});

$("body").delegate(".bwModalClose, .bwClose, .bwModalOverlay", 'click', function(e) {
	e.preventDefault();
	$(this).closest(".bwModal").hide();
});

// adds pseudo classes to buttons
if ($.browser.msie) {
	$(".bwButtonLink:first-child").addClass("bwButtonLinkFirst");
	$(".bwButtonLink:last-child").addClass("bwButtonLinkLast");
}

// truncate
$('.bwWidget LI H5, .bwWidget LI H5 ~ P').livequery(function () {
	$(this).truncate({
		width: 'auto',
		after: '&hellip;',
		center: false,
		addclass: 'jsTruncate',
		addtitle: true
	});
});
$('.bwTruncateAuthor').livequery(function () {
	$(this).truncate({
		width: '120',
		after: '&hellip;',
		center: false,
		addclass: 'jsTruncate',
		addtitle: true
	});
});
$('.bwTruncateMgmt').livequery(function () {
	$(this).truncate({
		width: '200',
		after: '&hellip;',
		center: false,
		addclass: 'jsTruncate'
	});
});


// input select
$(".bwInputSelect").click(function(){
	$(this).select();
});

// button submenu position
$('.bwButtonMenuRight').unbind('mouseenter').bind('mouseenter', function() {
	var $self = $(this);
	var $list = $self.find('.bwButtonMenuItems');
	if ($self.attr('id') && !isNaN($self.attr('id')) && !$list.hasClass('loaded')) {
		$list.addClass('loaded');
		$.get('/object/index/actions/', {id: $self.attr('id')}, function(data) {
			if (data.indexOf('no_data') != -1 && $.trim($list.html()) != '') {
				data = '';
			}
			$list.html($list.html() + data);
		});
	}
});

// list details expanding
$(".jsListExpand").click(function(){
	$(this).parents("LI").find(".bwListExpand").slideToggle();
});

// display tooltip over truncated elements
$('*[title]').livequery(function () {
	$(this).qtip({
		id: 'jsToolTip',
		content: {
		  text: $(this).attr('qtip-content')
		},
		position: {
			target: "mouse",
			adjust: {
				x: 20
			},
			viewport: $(window)
		},
		style: {
			classes: 'ui-tooltip-dark bwTooltip'
		}
	});
});

$('body').delegate('.jsNewWindow', 'click', function(e) {
	e.preventDefault();
	window.open($(this).attr('href'));
});

$('body').delegate('fieldset legend', 'click', function(e) {
	var $fieldset = $(this).closest('fieldset');
	var $fieldsetContent = $fieldset.find('.jsFieldsetContent');
	var $span = $(this).find('span');

	if ($span.hasClass('icAccordionMinus')) {
		$span.removeClass('icAccordionMinus');
		$span.addClass('icAccordionPlus');

	} else {
		$span.removeClass('icAccordionPlus');
		$span.addClass('icAccordionMinus');
	}

	$fieldset.toggleClass('bwFormFieldsetClosed');
	$fieldset.hasClass('bwFormFieldsetClosed')
		? $fieldsetContent.hide()
		: $fieldsetContent.show()
	;
});

$('.jsFieldClosed').livequery(function() {
	$(this).find('legend').click();
});

// removes embed tag for submenu visibility
if (jQuery.browser.msie && jQuery.browser.version <= 8 || jQuery.browser.mozilla && jQuery.browser.version < '4') {
	$("#object_actions").hover(function () {
			$(".bwFile object, .bwFile embed").css('visibility','hidden');
		}, function () {
			$(".bwFile object, .bwFile embed").css('visibility','visible');
		}
	);
}

// remover clear after filter button
$("#jsDataFiltersSubmit").parent().next('.clear').remove();

// orgchart
$(function(){
	$('.jsOrgchartExpand').click(function(){
		// show nodes
		$(this).parent().parent().children('ul').toggle();
		// click on title or the plus/minus
		if ($(this).is('.bwOrgchartItemTitle')) {
			$(this).siblings('.bwOrgchartExpand').children('.bwIcon').toggleClass('icCollapse').toggleClass('icExpand')
		} else {
			$(this).children('.bwIcon').toggleClass('icCollapse').toggleClass('icExpand');
		}
    });
});


});

