// JavaScript Document
$( document ).ready( function()
{
// INIT FLAG PRODUCT AND ADD TO MY LIST
	eventCreator_jqFlagItem_click();
// END INIT FLAG PRODUCT AND ADD TO MY LIST

// INIT "ADD TO FAVORITES"
	// Add Event Handler to Email button
	$( '#jq_add_to_favorites' ).click(
		function()
		{
			// Only get current location if not defined
			if( typeof reload_logged_in_url == 'undefined' )
			{
				var reload_logged_in_url = location.href;
				reload_logged_in_url = reload_logged_in_url.replace( 'http://', '' );
				reload_logged_in_url = reload_logged_in_url.replace( location.host, '' );
				if( reload_logged_in_url.indexOf( '#' ) != -1 )
					reload_logged_in_url = reload_logged_in_url.substr( 1, reload_logged_in_url.indexOf( '#' ) - 1 );
				else
					reload_logged_in_url = reload_logged_in_url.substr( 1, reload_logged_in_url.length );
					
				if( reload_logged_in_url[reload_logged_in_url.length - 1] != '/' )
					reload_logged_in_url += '/';
			}
			
			// Only if 'add_favorites_code' isn't defineg
			if( typeof add_favorites_code == 'undefined' )
				add_favorites_code = 'addFav';
				
			// Reload Page
			window.location = 'http://' + location.host + ajax_prefix + reload_logged_in_url + /*kiosk_id + '/' +*/ add_favorites_code;
				
			// Always return false
			return false;
		} );
// END "ADD TO FAVORITES"

// INIT "MODIFY LIST MODULES"
	fixWidthOnListingModules();
// END "MODIFY LIST MODULES"
} );

// Encapsulated Event handler for 'flag item'
function eventCreator_jqFlagItem_click()
{
	// Remove all previous bindings
	$( '.jq_flag_item' ).unbind( 'click' );
	
	// Create new event handler
	$( '.jq_flag_item' ).click(
		function()
		{
			// Current Item's data
			var item_data = $( '' ).parseHref( this.href );
			var this_id = $( this );
			
			// Parse Item's data
			item_data = item_data.split( '|' );
			item_data = { iid : item_data[1], itype : item_data[3] }; 
			
			// Proceed with Ajax
			var flagSuccess = 
				function( data )
				{
					var alertType;
					// Analyze data
					if( data.substr(0, 1) != '1' )
					{
						// Red bg
						alertType = 'a1';
					}
					else
					{
						// Hide Flag
						$( this_id ).parent().hide();
						// Green bg
						alertType = 'a2';
					}
					
					// Invalid login, please try again
					$( '' ).showAlert( alertType, data.substr(2, data.length) );
				}
				
			try
			{
				// Submit with new ajax url
				$.post( 'http://' + location.host + ajax_prefix + ajax_flag_product,
						{ item_id : item_data.iid, item_type : item_data.itype },
						flagSuccess );
			}
			catch( err )
			{
				// Alert Error
				alert( "Error Submitting via Ajax\n" + err );	
			}
			
			// Return false
			return false;
		} );
}

// Create function that checks and fixes bugs
function fixWidthOnListingModules()
{
	// Private method to help count
	var helpCount =
		function( id )
		{
			// Counter
			var i = 0;
			
			// Loop
			$( 'table tr', id ).each(
				function()
				{
					if( $( this ).get( 0 ).id != 'jq_end_my_fav_list' )
						i++;
				} );
			
			// Result
			return i;	
		}
	
	// Loop Thru all Listing modules
	$( '.box_lista' ).each(
		function()
		{
			// Add / Remove Scrolling Class
			if( helpCount( this ) > 5 )
				$( 'table', this ).removeClass( 'noscrolling' ).addClass( 'scrolling' );
			else
				$( 'table', this ).removeClass( 'scrolling' ).addClass( 'noscrolling' );
		} );
}
