/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var usePresentFinder = false;

function calculateBirthDaySelects() {
    for(var i=0; i<31; i++) {
        $("#user_birthDay").append($('<option></option>').val(i+1).html(i+1+"."));
    }
}
function calculateBirthMonthSelects() {
    var months = new Array("Januar", "Februar", "M&auml;rz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");

    for(var i=0; i<12; i++) {
        $("#user_birthMonth").append($('<option></option>').val(i+1).html(months[i]));
    }
}
function calculateBirthYearSelects() {
    var year = new Date().getFullYear();

    for(var i=year; i>(year-90); i--) {
        $("#user_birthYear").append($('<option></option>').val(i).html(i));
    }
}

function checkEmail(inputvalue){
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if(pattern.test(inputvalue)){
        return true;
    }else{
        return false;
    }
}

function startMenu() {
    $('#menu ul').hide();
    $('#menu li a').click(function() {
        $(this).next().slideToggle('slow');
    });
}

function getPersonSelectOptions() {
    $.post("/_ajax/presentfinder.php", {
        instruction:"getPersonArrayAsJSON"
    }, function(data) {
        $("#personSelectWait").hide();
        $("#personSelect").show();
        $.each(data, function(nr, name) {
            $("#personSelect").append(
                $('<option></option>').val(nr).html(name)
                );
        });
    }, "json");
}

function getPriceSelectOptions() {
    $.post("/_ajax/presentfinder.php", {
        instruction:"getPriceArrayAsJSON"
    }, function(data) {
        $("#priceSelectWait").hide();
        $("#priceSelect").show();
        $.each(data, function(nr, price) {
            $("#priceSelect").append(
                $('<option></option>').val(nr).html(price)
                );
        });
    }, "json");
}

function getCategorySelectOptions() {
    $.post("/_ajax/presentfinder.php", {
        instruction:"getCategoryArrayAsJSON"
    }, function(data) {
        $("#categorySelectWait").hide();
        $("#categorySelect").show();
        $.each(data, function(nr, category) {
            $("#categorySelect").append(
                $('<option></option>').val(nr).html(category)
                );
        });
    }, "json");
}

