var index_id = 0;
var __index_id = 0;

function __update_counter( module, id )
{
	var url = HTML_ROOT + '/includes/ajax/pages_' + module + '.ajax.php';
	
	$.ajax(
		{
			type: "POST",
			url: url,
			cache: false,
			async: true,
			data: "id=" + id,
			success: function( m )
			{
				$( '#module_' + module + '_views' ).html( m.views );
			}
		}
	);
}

function __cycle_item( module, page_id, direction )
{
	if( !js_array[(__index_id + direction)] )
	{
		return false;
	}
	
	// grab our item data from array
	__index_id = ( __index_id + direction );
	var item = js_array[__index_id];
	
	switch( module )
	{
		// This is our video cycle code
		case 'videos':
			// generate our new item output
			var FO = {
				movie: HTML_ROOT + "/includes/ufo.swf",
				width: item.width,
				height: item.height,
				majorversion: "7",
				build: "0",
				bgcolor: "transparent",
				wmode: "transparent",
				allowfullscreen: "true",
				flashvars: "file=" + HTML_ROOT + "/images/content/" + page_id + "/" + module + "/" + item.flv + "&image=" + HTML_ROOT + "/images/content/" + page_id + "/" + module + "/" + item.img
			};
			
			UFO.create( FO, "flvplayer" );
			
			$( '#module_' + module + '_runtime' ).html( item.runtime.minutes + ':' + item.runtime.seconds );
			$( '#module_' + module + '_id' ).html( __index_id + 1 );
			
			if( !js_array[(__index_id + 1)] )
			{
				$( '#module_' + module + '_next' ).unbind( 'click' );
			}
			
			if( !js_array[(__index_id - 1)] )
			{
				$( '#module_' + module + '_prev' ).unbind( 'click' );
			}
			
			// update item view counter
			//__update_counter( module, item.id );
			break;
		// This is our image cycle code
		case 'images':
			
			$( '#module_' + module + '_holder' ).attr( {
				'src': HTML_ROOT + "/images/content/" + page_id + "/" + module + "/large/" + item.image,
				'width': item.mwidth,
				'height': item.mheight,
				'style': 'cursor: pointer;'
			} );
			
			// item_id, id, image_width, image_height, image_path, count
			$( '#module_' + module + '_popup' ).unbind( 'click' ).bind( 'click', function() { return _open_images_popup( item.id, __index_id, item.width, item.height, HTML_ROOT + "/images/content/" + page_id + "/" + module + "/large/" + item.image, js_array.length ); } );
			$( '#module_' + module + '_holder' ).unbind( 'click' ).bind( 'click', function() { return _open_images_popup( item.id, __index_id, item.width, item.height, HTML_ROOT + "/images/content/" + page_id + "/" + module + "/large/" + item.image, js_array.length ); } );
			
			$( '#module_' + module + '_id' ).html( __index_id + 1 );
			
			if( !js_array[(__index_id + 1)] )
			{
				$( '#module_' + module + '_next' ).unbind( 'click' );
			}
			
			if( !js_array[(__index_id - 1)] )
			{
				$( '#module_' + module + '_prev' ).unbind( 'click' );
			}
			
			// update item view counter
			//__update_counter( module, item.id );
			
			break;
			// This is our image cycle code
		case 'events':
		
			$( '#module_' + module + '_holder img' ).attr( {
				'src': HTML_ROOT + "/images/events/" + page_id + "/images/large/" + item.image,
				'width': item.mwidth,
				'height': item.mheight,
				'style': 'cursor: pointer;'
			} );
			//next we want to change the image description
			
			$( '#module_' + module + '_desc' ).html( item.title + '-' + item.description);
			
			//then we want to bind the next image to the popup section
			//first we unbind the click function
			$('#module_events_holder').unbind('click').bind('click', function() {
				open_exhibition_gallery( page_id, __index_id, item.width, item.height, item.src, js_array.length );
			} ).css('cursor', 'pointer');
			
			// item_id, id, image_width, image_height, image_path, count
			//$( '#module_' + module + '_popup' ).unbind( 'click' ).bind( 'click', function() { return _open_images_popup( item.id, __index_id, item.width, item.height, HTML_ROOT + "/images/events/" + page_id + "/" + module + "/large/" + item.image, js_array.length ); } );
			//$( '#module_' + module + '_holder' ).unbind( 'click' ).bind( 'click', function() { return _open_images_popup( item.id, __index_id, item.width, item.height, HTML_ROOT + "/images/events/" + page_id + "/" + module + "/large/" + item.image, js_array.length ); } );
			
			$( '#module_' + module + '_id' ).html( __index_id + 1 );
			
			if( !js_array[(__index_id + 1)] )
			{
				$( '#module_' + module + '_next' ).unbind( 'click' );
			}
			
			if( !js_array[(__index_id - 1)] )
			{
				$( '#module_' + module + '_prev' ).unbind( 'click' );
			}
			
			// update item view counter
			//__update_counter( module, item.id );
			
			break;
			// This is our image cycle code
		case 'photos':
		
			$( '#module_' + module + '_holder img' ).attr( {
				'src': HTML_ROOT + "/images/photos/" + page_id + "/images/large/" + item.image,
				'width': item.width,
				'height': item.height,
				'style': 'cursor: pointer;'
			} );
			//next we want to change the image description
			
			$( '#module_' + module + '_desc' ).html( item.title + '-' + item.description);
			
			//then we want to bind the next image to the popup section
			//first we unbind the click function
			$('#module_photos_holder').unbind('click').bind('click', function() {
				open_photo_gallery( page_id, __index_id, item.width, item.height, item.src, js_array.length );
			} ).css('cursor', 'pointer');
			
			// item_id, id, image_width, image_height, image_path, count
			//$( '#module_' + module + '_popup' ).unbind( 'click' ).bind( 'click', function() { return _open_images_popup( item.id, __index_id, item.width, item.height, HTML_ROOT + "/images/events/" + page_id + "/" + module + "/large/" + item.image, js_array.length ); } );
			//$( '#module_' + module + '_holder' ).unbind( 'click' ).bind( 'click', function() { return _open_images_popup( item.id, __index_id, item.width, item.height, HTML_ROOT + "/images/events/" + page_id + "/" + module + "/large/" + item.image, js_array.length ); } );
			
			$( '#module_' + module + '_id' ).html( __index_id + 1 );
			
			if( !js_array[(__index_id + 1)] )
			{
				$( '#module_' + module + '_next' ).unbind( 'click' );
			}
			
			if( !js_array[(__index_id - 1)] )
			{
				$( '#module_' + module + '_prev' ).unbind( 'click' );
			}
			
			// update item view counter
			//__update_counter( module, item.id );
			
			break;
	}
}
