var opa = new Object();

jQuery(document).ready(function () {
    opa.initSubscribe();
    opa.initCopyright();
    opa.initMenus();
    opa.initTabs();
    opa.initFancybox();
    opa.initForms();
    opa.initAudioPlayer();
});

opa.initSubscribe =
function opa_init_subscribe() {
    var self = this;
    var defaultValue = "your email address...";

    jQuery("#subscribe").focus(function (e) {
        if (jQuery.trim(this.value) == defaultValue)
            jQuery(this).val('').removeClass('empty');
    });

    jQuery("#subscribe").blur(function (e) {
        if (!jQuery.trim(this.value))
            jQuery(this).addClass('empty').val(defaultValue);
    });

    jQuery("#subscribe").blur();

    jQuery("#subscribe").hover(
        function (e) { // mouseover
            if (!opa.subscribeSuccess)
                jQuery("#subscribe_tooltip").fadeIn();
        },
        function (e) { // mouseout
            jQuery("#subscribe_tooltip").fadeOut();
        }
    );

    jQuery("#subscribe").bind("keypress", function (e) {
        if (e.which != 13) // enter
            return;

        jQuery(this).attr('disabled', 'disabled');

        self.subscribe();
    });
}

opa.subscribe =
function opa_subscribe() {
    var self = this;
    var data = {
        form: 'subscribe',
        email: jQuery("#subscribe").val()
    };

    var onSuccess = function (data, textStatus) {
        opa.subscribeSuccess = true;
        if (data == self.SUCCESS_STRING) {
            jQuery("#subscribe_tooltip").fadeOut();
            jQuery("#subscribe").fadeOut("normal", function () {
                jQuery("#subscribe_success").fadeIn();
                setTimeout(function () {
                    jQuery("#subscribe_success").fadeOut("slow")
                }, 2500);
            });
        } else {
            jQuery('#subscribe').addClass('error');
            jQuery('#subscribe_tooltip').addClass('error');
            jQuery('#subscribe_tooltip').html(
                "We're sorry, but an error has occurred.<br />" +
                    "Please try again later. Thank you!"
            );

            setTimeout(function () {
                jQuery("#subscribe_tooltip").fadeOut("slow");
                jQuery("#subscribe").fadeOut("slow");
            }, 5000);
        }
        return false;
    };

    self.post(data, onSuccess);
}

opa.initCopyright =
function opa_init_copyright() {
    var html = "Copyright &copy;";
    html += new Date().getFullYear();
    html += " Opa! Authentic Greek Cuisine";

    jQuery('#copyright').html(html);
}

opa.initMenus =
function opa_init_menus() {
    var self = this;
    this.currentSlide = null;

    jQuery('#nav > li').hover(
        function () { jQuery(this).addClass("hover"); }, // mouseover
        function () { jQuery(this).removeClass("hover"); }  // mouseout
    );

    jQuery('#nav > li').click(function () {
        self.showSlide(jQuery(this).attr('slide'));
    });

    jQuery('#logo').click(function () {
        self.hideCurrentSlide();
    });
}

opa.hideCurrentSlide =
function opa_hide_current_slide() {
    if (!this.currentSlide)
        return;

    jQuery('#' + this.currentSlide).fadeOut();
    this.currentSlide = null;
}

opa.showSlide =
function opa_show_slide(slide) {
    if (slide == this.currentSlide)
        return;

    this.hideCurrentSlide();

    jQuery('#' + slide).fadeIn();
    this.currentSlide = slide;
}

opa.initTabs =
function opa_init_tabs() {
    var self = this;
    this.currentTab = null;

    jQuery('.tab-pane.init').show();
    jQuery('.tab.init').addClass("selected");

    jQuery('.tab').hover(
        function () { jQuery(this).addClass("hover"); }, // mouseover
        function () { jQuery(this).removeClass("hover"); }  // mouseout
    );

    jQuery('.tab').click(
        function () {
            jQuery(this).siblings().removeClass("selected");
            jQuery(this).addClass("selected");
        }
    );
}

opa.hideSiblingTabPanes =
function opa_hide_sibling_tab_panes(tab) {
    jQuery(tab).siblings('.tab-pane').fadeOut();
    this.currentTab = null;
}

opa.showTabPane =
function opa_show_tab_pane(tabPaneId) {
    var pane = jQuery('#' + tabPaneId);

    this.hideSiblingTabPanes(pane);
    pane.fadeIn();
}

