﻿/*
 * Random Trip functions
 *
 * Copyright (c) 2009 Wirepoint Media, LLC (www.wirepointmedia.com)
 *
 * @Author: Chris Cox <chris.cox@wirepointmedia.com> 
 * @Date: 11-22-09
 * @version: 1.2 REV.12-24-09
 */

 
/*
 * This function will dynamically load random trips for the travel agent page. 
 */
 
//Global variables
var activeTripArray = new Array();
var randomNumbers = new Array();
var asyncCallback = null;
 
function loadRandomTrips(max, excludeTrip, excludeRepeatedIds, callback) {   

    asyncCallback = callback;

    //First get a list of past trips by category "status-past-events"    
    $("#pageBody").prepend("<div id='past-trips-data'></div>");   
    $("#pageBody #past-trips-data").load("/trips/category/status-past-events .list-journal-entry-wrapper .journal-entry-wrapper .journal-entry", function(data) {
        $("#pageBody #past-trips-data .adminLinkTableWrapper").remove();
        
        var pastTrips = new Array();
        
        //Create array of past trips
        $("#pageBody #past-trips-data .journal-entry-text").each(function(i){ 
            pastTrips.push( $.trim($("#pageBody #past-trips-data .tagged-with .tag-element a").eq(i).text()) );
        });

        //Make array of active trip IDs
        $("#moduleContent5259568 ul.archive-item-list-pt li").each(function(i){

            var tag = $.trim($("#moduleContent5259568 ul.archive-item-list-pt li a").eq(i).text());
            
            //Also check that this trip is not excluded           
            if (excludeTrip != tag) {
                       
                //if matches past event, then do not even add
                var result = $.inArray(tag, pastTrips);
                if ( result == -1 ) {
                                   
                    var tripArray = tag.split("|");
                    var tripRef = tripArray[0].substring(7,10);  //(i.e. EGF)
                    
                    //Add first item to array
                    if (activeTripArray.length == 0) {
                         activeTripArray.push(tag);
                    }
                    
                    var match = false;
                    if (excludeRepeatedIds == true) {
                        //Make sure not a repeated id ref                        
                        for (i=0; i<activeTripArray.length; i++) {
                            var testArray = activeTripArray[i].split("|");
                            var testTripRef = testArray[0].substring(7,10);  //(i.e. EGF)
                            var testTripName = testArray[1]
                            
                            if (testTripRef == tripRef || testTripName == tripArray[1] ) {
                                match = true;
                                return;
                            }
                        }
                    }
                    
                    if (match == false) {                
                        activeTripArray.push(tag);
                    }           
                }
            }
        });
                
        //Create random trips
        var tripCount = activeTripArray.length;	        
 
        //Generate random numbers    
        if (tripCount >= max) {
            do {
                var match = false;
                var num = Math.floor(Math.random()* tripCount );
                $.each(randomNumbers, function(i, obj){	
                    if (obj == num) {
                        //Make sure not repeated number
                        match = true;
                        return;
                    }
                });
                if (match == false) {
                    randomNumbers.push(num);
                }
            } while ( randomNumbers.length < max);
        } 
        
        //Asynchronous callback
        window.setTimeout("asynchrounousCallback();", 1);
    
    });
}

function asynchrounousCallback() {
    asyncCallback();
}

