﻿
$('html').addClass('js');
  
function autoFill(id, v) {

    if ($(id).val() == "" || $(id).val() == v) {
        $(id).css({ color: "#b2adad" }).attr({ value: v }).focus(function() {
            if ($(this).val() == v) {
                $(this).val("").css({ color: "#333" });
            }
        }).blur(function() {
            if ($(this).val() == "") {
                $(this).css({ color: "#b2adad" }).val(v);
            }

        });
    }

}

jQuery.fn.mailto = function() {
    return this.each(function() {
        var email = $(this).html().replace(/\s*\(.+\)\s*/, "@");
        $(this).before('<a href="mailto:' + email + '" rel="nofollow" title="Email ' + email + '">' + email + '</a>').remove();
    });
};

location.querystring = (function() {

    // The return is a collection of key/value pairs

    var queryStringDictionary = {};

    // Gets the query string, starts with '?'

    var querystring = decodeURI(location.search);

    // document.location.search is empty if no query string

    if (!querystring) {
        return {};
    }

    // Remove the '?' via substring(1)

    querystring = querystring.substring(1);

    // '&' seperates key/value pairs

    var pairs = querystring.split("&");

    // Load the key/values of the return collection

    for (var i = 0; i < pairs.length; i++) {
        var keyValuePair = pairs[i].split("=");
        queryStringDictionary[keyValuePair[0]]
                        = keyValuePair[1];
    }

    // toString() returns the key/value pairs concatenated

    queryStringDictionary.toString = function() {

        if (queryStringDictionary.length == 0) {
            return "";
        }

        var toString = "?";

        for (var key in queryStringDictionary) {
            toString += key + "=" +
                        queryStringDictionary[key];
        }

        return toString;
    };

    // Return the key/value dictionary

    return queryStringDictionary;
})();

$.maxZIndex = $.fn.maxZIndex = function(opt) {
    /// <summary>
    /// Returns the max zOrder in the document (no parameter)
    /// Sets max zOrder by passing a non-zero number
    /// which gets added to the highest zOrder.
    /// </summary>    
    /// <param name="opt" type="object">
    /// inc: increment value, 
    /// group: selector for zIndex elements to find max for
    /// </param>
    /// <returns type="jQuery" />
    var def = { inc: 10, group: "*" };
    $.extend(def, opt);
    var zmax = 0;
    $(def.group).each(function() {
        var cur = parseInt($(this).css('z-index'));
        zmax = cur > zmax ? cur : zmax;
    });
    if (!this.jquery)
        return zmax;

    return this.each(function() {
        zmax += def.inc;
        $(this).css("z-index", zmax);
    });
}


