var high = function(cat)
{
    $("#menu li a").removeClass("open");
    $("#m" + cat + " a").addClass("open");
    $("a.high").removeClass("high");
    $("." + cat).addClass("high");
}
var showphoto = function(p, change)
{
    c = change;
    var parts = $("#foto").attr("src").split("/large/");
    $(".selected").removeClass("selected");
    $("#thumbimg" + p).addClass("selected");
    var path = parts[0] + "/large/" + p + ".jpg";
    $("#foto").animate(
    {
        opacity: 0
    }, 200, function ()
    {
        $("#fotoframe").html("<img src=\"" + path + "\" id=\"foto\" alt=\"\" />");
        setTimeout(function ()
        {
            $("#foto").animate(
            {
                opacity: 1
            }, 200)
        }, 300);
    });
}
var nc;
var c;
var cc;
var cF = function(i) {
    return i;
};

var preloadThumbs = function(){
      
      setupPhoto($("#fotoframe img"));

      var thumbs = $(".thumb");
 
      var thumbsCnt = thumbs.length;
      for(var t=0;t<thumbsCnt;t++){
           setupPhoto($(thumbs[t]));
      }
}

var setupPhoto = function(photo){
     
       var image = new Image();            
      image.src = $(photo).attr('src');
            $(photo).css({"opacity":"0"});
            image.onload = function() {  $(photo).animate({opacity: 1}, 200);};
}

