// Initialize TRAVELICIOUS
if(typeof this.TRAVELICIOUS=="undefined"){var TRAVELICIOUS={};}

TRAVELICIOUS.init = function(){
    
    /* COUPON COPY TEXT  **********/
    if ($('.coupon').length) TRAVELICIOUS.initCoupons('Click to Copy','Copied to Clipboard');
    
    /* INPUT 'FOCUS/BLUR **********/
    /*
    if ($('input[type="text"]').length) TRAVELICIOUS.focusBlur('input[type="text"]','');
    if ($('.input-search').length) TRAVELICIOUS.focusBlur('.input-search','Search'); // Header 'Search' - Focus/Blur
    if ($('.input-location-search').length) TRAVELICIOUS.focusBlur('.input-location-search','Where do you want to go?'); // 'Where do you want to go?' search bar (Deals) - Focus/Blur
    if ($('.input-email').length) TRAVELICIOUS.focusBlur('.input-email','Enter your email address'); // 'Enter your email address' search bar (Top 20) - Focus/Blur
    if ($('.input-enter-city').length) TRAVELICIOUS.focusBlur('.input-enter-city','Enter City'); // 'Enter City' Trip Search
    if ($('.input-place-name').length) TRAVELICIOUS.focusBlur('.input-place-name','Name of place'); // 'Name of place' Place options
    if ($('.input-city').length) TRAVELICIOUS.focusBlur('.input-city','City'); // 'City' Place options
    if ($('.input-desc-words').length) TRAVELICIOUS.focusBlur('.input-desc-words','Words that describe this place'); // Place 'Tags' : 'Words that describe this place' field
    if ($('.input-you-say').length) TRAVELICIOUS.focusBlur('.input-you-say','What do you want to say about it?'); // Place 'Tags' : 'Words that describe this place' field
    if ($('.input-comment').length) TRAVELICIOUS.focusBlur('.input-comment','Write a comment...'); // Enter Comment field
    if ($('.input-h-email').length) TRAVELICIOUS.focusBlur('.input-h-email','Email'); // Enter Comment field
    if ($('.input-password').length) TRAVELICIOUS.focusBlur('.input-password','*******'); // Password field on login
    if ($('.input-forgot-password').length) TRAVELICIOUS.focusBlur('.input-forgot-password','Enter the email address you use for this site'); // Overlay: Forgot Password field
    if ($('.input-currently').length) TRAVELICIOUS.focusBlur('.input-currently','Enter where you are'); // Overlay: Currently I Am In field
    if ($('.input-selectfile').length) TRAVELICIOUS.focusBlur('.input-selectfile','No file selected'); // Overlay: Photo : File Select field
    */
    /* IMAGE CAROUSEL **********/
    // Must come before TABS
    TRAVELICIOUS.carousel();
    
    /* TABS **********/
    // Hides the H2 titles in the tab-boxes when JS enabled. When JS disabled, the <ul> is hidden (currently being written out w/ JS) and the H2 then act as section headers.
    // With JS enabled, having these H2s show would be redundant. View homepage w/o JS enabled to see effect.
    if ($('.tabs').length) $(".tabs > DIV > .tab-title").css('display', 'none');
    // 'Hotel/Flight' Tabs
    if ($('.tabs').length) $(".tabs").tabs();
    
    /* SLIDER BAR **********/
//    TRAVELICIOUS.createSliderRanges('#slider-deals','$50','$725'); // creates min- and maxvalue <spans> where you can write the current range dynamically
//    if ($('#slider-deals').length) $('#slider-deals').slider({ range:true, animate:true, min:0, max:1000, step:5, values: [50,725] });


    TRAVELICIOUS.createSliderRanges('#slider-age','21','38'); // creates min- and maxvalue <spans> where you can write the current range dynamically
    if ($('#slider-age').length) $('#slider-age').slider({ range:true, animate:true, min:10, max:100, step:1, values: [21,78] });
    
    /* TOGGLEBOX (SHOW/HIDE) **********/
    // Hotel 'Filters' toggle box
    //TRAVELICIOUS.toggleBox('Hide Filters','Show Filters');

    /* CALENDAR (DATE SELECT)  **********/
    // 'Calender' choose date selector
    TRAVELICIOUS.calendar();

    /* OVERLAYS  **********/
    if ($('.overlaybox').length) $(".overlaybox").colorbox({iframe:true, innerWidth:600, innerHeight:450, opacity:.60, close:'Close'});
    if ($(".overlaybox-sm").length) $(".overlaybox-sm").colorbox({iframe:true, innerWidth:600, innerHeight:230, opacity:.60, close:'Close'});
    
    /* RADIO SWITCH  **********/
    TRAVELICIOUS.radioSwitch.init();
    
    /* BUTTON SWITCH  **********/
    // Switch Checks and Arrow on/off per click for the trip table
    TRAVELICIOUS.btnSwitch('yes');
    TRAVELICIOUS.btnSwitch('going');
    TRAVELICIOUS.btnSwitch('btn6','Visited?','Visited');
    TRAVELICIOUS.btnSwitch('btn-add-friend','Add As Friend','Friend');
    
	/* SHOW HIDDEN ELEMENT  **********/
	//TRAVELICIOUS.show('.input-you-say','.share-decide');
    
    // Items show when you click on input/hide when you click off
    $('.input-you-say').focus(function(e) { $('.share-decide').show(); });
    $(document).click(function(e) {
        if( $(e.target).is('.form-trip, .form-trip *')) return;
        $('.share-decide').hide();
    });

    /* MISC  **********/
    // 'Fix' the alignment of the first column of the 'Trips' tables. Left aligns just the first column
    if ($('.tbl-trips').length) $('.tbl-trips tr :nth-child(1)').addClass('col1');
    // Alternate background color on the My 10 sidebar list
    if ($('.list-rank li').length) $('.list-rank li:even').addClass('alt');
    // Rollover fix on the 'Menu' links on the Travel Buddies page
    if ($('.menu').length) { 
        $('.menu1').mouseover(function(){ $(this).addClass('menu1hover') });
        $('.menu1').mouseout(function(){ $('.menu1').removeClass('menu1hover') });
    };
    // 'Tool' Menu: Calendar/Print/Email (Trip pages)
    if ($('.tools').length) TRAVELICIOUS.tools();
    // Toggle-Box table padding
    if ($('.component-togglebox').length) $('.component-togglebox tr td:last-child').addClass('last');
};

