//jQuery.noConflict();

jQuery(document).ready(function() {

    //testFunctionThatDoesntExist();

    jQuery('.GoBookingSub').submit(function() {
        GoBookingSub()
        return false;
    })

    //http://arshaw.com/fullcalendar/docs/
    if (jQuery('#calendar').length > 0) {
        jQuery('#calendar').fullCalendar(getCalOptions());
    }

    //add custom stuff to calendar header
    insertLegendButtonAfterHeaderButton('today');
    
    if (jQuery("#startdate").length > 0) {

        if (dateFormat == "") {
            var dateFormat = "mm/dd/yyyy";
        }

        jQuery.datepicker.setDefaults({
            minDate: 0,
            buttonText: '&nbsp;&nbsp;&nbsp;',
            defaultDate: +1,
            showOn: 'both',
            buttonImageOnly: false//,
            //dateFormat: dateFormat

        });

        jQuery('#startdate').datepicker({

            onClose: function() {
                pushDate(1);
            }


        });
        jQuery('#enddate').datepicker({
            beforeShow: minRange,
            defaultDate: +3
        });
    }

    //jQuery('.languageChanger').singleDropMenu();


    //put additional functions here.
});

//pushes the endDate back to +2d of startdate
function pushDate(numberDaysAhead) {
    if (jQuery("#enddate").length > 0) {

        var startDate = jQuery("#startdate").datepicker("getDate");
        var endDate = jQuery("#enddate").datepicker("getDate");

        if (startDate > endDate) {
            startDate.setDate(startDate.getDate() + numberDaysAhead);
            jQuery('#enddate').datepicker('setDate', startDate);
            //alert('start gt end');
        } else {
            //alert('start lt end');
        }
    }
}

//customize mindate so that enddate cannot be b4 startdate
function minRange(input) {
    return {
        minDate: (jQuery("#startdate").datepicker("getDate") != null ? jQuery("#startdate").datepicker("getDate") : 2)
    };
}
//startdate cannot be after enddate
function maxRange(input) {
    return {
        maxDate: (jQuery("#enddate").datepicker("getDate") != null ? jQuery("#enddate").datepicker("getDate") : null)
    };
}

//mh tried GoBooking.js but 2 forms on page / not designed.
function GoBookingSub() {

    var strLink;
    var checkinDate = jQuery('#checkinDate').val();

    GroupCode2 = jQuery('#GroupCode2').val();
    if (GroupCode2 == '') {
        GroupCode2 = jQuery('#GroupCode2').attr("default");
    }
    // alert(GroupCode2)

    action = jQuery('.GoBookingSub').attr('action');
    strLink = action + ";" + GroupCode2 + ";" + checkinDate + ";00;00;00;001;?/";


    pageTracker._trackPageview('/outgoing/check-availability/');   //calls gif asynch

    var linkerUrl = replaceQuote(pageTracker._getLinkerUrl(strLink));

    window.open(linkerUrl, '', '');
    return false;
}



function getCalOptions() {

    calOptions = {
        header: {
            left: 'prev,next today ',
            center: 'title',
            right: ' month,basicWeek,basicDay'
        },
        theme: true,
        events: getEventsPage(),
        eventClick: function(event) {
            // opens events in a popup window

            var dHeight = 350;
            var dWidth = 640;
            var horizontalPadding = 30;
            var verticalPadding = 30;

            cal_modal = jQuery('<iframe  id="externalSite" class="externalSite" frameborder="0" src="' + event.url + '" />');

            cal_modal.dialog({
                title: event.title,
                autoOpen: true,
                width: dWidth,
                height: dHeight,
                modal: true,
                closeOnEscape: false
            }).width(dWidth - horizontalPadding).height(dHeight - verticalPadding);

            return false;
        },
        loading: function(bool) {
            if (bool) {
                jQuery('#loading').show();
            } else {
            jQuery('#loading').hide();
            }
        }
    }

    return calOptions;
}
 
function getLegendUrl() {
    //'legend.htm'
    return '/_ePresence/_eCalendar/Web/dyn_calendarLeyend.asp';
}


function insertLegendButtonAfterHeaderButton(name) {

    buttonToPutAfter = jQuery('.fc-button-' + name);
    nextTd = buttonToPutAfter.parent().next();

    nextTd.addClass('nextTd').attr('align', 'right');

    legendButton = buttonToPutAfter.clone();
    legendButton.addClass('fc-legend-button').addClass('fc-no-left').addClass('ui-no-left');
    legendButton.removeClass('.fc-button-' + name).removeClass('fc-state-disabled').removeClass('ui-state-disabled')

    legendButton.children().children().text('legend');

    nextTd.append(legendButton);
    jQuery('.nextTd .fc-header-space').remove();
    jQuery('.fc-legend-button').live('click', function() {
        modal(getLegendUrl(), 'Legend', 600, 400);
    });
}


function getEventsPage() {
    window.eventsPage = jQuery('#calendar').attr('title');
    //remove title so it doesnt show in tooltips
    jQuery('#calendar').attr('title', '');
    return window.eventsPage
}
 
 function modal(url, dTitle, dWidth, dHeight) {

     iframeDialog = jQuery('<iframe  id="externalSite" class="externalSite" frameborder="0" name="eg" src="' + url + '" />');

     var horizontalPadding = 30;
     var verticalPadding = 30;

     iframeDialog.dialog({
         title: dTitle,
         autoOpen: true,
         width: dWidth,
         height: dHeight,
         modal: true,
         closeOnEscape: false
     }).width(dWidth - horizontalPadding).height(dHeight - verticalPadding);

 }
						
