jQuery(document).ready(function() {
    resizeWindow();
    $(window).bind("resize", resizeWindow);
    /* features when js is detected */
    /*  $('#OT_searchWrapperAll').hide(); */
    var reservation_header = $('#OT_searchWrapperAll p#reservation_header').text();
    $('#OT_searchWrapperAll .OT_feedTitle').text(reservation_header);
    $('#OT_searchWrapperAll .OT_feedTitle').css('font-size', '16px').css('line-height', '18px').css('padding-left', '0').css('margin-left', '0');

    $('#content > div').css('display', 'none');

    if (!$.browser.msie && !$.browser.webkit) {
        $('#OT_submitWrap input#submit').attr('src', '').val('Tisch suchen');
    } else {
        $('#OT_submitWrap input#submit').css('border', '0');
    }

    $('#topmenu').fadeIn('slow');

    var url = window.location.toString();
    var pieces = url.split("#", 2);

    if (pieces[1] != undefined)
        openAnchor("#" + pieces[1]);
    else {
        $("#welcome").fadeIn('fast');
    }

    /*
     $('#reservation-button p.link a').live('click', function(e) {
     e.preventDefault();

     $('#content > div').hide();
     $('#mainmenu li.selected').removeClass('selected');
     $('#reservation, #contact').fadeIn('fast');
     $('html, body').animate({scrollTop:405}, 'slow');
     });


     $('#reservation-button p.link a').live('click', function(e) {
     e.preventDefault();

     $('html, body').animate({scrollTop:0}, 'slow');

     $('#reservation p.link').hide();
     $('#reservation').addClass("open");
     $('#OT_searchWrapperAll').fadeIn('slow');
     $('#OT_searchWrapperAll').css('display','block');
     });
     */

    $('#mainmenu li a').live('click', function(e) {
        e.preventDefault();
        var $this = $(this);
        if ($this.parent().attr("class") != "selected") {
            $('#content > div').hide();
            $('#mainmenu li.selected').removeClass('selected');
            $this.parent().addClass('selected');
            var id = $this.attr('href');
            switch (id) {
                case "#contact":
                    $('#contactmenu li').removeClass('selected');
                    $('#reservation, #contactmenu, #contact1, #events, #press').fadeIn('fast'); $('#map').html('<iframe width="314" height="284" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=chipps.eu&amp;sll=37.0625,-95.677068&amp;sspn=51.089971,113.994141&amp;ie=UTF8&amp;hq=chipps.eu&amp;hnear=&amp;radius=15000&amp;t=h&amp;cid=7517894620147027247&amp;ll=52.520399,13.3992&amp;spn=0.014832,0.026865&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>');
                    $('#contactmenu li:first-child').addClass('selected');
                    break;
                case "#menus": $('#menusmenu li').removeClass('selected');
                    $('#menusmenu li:first-child').addClass('selected');
                    var selected = $('#menusmenu li:first-child a').attr('href');
                    $('#menus > div').hide();
                    $('#menus').fadeIn('fast'); $('#menus ' + selected).fadeIn('fast');
                    break;
                case "#photos": $('#photos, #photos .gallery_box, #photos #chippsGallery, #photos #chipps2Gallery').fadeIn();
                    $('#chipps2Gallery').hide(); $('#photosmenu li').removeClass('selected');
                    $('#photosmenu li:first-child').addClass('selected');
                    $('#chippsGallery').galleria();
                    $('#chipps2Gallery').galleria();
                    break;
                case "#abonement": $('#abonement').fadeIn('fast');
                    break;
                case "#about": $('#aboutmenu li').removeClass('selected');
                    $('#aboutmenu li:first-child').addClass('selected');
                    var selected = $('#aboutmenu li:first-child a').attr('href');
                    $('#about > div').hide();
                    $('#about').fadeIn('fast'); $('#about ' + selected).fadeIn('fast');
                    break;
                case "#newsletter": $('#newsletter, #hotnews').fadeIn('fast');
                    break;
                case "#news": $('#news').fadeIn('fast');
                    break;
                case "#links": $('#links').fadeIn('fast');
                    break;

                default: break;
            }
        }
    });

    $('#menusmenu li a').live('click', function(e) {
        var $this = $(this);
        if ($this.attr("class") == 'download') {
            return true;
        }
        e.preventDefault();
        if ($this.parent().attr("class") != 'selected') {
            $('#menus > div').hide();
            $('#menusmenu li.selected').removeClass('selected');
            $this.parent().addClass('selected');
            var id = $this.attr('href');
            switch (id) {
                case "#seriousbreakfast": 
                case "#starter": 
                case "#lunch":
                case "#dessert":
                case "#salad": 
                case "#wine": ;
                case "#order":
                    $(id).fadeIn('fast');
                    break;
                default: break;
            }
        }
    });

    $('#photosmenu li a').live('click', function(e) {
        e.preventDefault();
        var $this = $(this);
        if ($this.parent().attr("class") != "selected") {
            $('#chippsGallery, #chipps2Gallery').hide();
            $('#photosmenu li.selected').removeClass('selected');
            $this.parent().addClass('selected');
            var id = $this.attr('href');
            switch (id) {
                case "#chipps": $('#chipps').fadeIn('fast');
                    $('#chippsGallery').galleria();
                    $('#chippsGallery').fadeIn();
                    break;
                case "#chipps2": $('#chipps2').fadeIn('fast');
                    $('#chipps2Gallery').galleria();
                    $('#chipps2Gallery').fadeIn();
                    break;

                default: $('#chippsGallery, #chipps2Gallery').hide(); break;
            }
        }
    });

    $('#aboutmenu li a').live('click', function(e) {
        e.preventDefault();
        var $this = $(this);
        if ($this.parent().attr("class") != "selected") {
            $('#about > div').hide();
            $('#aboutmenu li.selected').removeClass('selected');
            $this.parent().addClass('selected');
            var id = $(this).attr('href');
            switch (id) {
                case "#concept": $('#concept').fadeIn('fast');
                    break;
                case "#owners": $('#owners, #owners_photo').fadeIn('fast');
                    break;

                default: break;
            }
        }
    });

    $('#contactmenu li a').live('click', function(e) {
        e.preventDefault();
        var $this = $(this);
        if ($this.parent().attr("class") != "selected") {
            $('#content > div:not(#contactmenu)').hide();
            $('#contactmenu li.selected').removeClass('selected');
            $this.parent().addClass('selected');
            var id = $(this).attr('href');
            switch (id) {
                case "#chipps1":
                    $('#contactmenu, #reservation, #contact1, #events, #press').fadeIn('fast'); $('#map').html('<iframe width="314" height="284" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=chipps.eu&amp;sll=37.0625,-95.677068&amp;sspn=51.089971,113.994141&amp;ie=UTF8&amp;hq=chipps.eu&amp;hnear=&amp;radius=15000&amp;t=h&amp;cid=7517894620147027247&amp;ll=52.520399,13.3992&amp;spn=0.014832,0.026865&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>');
                    break;
                case "#chipps2":
                    $('#contactmenu, #contact2, #events, #press').fadeIn('fast'); $('#map2').html('<iframe width="314" height="284" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?oe=UTF-8&amp;q=friedrichstra%C3%9Fe+120,+berlin&amp;ie=UTF8&amp;hq=&amp;hnear=Friedrichstra%C3%9Fe+120,+Mitte+10117+Berlin,+Germany&amp;t=h&amp;ll=52.530739,13.383579&amp;spn=0.014829,0.026865&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>');
                    break;

                default: break;
            }
        }
    });

//weekly newsletter
    var name = $('#weeknews label[for=nl_name]').text();
    var surname = $('#weeknews label[for=nl_surname]').text();
    var email = $('#weekletter label[for=nl_email]').text();

    $('#nl_email').clearingInput({text: email, blurClass: 'blurClass'});
    $('#nl_surname').clearingInput({text: surname, blurClass: 'blurClass'});
    $('#nl_name').clearingInput({text: name, blurClass: 'blurClass'});

    $('#weeknews input.submit').live('click', function (e) {
        var input = $(this).parent().find('input[name=u_EMail]');
        if ($(input).val() == '' || $(input).val() == 'Email') {
            $(input).addClass('error');
            e.preventDefault();
        }
    });

//normal newsletter
    var name = $('#hotnews label[for=hn_name]').text();
    var surname = $('#hotnews label[for=hn_surname]').text();
    var email = $('#hotnews label[for=hn_email]').text();

    $('#hn_email').clearingInput({text: email, blurClass: 'blurClass'});
    $('#hn_surname').clearingInput({text: surname, blurClass: 'blurClass'});
    $('#hn_name').clearingInput({text: name, blurClass: 'blurClass'});

    $('#hotnews input.submit').live('click', function (e) {
        var input = $(this).parent().find('input[name=u_EMail]');
        if ($(input).val() == '' || $(input).val() == 'Email') {
            $(input).addClass('error');
            e.preventDefault();
        }
    });


    /*    $('#OT_searchWrapperAll .close').live('click', function(e){
     e.preventDefault();
     $(this).parent().hide();
     $('#reservation-button').removeClass("open").find('p.link').fadeIn();
     });
     */

    $("#topmenu li a[rel]").overlay({effect: 'apple', top:30});

    $('#topmenu li.sendit a').live('click', function(e) {
        e.preventDefault();
        $("#tellafriend").find(".success").remove();
        $("#tellafriend").find("#loading").remove();
        $("#tellafriend_form").fadeIn('normal');
        $('#tellafriend input.submit').show();
    });

    $('#topmenu li.booking a').live('click', function() {
        $('#reservation-overlay #OT_searchWrapperAll').fadeIn('slow');
        $('#reservation-overlay a.close').click(function() {
            $('#reservation-overlay #OT_searchWrapperAll').hide();

        });
    });

    $('#background').css('z-index', '-2');

    /*send a friend ajax call */
    $('#tellafriend input.submit').click(function () {
        $(".error").hide();
        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var emailToVal = $("#to").val();
        if (emailToVal == '') {
            $("#to").after('<span class="error">Email address is empty.</span>');
            hasError = true;
        } else if (!emailReg.test(emailToVal)) {
            $("#to").after('<span class="error">Email address is not valid.</span>');
            hasError = true;
        }

        var emailFromVal = $("#from").val();
        if (emailFromVal == '') {
            $("#from").after('<span class="error">Email address is empty.</span>');
            hasError = true;
        } else if (!emailReg.test(emailFromVal)) {
            $("#from").after('<span class="error">Email address is not valid.</span>');
            hasError = true;
        }

        var nameVal = $("#name").val();
        if (nameVal == '') {
            $("#name").after('<span class="error">Name is empty.</span>');
            hasError = true;
        }

        var subjectVal = $("#subject").val();
        if (subjectVal == '') {
            $("#subject").after('<span class="error">Subject is empty.</span>');
            hasError = true;
        }

        var messageVal = $("#message").val();
        if (messageVal == '') {
            $("#message").after('<span class="error">There is no message.</span>');
            hasError = true;
        }

        if (hasError == false) {
            $(this).hide();
            $("#tellafriend_form div.buttons").append('<img src="./img/tellafriend/loading.gif" class="margin_top right" alt="Loading" id="loading" />');
            var msg = "Hello! " + nameVal + " has something to tell you.\n\n\"" + messageVal + "\"\n\n----\nChipps · www.chipps.eu";

            $.post("/files/email/sendemail.php",
            { to: emailToVal, from: emailFromVal, name: nameVal, subject: subjectVal, message: messageVal },
                    function(data) {
                        $("#tellafriend_form").slideUp("normal", function() {

                            $("#tellafriend_form").before('<div class="success"><h3>Success</h3><p>Your message was sent.</p></div>');
                        });
                    }
                    );
        }

        return false;
    });

});

