﻿var tippos;

$(document).ready(function() {
    var current = $("#CurrentQuicktip").val();
    SetTipPosition(current);
    if (current != undefined) {
        if (current > 1) {
            current = 1;
            SetTipPosition(current);
        }
    }


    //click next
    $("#NextTip").click(function() {
        var tippos = GetTipPosition();
        if (tippos != 5) {
            tippos = tippos + 1;
        }

        OpenTip(tippos);
        SetTipPosition(tippos);
    });

    //click previous
    $("#PrevTip").click(function() {
        var tippos = GetTipPosition();

        if (tippos != 1) {
            tippos = tippos - 1;
        }

        OpenTip(tippos);
        SetTipPosition(tippos);
    });

    //mouseover
    $("#QuicktipBlock .tip").each(function(index) {
        $(this).hover(function() {
            index = index + 1;

            //$("#Billboards").cycle('stop');
            var tipid = $(this).attr('id');
            var tippos = tipid.substring(8);

            ResetAll();
            SetTipPosition(tippos);
            OpenTip(tippos);
        }, function() {
            $(this).stop().animate({
                top: '72px',
                height: '29px'
            }, 800, 'easeInBack', function() {
                //$("#Billboards").cycle('stop');
            });
        });
    });

    //easeOutBack
    function OpenTip(current) {
        var tip;
        if (current != undefined) {
            if (current == 1) {
                tip = $("#QuicktipBlock").find("#Quicktip1");
                tip.stop().animate({
                    top: '0px',
                    height: '101px'
                }, 800, 'easeOutBack', function() {
                    SetBackground("Quicktip1");
                });
            }
            else {
                tip = $("#QuicktipBlock").find("#Quicktip" + current);
                tip.stop().animate({
                    top: '0px',
                    height: '101px'
                }, 800, 'easeOutBack', function() {
                    SetBackground("Quicktip" + current);
                });
            }
        }

        ResetAll(current);
    }

    function SetTipPosition(cpos) {
        $("#CurrentQuicktip").val(cpos);

        if (cpos != 5) {
            if (cpos == 1) {
                $("#PrevTip").hide();
            } else {
                $("#PrevTip").show();
            }
            $("#NextTip").show();
        } else {
            $("#NextTip").hide();
        }
    }

    function GetTipPosition() {
        tippos = $("#CurrentQuicktip").val();
        if (tippos != undefined)
            return parseInt(tippos);
        else
            return 1;
    }

    function ResetAll(cpos) {
        $("#QuicktipBlock .tip").each(function(index) {
            index = index + 1;
            if (cpos != undefined) {
                if (cpos != index) {
                    $(this).stop().animate({
                        top: '72px',
                        height: '29px'
                    }, 600, 'easeInBack', function() { });
                }
            }
            else {
                $(this).stop().animate({
                    top: '72px',
                    height: '29px'
                }, 600, 'easeInBack', function() { });
            }
        });
    }

    function SetBackground(tip) {
        $("#HomeTopBlock .billboard").each(function() {
            $(this).stop().fadeOut(700);
        });


        var current = "_" + tip + "Billboard";
        $('img[id$=' + current + ']').stop().fadeIn(700);


        //$("#" + tip + "Billboard").stop().fadeIn(700);
    }
});