/* RUN INIT ON PAGE LOAD **********/
// Run our 'TRAVELICIOUS.init' initialization function on document ready
$(document).ready(function(){
  TRAVELICIOUS.init();
  TRAVELICIOUS.app();
});


/* FUNCTIONS *******************/

// Switch Checks and Arrow on/off per click for the trip table
TRAVELICIOUS.btnSwitch = function(obj,startText,endText) {
    newObj = '.'+obj;
    if ($(newObj).length) {
        $(newObj).click(function(){
            var on = obj+"-on";
            var off = obj+"-off";
            if ($(this).hasClass(on)) {
                $(this).removeClass(on);
                $(this).addClass(off);
                if (startText) $(this).children().text(startText);
                return false;
            } else {
                $(this).removeClass(off);
                $(this).addClass(on);
                if (endText) $(this).children().text(endText);
                return false;
            }
        });
    }
};

// JS for the calendars so a normal <input> appears if JS disabled */
TRAVELICIOUS.calendar = function() {
    if ($('.calendar').length) {
        $('.calendar').append('<span>&nbsp;</span>');
        $(".calendar input").addClass('showcalendar');
    }
};

// Image Carousel
TRAVELICIOUS.carousel = function() {
    if ($('.component-carousel').length) {
        $('.carousel-items').addClass('carousel-items-js');
        jQuery('.component-carousel').jcarousel({ scroll:3 });
    }
};

/* COUPON COPY TEXT  **********/
// Copies coupon code to clipboard. Example: Hotel Details
TRAVELICIOUS.initCoupons = function(noteStart,noteEnd) {

    $('.coupon').each(
    
        function(index,element){
    
            $.proxy(function(){
                
                var rid = 'cn-' + String((new Date()).getTime()).replace(/\D/gi,'');
                var clip = new ZeroClipboard.Client();
                clip.setHandCursor(true);        
                
                $(element).parent().append('<div id="' + rid + '" class="coupon-note coupon-off">'+noteStart+'</div>');
            
                $(element).mouseover(function(e){
                
                    $('#' + rid).removeClass('coupon-off');
                    
                    // set the clip text to our innerHTML
                    clip.setText( this.innerHTML );
                    
                    // reposition the movie over our element or create it if this is the first time
                    if( clip.div )
                    {
                        clip.receiveEvent('mouseout', null);
                        clip.reposition(this);
                    }
                    else
                    {
                        clip.glue(this);
                    }
                    
                    // Force events due to the Flash movie moving around. This insures the CSS effects are properly updated.
                    clip.receiveEvent('mouseover', null);
                    
                    // Mouseover 'Flash' button that is created when you mouseover .coupon for first time - .coupon-btn
                    $('#' + clip.div.id).hover( function(e) {
                        
                        $('#' + rid).removeClass('coupon-off');
                    },
                    function(e) {    
                        
                        $('#' + rid).toggleClass('coupon-off');
                    });
                });    
            
            
                clip.addEventListener('complete', function(client) {
                    var el = client.domElement;
                    $(element).addClass('coupon-copied');
                    $('#' + rid).addClass('coupon-note-copied').html(noteEnd);
                });
            
            }, element)();
    
        }
    
    );

}


