/*
 * This class encapsulates all of the javascript functionality used
 * in the adventures section 
 */
var AdventureController = Class.create({
    initialize: function() {
    	this.year = null;
    	this.years = new Array();
    	this.order = 'asc';
    	this.column = 'Type';
    	this.adventureId = null;
    	this.view = 'standard';
    },
    
    /*
     * Set the year to view on the pricing table
     */
    toYear: function(year) {
    	if(year == this.year) return;
    	
    	var tableId  = 'Prices'+year;
    	var switchId = 'switcher' + year;
    	var headId   = 'head' + year;
    	
    	var currentTableId  = 'Prices' + this.year;
    	var currentSwitchId = 'switcher' + this.year;
    	var currentHeadId   = 'head' + this.year;
    	
    	$(currentSwitchId).removeClassName('active');
    	$(switchId).addClassName('active');
    	
    	$(currentTableId).hide();
    	$(tableId).show();
    	
    	$(currentHeadId).hide();
    	$(headId).show();
    	
    	this.setCurrentYear(year);
    },
    
    setCurrency: function() {
    	$('currencyWorking').show();
    	var options = { parameters: { c: $('currencySelect').getValue() },
    	              onComplete: function(obj) {
                          this.refresh(function() {
                          	$('ccLink').prototip.hide(); 
                          });
    	              }.bind(this)
    	             };    
                             
        new Ajax.Request('/adventures/resources/setcurrency/',options);
    },
    
    /* 
     * set the years of this adventure date view.
     */
    setYears: function() {
    	for(i = 0; i < arguments.length; i ++) {
    		this.years.push(arguments[i]);
    	}
    },
    
    /*
     * Set the currently selected year.
     */
    setCurrentYear: function(newYear) {
    	this.year = newYear;
    },
    
    /*
     * Set the adventure Id, used in sorting
     */
     setAdventureId: function(id) {
     	this.adventureId = id;
     },
     
    /*
     * return the order opposite to the current order and set the current order
     * to the opposite (how's that for confusing?)
     */
    getOrder: function() {
        if(this.order == 'asc') {
            this.order = 'desc';
        } else {
        	this.order = 'asc';
        }
        return this.order;
    },
    
    /*
     * Sort the table by the requested column
     */
    sort: function(column) {
    	this.column = column;
    	var url     = '/adventures/resources/sort/';
        var params  = { column: column, 
                        order: this.getOrder(),
                        adventureId: this.adventureId,
                        year: this.year };
    	var options = { parameters: params,
    	                onSuccess: function(obj) {
    	                	$('priceBlock').update(obj.responseText);
    	                	this.tablesLoaded();
    	                }.bind(this)
    	              };
        new Ajax.Request(url,options);
    },
    
    refresh: function(callback) {
    	var url = '/adventures/resources/refresh/';
        var params  = { column: this.column, 
                        order: this.order,
                        adventureId: this.adventureId,
                        year: this.year,
                        view: this.view };
        var options = { parameters: params,
                        onSuccess: function(obj) {
                        	if(callback != undefined) {
                        	   callback();
                        	}
                            $('priceBlock').update(obj.responseText);
                            this.tablesLoaded();
                        }.bind(this)
                      };
        new Ajax.Request(url,options);    	
    },
    
    openTaag: function() {
          Lightview.show({
            href: '/adventures/resources/tours-at-a-glance/',
            rel: 'iframe',
            title: 'Tours at a Glance',
            radius: 0,
            options: {
              autosize: true,
              fullscreen: true
            }
          });    	
    },

    setView: function(view) {
    	this.view = view;
    },    
    
    /*
     * Initialize the links on the tables
     * If there is no javascript, the links fall back to normal links
     */
    tablesLoaded: function() {
        // dates
        $$('.dateLink').each(function(i) {
            i.href = 'javascript://';
            var dateId = i.id.substr(4);
            var url = '/adventures/resources/date/id/'+dateId+'/';
            var title = '<div>Trip Details for:</div>' + i.title.toUpperCase(); 
            new Tip(i.id, {
                title: title,
                ajax: { url: url },
                closeButton: true,
                showOn: 'click',
                hideOn: { element: 'closeButton', event: 'click'},
                stem: 'leftMiddle',
                hook: { target: 'topRight', tip: 'leftMiddle' },
                offset: { x: 0, y: 5 },
                width: 'auto',
                style: 'fna',
                viewport:true,
                radius: 0
                
            });
        });   
        // activities
        $$('.activity').each(function(i) {
        	 var desc = i.title.split('::');
        	 i.title = '';
        	 
            new Tip(i, '<div id="activityContainer">'+desc[1]+'</div>',{
             	title: desc[0],
                stem: 'rightBottom',
                hook: { target: 'topLeft', tip: 'rightBottom' },
                offset: { x: 0, y: 0 },
                width:250,
                style: 'fna',
                radius: 0
            });       	
        });
        
        var url = '/adventures/resources/currencies/';
        new Tip('ccLink', {
        	title: "Choose Your Desired Currency",
            ajax: { url: url },
            showOn: 'click',
            closeButton: true,
            hideOn: { element: 'closeButton', event: 'click'},
            stem: 'leftMiddle',
            hook: { target: 'topRight', tip: 'leftMiddle' },
            offset: { x: 0, y: 5 },
            width: 250,
            style: 'fna',
            radius: 0
        });
    } 
})

var ac = new AdventureController();
