$j(document).ready( function() {

	$j("#pagePopReminder").tabSlideOut( {
	    tabHandle: '.slideOutTabHandle',				//class of the element that will be your tab
	    pathToTabImage: 'cf_pagepops/pagePopsTab.png',	//path to the image for the tab *required*
	    imageHeight: '30px',							//height of tab image *required*
	    imageWidth: '30px',								//width of tab image *required*    
	    tabLocation: 'left',							//side of screen where tab lives, top, right, bottom, or left
	    speed: 600,										//speed of animation
	    action: 'click',								//options: 'click' or 'hover', action to trigger animation
	    topPos: '200px',								//position from the top
	    fixedPosition: false							//options: true makes it stick(fixed position) on scroll
	} );					
	
	// alert dialog
	$j("#pagePopModal").dialog( {
		autoOpen: false,
		bgiframe: true,
		resizable: false,
		resizeStart: function() { alert( "started" ) },
		modal: true,
		title: $j("#loopedSlider .sliderContainer .sliderSlides").children().eq(0).attr("rel"),
		width: 600,
		maxHeight: ( $j(window).height() - 200 ),
		position: ['center',100],
		buttons: { 
			"Don't Show Again": function() { 
				
				// get our current cookie list of suppressed PagePops
				var currentList = $j.cookie( "SUPPPAGEPOPS" );
				
				// add the newest PagePops to be suppressed
				var newList = currentList + "," + $j("#pagePopModal").data( "viewedPagePops" ).toString();
				
				// convert to an array
				newList = forceUnique( newList.split(",") );
				
				// record our cookie variables
				$j.cookie( "SUPPPAGEPOPS", newList.toString(), { expires: 11000 } );
				
				// if the user is logged in AND NOT in a published / unpublished page
				if ( isLoggedIn && !editMode ) {
					
					// make an ajax call to save these supressions to the DB
					$j.post( "cf_pagepops/ajaxhandler.cfm", { cmd: "saveSuppressedPagePops", pageid: pageid, suppPagePops: $j("#pagePopModal").data( "viewedPagePops" ).toString() }, function( data ) {
						
					} );
					
				}
				
				$j(this).dialog("close"); 
			} 
		},
		open: function() {
			
			// ensure there is a data container for viewed pagepops
			if ( !$j.isArray( $j("#pagePopModal").data( "viewedPagePops" ) ) ) {
				
				// if there is not, then this is our first time opening the modal
				// so we add our first pagepopID as viewed
				var viewedPagePops = [];
				var pagePopID = $j(".pagePopSlide").eq(0).attr("id").replace("pagePopSlide_","");
				viewedPagePops.push( pagePopID );
				
				$j("#pagePopModal").data( "viewedPagePops", viewedPagePops );
				
				// and we add our pagination
				$j("#loopedSlider").loopedSlider( {
					addPagination: true,
					container: ".sliderContainer",
					slides: ".sliderSlides",
					pagination: "pagePopPagination",
					containerClick: false,
					autoHeight: 500
				} );
												
				//show controls if there's more than one
				if( $j(".sliderSlides").children().length > 1 ){
					$j("#loopedSlider").useDialogControls();
				} else {//manually set the height on singular content
					$j(".sliderContainer").height($j(".pagePopSlide").outerHeight());
				}
				
				// hide the original controls
				$j("#loopedSlider > :not(.sliderContainer)").hide();
				
				// hide the original controls
				$j("#loopedSlider > :not(.sliderContainer)").hide();
				
			}
			
			$j(this).css({'max-height': ( $j(window).height() - 280 ), 'overflow': 'auto', 'height': 'auto'});
			
			$j(".ui-dialog-buttonpane").find(".ui-button-text").attr( "title", "Do not show me this PagePop in the future unless it is updated" );
		}
	} );

	// we create a custome handler which opens the dialog
	$j(".pagePopLink").live( "pagePopOpen", function() {
		
		$j("#pagePopModal").dialog( "open" );
		
	} );

	// we convert a click into a custom handler to get around some limitations of the tab slider plugin
	$j(".pagePopLink").live( "click", function() {
		
		$(this).trigger( "pagePopOpen" );
		
	} );
	
	//hover states on the static widgets
	$j("#pagePopModal").find("a.previous, a.next").hover( 
		function() { $j(this).addClass('ui-state-hover'); }, 
		function() { $j(this).removeClass('ui-state-hover'); }
	);
	
	// if the pagePop is not suppressed AND the user is not logged in
	if ( !isSuppressed && !isLoggedIn ) {
		
		// test for acepting cookies
		$j.cookie('testcookiesenabled', null);
		$j.cookie('testcookiesenabled', 'enabled');
		
		// reset the isSuppressed based on cookie acceptance (if the cookie value is false then issuppressed is true)
		isSuppressed = ( !$j.cookie('testcookiesenabled') );
		
		// get rid of the cookie
		$j.cookie('testcookiesenabled', null);
		
	}
	
	
	// if the pagePop is still not suppressed, launch it
	if ( !isSuppressed ) {
		$j("#pagePopModal").dialog( "open" );
	}
	
} );