var nextset = function ()
{
    var current = $("#nextset").attr("name") * 1;
    var max = $("#prevset").attr("name") * 1;
    var next = current + 1;
    if (max >= next)
    {
        $("#thumbs" + current).hide("slide", {
            direction: "left"
        }, 300, function ()
        {
            $("#thumbs" + next).show("slide", {
                direction: "right"
            }, 300, function ()
            {
                /// $("#nextset").fadeIn(100);
                //  $("#prevset").fadeIn(100);
                next++;
                $("#prevset").animate(
                {
                    opacity: 1
                }, 200);
                if (max < next)
                {
                    $("#nextset").animate(
                    {
                        opacity: 0.2
                    }, 200);
                }
                else
                {
                    $("#nextset").animate(
                    {
                        opacity: 1
                    }, 200);
                }
            });
        });
        $("#nextset").attr("name", next);
    }
    return false;
}
var nextphoto = function()
{
    var parts = $("#foto").attr("src").split("/large/");
    cc = c;
    c++;
    if ($("#thumblink" + c).attr("name") != undefined)
    {
        var p = $("#thumblink" + c).attr("name");
        if ($("#thumblink" + c).parent().attr("id") != $("#thumblink" + cc).parent().attr("id"))
        {
            nextset();
        }
        var parts = $("#foto").attr("src").split("/large/");
        $(".selected").removeClass("selected");
        $("#thumbimg" + p).addClass("selected");
        var path = parts[0] + "/large/" + p + ".jpg";
        $("#foto").animate(
        {
            opacity: 0
        }, 200, function ()
        {
            $("#fotoframe").html("<img src=\"" + path + "\" id=\"foto\" alt=\"\" />");
            setTimeout(function ()
            {
                $("#foto").animate(
                {
                    opacity: 1
                }, 200)
            }, 300);
        });
    }
}
var subsHTML = "";
var unsubsHTML = "";
var subscribe = function()
{
    var str = $("#subsdata").serialize();
    $.post("/sections/subscribe.php", str, function (data, textStatus)
    {
        $("#subscribe").fadeOut("fast", function ()
        {
            subsHTML = $("#subscribe").html();
            $("#subscribe").html("<p>" + data + "</p>");
            $("#subscribe").fadeIn();
            setTimeout(function ()
            {
                $("#subscribe").fadeOut("fast", function ()
                {
                    $("#subscribe").html(subsHTML);
                    $("#subscribe").fadeIn();
                });
            }, 3000);
        });
    });
}
var unsubscribe = function()
{
    var str = $("#unsubsdata").serialize();
    $.post("/sections/unsubscribe.php", str, function (data, textStatus)
    {
        $("#unsubscribe").fadeOut("fast", function ()
        {
            unsubsHTML = $("#unsubscribe").html();
            $("#unsubscribe").html("<p>" + data + "</p>");
            $("#unsubscribe").fadeIn();
            setTimeout(function ()
            {
                $("#unsubscribe").fadeOut("fast", function ()
                {
                    $("#unsubscribe").html(unsubsHTML);
                    $("#unsubscribe").fadeIn();
                });
            }, 3000);
        });
    });
}
var showsending = function()
{
    $("#infotekst").html("Trwa wysyłanie pliku...");
}
$(document).ready(function ()
{
      preloadThumbs();
    if ($("#foto").attr("name") != undefined)
    {
        c = $("#foto").attr("name");
    }
    $("#fotoframe").css("cursor", "pointer");
    $("#fotoframe").live('click', function (event)
    {
        nextphoto();
    });
    $("h2#model a").hover(function ()
    {
        var cat = $(this).attr("name");
        $("#menu li a").removeClass("open");
        $("#m" + cat + " a").addClass("open");
        $("a.high").removeClass("high");
        $(this).addClass("high");
    })
    $("a.cat").click(function ()
    {
        var cat = $(this).attr("name");
        high(cat);
        return false;
    });
    $("a.cat").hover(function ()
    {
        var cat = $(this).attr("name");
        high(cat);
    }, function ()
    {
        var cat = $(this).attr("name");
        high(cat);
    });
    $("h1 a").css("position", "relative").css("left", 0).css("top", 0);
    $("#thumbsblock img").css("position", "relative").css("left", 0).css("top", 0);
    $(".celllast img, .cell img").css("position", "relative").css("left", 0).css("top", 0);
    $(".bigcol img, .smallcol img").css("position", "relative").css("left", 0).css("top", 0);
    $("h1 a").hover(function ()
    {
        $(this).stop(true, true);
        $(this).animate(
        {
            opacity: 0.5
        }, 300)
    }, function ()
    {
        $(this).animate(
        {
            opacity: 1
        }, 400)
    });
    $("#modelthumbs img").hover(function ()
    {
        $(this).stop(true, true);
        $(this).animate(
        {
            opacity: 0.5
        }, 300)
    }, function ()
    {
        $(this).animate(
        {
            opacity: 1
        }, 400)
    });
    $(".cell img, .celllast img").hover(function ()
    {
        $(this).stop(true, true);
        $(this).animate(
        {
            opacity: 0.5
        }, 300)
    }, function ()
    {
        $(this).animate(
        {
            opacity: 1
        }, 400)
    });
    $(".smallcol img, .bigcol img").hover(function ()
    {
        $(this).parent().parent().find("h3.maincols a").addClass("opened");
    }, function ()
    {
        $(this).parent().parent().find("h3.maincols a").removeClass("opened");
    });
    $(".email").val("e-mail");
    $(".email").focus(function ()
    {
        var savedval = $(this).val();
        if ($(this).val() == "e-mail")
        {
            $(this).val("");
        }
    });
    $("input").blur(function ()
    {
        if ($(this).val().length > 0)
        {
            $(this).val(savedval);
        }
        else
        {
            $(this).val("e-mail");
        }
    });
    var max = $("#prevset").attr("name") * 1;
    var current = $("#nextset").attr("name") * 1;
    var next = current + 1;
    var prev = current - 1;
    if (max < next)
    {
        $("#nextset").animate(
        {
            opacity: 0.2
        }, 200);
    }
    if (prev == 0)
    {
        $("#prevset").animate(
        {
            opacity: 0.2
        }, 200);
    }
    if (max == 1)
    {
        $("#nextset").hide();
        $("#prevset").hide();
    }
    $("#nextset").live('click', function (event)
    {
        var current = $(this).attr("name") * 1;
        var max = $("#prevset").attr("name") * 1;
        var next = current + 1;
        if (max >= next)
        {
            $("#thumbs" + current).hide("slide", {
                direction: "left"
            }, 300, function ()
            {
                $("#thumbs" + next).show("slide", {
                    direction: "right"
                }, 300, function ()
                {
                    /// $("#nextset").fadeIn(100);
                    //  $("#prevset").fadeIn(100);
                    next++;
                    $("#prevset").animate(
                    {
                        opacity: 1
                    }, 200);
                    if (max < next)
                    {
                        $("#nextset").animate(
                        {
                            opacity: 0.2
                        }, 200);
                    }
                    else
                    {
                        $("#nextset").animate(
                        {
                            opacity: 1
                        }, 200);
                    }
                });
            });
            $(this).attr("name", next);
        }
        return false;
    });
    $("#prevset").live('click', function (event)
    {
        var current = $("#nextset").attr("name") * 1;
        var max = $("#prevset").attr("name") * 1;
        var prev = current - 1;
        if (prev >= 1)
        {
            $("#thumbs" + current).hide("slide", {
                direction: "right"
            }, 300, function ()
            {
                $("#thumbs" + prev).show("slide", {
                    direction: "left"
                }, 300, function ()
                {
                    if (prev <= 1)
                    {
                        $("#prevset").animate(
                        {
                            opacity: 0.2
                        }, 200);
                    }
                    else
                    {
                        $("#prevset").animate(
                        {
                            opacity: 1
                        }, 200);
                    }
                    $("#nextset").animate(
                    {
                        opacity: 1
                    }, 200);
                    //    $("#nextset").fadeIn(100);
                    //     $("#prevset").fadeIn(100);
                });
            });
            $("#nextset").attr("name", prev);
        }
        return false;
    });
    var currentTab = $(".current").attr("id").replace("showTab", "");
    $(".showTab").live('click', function (event)
    {
        var show = $(this).attr("id").replace("showTab", "");
        if (show > currentTab)
        {
            $("#tab" + currentTab).hide("slide", {
                direction: "left"
            }, 400, function ()
            {
                $("#showTab" + currentTab).removeClass("open");
                $("#tab" + show).show("slide", {
                    direction: "right"
                }, 400, function ()
                {
                    $("#showTab" + show).addClass("open");
                    currentTab = show;
                });
            });
        }
        else
        {
            $("#tab" + currentTab).hide("slide", {
                direction: "right"
            }, 400, function ()
            {
                $("#showTab" + currentTab).removeClass("open");
                $("#tab" + show).show("slide", {
                    direction: "left"
                }, 400, function ()
                {
                    $("#showTab" + show).addClass("open");
                    currentTab = show;
                });
            });
        }
        return false;
    });
    $("#showNextTab").live('click', function (event)
    {
        var next = currentTab * 1 + 1;
        if ($("#showTab" + next).html() == undefined)
        {
            next = 1;
        }
        $("#showTab" + currentTab).removeClass("open");
        $("#tab" + currentTab).hide("slide", {
            direction: "left"
        }, 400, function ()
        {
            $("#tab" + next).show("slide", {
                direction: "right"
            }, 400, function ()
            {
                $("#showTab" + next).addClass("open");
            });
        });
        currentTab = next;
        return false;
    });
    $("#showPrevTab").live('click', function (event)
    {
        var prev = currentTab * 1 - 1;
        if (prev <= 0)
        {
            prev = 1;
            while ($("#showTab" + prev).html() != undefined)
            {
                prev++;
            }
            prev--;
        }
        $("#showTab" + currentTab).removeClass("open");
        $("#tab" + currentTab).hide("slide", {
            direction: "right"
        }, 400, function ()
        {
            $("#tab" + prev).show("slide", {
                direction: "left"
            }, 400, function ()
            {
                $("#showTab" + prev).addClass("open");
            });
        });
        currentTab = prev;
        return false;
    });
    
});