$(document).ready(function () {

    //            $(function(){
    //                $('.wysiwyg').wysiwyg();
    //            });


    var d = new Date();
    var t_date = d.getDate();      // Returns the day of the month
    var t_mon = d.getMonth();      // Returns the month as a digit
    var t_year = d.getFullYear();  // Returns 4 digit year
    var dow = d.getDay();
    t_mon++;

    switch (dow) {
        case 0: //sunday
        case 1: //monday
        case 2: //tuesday
        case 3: //wednesday
        case 4: //thursday            
        case 5: //friday
        case 6: //saturday
            t_date = (t_date + 3);
            break;
    }

    var sDate = t_mon + "/" + t_date + "/" + t_year;

    sDate = new Date(sDate);

    $('.email').mailto();

    autoFill($("#KeyWord"), "Search");
    autoFill($("#Keyword"), "Key Word");
    autoFill($("#keyword"), "Key Word");
    autoFill($("#EmailAddress"), "Email Address");
    autoFill($("#firstName"), "First Name");
    autoFill($("#lastName"), "Last Name");
    autoFill($("#eventDate"), "Event Date");
    autoFill($(".paymentAmount"), "Payment Amount");
    autoFill($(".referenceNumber"), "Optional Reference #");


    //$(".datepicker").datepicker({dateFormat: "m/d/yy"});

    $('.datepicker').datepicker({ beforeShow: function () { $('#ui-datepicker-div').maxZIndex(); },
        dateFormat: 'm/d/yy'
    });


    $('.datepickerShipping').datepicker({ beforeShow: function () { $('#ui-datepicker-div').maxZIndex(); },
        dateFormat: 'm/d/yy',
        minDate: sDate,
        beforeShowDay: nonWorkingDatesNYC
    });

    $('.datepickerShippingDC').datepicker({ beforeShow: function () { $('#ui-datepicker-div').maxZIndex(); },
        dateFormat: 'm/d/yy',
        minDate: sDate,
        beforeShowDay: nonWorkingDatesDC
    });

    $('.datepickerGilt').datepicker({ beforeShow: function () { $('#ui-datepicker-div').maxZIndex(); },
        dateFormat: 'm/d/yy',
        minDate: new Date('2/1/2011'),
        maxDate: new Date('2/28/2011'),
        beforeShowDay: nonWorkingDatesGilt
    });


    var startDateBuffer = new Date();
    startDateBuffer.setDate(startDateBuffer.getDate() + 7);
    var ddStart = new Date('8/8/2011');
    if (ddStart > startDateBuffer)
        startDateBuffer = ddStart;

    $('.datepickerDD').datepicker({ beforeShow: function () { $('#ui-datepicker-div').maxZIndex(); },
        dateFormat: 'm/d/yy',
        minDate: startDateBuffer,
        beforeShowDay: nonWorkingDatesDC,
        defaultDate: startDateBuffer
    });

    var gccStartDateBuffer = new Date();
    gccStartDateBuffer.setDate(gccStartDateBuffer.getDate() + 7);
    var gccStart = new Date('9/22/2011');
    if (gccStart > gccStartDateBuffer)
        gccStartDateBuffer = gccStart;

    $('.datepickerGCC').datepicker({ beforeShow: function () { $('#ui-datepicker-div').maxZIndex(); },
        dateFormat: 'm/d/yy',
        minDate: gccStartDateBuffer,
        beforeShowDay: nonWorkingDatesDC,
        defaultDate: gccStartDateBuffer
    });

    function nonWorkingDatesDC(date) {
        var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;

        var closedDates = [[12, 24, 2010], [12, 27, 2010], [12, 28, 2010], [12, 29, 2010], [12, 30, 2010], [12, 31, 2010]];
        var openDates = [[5, 8, 2011]];
        var closedDays = [[Sunday], [Saturday]];

        for (i = 0; i < openDates.length; i++) {
            if (date.getMonth() == openDates[i][0] - 1 && date.getDate() == openDates[i][1] && date.getFullYear() == openDates[i][2]) {
                return [true];
            }
        }


        for (var i = 0; i < closedDays.length; i++) {
            if (day == closedDays[i][0]) {
                return [false];
            }
        }

        for (i = 0; i < closedDates.length; i++) {
            if (date.getMonth() == closedDates[i][0] - 1 && date.getDate() == closedDates[i][1] && date.getFullYear() == closedDates[i][2]) {
                return [false];
            }
        }

        return [true];
    }

    function nonWorkingDatesNYC(date) {
        var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;

        var closedDates = [[1, 1, 2011], [2, 14, 2011]];
        var openDates = [[5, 8, 2011]];
        var closedDays = [[Sunday]];

        for (i = 0; i < openDates.length; i++) {
            if (date.getMonth() == openDates[i][0] - 1 && date.getDate() == openDates[i][1] && date.getFullYear() == openDates[i][2]) {
                return [true];
            }
        }

        for (var i = 0; i < closedDays.length; i++) {
            if (day == closedDays[i][0]) {
                return [false];
            }
        }

        for (i = 0; i < closedDates.length; i++) {
            if (date.getMonth() == closedDates[i][0] - 1 && date.getDate() == closedDates[i][1] && date.getFullYear() == closedDates[i][2]) {
                return [false];
            }
        }

        return [true];
    }

    function nonWorkingDatesGilt(date) {
        var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;

        //var closedDates = [[1, 1, 2011]];

        var closedDays = [[Sunday]];

        for (var i = 0; i < closedDays.length; i++) {
            if (day == closedDays[i][0]) {
                return [false];
            }

        }

        //        for (i = 0; i < closedDates.length; i++) {
        //            if (date.getMonth() == closedDates[i][0] - 1 && date.getDate() == closedDates[i][1] && date.getFullYear() == closedDates[i][2]) {
        //                return [false];
        //            }
        //        }

        return [true];
    }

    $(".zebra > tbody > tr:even").addClass("even");


    $('.confirmlive').live('click', function () {
        var answer = confirm('Are you sure you want to delete this ' + $(this).attr('title'));
        return answer;
    });

    $('.confirm').click(function () {
        var answer = confirm('Are you sure you want to delete this ' + $(this).attr('title'));
        return answer;
    });

    $('.confirmMessage').click(function () {
        var answer = confirm($(this).attr('title'));
        return answer;
    });

    // Dialog			
    $('#dialog').dialog({
        autoOpen: false,
        modal: true,
        width: 968
    });

    $('#dialog').bind("dialogclose", function (event, ui) {

        $.validationEngine.closePrompt('.formError', true)
    });

    // Dialog Link
    $('#dialog_link').click(function () {
        $('#dialog').dialog('open');
        return false;
    });

    // Dialog Link
    $('.dialog_link').click(function () {
        $('#dialog').dialog('open');
        return false;
    });

    // Dialog			
    $('#dialog_manhattan').dialog({
        autoOpen: false,
        modal: true,
        width: 800
    });

    $('#dialog_manhattan').bind("dialogclose", function (event, ui) {

        $.validationEngine.closePrompt('.formError', true)
    });

    // Dialog Link
    $('#dialog_link_manhattan').click(function () {
        $('#dialog_manhattan').dialog('open');
        return false;
    });

    $('.dialog_link_manhattan').click(function () {
        $('#dialog_manhattan').dialog('open');
        return false;
    });


    // Dialog			
    $('#dialog_bloombag').dialog({
        autoOpen: false,
        modal: true,
        width: 400
    });

    $('#dialog_bloombag').bind("dialogclose", function (event, ui) {

        $.validationEngine.closePrompt('.formError', true)
    });

    // Dialog Link
    $('.dialog_link_bloombag').click(function () {
        $('#dialog_bloombag').dialog('open');
        return false;
    });


    $(".uniForm").validationEngine()
    $(".validate").validationEngine()

    jQuery(function ($) {
        $(".maskDate").mask("99/99/9999");
        $(".maskPhone").mask("(999) 999-9999");
        $(".maskZip").mask("99999");
    });

    $('#loading')
    .hide()  // hide it initially
    .ajaxStart(function () {
        $(this).show();
    })
    .ajaxStop(function () {
        $(this).hide();
    });

});
