﻿//Paging setup..
var categoriesPerPage = 5;
var cruisesPerPage = 5;

//Will contain all the cruises data
var dataDictionary = new Object;


//Initialize the Grid
function CabinTypeAndCruiseGrid_InitGrid( controlID )
{
    //Get the JSON Data
    var jsonData = $("#" + controlID + "_gridData" ).text(); 
        
    if( jsonData != null )
    {
        if ( jsonData != "" )
        {
            var data = Sys.Serialization.JavaScriptSerializer.deserialize( jsonData ); 
            //var data = eval('('+jsonData +')');
            
            //deserialized data...
            dataDictionary[ controlID ] = data; 
                    
            //get current pages...
            var categoryPage = getCurrentCategoryPage( controlID );
            var cruisePage = getCurrentCruisesPage( controlID );
            
            //Render Grid @ current pages...
            renderGrid( controlID, categoryPage, cruisePage );          
        }  
    }
}


function renderGrid( controlID, categoryPage, cruisePage )
{
    var data = dataDictionary[ controlID ];
    
    if ( data == null )
    {
        return;
    }
    
    var jCategories = $ ( data.Categories );
    var jCruises = $( data.Cruises );

    var numberOfCategories = jCategories.length;
    var numberOfCruises = jCruises.length;
    
    var minCategoryIndex = (categoryPage - 1) * categoriesPerPage;
    var maxCategoryIndex = (categoryPage * categoriesPerPage) - 1;
    var minCruiseIndex = (cruisePage - 1) * cruisesPerPage;
    var maxCruiseIndex = (cruisePage * cruisesPerPage) - 1;
    
    if ( maxCategoryIndex >= numberOfCategories )
    {
        maxCategoryIndex = numberOfCategories - 1;
    }
    
    if ( maxCruiseIndex >= numberOfCruises )
    {
        maxCruiseIndex = numberOfCruises - 1;
    }

    //SHOW HIDE PAGE NAVIGATION LINKS...
    var showPreviousCategoryLink = false;
    var showNextCategoryLink = false;
    var showPreviousCruiseLink = false;
    var showNextCruiseLink = false;
    
    if ( categoryPage > 1 )
    {
        showPreviousCategoryLink = true;
    }
    
    if ( numberOfCategories > ( categoryPage * categoriesPerPage ) )
    {
        showNextCategoryLink = true;
    }
    
    if ( cruisePage > 1 )
    {
        showPreviousCruiseLink = true;
    }
    
    if ( numberOfCruises > ( cruisePage * cruisesPerPage ) )
    {
        showNextCruiseLink = true;
    }



    var previousCategoryLink = $("#" + controlID + "_previousCategoryLink" );
    
    if ( !showPreviousCategoryLink )
    {
        previousCategoryLink.hide();
    }
    else
    {
        previousCategoryLink.show();
    }

    var nextCategoryLink = $("#" + controlID + "_nextCategoryLink" );
    
    if ( !showNextCategoryLink )
    {
        nextCategoryLink.hide();
    }
    else
    {
        nextCategoryLink.show();
    }
    
    var previousCruiseLink = $("#" + controlID + "_previousCruiseLink" );
    
    if ( !showPreviousCruiseLink )
    {
        previousCruiseLink.hide();
    }
    else
    {
        previousCruiseLink.show();
    }
    
    var nextCruiseLink = $("#" + controlID + "_nextCruiseLink" );
    
    if ( !showNextCruiseLink )
    {
        nextCruiseLink.hide();
    }
    else
    {
        nextCruiseLink.show();
    }
    
      
    var categoryDescription = data.CatDescr;
    var categoryCode = data.CatCode;
    var categoryOccupancy = data.CatOcc;
    
    //OK NOW RENDER GRID CONTENT!
    
    //This is the Dynamic Table that contains the category prices
    var dynamicTable = $("#" + controlID + "_categoryPriceTable" );
    dynamicTable.empty(); //first of all... clear!
    
    //this is a template of header row...
    var rowHeaderTemplate = $('<tr id="rowHeader" runat="server">' +
    '<td class="categoryPriceGridHeader">'+ categoryDescription +'</td>'+
    '<td class="categoryPriceGridHeaderCentered">'+ categoryCode + '</td>'+
    '<td class="categoryPriceGridHeaderCentered">'+ categoryOccupancy + '</td></tr>');
    
    //Append the Cruise columns to header   
    for ( var cruiseIndex = minCruiseIndex;  cruiseIndex <= maxCruiseIndex; cruiseIndex++ )
    {
        var cruiseDate = jCruises[cruiseIndex].DepDate;
        var aCruiseColumn = $('<td class="categoryPriceGridHeaderCentered">'+ cruiseDate + '</td>');
        rowHeaderTemplate.append( aCruiseColumn );    
    }
   
    //Append the Grid Header!
    dynamicTable.append( rowHeaderTemplate );
    
    
    //now build data row...
    var dataCss = 'categoryPriceGridRow';
    var alternateDataCss = 'categoryPriceGridRowAlternate';    
    var rowDataCss = dataCss;    
       
    for( var categoryIndex = minCategoryIndex; categoryIndex <= maxCategoryIndex ; categoryIndex++)
    {
        var categoryDescription = jCategories[categoryIndex].Descr;
        var categoryCode = jCategories[categoryIndex].Code;
        var categoryOccupancy = jCategories[categoryIndex].Min + "-" + jCategories[categoryIndex].Max;
    
        //A RowData Template
        var rowDataTemplate = $('<tr class="' + rowDataCss + '">' + 
        '<td class="categoryPriceCategoryDescriptionCell">' + categoryDescription + '</td>'+
        '<td class="categoryPriceCategoryCodeAndCapCell">' + categoryCode + '</td>'+
        '<td class="categoryPriceCategoryCodeAndCapCell">' + categoryOccupancy + '</td></tr>');
    
        for ( var cruiseIndex = minCruiseIndex;  cruiseIndex <= maxCruiseIndex; cruiseIndex++ )
        {
            var cruise = jCruises[cruiseIndex];
            var prize = getCruisePriceByCategoryCode( cruise, categoryCode );
            
            
//            var aCategoryPriceCell = $('<td class="categoryPriceCell">' +
//            '<span onclick="javascript:categorySelected(\'' + controlID + '\',\'' + cruise.Code + '\',\''+ categoryCode + 
//            '\');" style="cursor:pointer;">' + prize +'</span></td>');




//            var aCategoryPriceCell = $('<td class="categoryPriceCell">' +
//            '<span style="cursor:pointer;">' + prize +'</span></td>');

//            if ( prize != "-" )
//            {
//                $("span" , aCategoryPriceCell).click( function(){
//                    categorySelected( controlID , cruise.Code , categoryCode );
//                    } );
//            }


            var aCategoryPriceCell;
            
            if ( prize != "-" )
            {
                aCategoryPriceCell = $('<td class="categoryPriceCell">' +
                '<span style="cursor:pointer;" cid="' + controlID + '" crc="' + cruise.Code + '" cac="'+ categoryCode + '">' + prize +'</span></td>');

                $("span" , aCategoryPriceCell).click( function(){
                                        
                    var cid = this.attributes["cid"].value;
                    var crc = this.attributes["crc"].value;
                    var cac = this.attributes["cac"].value;
                                    
                    categorySelected( cid , crc , cac );
                    } );
            }   
            else
            {
                aCategoryPriceCell = $('<td class="categoryPriceCell">' +
                '<span style="cursor:pointer;">-</span></td>');
            }         
        
            rowDataTemplate.append( aCategoryPriceCell );
            
            aCategoryPriceCell = null;
        }
        
          
        //Append the Grid Data!
        dynamicTable.append( rowDataTemplate );
        
        //next row css
        if ( rowDataCss == dataCss )
        {
            rowDataCss = alternateDataCss;
        }
        else
        {
            rowDataCss = dataCss;
        }
    } 

}

