var actualPage=1;
function reload()
{
    $(document).ready(function() {
        $("a[rel*=facebox]").fancybox({
            'transitionIn'		: 'none',
            'transitionOut'		: 'none',
            'titlePosition' 	: 'inside',
            'autoScale':false,
            'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
                modTitle = title.split(',');
                return '<div class="left textLeft"><b>' + modTitle.shift() + '</b><br>' + modTitle.join() + ' </div><div class="right">' + (currentIndex + 1) + ' of ' + currentArray.length + '</div>';
            }
        });


    });
};
reload();

function changeNum(source, toWhat)
{
    var oppArray = new Array();
    oppArray[1] = "2";
    oppArray[2] = "1";
    $("#test").html( source );
    //var parts = source.split(".");
    //var ret=parts[0].substr(0,parts[0].length - 1 ) + toWhat +"." + parts[1];
    var ret = source.replace(oppArray[toWhat]+".", toWhat + ".");
    return  ret;
}
var num=0;

$(document).ready(function() {
    //$('#img_frame_second').galleria();
    setInterval("checkHash();", 500);
    $(".hide").hide();
    if( location.hash )
    {
        $("#page_" + location.hash.substr(1)).show();
        $('a[rel=animate] > img[src*='+location.hash.substr(1)+'_p1.gif]').attr("src",function(){
            return changeNum($(this).attr("src"),2);
        });
    //page(location.hash.substr(1));
    //alert("#page_" + location.hash.substr(1));
    }
    else
    {
        $("#page_1").show();
        
        $('a[rel=animate] > img[src*=1_p1.gif]').attr("src",function(){
            return changeNum($(this).attr("src"),2);
        });
    }
    $('a[rel=animate] > img').mouseenter(function(){
        $(this).attr("src",changeNum($(this).attr("src"),2));
        if( location.hash.length == "0" )
        {
            location.hash="1";
        }
    });

    $('a[rel=animate] > img').mouseleave(function(){
        if( !($(this).attr("src").lastIndexOf( location.hash.substr(1) + "_p2.gif") != -1 && location.hash.length != "0"))
        {
            $(this).attr("src",changeNum($(this).attr("src"),1)) ;
        }
    });
    //document.getElementById("testdiv").innerHTML = location.pathname;
    //alert("href=http://test.foto-pes.cz" + location.pathname);
    $('a[href=http://www.foto-pes.cz' + location.pathname + ']').attr("rel","non-animate");
    $('a[href=http://www.foto-pes.cz' + location.pathname + '] > img').unbind('mouseenter');
    $('a[href=http://www.foto-pes.cz' + location.pathname + '] > img').unbind('mouseleave');
    $('a[href=http://foto-pes.cz' + location.pathname + ']').attr("rel","non-animate");
    $('a[href=http://foto-pes.cz' + location.pathname + '] > img').unbind('mouseenter');
    $('a[href=http://foto-pes.cz' + location.pathname + '] > img').unbind('mouseleave');
    $('a[rel=non-animate] > img').attr('src',function(){
        return changeNum($(this).attr("src"),2) ;
    });
    
    preloader();
});

function checkHash()
{
    if(  actualPage != location.hash.substr(1) && location.hash )
    {
        loc = location.hash.substr(1);
        if( loc == "" )
        {
            loc=1;
        }
        page(loc);
    }
    
}





function page(which)
{
    //alert ('a[rel*=animate] > img[src*=page'+actualPage+'_p2.gif'+which);
    $('a[rel*=animate] > img[src*='+actualPage+'_p2.gif]').attr("src",function()
    {
        return changeNum($(this).attr("src"),1) ;
    });

    $('a[rel*=animate] > img[src*='+which+'_p1.gif]').attr("src",function()
    {
        return changeNum($(this).attr("src"),2) ;
    });
    //alert(actualPage+"!="+which)
    if(actualPage!=which)
    {
        $("#page_"+actualPage).fadeOut("normal", function(){
            $("#page_"+which).fadeIn("normal");
        //alert("fade in");
        });
        actualPage=which;
        location.hash = which;
    }
//clearInterval();
//setInterval("checkHash();", 100);
}