function openAnchor(anchor) {
    $('#menus > div').hide();
    $('html, body').animate({scrollTop:0}, 'slow');
    switch (anchor) {
        //bugfix because they sent wrong newsletters with this link to the public...
        case "#kontankt":
        case "#kontact":
        case "#kontakt":
            window.location = "http://www.chipps.eu/#contact";
            break;
        case "#chipps1":
        case "#contact": $('#reservation, #contactmenu, #chipps1, #contact1, #events, #press').fadeIn('fast'); $('#map').html('<iframe width="314" height="284" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=chipps.eu&amp;sll=37.0625,-95.677068&amp;sspn=51.089971,113.994141&amp;ie=UTF8&amp;hq=chipps.eu&amp;hnear=&amp;radius=15000&amp;t=h&amp;cid=7517894620147027247&amp;ll=52.520399,13.3992&amp;spn=0.014832,0.026865&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>');
            $("#mainmenu li a[href=#contact]").parent().addClass('selected');
            $("#contactmenu li").removeClass('selected');
            $("#contactmenu li a[href=" + anchor + "]").parent().addClass('selected');
            break;
        case "#chipps2": $('#chipps2, #contactmenu, #contact2, #events, #press').fadeIn('fast'); $('#map').html('<iframe width="314" height="284" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=chipps.eu&amp;sll=37.0625,-95.677068&amp;sspn=51.089971,113.994141&amp;ie=UTF8&amp;hq=chipps.eu&amp;hnear=&amp;radius=15000&amp;t=h&amp;cid=7517894620147027247&amp;ll=52.520399,13.3992&amp;spn=0.014832,0.026865&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe>');
            $("#mainmenu li a[href=#contact]").parent().addClass('selected');
            $("#contactmenu li").removeClass('selected');
            $("#contactmenu li a[href=" + anchor + "]").parent().addClass('selected');
            break;
        case "#menus": $('#menusmenu li').removeClass('selected');
            $('#menusmenu li:first-child').addClass('selected');
            var selected = $('#menusmenu li:first-child a').attr('href');
            $('#menus > div').hide();
            $('#menus').fadeIn('fast'); $('#menus ' + selected).fadeIn('fast');
            $("#mainmenu li a[href=" + anchor + "]").parent().addClass('selected');
            break;
        case "#photos": $('#photos, #photos .gallery_box, #photos #chippsGallery, #photos #chipps2Gallery').fadeIn();
            $('#chipps2Gallery').hide(); $('#photosmenu li').removeClass('selected');
            $('#photosmenu li:first-child').addClass('selected');
            $('#chippsGallery').galleria();
            $('#chipps2Gallery').galleria();
            $("#mainmenu li a[href=" + anchor + "]").parent().addClass('selected');
            break;

        case "#abonement": $('#abonement').fadeIn('fast');
            $("#mainmenu li a[href=" + anchor + "]").parent().addClass('selected');
            break;
        case "#about": $('#aboutmenu li').removeClass('selected');
            $('#aboutmenu li:first-child').addClass('selected');
            var selected = $('#aboutmenu li:first-child a').attr('href');
            $('#about > div').hide();
            $('#about').fadeIn('fast'); $('#about ' + selected).fadeIn('fast');
            $("#mainmenu li a[href=" + anchor + "]").parent().addClass('selected');
            break;
        case "#newsletter": $('#newsletter').fadeIn('fast');
            $("#mainmenu li a[href=" + anchor + "]").parent().addClass('selected');
            break;
        case "#news": $('#news').fadeIn('fast');
            $("#mainmenu li a[href=" + anchor + "]").parent().addClass('selected');
            break;
        case "#links": $('#links').fadeIn('fast');
            $("#mainmenu li a[href=" + anchor + "]").parent().addClass('selected');
            break;

        case "#seriousbreakfast": 
        case "#starter": 
        case "#lunch": 
        case "#dessert":
        case "#salad": 
        case "#wine": 
        case "#order": 
            $(anchor).fadeIn('fast');
            $("#mainmenu li a[href=#menus]").parent().addClass('selected');
            $('#menus').fadeIn('fast');
            $("#menusmenu li a[href=" + anchor + "]").parent().addClass('selected');
            break;

        case "#concept": $('#concept').fadeIn('fast');
            $("#mainmenu li a[href=#about]").parent().addClass('selected');
            $('#about').fadeIn('fast');
            $("#aboutmenu li a[href=" + anchor + "]").parent().addClass('selected');
            break;

        case "#welcome":
        default: $('#welcome').fadeIn('fast'); break;

    }

}