$(document).ready(function() {
    // param defines
    var origFontSize = parseFloat($('html').css('font-size'));

    // page initial calls
    startMenu();
    Xmascountdown();

    if (usePresentFinder) {
        getPersonSelectOptions();
        getPriceSelectOptions();
        getCategorySelectOptions();
    }

    $("select").change(function() {
        var ready = true;

        if ($("#categorySelect").val() == 0)
            ready = false;
        if ($("#personSelect").val() == 0)
            ready = false;
        if ($("#priceSelect").val() == 0)
            ready = false;

        if (!ready) {
            $("#presentFinderReady").fadeOut("slow");
        } else {
            $("#presentFinderReady").fadeIn("slow");
        }
    });

    $("#searchPresents").click(function() {
        $.post("/_ajax/presentfinder.php", {
            instruction:"getPresentSuggestions",
            person: $("#personSelect").val(),
            price:$("#priceSelect").val(),
            category:$("#categorySelect").val()
        }, function(data) {
            window.location.href = "http://weihnachtsgeschenke.de" + data;
            return false;
        });
    });

    // jquery events handlings
    $("#small").click(function() {
        var curSize = parseFloat($('html').css('font-size'), 10);
        var newSize = curSize*0.9;
        $('html').css('font-size', newSize);
        $('.accessibility').css('font-size', origFontSize);
    });

    $("#large").click(function() {
        var curSize = parseFloat($('html').css('font-size'), 10);
        var newSize = curSize*1.1;
        $('html').css('font-size', newSize);
        $('.accessibility').css('font-size', origFontSize);
    });

    $("#medium").click(function() {
        $('html').css('font-size', origFontSize);
    });

    $("#username").focus(function() {
        if ($("#username").val() == "Benutzer")
            $("#username").val("");
    });

    $("#username").blur(function() {
        if ($("#username").val() == "")
            $("#username").val("Benutzer");
    });

    $("#password").focus(function() {
        if ($("#password").val() == "Passwort")
            $("#password").val("");
    });

    $("#password").blur(function() {
        if ($("#password").val() == "")
            $("#password").val("Passwort");
    });

    $("#searchString").focus(function() {
        if ($("#searchString").val() == "Suchbegriff")
            $("#searchString").val("");
    });

    $(".delete_produkt").click(function() {
        var id = $(this).parent().parent().attr("id");
        $(this).parent().parent().remove();
        $.post("/_ajax/presentfinder.php",
        {
            instruction:"removeFromWishlist",
            id : id
        }, function(data) {
            alert(data);
        });
    });

    $("#searchString").blur(function() {
        if ($("#searchString").val() == "")
            $("#searchString").val("Suchbegriff");
    });

    $(".wishlistadder").click(function() {
        var id = $(this).attr("id");
        $.post("/_ajax/presentfinder.php", {
            instruction:"toWishlist",
            id: id
        }, function(data) {
            var error = data.search(/ERRO.+/);
            if (error != -1)
                window.location = data.substr(6);
        });
    });

    $("#showLogin").click(function() {
        $("#username").focus();
        $("#username").attr("style", "background: transparent; background-color:#cd0003 !important;");
        $("#password").attr("style", "background: transparent; background-color:#cd0003 !important;");
    });

    $("#user_nickname").change(function() {
        // show red status bar {information}
        $("#user_nickname_status").show();
        $("#user_nickname_status").text("Verfügbarkeit des Nicknames wird überprüft...");

        $.post("/_ajax/registration_user_unique.php", {
            user_nickname:$("#user_nickname").val()
        }, function(data) {
            if (data == "false") {
                $("#user_nickname").attr("style", "border:1px solid #900");

                // show red status bar
                $("#user_nickname_status").text("Der Nickname ist schon vergeben.");
            } else {
                $("#user_nickname_status").hide();
                $("#user_nickname").attr("style", "");
            }
        });
    });

    $("#user_password_verify").change(function() {
        if ($("#user_password_verify").val() != $("#user_password").val()) {
            $("#user_password_verify").attr("style", "border: 1px solid #900;");
            $("#user_password").attr("style", "border: 1px solid #900;");

            // show red status bar
            $("#user_password_status").show();
            $("#user_password_status").text("Die Passwörter stimmen nicht überein.");
        } else
        {
            $("#user_password_verify").attr("style", "");
            $("#user_password").attr("style", "");

            // hide red status bar
            $("#user_password_status").hide();
        }
    });

    $("#user_email").change(function() {
        if (!checkEmail($("#user_email").val())) {
            $("#user_email").attr("style", "border: 1px solid #900;");
            useremailok = false;
            // show red status bar
            $("#user_email_status").show();
            $("#user_email_status").text("Die E-Mail Adresse hat kein gültiges Format.");
        } else {
            $("#user_email").attr("style", "");
            useremailok = true;
            // hide red status bar
            $("#user_email_status").hide();
        }

        $.post("/_ajax/registration_user_unique.php", {
            user_email:$("#user_email").val()
        }, function(data) {
            if (data == "false") {
                $("#user_email").attr("style", "border:1px solid #900");
                useremailok = false;
                // show red status bar
                $("#user_email_status").show();
                $("#user_email_status").text("Diese E-Mail Adresse wird schon verwendet.");
            } else {
                $("#user_email_status").hide();
                useremailok = true;
                $("#user_email").attr("style", "");
            }
        });
    });

    $("#register").click(function() {

        var status = true;

        if ($("#user_name").val().length < 5) {
            $("#user_name_status").show();
            $("#user_name_status").text("Bitte mind. 5 Zeichen eingeben.");
            status = false;
            return;
        } else {
            status = true;
            $("#user_name_status").hide();
        }
        if ($("#user_nickname").val().length < 5) {
            $("#user_nickname_status").show();
            $("#user_nickname_status").text("Bitte mind. 5 Zeichen eingeben.");
            status = false;
            return;
        } else {
            $.post("/_ajax/registration_user_unique.php", {
                user_nickname:$("#user_nickname").val()
            }, function(data) {
                if (data == "false") {
                    // show red status bar
                    $("#user_nickname_status").show();
                    $("#user_nickname_status").text("Der Nickname ist schon vergeben.");
                    status = false;
                    return;
                } else {
                    status = true;
                    $("#user_nickname_status").hide();
                }
            });
        }
        if ($("#user_password").val().length < 6) {
            $("#user_password_status").show();
            $("#user_password_status").text("Bitte mind. 6 Zeichen eingeben.");
            status = false;
            return;
        } else {
            status = true;
            $("#user_password_status").hide();
        }
        if ($("#user_email").val().length < 5) {
            $("#user_email_status").show();
            $("#user_email_status").text("Bitte mind. 5 Zeichen eingeben.");
            status = false;
            return;
        } else {
            if (!checkEmail($("#user_email").val())) {
                status = false;

                if (!useremailok)
                    status = false;
                
                $("#user_email_status").show();
                $("#user_email_status").text("Die E-Mail Adresse hat kein gültiges Format.");
                return;
            } else
                status = true;
            $("#user_email_status").hide();
        }

        if (status) {
            $("#user_registration").submit();
        }
    });

});

var startPosition=0;
function Xmascountdown()
{
    var XmasDay = "December 25,"
    var today = new Date()
    var thisYear = today.getFullYear()
    var XmasDate = new Date(XmasDay + thisYear)
    var diff = XmasDate.getTime() - today.getTime();
    var days = Math.floor(diff / (1000 * 60 * 60 * 24));

    if ((days < 0) && (days >= -1)) {
        var mainMessage =" FROHE WEIHNACHTEN !!! ... ";
        var speed=175;
        var scrollingRegion=100;
        var tempLoc=(scrollingRegion*3/mainMessage.length)+1;
        if (tempLoc<1) {
            tempLoc=1
        }
        var aut="emsanta"
        var counter;
        for(counter=0;counter<=tempLoc;counter++);
        mainMessage+=mainMessage;
        document.clock.Xmascountdown.value =mainMessage.substring(startPosition,startPosition+scrollingRegion);
        startPosition++;
        if(startPosition>scrollingRegion) startPosition=0;
        setTimeout("Xmascountdown()",speed);
    }

    else {
        if (days >= 0) {
            var theDay = XmasDate;
        } else {
            var theDay = new Date(XmasDate.getYear()+1,XmasDate.getMonth(),XmasDate.getDate());
        }
        var second = Math.floor((theDay.getTime() - today.getTime())/1000);
        var minute = Math.floor(second/60);
        var hour = Math.floor(minute/60);
        var day = Math.floor(hour/24);
        CDay= day;
        CHour= hour % 24;
        CMinute= minute % 60;
        CSecond= second % 60;
        var DayTill =  CDay + " Tagen, " + CHour + " Stunden, " + CMinute + " Minuten und " + CSecond + " Sekunden";
        document.clock.Xmascountdown.value = DayTill;
        var counter = setTimeout("Xmascountdown()", 1000);
    }
}


