// JavaScript Document
$( document ).ready( function()
{
	
// INIT LOADING OF MODULES
	// Load friends and followers here
	if( typeof $( '#jq_load_friends_here' ).get( 0 ) != 'undefined' )
	{
		
		try
		{
			$( '#jq_load_friends_here' ).load( 'http://' + location.host + ajax_prefix + ajax_load_modules + '4/' + kiosk_id, { },
				function( data )
				{
					// Slide this down
					//$( '#jq_load_friends_here > .jq_hide' ).slideDown();
					
					// Conditional Event Handlers
					if( typeof $( '#jq_lightbox_show_kiosk_friends' ).get( 0 ) != 'undefined' )
					{
						// Prepare the lightbox
						prepareLoadedContactsLightbox();	// Function 'prepareLoadedContactsLightbox' located in file 'lightbox_show_kiosk_friends_view.php'
					}
					
					// Fix Listing Module Glitch
					fixWidthOnListingModules();
				} );
		}
		catch(err){ /* Do Nothing */ }
	}
	//else
	//{
		//alert( 'trtrtr' );	
	//}
	
	// Load other kiosk products here
	if( typeof $( '#jq_load_other_products_here' ).get( 0 ) != 'undefined' )
	{
		try
		{
			$( '#jq_load_other_products_here' ).load( 'http://' + location.host + ajax_prefix + ajax_load_modules + '1/' + kiosk_id + '/' + product_id, { },
				function( data )
				{
					// Apply Nifty to new loaded elements
					//Nifty("div.left_bubble","transparent ");
					
					// Add Event Handler
					if( user_logged_in )
						activateAddToFavList();
					
					// Slide this down
					//$( '#jq_load_other_products_here > div > .jq_hide' ).slideDown();
					
					// Fix Listing Module Glitch
					fixWidthOnListingModules();
				} );
		}
		catch(err){ /* Do Nothing */ }
	}
	
	// Load Recommended / Similar products here
	if( typeof $( '#jq_load_recommended_products_here' ).get( 0 ) != 'undefined' )
	{
		try
		{
			$( '#jq_load_recommended_products_here' ).load( 'http://' + location.host + ajax_prefix + ajax_load_modules + '2/' + product_id + '/' + listing_type, { },
				function( data )
				{
					if( user_logged_in )
						activateAddToFavList();
					
					// Slide this down
					//$( '#jq_load_recommended_products_here > .jq_hide' ).slideDown();
					
					// Fix Listing Module Glitch
					fixWidthOnListingModules();
				} );
		}
		catch(err){ /* Do Nothing */ }
	}
	
	// Load Favorites list here
	if( typeof user_logged_in != 'undefined' && user_logged_in && typeof $( '#jq_load_my_favorites_here' ).get( 0 ) != 'undefined' )
	{
		try
		{
			$( '#jq_load_my_favorites_here' ).load( 'http://' + location.host + ajax_prefix + ajax_load_modules + '3', { },
				function( data )
				{
					// Fix noscroll/scroll css glitch
					nullCallback();
					
					// Event Handler for 'my list' btn
					$( '#jq_my_list_btn' ).click(
						function()
						{
							if( $( '#jq_my_fav_list' ).css( 'display' ) == 'none' )
								$( '#jq_my_fav_list' ).fadeIn( 'fast' );
							else
								$( '#jq_my_fav_list' ).fadeOut( 'fast' );
							
							// Kill Propogation
							return false;
						} );
					
					// Slide this down
					if( showHideMyFavList( false, true ) == 'nonempty' )
					{
						/*$( '#jq_load_my_favorites_here > .jq_hide' ).slideDown( 'normal',
							function()
							{*/
								// Activate Delete btn handler
								activateDeleteFromFavList();
								
								eliminateRedundancy();
							/*} );*/
					}
					
					// Create Event Handler
					$( '.rapida_cerrar > a' ).click(
						function()
						{
							// Hide Module
							$( '#jq_my_fav_list' ).fadeOut( 'fast' );
							
							// Kill Propogation
							return false;
						} );
					
					// Fix Listing Module Glitch
					fixWidthOnListingModules();
				} );
		}
		catch(err){ /*alert( err );/* Do Nothing */ }
	}
	
	// Load Recommended / Similar products here
	if( typeof $( '#jq_load_recent_products_here' ).get( 0 ) != 'undefined' )
	{
		try
		{
			$( '#jq_load_recent_products_here' ).load( 'http://' + location.host + ajax_prefix + ajax_load_modules + '5/products', { },
				function( data )
				{
					//Center All Images
					$( '' ).centerVerticalImage();
					
					// Apply My List Event Handler
					activateAddToFavList();
					
					// Fix Listing Module Glitch
					fixWidthOnListingModules();
				} );
		}
		catch(err){ /* Do Nothing */ }
	}
// END INIT LOADING OF MODULES

// INIT ADD/DEL TO FAV LIST
	
// END INIT ADD/DEL TO FAV LIST

} );


	// Click on Add: event handler encapsulated in a function
	function activateAddToFavList()
	{
		// First eliminate existing events
		$( '.jq_add_fav_list' ).unbind( 'click' );
		
		// Replace it
		$( '.jq_add_fav_list' ).click(
			function()
			{
				//alert( 'hello' );
				// Show My Fav List if hidden
				showHideMyFavList( true, false );
				
				// Parse and save important data
				var data = $( '' ).parseHref( $( this ).get( 0 ).href );
				data = data.split( "|" );
				var pid = data[0];
				var kid = data[1];
				var itype = data[2];
				
				// AJAX - Add to list here
				processMyFavListManager( pid, itype, 'add', nullCallback );
				
				// Begin appending
				var new_record_to_append = '<tr class="jq_newly_added jq_hide show_border">';
					
					// Determine what HTML to use - New Hack - 2/10/10
					if( $( this ).hasClass( 'jq_not_in_module' ) )
						new_record_to_append += buildListHtml( $( this ).parent().next().html() );
					else
						new_record_to_append += $( this ).parent().parent().html();
				
				// Finish html
				new_record_to_append += '</tr>';
				
				// Insert
				var first_tr = $( 'table', '#jq_my_fav_list ' ).children().children().get( 0 );
				$( new_record_to_append ).insertBefore( first_tr );
				
				// Fade in new record
				$( '.jq_newly_added' ).fadeIn( 'normal',
					function()
					{
						// Change Add btn to Delete btn
						var this_url = $( '.jq_add_fav_list', '.jq_newly_added' );
						$( this_url ).addClass( 'jq_del_fav_list' ).removeClass( 'jq_add_fav_list' ).attr( 'href', '#' + pid + '|' + kid + '|' + itype);
						
						// Remove clase of newly added
						$( this ).removeClass( 'jq_newly_added' );
						
						// Activate New Delete Button
						activateDeleteFromFavList();
					} );
				
				// Remove button
				$( this ).fadeOut();
				
				return false;
			} );
	}
	
	// Null Callback function - do nothing
	function nullCallback( data )
	{
		// Required Vars
		var curr_count = $( 'div', '#jq_my_list_btn' ).html();
		var new_count = countItemsInList();
		
		// Update count on red bubble of dock for mi lista
		if( new_count > 0 )
			$( 'div', '#jq_my_list_btn' ).html( new_count );
		else
			$( 'div', '#jq_my_list_btn' ).html( '' );
		
		// Add / Remove Red Bubble
		if( new_count > 0 && $( 'div', '#jq_my_list_btn' ).hasClass( 'lista_a' ) )
			$( 'div', '#jq_my_list_btn' ).addClass( 'lista_b' ).removeClass( 'lista_a' );
		else if( new_count == 0 )
			$( 'div', '#jq_my_list_btn' ).addClass( 'lista_a' ).removeClass( 'lista_b' );
		
		// Add / Remove Scrolling Class
		if( new_count > 5 )
		{
			//alert( $( 'table', '#jq_my_fav_list' ).get( 0 ).className );
			$( 'table', '#jq_my_fav_list' ).removeClass( 'noscrolling' ).addClass( 'scrolling' );
		}
		else
		{
			//alert( $( 'table', '#jq_my_fav_list' ).get( 0 ).className );
			$( 'table', '#jq_my_fav_list' ).removeClass( 'scrolling' ).addClass( 'noscrolling' );
		}
			
		// Open Dock List only when going from 0 to 1 items added
		if( curr_count == 0 && new_count == 1 )
			$( '#jq_my_fav_list' ).fadeIn( 'fast' );
	}
	
	
		
	// Click on Delete: event handler encapsulated in a function
	function activateDeleteFromFavList()
	{
		// First eliminate existing events
		$( '.jq_del_fav_list' ).unbind( 'click' );
		
		// Replace it
		$( '.jq_del_fav_list' ).click(
			function()
			{
				// Parse and save important data
				var data = $( '' ).parseHref( $( this ).get( 0 ).href );
				data = data.split( "|" );
				var pid = data[0];
				var kid = data[1];
				var itype = data[2];
				
				// AJAX - Remove to list here
				processMyFavListManager( pid, itype, 'delete', nullCallback );
				
				// Begin removing
				$( this ).parent().parent().fadeOut().remove();
				
				// Show eauivilent add btn
				showPrevHiddenAddBtns( pid, kid, itype );
				
				// Hide My Fav List if displayed
				showHideMyFavList( false, false );
				
				return false;
			} );
	}
		
	// Show any previously hidden "add to list" btn's
	function showPrevHiddenAddBtns( pid, kid, itype )
	{
		// Show any hiding add to list
		$( 'a[href=#' + pid + '|' + kid + '|' + itype + ']' ).each(
			function()
			{
				if( $( this ).hasClass( 'jq_add_fav_list' ) )
					$( this ).fadeIn();
			} );
	}
		
	// Determine if we should hide my list or not
	function showHideMyFavList( add_event, first_call )
	{
		var first_tr = $( 'table', '#jq_my_fav_list ' ).children().children().get( 0 );
		if( first_tr.id == 'jq_end_my_fav_list' && !add_event )
		{
			if( first_call )
				$( '#jq_my_fav_list' ).hide();
			else
				$( '#jq_my_fav_list' ).slideUp();
			return 'empty';
		}
		else
		{
			// POP IN AUTOMATICALLY
			// $( '#jq_my_fav_list' ).fadeIn();
			return 'nonempty';
		}
	}
		
	// Function that process all the ajax
	function processMyFavListManager( pid, type, action, callback )
	{
		try
		{
			// Submit with new ajax url
			$.post( 'http://' + location.host + ajax_prefix + ajax_add_product_to_list,
					{ 'product_id' : pid, 'viewer_kiosk_id' : user_kiosk_id, item_type : type, 'action' : action },
					callback );
		}
		catch( err )
		{
			// Alert Error
			alert( "Error Submitting via Ajax\n" + err );
			
			// Return
			return false;
		}	
	}
		
	// Function that will hide "add" button to items already in list
	function eliminateRedundancy()
	{
		// Local function that loops thru all 'jq_add_fav_list' to check for matches
		var performCheck =
			function( needle )
			{
				$( '.jq_add_fav_list' ).each(
				function()
				{
					if( this.href == needle )
						$( this ).hide();
				} );	
			}
		
		var temp_search = null;
		$( '.jq_del_fav_list' ).each(
			function()
			{
				performCheck( this.href );
				// temp_search = $( '' ).parseHref( this.href );
			} );
	}
	
	// Function that counts the item at any given time from the list
	function countItemsInList()
	{
		// Counter
		var i = 0;
		
		// Loop
		$( 'tr', '#jq_my_fav_list table' ).each(
			function()
			{
				if( $( this ).get( 0 ).id != 'jq_end_my_fav_list' )
					i++;
			} );
		
		// Result
		return i;
	}
	
	// Function that prepares html for insertion to my list
	function buildListHtml( data )
	{
		// Split chucnks
		var first = data.substring( 0, data.indexOf( '</div>' ) + 6 );
		var second = data.substring( data.indexOf( '</div>' ) + 6, data.lastIndexOf( '<a href' ) );
		var third = data.substring( data.lastIndexOf( '<a href' ) );
		
		// Build HTML
		var html = '<td width="60">' +
						$.trim( first ) +
					'</td><td>' +
						$.trim( second ) +
					'</td><td class="w20">' +
						$.trim( third ) +
					'</td>';
					
		// Return
		return html;	
	}