function getCruisePriceByCategoryCode( cruiseObj , categoryCode )
{
    var jPrices = $(cruiseObj.CatPrices);
    
    for ( var prizeIndex = 0; prizeIndex < jPrices.length; prizeIndex++)
    {
        if ( jPrices[prizeIndex].CategoryCode == categoryCode)
        {
            return jPrices[prizeIndex].Price;
        }
    }
    
    return '-';
}


function browsePreviousCategory( controlID )
{
    //get current pages...
    var categoryPage = getCurrentCategoryPage( controlID );
    var cruisePage = getCurrentCruisesPage( controlID );
    
    categoryPage--;
    setCurrentCategoryPage( controlID, categoryPage);
    
    //Render Grid @ current pages...
    renderGrid( controlID, categoryPage, cruisePage );
}

function browseNextCategory( controlID )
{
    //get current pages...
    var categoryPage = getCurrentCategoryPage( controlID );
    var cruisePage = getCurrentCruisesPage( controlID );
    
    categoryPage++;
    setCurrentCategoryPage( controlID, categoryPage);
    
    //Render Grid @ current pages...
    renderGrid( controlID, categoryPage, cruisePage );
}

function browsePreviousCruise( controlID )
{
    //get current pages...
    var categoryPage = getCurrentCategoryPage( controlID );
    var cruisePage = getCurrentCruisesPage( controlID );
    
    cruisePage--;
    setCurrentCruisesPage( controlID, cruisePage);
 
    //Render Grid @ current pages...
    renderGrid( controlID, categoryPage, cruisePage );
}

function browseNextCruise( controlID )
{
    //get current pages...
    var categoryPage = getCurrentCategoryPage( controlID );
    var cruisePage = getCurrentCruisesPage( controlID );
    
    cruisePage++;
    setCurrentCruisesPage( controlID, cruisePage);

    //Render Grid @ current pages...
    renderGrid( controlID, categoryPage, cruisePage );
}




function getCurrentCategoryPage( controlID )
{
    return $("#" + controlID + "_hfCategoryPageIndex" ).val();
}

function setCurrentCategoryPage( controlID , value )
{
    $("#" + controlID + "_hfCategoryPageIndex" ).val(value);
}

function getCurrentCruisesPage( controlID )
{
    return $("#" + controlID + "_hfCruisesPageIndex" ).val();
}

function setCurrentCruisesPage( controlID , value )
{
    $("#" + controlID + "_hfCruisesPageIndex" ).val(value);
}

function categorySelected( controlID, cruiseCode, categoryCode )
{
    //Open the Waiting Box before the postback!
    ShowWaitingBox();  
    
    var selectedCruiseInfo = new Object();
    selectedCruiseInfo.CruiseCode = cruiseCode;
    selectedCruiseInfo.CategoryCode = categoryCode;

    //set into the hidden field the selected cruise...    
    var jsonSelectedCruiseInfo = Sys.Serialization.JavaScriptSerializer.serialize( selectedCruiseInfo );
    $("#" + controlID + "_hfSelCatCru").val(jsonSelectedCruiseInfo);    

//    var scriptPostback = $("#" + controlID + "_hfPbJs").val();    
//    eval( scriptPostback );
    
    //__doPostBack("#" + controlID + "_hbtnCruSel");
    
    $("#" + controlID + "_hbtnCruSel").click();
}


if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();