// Hive Solutions Website
// Copyright (C) 2010 Hive Solutions Lda.
//
// This file is part of Hive Solutions Website.
//
// Hive Solutions Website is confidential and property of Hive Solutions Lda,
// its usage is constrained by the terms of the Hive Solutions
// Confidential Usage License.
//
// Hive Solutions Website should not be distributed under any circumstances,
// violation of this may imply legal action.
//
// If you have any questions regarding the terms of this license please
// refer to <http://www.hive.pt/licenses/>.

// __author__    = Joćo Magalhćes <joamag@hive.pt>
// __version__   = 1.0.0
// __revision__  = $LastChangedRevision$
// __date__      = $LastChangedDate$
// __copyright__ = Copyright (c) 2010 Hive Solutions Lda.
// __license__   = Hive Solutions Confidential Usage License (HSCUL)

$(document).ready(function() {
    // retrieves the browser name and converts it to lowercase
    var browserName = browserDetect.browser.toLowerCase();

    // retrieves the browser version
    var browserVersion = browserDetect.version;

    // retrieves the browser operative system and converts it to lowercase
    var browserOs = browserDetect.os.toLowerCase();

    // adds the browser classes to the body item
    $("body").addClass(browserName);
    $("body").addClass(browserName + "-" + browserVersion);
    $("body").addClass(browserOs);

    // starts the slideshow
    $("#screenshots-button").slideshow({
        screenshotsPrefix : "resources/screenshots/",
        screenshots : ["omni-screen-1.jpg", "omni-screen-2.jpg",
                "omni-screen-3.jpg", "omni-screen-4.jpg"]
    });

    $("#screenshots-button").click(function() {
                $.scrollTo($("#jquery-slideshow"), 800, {
                            offset : {
                                top : -50,
                                left : 0
                            }
                        });
            });

    $(":text").each(function(index, value) {
                // retrieves the value reference
                var valueReference = $(value);

                // retrieves the current status
                var currentStatus = valueReference.attr("current_status");

                // retrieves the original value
                var originalValue = valueReference.attr("original_value");

                // in case the current status is invalid
                if (currentStatus == "invalid") {
                    // adds the invalid mode class
                    valueReference.addClass("invalid");
                } else if (currentStatus != "") {
                    valueReference.attr("value", currentStatus);
                }

                // retrieves the current value
                var currentValue = valueReference.attr("value");

                // in case the current value is the original one
                if (currentValue == originalValue) {
                    // adds the lower (background) mode class
                    valueReference.addClass("lower");
                }

                // registers for the focus event
                valueReference.focus(function(event) {
                            // retrieves the current value
                            var currentValue = valueReference.attr("value");

                            // in case the current value is
                            // the original one
                            if (currentValue == originalValue) {
                                valueReference.attr("value", "");
                                valueReference.removeClass("lower");
                                if (currentStatus == "invalid") {
                                    // removes the invalid mode class
                                    valueReference.removeClass("invalid");
                                }
                            }
                        });

                // registers for the blur event
                valueReference.blur(function(event) {
                            // retrieves the current value
                            var currentValue = valueReference.attr("value");

                            // in case the current value is empty
                            if (currentValue == "") {
                                valueReference.attr("value", originalValue);
                                valueReference.addClass("lower");
                                if (currentStatus == "invalid") {
                                    // adds the invalid mode class
                                    valueReference.addClass("invalid");
                                }
                            }
                        });
            });

    $("textarea").each(function(index, value) {
                // retrieves the value reference
                var valueReference = $(value);

                // retrieves the current status
                var currentStatus = valueReference.attr("current_status");

                // retrieves the original value
                var originalValue = valueReference.attr("original_value");

                // in case the current status is invalid
                if (currentStatus == "invalid") {
                    // adds the invalid mode class
                    valueReference.addClass("invalid");
                } else if (currentStatus != "") {
                    // in case the browser is webkit based the text area
                    // requires some time before it is changed
                    // otherwise the browser might crash
                    if ($.browser.safari) {
                        // sets the timeout function to change the
                        // text area value
                        setTimeout(function() {
                                    valueReference.get(0).value = currentStatus;
                                    valueReference.removeClass("lower");
                                }, 10);
                    } else {
                        // sets the text area value
                        valueReference.get(0).value = currentStatus;
                    }
                }

                // retrieves the current value
                var currentValue = valueReference.get(0).value;

                // in case the current value is the original one
                if (currentValue == originalValue) {
                    // adds the lower (background) mode class
                    valueReference.addClass("lower");
                }

                // registers for the focus event
                valueReference.focus(function(event) {
                            // retrieves the current value
                            var currentValue = valueReference.get(0).value;

                            // in case the current value is
                            // the original one
                            if (currentValue == originalValue) {
                                valueReference.get(0).value = "";
                                valueReference.removeClass("lower");
                                if (currentStatus == "invalid") {
                                    // removes the invalid mode class
                                    valueReference.removeClass("invalid");
                                }
                            }
                        });

                // registers for the blur event
                valueReference.blur(function(event) {
                            // retrieves the current value
                            var currentValue = valueReference.get(0).value;

                            // in case the current value is empty
                            if (currentValue == "") {
                                valueReference.get(0).value = originalValue;
                                valueReference.addClass("lower");

                                if (currentStatus == "invalid") {
                                    // adds the invalid mode class
                                    valueReference.addClass("invalid");
                                }
                            }
                        });
            });

    $(".error").each(function(index, value) {
                // retrieves the value reference
                var valueReference = $(value);

                // retrieves the current status
                var currentStatus = valueReference.attr("current_status");

                // in case there is a status defined
                if (currentStatus != "") {
                    // shows the value
                    valueReference.addClass("visible");
                }
            });

    $("#send-form-button").click(function(event) {
                // in case the form is already being sent
                if ($("#send-form-button").attr("sending") == "true") {
                    return;
                }

                $(":text").each(function(index, value) {
                            // retrieves the value reference
                            var valueReference = $(value);

                            // retrieves the current value
                            var currentValue = valueReference.attr("value");

                            // retrieves the original value
                            var originalValue = valueReference.attr("original_value");

                            // in case the current value is the original one
                            if (currentValue == originalValue) {
                                valueReference.attr("value", "");
                            }
                        });

                $("textarea").each(function(index, value) {
                            // retrieves the value reference
                            var valueReference = $(value);

                            // retrieves the current value
                            var currentValue = valueReference.get(0).value;

                            // retrieves the original value
                            var originalValue = valueReference.attr("original_value");

                            // in case the current value is the original one
                            if (currentValue == originalValue) {
                                valueReference.get(0).value = "";
                            }
                        });

                // sets the sending flag as true
                $("#send-form-button").attr("sending", "true");

                // changes the form button label
                $("#send-form-button").html("Sending...");

                // submits the contact form
                $("#contact-form").submit();
            });

    $("#find-us-location-button").click(function(event) {
                $("#map-balloon").toggle(300)
            });
});