opa.initForms =
function opa_init_forms() {
    var self = this;
    this.SUCCESS_STRING = "success";

    jQuery('#catering_submit').click(function () {
        var data = jQuery("#catering_form").serialize();
        var onSuccess = function (data, textStatus) {
            if (data == self.SUCCESS_STRING) {
                self.showTabPane("_catering_request_success");
            } else {
                jQuery("#catering_request_error_details").text(data);
                self.showTabPane("_catering_request_error");
            }
            return false;
        };

        self.post(data, onSuccess);
        return false;
    });

    jQuery('#feedback_next').click(function () {
        jQuery('#feedback_page1').fadeOut(function () {
            jQuery('#feedback_page2').fadeIn();
        });
        return false;
    });

    jQuery('#feedback_back').click(function () {
        jQuery('#feedback_page2').fadeOut(function () {
            jQuery('#feedback_page1').fadeIn();
        });
        return false;
    });

    jQuery('#feedback_submit').click(function () {
        // look for required fields that are empty
        var missingLabels = [];
        jQuery("#feedback_page2 tr.required").each( function () {
            if (jQuery(this).find("input").val() == "") {
                missingLabels.push(jQuery(this).find("label").text());
                this.addClass("required-missing");
            } else {
                this.removeClass("required-missing");
            }
        });

        // squak about required fields that are empty
        if (missingLabels.length !== 0) {
            alert("Please fill in these required fields: " +
                  missingLabels.join(", ") + ".");
            return false;
        }

        // looks good, try sending
        var data = jQuery("#feedback_form").serialize();
        var onSuccess = function (data, textStatus) {
            // success
            var slide = "_feedback_success";

            // fail
            if (data != self.SUCCESS_STRING) {
                jQuery("#feedback_error_details").text(data);
                slide = "_feedback_error";
            }

            jQuery('#feedback_page2').hide();
            jQuery('#feedback_page1').show();
            jQuery("#feedback_form")[0].reset();

            self.showSlide(slide);
            return false;
        };

        self.post(data, onSuccess);
        return false;
    });
}

opa.post =
function opa_post(data, onSuccess) {
    return jQuery.ajax({
        cache: false,
        data: data,
        dataType: "text",
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("Oops! Something went wrong here..." +
                  "\n\nPlease give us a call at (408) 402-5147." +
                  "\n\nThanks," +
                  "\n- Opa!" +
                  "\n\n" + "-------------------" +
                  "\n\nERROR - form could not be submitted: " + textStatus +
                  "\n" + errorThrown);
        },
        success: onSuccess,
        type: "POST",
        url: "post.php"
    });
}

opa.initFancybox =
function opa_init_fancybox() {
    jQuery(".fancybox").fancybox({
        overlayOpacity: 0.7,
        overlayColor: "#000",
        autoScale: false,
        centerOnScroll: false
    });
}

opa.initAudioPlayer =
function opa_init_audio_player() {
    AudioPlayer.setup("audio-player/player.swf", {
        transparentpagebg: "yes"
    });

    AudioPlayer.embed("music", {
        soundFile: "media/mp3/opa-opa.mp3",
        titles: "Opa Opa",
        artists: "Notis Sfakianakis",
        autostart: "yes",
        initialvolume: 60,
        loader: "DBAC6D",
        width: 81 // (closed)
   //   width: 300 // (open)
    });
}

/********** MooTools code below **************/

window.addEvent('domready', function() {
    var data = {
	'0006.jpg': { },
	'0014.jpg': { },
	'0029.jpg': { },
	'0040.jpg': { },
	'0050.jpg': { },
	'0057.jpg': { },
	'0062.jpg': { },
	'0084.jpg': { },
	'0098.jpg': { },
	'0104.jpg': { },
	'0104.jpg': { },
	'0114.jpg': { },
	'0132.jpg': { },
	'0141.jpg': { },
	'0147.jpg': { },
	'0158.jpg': { },
	'0164.jpg': { },
	'0168.jpg': { },
	'0172.jpg': { },
	'0198.jpg': { },
	'0210.jpg': { },
	'0222.jpg': { },
	'0262.jpg': { }
    };

    opa.myShow = new Slideshow.KenBurns('slideshow', data, {
        coller: false,
        delay: 2000,
        duration: 5000,
        hu: 'media/images/cuisine/1056/',
        thumbnails: false,
        height: 400,
        width: 880,
        random: true,
        /*** KenBurns-specific options below ***/
        pan: 100,
        zoom: 20
    });
});
