﻿
$('document').ready(function () {

    // Text inside textfield
    var active_color = '#000'; // Colour of user provided text
    var inactive_color = '#969696'; // Colour of default text

    $('input[type="text"], input[type="password"], textarea').each(function () {
        if (this.value == '') {
            var value = $(this).parent().children('label').html();
            $(this).attr("value", value);
        }
    });

    $('input[type="text"], input[type="password"], textarea').css("color", inactive_color);
    var default_values = new Array();

    $('input[type="text"], input[type="password"], textarea').focus(function () {
        if (!default_values[this.id]) {
            default_values[this.id] = this.value;
        }
        if (this.value == default_values[this.id]) {
            this.value = '';
            this.style.color = active_color;
        }

        $(this).blur(function () {
            if (this.value == '') {
                this.style.color = inactive_color;
                this.value = default_values[this.id];
            }
        });
    });

    // Modal

    jQuery.fn.fadeToggle = function (speed, easing, callback) { // Custom fade toggle function
        return this.animate({ opacity: 'toggle' }, speed, easing, callback);
    };

    $('a[rel="modal"]').click(function () { // Click a rel="modal"
        $('.modalbox').fadeToggle(200); // Fade toggle modal mailbox
        $('#overlay').toggle(); // Show overlay over entire screen
        return false;
    });

    $('body').append('<div id="overlay" />'); // Add overlay to DOM

    $('#overlay').click(function () { // When the overlay is clicked the mailbox will disappear
        $('.modalbox').fadeToggle(200, function () {
            $('#overlay').hide();
        });
    });

    // Collapse content boxes:

    $('.content-box-header h3').click(function () {
        if ($(this).parent().parent().hasClass('closed')) {
            $(this).parent().parent().children('.content-box-content').slideDown();
            $(this).parent().parent().removeClass('closed');
        } else {
            $(this).parent().parent().children('.content-box-content').slideUp();
            $(this).parent().parent().addClass('closed');
        }
    });

    // Close notifications:

    $('div.notification').click(function () {
        $(this).fadeOut(200, function () {
            $(this).hide();
        });
    });

    // PopupWindows:
    var profiles =
    {
	    window800: {height:800, width:800, status:1},
	    window200: {height:200, width:200, status:1, resizable:0},
	    windowCenter: {height:350, width:500, center:1},
	    windowNotNew: {height:300, width:400, center:1, createnew:0},
	    windowCallUnload: {height:300, width:400, center:1}
    };

    $('.popupwindow').popupwindow(profiles);
});


// Based on SubmitMessage from graffiti.js in Graffiti CMS 1.3 Alpha
var SubmitListing = new Object();

SubmitListing.submitMessage = function (url) {
    $('#submit_status').attr('class', 'notification information');
    GraffitiHelpers.statusMessage('submit_status', 'Sending data...', true);

    $.ajax({
        type: "POST",
        url: url + '?command=newListing',
        data: $("#listing_form").serialize(),
        success: function (transport) {
            var serverResponse = transport || "error|We sincerely apologize but there was no response from the server. Please try again later.";
            var res = serverResponse.split("|", 2);
            var notiClass = res[0];
            var response = res[1];

            $('#submit_status').attr('class', 'notification ' + notiClass);
            GraffitiHelpers.statusMessage('submit_status', response, true);

            if (notiClass == "success")
                $('#listing_form').fadeOut('slow');
        },
        error: function () {
            $('#submit_status').attr('class', 'notification error');
            GraffitiHelpers.statusMessage('submit_status', 'Something went wrong. The directory submission was likely not sent.', true);
        }
    });
}


// Based on SubmitMessage from graffiti.js in Graffiti CMS 1.3 Alpha
var SendSms = new Object();

SendSms.sendCoupon = function (url) {
    GraffitiHelpers.statusMessage('submit_status', 'Sending...', true);

    $.ajax({
        type: "POST",
        url: url + '?command=sendCoupon',
        data: $("#smsForm").serialize(),
        success: function (transport) {
            var serverResponse = transport || "We sincerely apologize but there was no response from the server. Please try again later.";
            var res = serverResponse

            GraffitiHelpers.statusMessage('submit_status', serverResponse, true);
        },
        error: function () {
            GraffitiHelpers.statusMessage('submit_status', 'Something went wrong. The SMS was likely not sent.', true);
        }
    });
}


// http://rip747.github.com/popupwindow/
// Copyright (c) 2010 Anthony Petruzzi
jQuery.fn.popupwindow = function (p) {

    var profiles = p || {};

    return this.each(function (index) {
        var settings, parameters, mysettings, b, a, winObj;

        // for overrideing the default settings
        mysettings = (jQuery(this).attr("rel") || "").split(",");


        settings = {
            height: 600, // sets the height in pixels of the window.
            width: 600, // sets the width in pixels of the window.
            toolbar: 0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
            scrollbars: 0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
            status: 0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
            resizable: 1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
            left: 0, // left position when the window appears.
            top: 0, // top position when the window appears.
            center: 0, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
            createnew: 1, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
            location: 0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
            menubar: 0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
            onUnload: null // function to call when the window is closed
        };

        // if mysettings length is 1 and not a value pair then assume it is a profile declaration
        // and see if the profile settings exists

        if (mysettings.length == 1 && mysettings[0].split(":").length == 1) {
            a = mysettings[0];
            // see if a profile has been defined
            if (typeof profiles[a] != "undefined") {
                settings = jQuery.extend(settings, profiles[a]);
            }
        }
        else {
            // overrides the settings with parameter passed in using the rel tag.
            for (var i = 0; i < mysettings.length; i++) {
                b = mysettings[i].split(":");
                if (typeof settings[b[0]] != "undefined" && b.length == 2) {
                    settings[b[0]] = b[1];
                }
            }
        }

        // center the window
        if (settings.center == 1) {
            settings.top = (screen.height - (settings.height + 110)) / 2;
            settings.left = (screen.width - settings.width) / 2;
        }

        parameters = "location=" + settings.location + ",menubar=" + settings.menubar + ",height=" + settings.height + ",width=" + settings.width + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left + ",screenX=" + settings.left + ",top=" + settings.top + ",screenY=" + settings.top;

        jQuery(this).bind("click", function () {
            var name = settings.createnew ? "PopUpWindow" + index : "PopUpWindow";
            winObj = window.open(this.href, name, parameters);

            if (settings.onUnload) {
                // Incremental check for window status
                // Attaching directly to window.onunlaod event causes invoke when document within window is reloaded
                // (i.e. an inner refresh)
                unloadInterval = setInterval(function () {
                    if (!winObj || winObj.closed) {
                        clearInterval(unloadInterval);
                        settings.onUnload.call($(this));
                    }
                }, 500);
            }

            winObj.focus();

            return false;
        });
    });

};