function resizeWindow(e) {
    var h = $(window).height();
    var w = $(window).width();
    var videoWidth = w;
    var videoHeight = h;

    var vratio = 320 / 234;
    var wratio = w / h;

    if (wratio > vratio) {
        videoHeight = (w / 320) * 234;
    } else {
        videoWidth = h * vratio;
    }

    $("#background").empty().removeClass();
    $("#background").flash({ src: "../swf/chipps.swf", width: videoWidth, height: videoHeight, wmode: 'transparent'});

    $('div.info').fadeIn().css("visibility", "visible");

    /* $("div.info").css("left",(w/2)-($("div.info").outerWidth()/2));*/
}

/*
 * clearingInput: a jQuery plugin
 *
 * clearingInput is a simple jQuery plugin that provides example/label text
 * inside text inputs that automatically clears when the input is focused.
 * Common uses are for a hint/example, or as a label when space is limited.
 *
 * For usage and examples, visit:
 * http://github.com/alexrabarts/jquery-clearinginput
 *
 * Licensed under the MIT:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright (c) 2008 Stateless Systems (http://statelesssystems.com)
 *
 * @author   Alex Rabarts (alexrabarts -at- gmail -dawt- com)
 * @requires jQuery v1.2 or later
 * @version  0.1.2
 */

(function ($) {
    $.extend($.fn, {
        clearingInput: function (options) {
            var defaults = {blurClass: 'blur'};

            options = $.extend(defaults, options);

            return this.each(function () {
                var input = $(this).addClass(options.blurClass);
                var form = input.parents('form:first');
                var label, text;

                text = options.text || textFromLabel() || input.val();

                if (text) {
                    input.val(text);

                    input.blur(
                            function () {
                                if (input.val() === '') {
                                    input.addClass(options.blurClass).val(text);
                                }
                            }).focus(function () {
                        if (input.val() === text) {
                            input.val('');
                        }
                        input.removeClass(options.blurClass);
                    });

                    form.submit(function() {
                        if (input.hasClass(options.blurClass)) {
                            input.val('');
                        }
                    });

                    input.blur();
                }

                function textFromLabel() {
                    label = form.find('label[for=' + input.attr('id') + ']');
                    // Position label off screen and use it for the input text
                    return label ? label.css({position: 'absolute', left: '-9999px'}).text() : '';
                }
            });
        }
    });
})(jQuery);