// ============================================================ 
// ! MOVE OUR PAGINATION CONTROLS TO THE DIALOG'S BUTTON PANE   
// ============================================================ 
jQuery.fn.extend( {
	useDialogControls: function() {
		
		// find the dialog content
		var dialogObj = jQuery(this).parent();
		
		// find the dialog button pane
		var buttonPane = jQuery(dialogObj).next(".ui-dialog-buttonpane");
		
		// find any buttons in the button pane
		var buttonCtrls = jQuery(buttonPane).find(".ui-button");
		
		// we will track the width of all buttons combined
		var buttonWidth = 0;
		jQuery(buttonCtrls).each( function() {
			buttonWidth += jQuery(this).width() + 24;
		} );
		
		// find the dialog title container
		var titleObj = jQuery(dialogObj).prev(".ui-dialog-titlebar").find("span.ui-dialog-title");
		
		// determine the free width inside the button pane
		var freeSpace = (jQuery(buttonPane).width() - buttonWidth);
		
		// get the pagination controls
		var paginationCtrls = jQuery(dialogObj).find("ul.pagePopPagination");
		
		// get the next / last controls
		var prevCtrl = jQuery(dialogObj).find("a.previous");
		var nextCtrl = jQuery(dialogObj).find("a.next");
		
		// create a container for the new controls
		var paginationHTML = '<div class="dialogPagination"></div>';
		
		// insert the container into the button pane
		jQuery(buttonPane).prepend( jQuery(paginationHTML).width( freeSpace ) );
		
		// get the new page controls container
		var newPagination = jQuery(buttonPane).find(".dialogPagination");
		
		// clone the pagination controls and move them to the new controls container
		jQuery(paginationCtrls).clone(true).appendTo( jQuery(newPagination) );
		
		// re-class our newly created controls
		jQuery(newPagination).find(".pagePopPagination").addClass("dialogPaginationList");
				
		// update the class of our controls
		jQuery(".dialogPaginationList").find("a").addClass("status");		
		
		// add the previous and next controls
		jQuery(".dialogPaginationList").append( '<li class="nextLi"></li>' );
		jQuery(".dialogPaginationList").prepend( '<li class="prevLi"></li>' );
		jQuery(prevCtrl).clone(true).appendTo( jQuery(".dialogPaginationList .prevLi") );
		jQuery(nextCtrl).clone(true).appendTo( jQuery(".dialogPaginationList .nextLi") );		
		
	}
} );



// ==================================================================== 
// ! FORCE ONE DIMENSIONAL ARRAY TO CONTAIN ONLY SINGLE UNIQUE VALUES   
// ==================================================================== 
function forceUnique( arrayObj ) {
	
	// the returned array
	var returnArray = [];
	
	// loop over our array object
	label: for (var i=0; i<arrayObj.length;i++ ) {  
	
		// if the array item is empty or undefined, try the next item
		if ( $j.trim(arrayObj[i]) == "" || $j.trim(arrayObj[i]) == "undefined" ) {
			continue label;
		}
		
		// loop over our returned array
		for (var j=0; j<returnArray.length;j++ ) {
			
			// if our returned array already contains this array object item
			if (returnArray[j]==arrayObj[i]) {
				// then move back to the beginning of the outer loop for the next object
				continue label;
			}
		}
	
		// if we made it this far, add the aray object item to the new array
		returnArray[returnArray.length] = arrayObj[i];
	}
	
	return returnArray;	
}

