BNJQ(function () {
  BNui.applyFormControls();
  BNui.applyUITweaks();
  BNJQ('.login a').bind('click', function(event) {
	 BNui.showLogin(event); 
  });
  BNJQ('.bn-tooltip-bottomLeft[title]').qtip({
      content: {
         text: false // Use each elements title attribute
      },
      position : {
        /*target: 'mouse',*/
    	corner: { tooltip: 'topLeft', target: 'leftBottom' },
        adjust: { screen: true, y: 5 }
      },
	    'style': {
				border : {
					color : "#ffffff",
					width : '2'
				},
				background: '#fff9d7 url(http://assets.bravenet.com/bn/bravenet/img/icons/fam/lightbulb.png) 3px 5px no-repeat',
				padding : '5px 5px 5px 24px',
				color : '#4f4f4f',
				width : {
					max: '350'
				}
		  }
   });
  BNJQ('.bn-tooltip-topLeft[title]').qtip({
      content: {
         text: false // Use each elements title attribute
      },
      position : {
        /*target: 'mouse',*/
    	corner: { tooltip: 'bottomLeft', target: 'leftTop' },
        adjust: { screen: true, y: -5 }
      },
	    'style': {
				border : {
					color : "#ffffff",
					width : '2'
				},
				background: '#fff9d7 url(http://assets.bravenet.com/bn/bravenet/img/icons/fam/lightbulb.png) 3px 5px no-repeat',
				padding : '5px 5px 5px 24px',
				color : '#4f4f4f',
				width : {
					max: '350'
				}
		  }
   });
	 BNJQ('.slider').each(function() {
			var input_element = BNJQ(this);
			var slider = BNJQ('<div id="slider_'+BNJQ(input_element).attr('name')+'"></div>').appendTo(BNJQ(input_element).parent()).slider({
				slide: function(event, ui) {
					var uval = ((ui.value-1)==0) ? 1:ui.value-1; 
					BNJQ(input_element).val(uval);
				}
			});
			BNJQ(input_element).bind('change', function () {
				BNJQ('#slider_'+BNJQ(input_element).attr('name')).slider('value',BNJQ(this).val());
			});
	 });
	 
   BNJQ('.bn-slide-menu').slideMenu();
   BNJQ('.bn-dialog-link').bnDialogLinks();
   BNJQ.fn.setupDeepLinks();
   BNJQ('.bn-history-select').historySelect();
   if(BNJQ('.helper-input').length>0) {
	   BNJQ('.helper-input').bt({
		   trigger:['focus','blur'],positions:['right','left'],fill:'#363636',strokeStyle:'#000000',cssStyles:{color:'#fff'},spikeLength:6,spikeGirth:10,width:350,overlap:0,centerPointY:1,cornerRadius:5,shadow:true,shadowColor:'rgba(0,0,0,.5)',shadowBlur:5,shadowOffsetX:2,shadowOffsetY:2,
		   showTip: function(box){
		    var $content = BNJQ('.bt-content', box).hide(); /* hide the content until after the animation */
		    var $canvas = BNJQ('canvas', box).hide(); /* hide the canvas for a moment */
		    var origWidth = $canvas[0].width; /* jQuery's .width() doesn't work on canvas element */
		    var origHeight = $canvas[0].height;
		    BNJQ(box).show(); /* show the wrapper, however elements inside (canvas, content) are now hidden */
		    $canvas
		      .css({width: origWidth * .5, height: origHeight * .5, left: origWidth * .25, top: origHeight * .25, opacity: .1})
		      .show()
		      .animate({width: origWidth, height: origHeight, left: 0, top: 0, opacity: 1}, 400, 'easeOutBounce',
		        function(){$content.show()} /* show the content when animation is done */
		        );
		  },
		  /* when using hideTip, do NOT forget 'callback' at end of animation */
		  hideTip: function(box, callback){
		    var $content = BNJQ('.bt-content', box).hide();
		    var $canvas = BNJQ('canvas', box);
		    var origWidth = $canvas[0].width;
		    var origHeight = $canvas[0].height;
		    $canvas
		      .animate({width: origWidth * .5, height: origHeight * .5, left: origWidth * .25, top: origHeight * .25, opacity: 0}, 200, 'swing', callback); /* callback */
		  }
	   });
   }
   // fix to deal with broken richtext editor (richtext.js should be refactored to access both editors when editing)
   BNJQ(document).bind('ready',function() {
   if(BNJQ('form').find('iframe.iframeeditor')) {
     
     var frames = BNJQ('form iframe.iframeeditor');
     var form = BNJQ(frames).parents('form').eq(0);
     var inputs = BNJQ('input[type=text]',BNJQ(form));
     var submitBtn = BNJQ('button[type=submit]',BNJQ(form));
     // gather the blur events that don't work from the iframe editor and attach them to the submit button
     var blurEvent = '';
     BNJQ.each( frames, function () {
       var blurAttr = Object.toString.call(BNJQ(this).attr('onblur'));
       blurEvent += blurAttr.match(/updateEditor\(.*?\)/i)[0].replace(/\"+/g,"'") + ';';
     } );
     BNJQ(submitBtn).bind('mousedown', function() { eval(blurEvent); });
     
     // lastly to mimic the behaviour of enter keypress on inputs, we are going to trigger the mousedown event on the submit button, then submit the form
     BNJQ(inputs).bind('keypress', function(event) {
       if(event.keyCode==13) {
          event.stopPropagation();
          BNJQ(submitBtn).trigger('mousedown');
          BNJQ(submitBtn).trigger('click');
          return false;
       }
     });
     
   }
   });
});

