$(function() { 
		

		/*** BEGIN TOOLTIP POPUP ***/

		var hideDelay = 100;  
		var currentID;
		var hideTimer = null;
		
		// One instance that's reused to show info for the current person
		var container = $('<div id="ToolTipContainer">'
		  + '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="ToolTipPopup">'
		  + '<tr>'
		  + '   <td class="corner topLeft"></td>'
		  + '   <td class="top"></td>'
		  + '   <td class="corner topRight"></td>'
		  + '</tr>'
		  + '<tr>'
		  + '   <td class="left">&nbsp;</td>'
		  + '   <td><div id="ToolTipContent"></div></td>'
		  + '   <td class="right">&nbsp;</td>'
		  + '</tr>'
		  + '<tr>'
		  + '   <td class="corner bottomLeft">&nbsp;</td>'
		  + '   <td class="bottom">&nbsp;</td>'
		  + '   <td class="corner bottomRight"></td>'
		  + '</tr>'
		  + '</table>'
		  + '</div>');
		
		$('body').append(container);
		
		$('.ToolTipTrigger').live('mouseover', function()
		{
		  if (hideTimer)
		      clearTimeout(hideTimer);
		
		  var pos = $(this).offset();
		  var width = $(this).width();
		  container.css({
		      left: (pos.left + width) + 'px',
		      top: pos.top - 5 + 'px'
		  });
		
		  $('#ToolTipContent').html('&nbsp;');
		
		  $.ajax({
		      type: 'GET',
		      url: '/tooltips/' + $(this).attr('rel') + '.html',
		      success: function(data)
		      {
		              $('#ToolTipContent').html( data );
		      }
		  });
		
		  container.css('display', 'block');
		});
		
		$('.ToolTipTrigger').live('mouseout', function()
		{
		  if (hideTimer)
		      clearTimeout(hideTimer);
		      
		  hideTimer = setTimeout(function()
		  {
		      container.css('display', 'none');
		  }, hideDelay);
		});
		
		// Allow mouse over of details without hiding details
		$('#ToolTipContent').mouseover(function()
		{
		  if (hideTimer)
		      clearTimeout(hideTimer);
		});
		
		// Hide after mouseout
		$('#ToolTipContent').mouseout(function()
		{
		  if (hideTimer)
		      clearTimeout(hideTimer);
		  hideTimer = setTimeout(function()
		  {
		      container.css('display', 'none');
		  }, hideDelay);
		});
		
		/*** END TOOLTIP POPUP ***/
		
		
		/*** COLORBOX POPUP OPTIONS ***/

		var cboptions = { width:750, height:600 }
		
		$("a.ajaxload").live('click',function(){
			$.colorbox({ width:750, height:800, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.videoPopup").live('click',function(){
			$.colorbox({ width:1000, height:600, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.clientForm").live('click',function(){
			$.colorbox({ width:600, height:600, overlayClose: false, href:$(this).attr('href')});
			return false;
		});
		
		$("a.userForm").live('click',function(){
			$.colorbox({ width:525, height:340, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.vendorForm").live('click',function(){
			$.colorbox({ width:525, height:500, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.groupAdminForm").live('click',function(){
			$.colorbox({ width:525, height:250, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.groupForm").live('click',function(){
			$.colorbox({ width:525, height:600, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.inviteeForm").live('click',function(){
			$.colorbox({ width:525, height:475, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.changeCompanyForm").live('click',function(){
			$.colorbox({ width:525, height:180, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.refundForm").live('click',function(){
			$.colorbox({ width:525, height:200, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.questionForm").live('click',function(){
			$.colorbox({ width:525, height:500, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.answerForm").live('click',function(){
			$.colorbox({ width:525, height:400, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.sponsorshipForm").live('click',function(){
			$.colorbox({ width:650, height:450, overlayClose: false, href:$(this).attr('href')});
			return false;
		});
			
		$("a.ticketTypeForm").live('click',function(){
			if ( jQuery.trim( $( "#EventMaxSize" ).val() ) == "" || jQuery.trim( $( "#EventMaxSize" ).val() ) == "0" )
			{
				jAlert( 'Maximum Size of the Event Must Be Set', 'Event Farm' );
				return false;
			}
			else
			{
				$.colorbox({ width:525, height:400, overlayClose: false, href:$(this).attr('href') + "/" + $( "#EventMaxSize" ).val() });
				return false;
			}
		});

		$("a.promoCodeForm").live('click',function(){
			$.colorbox({ width:650, height:350, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.userRoleForm").live('click',function(){
			$.colorbox({ width:575, height:300, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.emailForm").live('click',function(){
			$.colorbox({ width:850, height:800, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		$("a.invitationForm").live('click',function(){
			$.colorbox({ width:750, height:450, overlayClose: false, href:$(this).attr('href')});
			return false;
		});
		
		$("a.transactionForm").live('click',function(){
			$.colorbox({ width:525, height:650, overlayClose: false, href:$(this).attr('href')});
			return false;
		});
			
		$("a.purchaseForm").live('click',function(){
			$.colorbox({ width:950, height:850, overlayClose: false, href:$(this).attr('href')});
			return false;
		});
		
		$("a.eventPageForm").live('click',function(){
			$.colorbox({ width:800, height:750, overlayClose: false, iframe: true, href:$(this).attr('href')});
			return false;
		});

		$("a.messageSlugs").live('click',function(){
			$.colorbox({ width:400, height:300, overlayClose: false, iframe: true, href:$(this).attr('href')});
			return false;
		});
			
		/*** END - COLORBOX POPUP OPTIONS ***/

		/*** BEGIN - DYMO LABEL PRINTING ***/

		$(".label_print").live('click',function(){
			$.colorbox({ width:300, height:200, overlayClose: false, href:$(this).attr('href')});
			return false;
		});

		/*** END - DYMO LABEL PRINTING ***/
		
		//invitees autocomplete
		$("#invitees_ac").live("click", function(){
      		$("#invitees_ac").autocomplete("/invitees/acsearch/", {
				selectFirst: true,
				width: 200
			}).result(function(event, item) {
				$.fn.colorbox({width:750,href:item[1]});
  			});
    	});
    	
    	//vendors add to event
        $('#addvendortoevent').live('click',function(){
			// selected value
			var selected = $('#EventVendors').val();
			var event = $('#EventId').val();
			
			ajax_loading_image('#EventVendorsList');

			$.ajax({
				type: "POST",
				url: '/events/addVendorToEventWF/'+event+'/'+selected,
				data: "ajax=true",
				success: function(msg){
					$('#EventVendorsList').html(msg);
				}
			});
			return false;
		});		

    	//vendors remove from event
        $('#removevendorfromevent').live('click',function(){

			var result = confirm( 'Remove Vendor From Event?', 'Event Farm' ); 

			if ( result ) 
			{			
				// selected value
				var event = $('#EventId').val();
				var selected = $(this).attr('href').substring( 8 );
				
				ajax_loading_image('#EventVendorsList');
	
				$.ajax({
					type: "POST",
					url: '/events/removeVendorFromEventWF/'+event+'/'+selected,
					data: "ajax=true",
					success: function(msg){
						$('#EventVendorsList').html(msg);
					}
				});
			}
			return false;
		});			    

    	//question remove from event
        $('#removequestionfromevent').live('click',function(){

			var result = confirm( 'Remove This Question From Event?', 'Event Farm' ); 

			if ( result ) 
			{			
				// selected value
				var event = $('#EventId').val();
				var selected = $(this).attr('href').substring( 1 );
				
				ajax_loading_image('#EventQuestionsList');
	
				$.ajax({
					type: "POST",
					url: '/questions/removeQuestionWF/'+event+'/'+selected,
					data: "ajax=true",
					success: function(msg){
						$('#EventQuestionsList').html(msg);
					}
				});
			}
			return false;
		});			    

    	//event role remove from event
        $('#removeeventrolefromevent').live('click',function(){

			var result = confirm( 'Remove User\'s Access to Event?', 'Event Farm' ); 

			if ( result ) 
			{			
				// selected value
				var event = $('#EventId').val();
				var selected = $(this).attr('href').substring( 1 );
				
				ajax_loading_image('#EventRolesList');
	
				$.ajax({
					type: "POST",
					url: '/eventroles/removeEventroleWF/'+event+'/'+selected,
					data: "ajax=true",
					success: function(msg){
						$('#EventRolesList').html(msg);
					}
				});
			}
			return false;
		});		

    	//ticket type remove from event
        $('#removesponsorshiprolefromsponsorship').live('click',function(){

			var result = confirm( 'Remove User\'s Access to Ticket Block?', 'Event Farm' ); 

			if ( result ) 
			{			
				var sponsorship = $('#SponsorshipId').val();
				var selected = $(this).attr('href').substring( 1 );
	
				ajax_loading_image('#SponsorshipRolesList');
	
				$.ajax({
					type: "POST",
					url: '/sponsorshiproles/removeSponsorshiproleWF/'+selected+'/'+sponsorship,
					data: "ajax=true",
					success: function(msg){
						$('#SponsorshipRolesList').html(msg);
					}
				});
			}
			return false;
		});

    	//event sitepage remove from event
        $('#removesitepagefromevent').live('click',function(){

			var result = confirm( 'Remove Site Page from Event?', 'Event Farm' ); 

			if ( result ) 
			{			
				// selected value
				var selected = $(this).attr('href').substring( 1 );
				
				ajax_loading_image('#EventPagesList');
	
				$.ajax({
					type: "POST",
					url: '/sitepages/removeSitepageWF/'+selected,
					data: "ajax=true",
					success: function(msg){
						$('#EventPagesList').html(msg);
					}
				});
			}
			return false;
		});		

    	//event sitepage remove from event
        $('#removedesignfromevent').live('click',function(){

			var result = confirm( 'Remove Email Design from Event?', 'Event Farm' ); 

			if ( result ) 
			{			
				// selected value
				var selected = $(this).attr('href').substring( 1 );
				
				ajax_loading_image('#EventDesignsList');
	
				$.ajax({
					type: "POST",
					url: '/designs/removeDesignWF/'+selected,
					data: "ajax=true",
					success: function(msg){
						$('#EventDesignsList').html(msg);
					}
				});
			}
			return false;
		});		
			    
    	//event sitepage make default for event
        $('#makedefaultsitepageforevent').live('click',function(){

			var result = confirm( 'Make Default Page for Event?', 'Event Farm' ); 

			if ( result ) 
			{			
				// selected value
				var selected = $(this).attr('href').substring( 1 );
				
				ajax_loading_image('#EventPagesList');
	
				$.ajax({
					type: "POST",
					url: '/sitepages/makeDefaultSitepageWF/'+selected,
					data: "ajax=true",
					success: function(msg){
						$('#EventPagesList').html(msg);
					}
				});
			}
			return false;
		});		
			    

    	//ticket type remove from event
        $( '#removetickettypefromevent' ).live('click',function() {
        
			var result = confirm( 'Remove Ticket Type From Event?', 'Event Farm' ); 

			if ( result ) 
			{			
				var event = $('#EventId').val();
				var selected = $(this).attr('href').substring( 1 );
				
				ajax_loading_image( '#EventTicketsList' );
	
				$.ajax({
					type: "POST",
					url: '/tickettypes/removeTickettypeWF/'+event+'/'+selected,
					data: "ajax=true",
					success: function(msg){
						$('#EventTicketsList').html(msg);
					}
				});
			}
			
			return false;
		});			    

    	//ticket type remove from event
        $('#removesponsorshipfromevent').live('click',function(){

			var result = confirm( 'Remove Ticket Block?\n\nRemoving this ticket block will delete all tickets\ncurrently in use on this Ticket Block.', 'Event Farm' ); 

			if ( result ) 
			{			
				// selected value
				var event = $('#EventId').val();
				var selected = $(this).attr('href').substring( 1 );
				
				ajax_loading_image('#EventSponsorList');

				$.ajax({
					type: "POST",
					url: '/sponsorships/removeSponsorshipWF/'+selected,
					data: "ajax=true",
					success: function(msg){
						$('#EventSponsorList').html(msg);
					}
				});
			}

			return false;
			
		});			    

    	//promotional code remove from event
        $('#removecodefromevent').live('click',function(){

			var result = confirm( 'Remove Promotional Code from Event ?', 'Event Farm' ); 

			if ( result ) 
			{			
				var event = $('#EventId').val();
				var selected = $(this).attr('href').substring( 1 );
	
				ajax_loading_image( '#EventCodesList' );
	
				$.ajax({
					type: "POST",
					url: '/promotions/removePromotionWF/'+event+'/'+selected,
					data: "ajax=true",
					success: function(msg){
						$('#EventCodesList').html(msg);
					}
				});
			}

			return false;
		});

    	//promotional code remove from event
        $('#removeinvitationfromevent').live('click',function(){

			var result = confirm( 'Remove this Ticket?', 'Event Farm' ); 

			if ( result ) 
			{			
				var selected = $(this).attr('rel');
	
				$.ajax({
					type: "POST",
					url: '/invitations/recycleInvitationWF/'+selected,
					data: "ajax=true",
					success: function(msg){
						location.reload();
					}
				});
			}
		});

	 	$('#quicksave').click(function(){
 			$(this).parents("form:first").ajaxSubmit({
 				success: function(responseText, responseCode) {
 					$('#ajax-save-message').hide().html(responseText).fadeIn();
 					setTimeout(function(){ 
 						$('#ajax-save-message').fadeOut(); 
 					}, 5000);
 				}
 			});
 			return false;
 		}); 

	 	$('.quicksaveandopen').click(function(){
	 		var destination = $(this).attr('href');
 			$(this).parents("form:first").ajaxSubmit({
 				success: function() {
		 			$.colorbox({href:destination, iframe:true, innerWidth:850, innerHeight:800});
 				}
 			});
 			return false;
 		}); 

	 	$('.previewopen').click(function(){
	 		var destination = $(this).attr('href');
 			$.colorbox({href:destination, iframe:true, innerWidth:1000, innerHeight:650});
 			return false;
 		}); 

		$('.previewemaildesign').click(function() {
            var newHref = $(this).attr('href') + '/' + $( "#" + $(this).attr('rel') ).val();
 			$.colorbox({href:newHref, iframe:true, innerWidth:1000, innerHeight:650});
            return false;
		});
 		
 		/**** SLIDING PANEL ****/

		// Expand Panel
		$("#open").click(function(){
			$("div#slide-panel").slideDown("slow");
			return false;
		
		});	
		
		// Collapse Panel
		$("#close").click(function(){
			$("div#slide-panel").slideUp("slow");	
			return false;
		});		
		
		// Switch buttons from "Log In | Register" to "Close Panel" on click
		$("#toggle a").click(function () {
			$("#toggle a").toggle();
		});		

 		/**** END - SLIDING PANEL ****/
 		
 		/**** CUSTOM SELECT INPUT FIELDS ****/
 		
		$( 'select.custom-select-small' ).selectmenu({
				style:'popup', 
				width: 75,
				menuWidth: 150,
				maxHeight: 200
		});

		$( 'select.custom-select-medium' ).selectmenu({
				style:'popup', 
				width: 150,
				menuWidth: 250,
				maxHeight: 200
		});
		
 		/**** END - CUSTOM SELECT INPUT FIELDS ****/

		$( '.rollover' ).tooltip({
			effect: "fade",
			position: "top center",
			offset: [-10, 0],
			opacity: 0.8,
			delay: 0
		});
		
});

// set a loading image
function ajax_loading_image(div) 
{
	$(div).html('<center><img src="/img/site-spinner.gif" border="0"></center>');
}

// remove loading image
function ajax_remove_loading_image(div) 
{
	$(div).html('');
}

function appendlink( link, dropdown )
{
	link.href = link.href + "/" + dropdown.options[dropdown.selectedIndex].value;
}

function appendValue( link, id )
{	
	//
	// TODO: Fix this mess
	//	
	if ( link.href.substr( link.href.length - 2, link.href.length ) == "/#" )
	{
		var old_link = link.href.substr( 0, link.href.length - 2 );
		link.href = old_link.substr( 0, old_link.lastIndexOf("/") ) + "/" + document.getElementById( id ).value + "/#";
	}
	else
	{
		link.href = link.href + "/" + document.getElementById( id ).value + "/#";
	}
}

function toggleDiv( id )
{
   var e = document.getElementById(id);
   if(e.style.display == 'block')
      e.style.display = 'none';
   else
      e.style.display = 'block';	
}

function showDiv( id )
{
   var e = document.getElementById(id);
   e.style.display = 'block';	
}

function inlineDiv( id )
{
   var e = document.getElementById(id);
   e.style.display = 'inline';	
}

function hideDiv( id )
{
   var e = document.getElementById(id);
   e.style.display = 'none';	
}

function loadPiece(href,divName) 
{    
    $(divName).load(href, function(){
        $(divName).find(".paginate").each(function(i){
          $(this).bind('click', function(){
            var thisHref = $(this).attr("href");
            loadPiece(thisHref,divName);
            return false;
          });
        });
    }); 
} 
