var $ = jQuery;
$( document ).ready(
	function($){
		

		/**
		 * On click of the floating link, will scroll to top
		 */
		$( '.fl_top.fl_icon_holder' ).click(
			function(){
				$( 'html, body' ).animate( {scrollTop:0}, 'slow' );
			}
		);

		/**
		 * On click of the floating link, will scroll to bottom
		 */
		$( '.fl_bottom.fl_icon_holder' ).click(
			function(){
				$( "html, body" ).animate( { scrollTop: $( document ).height() }, "slow" );
			}
		);

		/**
		 * Display or hide the floating link
		 * For vertical positions: slide up/down (height animation)
		 * For horizontal positions (Top Center / Bottom Center): slide left/right (width animation)
		 */
		$( '#fl_slimer_primary_wrap' ).click(
			function(e){
				e.preventDefault();
				var $innerWrap = $( '#fl_inner_primary_wrap' );
				var $wrapper = $( '.floating_next_prev_wrap.fl_primary_bar' );
				var $minimizer = $( '.fl_primary_bar .fl_slimer_Wrap' );
				
				// Check if the bar is in horizontal position (Top Center or Bottom Center)
				// Primary bar uses 'fl_top' and 'fl_bottom' classes
				// Check both with and without 'fl_' prefix for compatibility
				var isHorizontal = $wrapper.hasClass( 'fl_top' ) || $wrapper.hasClass( 'fl_bottom' ) ||
								   $wrapper.hasClass( 'fl_fl_top' ) || $wrapper.hasClass( 'fl_fl_bottom' );
				
				if ( isHorizontal ) {
					// Horizontal collapse: animate width from left to right
					var isCurrentlyVisible = $innerWrap.is( ':visible' );
					
					if ( isCurrentlyVisible ) {
						// Closing: animate from current width to 0 (left to right collapse)
						var currentWidth = $innerWrap.outerWidth();
						$innerWrap.css( {
							'overflow': 'hidden',
							'width': currentWidth + 'px'
						} );
						$innerWrap.animate( { 'width': '0' }, 'slow', function() {
							$innerWrap.css( {
								'display': 'none',
								'width': '',
								'overflow': ''
							} );
						} );
					} else {
						// Opening: animate from 0 to natural width (right to left expand)
						// First, set display and get natural width
						$innerWrap.css( {
							'display': 'flex',
							'width': '0',
							'overflow': 'hidden'
						} );
						// Force reflow to ensure width calculation
						var naturalWidth = $innerWrap[0].scrollWidth;
						$innerWrap.animate( { 'width': naturalWidth + 'px' }, 'slow', function() {
							$innerWrap.css( {
								'width': '',
								'overflow': ''
							} );
						} );
					}
				} else {
					// Vertical collapse: use existing slideToggle (height animation)
					$innerWrap.slideToggle( 'slow' );
				}
				
				// Toggle the close class for icon state
				$minimizer.toggleClass( 'fl-close' );
			}
		);

		// Social icons bar: use the same open/close behaviour as the primary next/prev bar
		// For vertical positions: slide up/down (height animation)
		// For horizontal positions (Top Center / Bottom Center): slide left/right (width animation)
		$( '#fl_slimer_social_wrap' ).click(
			function(e){
				e.preventDefault();
				var $innerWrap = $( '#fl_social_icons_inner_wrap' );
				var $wrapper = $( '.floating_next_prev_wrap.fl_social_icons_bar' );
				var $minimizer = $( this ); // Use the clicked element directly
				
				// Check if the bar is in horizontal position (Top Center or Bottom Center)
				// Social icons bar uses 'fl_top' and 'fl_bottom' classes for horizontal layout
				// Only exact matches - hasClass() prevents substring false positives
				// (e.g., 'fl_left_top' should NOT match as horizontal)
				var isHorizontal = $wrapper.hasClass( 'fl_top' ) || $wrapper.hasClass( 'fl_bottom' ) ||
								   $wrapper.hasClass( 'fl_fl_top' ) || $wrapper.hasClass( 'fl_fl_bottom' );
				
				if ( isHorizontal ) {
					// Horizontal collapse: animate width from left to right
					// Check visibility more reliably - check both display and width
					var currentDisplay = $innerWrap.css('display');
					var currentWidth = $innerWrap.css('width');
					var isCurrentlyVisible = $innerWrap.is( ':visible' ) && currentDisplay !== 'none' && ( !currentWidth || currentWidth === 'auto' || parseFloat(currentWidth) > 0 );
					
					if ( isCurrentlyVisible ) {
						// Closing: animate from current width to 0 (left to right collapse)
						// Get actual rendered width
						var actualWidth = $innerWrap.outerWidth();
						if ( actualWidth === 0 ) {
							// If width is 0 but element is visible, get natural width first
							$innerWrap.css( { 'width': 'auto', 'display': 'flex' } );
							actualWidth = $innerWrap.outerWidth();
						}
						$innerWrap.css( {
							'overflow': 'hidden',
							'width': actualWidth + 'px',
							'display': 'flex'
						} );
						$innerWrap.animate( { 'width': '0' }, 'slow', function() {
							$innerWrap.css( {
								'display': 'none',
								'width': '',
								'overflow': ''
							} );
						} );
					} else {
						// Opening: animate from 0 to natural width (right to left expand)
						// First, set display and get natural width
						$innerWrap.css( {
							'display': 'flex',
							'width': 'auto',
							'overflow': 'hidden'
						} );
						// Force reflow to ensure width calculation
						var naturalWidth = $innerWrap[0].offsetWidth || $innerWrap[0].scrollWidth;
						// Reset to 0 for animation
						$innerWrap.css( { 'width': '0' } );
						// Now animate to natural width
						$innerWrap.animate( { 'width': naturalWidth + 'px' }, 'slow', function() {
							$innerWrap.css( {
								'width': '',
								'overflow': ''
							} );
						} );
					}
				} else {
					// Vertical collapse: use existing slideToggle (height animation)
					$innerWrap.slideToggle( 'slow' );
				}
				
				// Toggle the close class for icon state
				$minimizer.toggleClass( 'fl-close' );
			}
		);

		$( document ).on(
			"click",
			".fl_social_icons_bar .fl_icon_holder",
			function(e) {
				e.preventDefault();
				window.open( $( this ).attr( "href" ), "popupWindow", "width=600,height=600,scrollbars=yes" );
				return false;
			}
		);

		/*
		* Copy to Clipboard functions
		*/
		$.fn.CopyToClipboard = function() {
			var textToCopy = false;
			if (this.is( 'select' ) || this.is( 'textarea' ) || this.is( 'input' )) {
				textToCopy = this.val();
			} else {
				textToCopy = this.text();
			}
			CopyToClipboard( textToCopy );
		};

		function CopyToClipboard( val ){
			var hiddenClipboard = $( '#_hiddenClipboard_' );
			if ( ! hiddenClipboard.length) {
				$( 'body' ).append( '<textarea style="position:absolute;top: -9999px;" id="_hiddenClipboard_"></textarea>' );
				hiddenClipboard = $( '#_hiddenClipboard_' );
			}
			hiddenClipboard.html( val );
			hiddenClipboard.select();
			document.execCommand( 'copy' );
			document.getSelection().removeAllRanges();
		}

		$(
			function(){
				$( '[data-clipboard-target]' ).each(
					function(){
						$( this ).click(
							function() {
								$( $( this ).data( 'clipboard-target' ) ).CopyToClipboard();
							}
						);
					}
				);
				$( '[data-clipboard-text]' ).each(
					function(){
						$( this ).click(
							function(){
								CopyToClipboard( $( this ).data( 'clipboard-text' ) );
							}
						);
					}
				);
			}
		);

		$( '.fl_copy_url' ).click(
			function(){
				$( ".fl_copied" ).slideDown();
				setTimeout( function(){ $( ".fl_copied" ).slideUp(); }, 3000 );
			}
		);

	}
);
