

TRAVELICIOUS.app = function() {

    $('input[title!=""]').hint();

    /* SLIDER BAR **********/
    var min = 0;
    var max = 1500;

    if ($('#minPrice').val()) {
        min = $('#minPrice').val();
    }
    if ($('#maxPrice').val()) {
        max = $('#maxPrice').val();
    }

    TRAVELICIOUS.createSliderRanges('#slider-deals', '$' + min, '$' + max); // 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:1500, step:10, values: [min, max] });
    //$('#slider-deals').slider();

    /*
    TRAVELICIOUS.createSliderRanges('#slider-deals','$50','$2000'); // 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:50, max:2000, step:5, values: [50,2000] });
    */

    /* Deal slider event */
    $('#slider-deals').bind('slide', function(event, ui) {
        $('.minvalue').text('$' + $(this).slider('values', 0));
        $('.maxvalue').text('$' + $(this).slider('values', 1));
    });

    $.blockUI.defaults.overlayCSS.opacity = 0.4;

    $('#slider-deals').bind('slidechange', function(event, ui) {
        $.blockUI({
            message:'<div><img src="/assets/images/trans-spinner.gif" ALIGN=ABSMIDDLE />Loading...</div>',
            css: {
                border: 'none',
                top:  ($(window).height() - 40) /2 + 'px',
                left: ($(window).width() - 150) /2 + 'px',
                width: '150px',
                Height: '40px',                
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px'
            }
        });

        var minPrice = $(this).slider('values', 0);
        var maxPrice = $(this).slider('values', 1);

        var destId = $('#currentDest').val();
        var catName = $('#currentCat').val();
        var destName = $('#currentDestName').val();
        var sortBy = $('#sortBy').val();
        
        $.get('/deal/filterDeal', {'destId' : destId, 'destName' : destName, 'catName' : catName, 'minPrice' : minPrice, 'maxPrice' : maxPrice, 'sortBy' : sortBy, 'ajax':'ajax'},
            function(data) {
                $('#deal-list-filter').html(data);
                $.unblockUI();

            }
        );
        
    });


    /* Hotels Search **********/
    $('#extHotelSearch').click(function() {

        var kayakBaseUrl = 'http://trk.acetrk.com/r/BJ/1Z8/23N/?url_id=409&url=';
        var travelocityBaseUrl = 'http://www.anrdoezrs.net/interactive?aid=10413824&pid=3413840&url=';
        var priceLineBaseUrl = 'http://www.kqzyfj.com/interactive?ProductID=5&aid=10496001&pid=3413840';
        var orbitzBaseUrl = 'http://click.linksynergy.com/fs-bin/click?id=nsrC9fS*Xww&offerid=191979.5732&type=14&catid=77&subid=0&hid=5732&tmpid=1118&RD_PARM1=';
        var hotwireBaseUrl = 'http://click.linksynergy.com/fs-bin/click?id=nsrC9fS*Xww&offerid=120349.5208&type=14&catid=27&subid=0&hid=5208&tmpid=1268&RD_PARM1=';
        var searchUrl;

        var city = $('#f-city').val();

        var checkInDay = $('#f-check-in_day').val();
        var checkInMonth = $('#f-check-in_month').val();
        var checkInYear = $('#f-check-in_year').val();
        var checkInDate = checkInMonth + '/' + checkInDay + '/' + checkInYear;

        var checkOutDay = $('#f-check-out_day').val();
        var checkOutMonth = $('#f-check-out_month').val();
        var checkOutYear = $('#f-check-out_year').val();
        var checkOutDate = checkOutMonth + '/' + checkOutDay + '/' + checkOutYear;

        var guestNum = $('#f-guests').val();

        //KAYAK
        if ($('#f-hotel-kayak').attr('checked')) {
            searchUrl = 'http://www.kayak.com/s/search/hotel?ai=atrinsic&p=409/23N/1Z8&crc=' + city + '&d1=' + checkInDate + '&d2=' + checkOutDate;
            var kayakUrl = kayakBaseUrl + encodeURIComponent(searchUrl);
            window.open(kayakUrl, 'mywindow_kayak_hotel', 'scrollbars=yes, menubar=yes, width=1000, height=600', true);
        }

        //CJ
        if ($('#f-hotel-travelocity').attr('checked')) {
            searchUrl = 'http://travel.travelocity.com/hotel/HotelCobrand.do?searchMode=city&expr_path=Y&city=' + city + '&dateReturningMonth=' + checkOutMonth + '&dateReturningDay=' + checkOutDay  + '&dateLeavingMonth=' + checkInMonth + '&dateLeavingDay=' + checkInDay +'&adults=' + guestNum;
            var travelocityUrl = travelocityBaseUrl + encodeURIComponent(searchUrl);
            window.open(travelocityUrl, 'mywindow_travelocity_hotel', 'scrollbars=yes, menubar=yes, width=1000, height=600', true);
        }

        if ($('#f-hotel-priceline:checked').attr('checked')) {
            searchUrl = '&CITY=' + city + '&CheckInDate=' + checkInDate + '&CheckOutDate=' + checkOutDate + '&url=http://www.priceline.com/qp.asp&refclickID=3413840&refid=CO10496001SID';
            var pricelineUrl = priceLineBaseUrl + searchUrl;
            window.open(pricelineUrl, 'mywindow_priceline_hotel', 'scrollbars=yes, menubar=yes, width=1000, height=600', true);
        }

        //Linkshare
        if ($('#f-hotel-orbitz:checked').attr('checked')) {
            searchUrl = 'http://www.orbitz.com/App/Landing?placementID=1&pageID=41&startDate:useDateText=true&endDate:useDateText=true&city=' + city + '&startDate:date=' + checkInDate + '&endDate:date=' + checkOutDate + '&adults=' + guestNum;
            var orbitzUrl = orbitzBaseUrl + escape(escape(searchUrl));
            window.open(orbitzUrl, 'mywindow_orbitz_hotel', 'scrollbars=yes, menubar=yes, width=1000, height=600', true);
        }

        if ($('#f-hotel-hotwire:checked').attr('checked')) {
            searchUrl = 'hotel/search-options.jsp?destCity=' + city + '&inputId=index&startMonth=' + checkInMonth + '&startDay=' + checkInDay + '&endMonth=' + checkOutMonth + '&endDay=' + checkOutDay;
            var hotwireUrl = hotwireBaseUrl + escape(escape(searchUrl));
            window.open(hotwireUrl, 'mywindow_hotwire_hotel', 'scrollbars=yes, menubar=yes, width=1000, height=600', true);
        }

        return false;
    });

    /* Flight Search **********/
    $('#extFlightSearch').click(function() {
        var kayakBaseUrl = 'http://trk.acetrk.com/r/BJ/1Z8/23N/?url_id=413&url=';
        var travelocityBaseUrl = 'http://www.jdoqocy.com/interactive?aid=10398279&pid=3413840&url=';
        var priceLineBaseUrl = 'http://www.dpbolvw.net/interactive?ProductID=1&aid=10496001&pid=3413840&refclickID=3413840&refid=CO10491509SID';
        var orbitzBaseUrl = 'http://click.linksynergy.com/fs-bin/click?id=nsrC9fS*Xww&offerid=191979.5731&type=14&catid=77&subid=0&hid=5731&tmpid=1118&RD_PARM1=';
        var hotwireBaseUrl = 'http://click.linksynergy.com/fs-bin/click?id=nsrC9fS*Xww&offerid=120349.5219&type=14&catid=29&subid=0&hid=5219&tmpid=1268&RD_PARM1=';
        var searchUrl;
        
        var fromCity = $('#f-from').val();
        var toCity = $('#f-to').val();
        var travelers = $('#f-travelers').val();

        var departDay = $('#f-depart_day').val();
        var departMonth = $('#f-depart_month').val();
        var departYear = $('#f-depart_year').val();
        var departDate = departMonth + '/' + departDay + '/' + departYear;

        var returnDay = $('#f-return_day').val();
        var returnMonth = $('#f-return_month').val();
        var returnYear = $('#f-return_year').val();
        var returnDate = returnMonth + '/' + returnDay + '/' + returnYear;

        if ($('#f-flight-kayak').attr('checked')) {
            searchUrl = 'http://www.kayak.com/s/search/air?ai=atrinsic&p=413/23N/1Z8&l1=' + fromCity + '&l2=' + toCity + '&d1=' + departDate + '&d2=' + returnDate;
            var kayakUrl = kayakBaseUrl + encodeURIComponent(searchUrl);
            window.open(kayakUrl, 'mywindow_kayak_flight', 'scrollbars=yes, menubar=yes, width=1000, height=600', true);
        }

        if ($('#f-flight-travelocity').attr('checked')) {
            searchUrl = 'http://travel.travelocity.com/flights/InitialSearch.do?Service=CJUS&dateLeavingTime=Anytime&dateReturningTime=Anytime&seniors=0&children=0&flightType=roundtrip&dateReturningMonth=' + returnMonth + '&dateReturningDay=' + returnDay + '&adults=' + travelers + '&leavingFrom=' + fromCity + '&dateLeavingMonth=' + departMonth + '&goingTo=' + toCity + '&dateLeavingDay=' + departDay;
            var travelocityUrl = travelocityBaseUrl + encodeURIComponent(searchUrl);
            window.open(travelocityUrl, 'mywindow_travelocity_flight', "menubar=1,scrollbars=1,resizable=1,width=1000,height=600");
        }

        if ($('#f-flight-priceline:checked').attr('checked')) {
            searchUrl = '&DepCity=' + fromCity + '&ArrCity=' + toCity + '&DepartureDate=' + departDate + '&ReturnDate=' + returnDate + '&NumTickets=' + travelers + '&url=http://www.priceline.com/qp.asp';
            var pricelineUrl = priceLineBaseUrl + searchUrl;
            window.open(pricelineUrl, 'mywindow_priceline_flight', 'scrollbars=yes, menubar=yes, width=1000, height=600', true);
        }

        if ($('#f-flight-orbitz:checked').attr('checked')) {
            searchUrl = 'http://www.orbitz.com/App/Landing?placementID=1&pageID=10&&slice1:dateTime:useDateText=true&slice2:dateTime:useDateText=true&slice1:departCity=' + fromCity + '&slice1:arriveCity=' + toCity + '&slice1:dateTime:date=' + departDate + '&slice2:dateTime:date=' + returnDate + '&adult=' + travelers;
            var orbitzUrl = orbitzBaseUrl + escape(escape(searchUrl));
            window.open(orbitzUrl, 'mywindow_orbitz_flight', 'scrollbars=yes, menubar=yes, width=1000, height=600', true);
        }

        if ($('#f-flight-hotwire:checked').attr('checked')) {
            searchUrl = 'air/search-options.jsp?origCity=' + fromCity + '&destinationCity=' + toCity + '&noOfTickets=' + travelers + '&inputId=index&startMonth=' + departMonth + '&startDay=' + departDay + '&endMonth=' + returnMonth + '&endDay=' + returnDay;
            var hotwireUrl = hotwireBaseUrl + escape(escape(searchUrl));
            window.open(hotwireUrl, 'mywindow_hotwire_flight', 'scrollbars=yes, menubar=yes, width=1000, height=600', true);
        }

        return false;
    });

    /***********************************
     * Destination dropdown
     ***********************************/
    /*
    $('.destCollapseLink').click(
        function() {
            
            if ($(this).children().attr('src') == '/assets/images/bullet_toggle_minus.png') {
                $(this).children().attr('src', '/assets/images/bullet_toggle_plus.png')
            } else {
                $(this).children().attr('src', '/assets/images/bullet_toggle_minus.png')
            }

            $(this).next().slideToggle('normal');
            return false;
        }
    );
    */
   
    $('.external-search-logo').click(
        function() {
            var chkBoxId = $(this).attr('name');
            var chkBox = $('#' + chkBoxId);

            if (chkBox.attr('checked')) {
                chkBox.removeAttr('checked');
            } else {
                chkBox.attr('checked','checked');
            }

            return false;

        }
    );

    $('#signup-top-20').click(
        function() {
            var email = $('#top-20-email').val();

            $.ajax({
                type: 'GET',
                url: '/mailingList/add',
                data: {'email' : email},
                dataType: 'json',
                success: function(json) {
                    $('#signup-message').html(json.msg);
                },
                error: function() {
                    $('#signup-message').html('Please try again.');
                }
            });
            
            return false;
        }
    );
}