// Creates min- and maxvalue <spans> where you can write the current range dynamically
TRAVELICIOUS.createSliderRanges = function(id,min,max) {
    if ($(id).length) {
        $(id).append('<span class="minvalue">'+min+'</span>');
        $(id).append('<span class="maxvalue">'+max+'</span>');
    }
};

// INPUT 'Focus/Blur'
/*
TRAVELICIOUS.focusBlur = function(id,text) {
    $(id).focus(function(){
        if ($(this).val() == text) $(this).val('')
        $(this).css('color','#142835');
    });
    $(id).blur(function(){
        if ($(this).val() == '') $(this).val(text)
        //$(this).css('color','#bebebe');
        $(this).css('color','#707070');
    });
};
*/
// RADIO switch. Swaps input and select visibility
TRAVELICIOUS.radioSwitch = {
    init : function() {
        if ($('.radio-switch').length) {            
            // Actions when radio button clicked
            $('.radio-switch .radio').click(function() {
                TRAVELICIOUS.radioSwitch.clickAction(this);
            })
        }
    },    
    clickAction : function(currentItem) {
        var rowParent = $(currentItem).parent().parent(); // 'Row' of radios parent so that show/hiding only occurs for one set of items. Allows for other instances on same page
        var thisParent = $(currentItem).parent(); // Immediate parent for local changing
        var labelShow = thisParent.find('.label-show'); // Get the LABELS in their shown state
        var selectShow = thisParent.find('select'); // Get the SELECTS
    
        /* RESET ALL ITEMS *********/
        // We want to show all LABELS and hide all SELECTS first, then we'll turn on active SELECT below
        rowParent.find('label').removeClass();
        rowParent.find('label').addClass('label-show');
        rowParent.find('select').removeClass();
        
        /* SHOW ACTIVE SELECT *********/
        // Turn LABEL off (turned on up above)
        if (labelShow) labelShow.toggleClass('label-hide');
        // Show target SELECT
        selectShow.addClass('select-show');
    }
};

// Write out the tabs in JS so that w/ JS disabled we don't show buttons that don't work!
// tabID = ID of tabs <ul>
// tabName = Array of tab names
TRAVELICIOUS.tabs = function(tabName) {
    $('.tabs').addClass('tabs-jsenabled');
    document.write('<ul class="tab-links">');
    for (i=0; i<=(tabName.length-1); i++) {
        // If only one tab don't write out a link
        if (tabName.length <= 1) document.write('<li id="tab-'+tabName[i].toLowerCase()+'">'+tabName[i].toUpperCase()+'<span>&nbsp;</span></li>');
        //else document.write('<li><a href="#tabbox'+(i+1)+'"><span>'+tabName[i]+'</span></a></li>');
        else document.write('<li><a href="#tabbox-'+tabName[i].toLowerCase()+'"><span>'+tabName[i]+'</span></a></li>');
    }
    document.write('</ul>');
};

// Show/Hide Toggle Box ('Hide Filters' on Hotel pages)
TRAVELICIOUS.toggleBox = function(startText,endText) {
    if ($('.component-togglebox').length) {
        $('.component-togglebox').addClass('component-togglebox-js');
        $('.component-togglebox').append('<span id="toggle" class="open"><em>'+startText+'</em></span>');
        var startHeight = $('.component-togglebox .inner').height();
        $("#toggle").click(function(){
            if ($(this).is('.open')) {
                $(this).removeClass('open').addClass('closed');
                $("#toggle em").html(endText);
                $('.component-togglebox .inner').animate({ opacity:0, height:'0'}, 700, function() {/*Animation complete.*/});
            } else {
                $(this).removeClass('closed').toggleClass('open');            
                $("#toggle em").html(startText);
                $('.component-togglebox .inner').animate({ opacity:1, height:startHeight}, 700, function() {/*Animation complete.*/});
            }
        });
    }
};

// 'Tool' Menu: Calendar/Print/Email (Trip pages)
TRAVELICIOUS.tools = function() {
    $('.tools').css('display','block').append('<ul><li><a href="#" class="link-calendar" title="Click here to change dates">Calendar<span>&nbsp;</span></a></li><li><a href="#" class="link-print" title="Click here to Print this page">Print<span>&nbsp;</span></a></li><li><a href="#" class="link-email" title="Click here to Email this trip to a friend">Email<span>&nbsp;</span></a></li></ul>');
};

TRAVELICIOUS.show = function(el,tgt) {
	$(el).click(function(){
		//alert(tgt);
		$(tgt).show();				
		
	});

};