function Book(gallery, book)
{
    //	alert( hex_md5("000") + " c6f057b86584942e415435ffb1fa93d4 " + str_md5("000"));

    //	mdvalue = hex_md5(document.getElementById("imgcheck2").value);

    if( $(".gallery_" + gallery + " #imgcheck2").attr("value") == '7' || $(".gallery_" + gallery + " #imgcheck2").attr("value") == 'sedm' || $(".gallery_" + gallery + " #imgcheck2").attr("value") == 'seven' )
    {
        if( $(".gallery_" + gallery + " #jmeno").attr("value") != "" && $(".gallery_" + gallery + " #popis").attr("value") != "" )
        {
            //alert(str_md5(mdvalue) );
            //alert($("#jmeno").attr("value"));
            $.post("index.php", {
                booksend: "1",
                name: $(".gallery_" + gallery + " #jmeno").attr("value"),
                mail: $(".gallery_" + gallery + " #email").attr("value"),
                web: $(".gallery_" + gallery + " #web").attr("value"),
                text: $(".gallery_" + gallery + " #popis").attr("value"),
                odeslano: $(".gallery_" + gallery + " #odeslano").attr("value"),
                gallery: gallery,
                imgcheck2: $(".gallery_" + gallery + " #imgcheck2").attr("value"),
                "book":book,
                first:'1'
            },function(data){
                document.getElementById("replace_" + gallery).innerHTML = data;
                $("#comments").slideUp("fast", function(){
                    $.post("index.php", {
                        lastComments:"1"
                    },function(data1){
                        document.getElementById("comments").innerHTML = data1;
                        $("#comments").slideDown("fast");
                    });
                });
            });
        }
        else
        {
            alert("Není vyplneno jméno nebo text zpravy!");
        }
    }
    else
    {
        alert("Špatně jste spočítali příklad!");
    }
}

function zobrazBook(gallery)
{
    if( $("#replace_" + gallery).is(":hidden") )
    {

        $.post("index.php",{
            'bookshow':'1',
            'gallery':gallery,
            'first':'1'
        }, function(data) {
            document.getElementById("replace_" + gallery).innerHTML = data;
            $("#replace_" + gallery).slideDown("slow");
            $('a[rel*=animate] > img').mouseenter(function(){
                $(this).attr("src",changeNum($(this).attr("src"),2));
                if( location.hash.length == "0" )
                {
                    location.hash="1";
                }
            });

            $('a[rel*=animate] > img').mouseleave(function(){
                if( !($(this).attr("src").lastIndexOf( location.hash.substr(1) + "_p2.gif") != -1 && location.hash.length != "0"))
                {
                    $(this).attr("src",changeNum($(this).attr("src"),1)) ;
                }
            });
        });
    }
    else
    {
        $("#replace_" + gallery).slideUp("slow",function(){
            $("#replace_" + gallery).hide();
        })
        
    }
}

function zobrazZbytek(gallery)
{
    //alert("#replace_" +gallery);
    //document.getElementById("zobraz_" + gallery).innerHTML = "<img src='http://www.foto-pes.cz/img/loading.gif' alt='loading...' id='load_" + gallery + "'>";

    //$("#zobraz_" + gallery).replaceWith("<img src='http://www.foto-pes.cz/img/loading.gif' alt='loading...' id='load_" + gallery + "'>");
    //$("XX<img src='/img/loading.gif' alt='loading...' id='load_" + gallery + "'>").appendTo("#replace_" + gallery);
    $.post("http://test.foto-pes.cz/index.php",{
        'bookshow':'1',
        'gallery':gallery
    }, function(data) {
        $("#zobraz_" + gallery).replaceWith(data);
    });
}


function preloader()
{
    var i = 0;
    var i2 = 0;
    // create object
    imageObj = new Image();
    // set image list
    images = new Array();

    $("a[rel*=animate] > img").each(function(){
        images[i] = changeNum($(this).attr("src"),2);
        i = i + 1;
    });
    images[i] = "/img/loading.gif";

    //var check;
    // start preloading
    for(i2=0; i2<=i; i2++)
    {
        imageObj.src=images[i2];
    //check += images[i2];
    }
//alert(check);


}

function sortNumber(a,b)
{
    return a - b;
}

