// *****************************************************************************************
// Script:		js/ajax.js
// Author(s):		Guillaume Lambert - guillaume@falzhobel.ca
//			Alex Gignac - alex@ax2.ca
//
// Last Update:		Jan 2010
//
// Javascript functions for all AJAX-based calls
//
// Functions:
//	-
//
// *****************************************************************************************

// Digital Issues Functions
// -----------------------------------------------------------------------------------------

/**
 * digital_issue_get_page()
 * sends AJAX request to get current page and load it into the current scrollable index
 *
 * digital_issues_id	required	id of the digital issue
 * first_page_rank	required	rank number of first page to load
 * sec_page_rank	required	rank number of second page to load, put 0 for none
 *
 */

function digital_issue_get_page(digital_issues_id, first_page_rank) {

	// set second page rank for dual view
	sec_page_rank = first_page_rank + 1;

	// begin ajax request
	$.ajax({
   		type: "get",
   		url: appWebPath + "_ajax/digital_issue_get_page.php",
   		dataType: "json",
   		data: { 'current_view_mode' : current_view_mode, 'digital_issues_id' : digital_issues_id, 'first_page_rank' : first_page_rank, "sec_page_rank" : sec_page_rank },
   		success: function(data){

     			// load pages depending on view mode and attach click event for view switch
     			if (current_view_mode == 'dual') {
     			
     				// make sure dual views are empty
				$('#page_dual_first, #page_dual_second').html('');
     			
     				// special case for dual view, need to verify if first page, to show table of contents
     				if (first_page_rank == 1) {
     					$('#issue_table_contents_dummy').clone().attr('id', 'issue_table_contents').appendTo('#page_dual_first');
     					$('#page_dual_first #issue_table_contents').show();	
     					$('#page_dual_second').html('<img src="' + data[0].page_image + '" border="0" class="page_image" alt="" />');
     					$('#page_dual_second img.page_image').click(function(event) { switch_view_mode('full', current_page); });	
     				} else {
     					$('#page_dual_first').html('<img src="' + data[0].page_image + '" border="0" class="page_image" alt="" />');
     					$('#page_dual_first img.page_image').click(function(event) { switch_view_mode('full', current_page); });
     					if (data.length > 1) {
     						$('#page_dual_second').html('<img src="' + data[1].page_image + '" border="0" class="page_image" alt="" />');
     						$('#page_dual_second img.page_image').click(function(event) { switch_view_mode('full', parseInt(current_page + 1)); });
     					} else {
     						$('#page_dual_second').html('<img src="' + appStaticWebPath + 'images/global/spacer_white.gif" width="367" height="507" border="0" alt="" />');
     					}
     				}

     			} else {
				$('#page_full_first').html('<img src="' + data[0].page_image + '" width="' + data[0].page_image_w + '" height="' + data[0].page_image_h + '" class="page_image" border="0" alt="" />');
				$('#page_full_first img.page_image').click(function(event) { switch_view_mode('dual', current_page); });
     			}
     			
   		}
 	});


}