/// <reference path="jquery.min.js" /> 
/// <reference path="jquery.mobile-1.0rc2.min.js" />


Itizen = (function () {

    var iphone = function () {

        var right = parseInt($(".ints").css("right").replace("px", "")),
         mouseout = function () { $(this).animate({ right: right }, 150); },
         mousein = function () { $(this).animate({ right: (right + 10) }, 150, mouseout); },
         pos = '1',
         selectedone = false,
         timer = null,
        killTimer = function () {
            if (timer) {
                window.clearInterval(timer);
                timer = null
            }
        },
        mouseactions = function (el) {

            var visible = $(el).find("img:visible");
            var hidden = $(el).find("img:hidden");
            //reset
            var old = $(".ints:has(img:first:hidden)");
            if ($(el).find("img:first").is(":visible")) {
                old.find("img:hidden").show().animate({ right: 0 }, 300).siblings().animate({ right: 70 }, 300, function () { $(this).hide(); });
                //set new
                visible.animate({ right: 70 }, 300, function () { visible.hide(); hidden.show().animate({ right: 0 }, 300); });
            }
        },
        slidenext = function (skiptosh) {

            var activeslide = $(".slides .active");
            var slides = $("#slide" + pos);
            //at end of album go to next album
            if (activeslide.next().attr("src") == undefined) {
                if (selectedone) //stay in album
                {
                    activeslide.removeClass("active").animate({ left: -203 }, 500, function () { $(this).css({ left: 203 }) });
                    activeslide = slides.find("img:first");
                }
                else { //pick next album

                    var ints = $("#int" + pos);
                    if (ints.next().attr("id") == undefined) {
                        //pos = 1;
                        killTimer();
                        return;
                    }
                    else {
                        pos = ints.next().attr('id').toString().substring(3);
                        slides = $("#slide" + pos);
                    }
                    mouseactions($("#int" + pos));
                    activeslide.removeClass("active").animate({ left: -203 }, 500, function () { $(this).css({ left: 203 }) });
                }
            }
            else { //next slide
                activeslide.removeClass("active").animate({ left: -203 }, 500, function () { $(this).css({ left: 203 }) });
                activeslide = activeslide.next();
            }

            //slide label
            if (("slide" + pos) == "slide1" && $("#labels img").css("left") != "0px")
                $("#labels img").animate({ left: 0 }, 250);
            else if (("slide" + pos) == "slide2" && $("#labels img").css("left") != "-58px")
                $("#labels img").animate({ left: -58 }, 250);
            else if (("slide" + pos) == "slide3" && $("#labels img").css("left") != "-115px")
                $("#labels img").animate({ left: -115 }, 250);


            if (activeslide.parent().attr('id') != ("slide" + pos)) {


                activeslide = slides.find("img:first");
            }
            if (skiptosh > 0) { //if sppecific slide (dot) is selected{
                activeslide = slides.find("img:eq(" + skiptosh + ")");
            }

            activeslide.addClass("active").animate({ left: 0 }, 500);

            //flip dots
            $(".dots .active").removeClass("active");
            $(".dots:eq(" + slides.find("img").index(activeslide) + ") img").addClass("active");

            if (timer == null && !(skiptosh > 0))
                timer = window.setInterval(function () { slidenext(); }, 3500);


        };

        $(".ints").click(function () {
            selectedone = true;
            pos = $(this).attr('id').toString().substring(3);
            killTimer();
            slidenext(undefined);
            mouseactions(this);
        });

        $(".dots").click(function () {
            slidenext($(".dots").index($(this)));
            killTimer();
        });

        $(".ints").hover(mousein, mouseout);

        setTimeout(slidenext, 2000);

    },
    SignUp = function () {

        var Submit = function () {

            $.post("proxy.ashx?url=/users/register"
            , $("#TheSignUpForm").serialize()
            , HandleResponse
            , "json");
            $.mobile.showPageLoadingMsg(); //show loading
            return false;
        },
        HandleResponse = function (result) {
            if (result.Success === true)
                $.mobile.changePage("thanks.htm");
            else {
                //show errors
                for (error in result.Errors) {
                    $("#errors").append($("<li>").text(error.Message));
                }
                $("#errors").fadeIn();
                $.mobile.hidePageLoadingMsg(true); //hide
            }
        };

        $("#SignUpPage #submit").click(Submit);

    },
    init = function () {

        iphone();

        //background fix position
        function fixbackground() {
            var width = parseInt($("body").width());
            if (width >= 1200)
                $("body").css("background-position", ((width) / 2) - 2500 + "px 0px");
            else
                $("body").css("background-position", 603 - 2500 + "px 0px");
        }
        fixbackground();
        $(window).resize(fixbackground);

    };



    $().ready(init);

    $('[data-role=page]').live('pageshow', function (event, ui) {
        try {
            var pageTracker = _gat._getTracker("UA-11862539-1");
            pageTracker._trackPageview();
        } catch (err) {

        }
    });

    $(document).delegate("#SignUpPage", "pageshow", SignUp);

    return { iphone: iphone };

})();
