/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function(){
    var loadTime = 100;
    var imgCount = $(".imgCont").size();
    var marginTop = 0;
    var totalHeight = Math.ceil(imgCount/5)*80;
    $("#galNav").height(totalHeight);
    var steps = parseInt(totalHeight/80/4);
    var lastStep = totalHeight-(320*steps);
    var step = 1;

    //Description animation

    $("#galImgOver").click(function(){
        $("#galImgWrap").animate({"marginTop": "220px"},1000,"easeOutCirc");
    });
    $("#galImgOver").hover(function(){ return true },
    function(){
        $("#galImgWrap").animate({"marginTop": "318px"},1000,"easeOutCirc");
    });
    //Hide up arrow
    $("#galUp img").hide();
    /* Preloading all images and fadeIn one by one*/

        $.preload('.imgCont img',{
            onFinish:function(){
                $("#galLoadingLeft").remove();
                $(".imgCont").each(function(i){
                    $(this).animate({opacity:1.0},200+(i*loadTime),function(){$(this).fadeIn("slow")});
                });
            }
            } 
    );

    /*   On hover effect   */
    $(".imgCont").hover(function(){
        var alt = $(this).find("img").animate({width:"46px",height:"52px",padding:"4px"},100).attr("alt");
        $("#galFooter span").text(alt);
    }, function(){
        $(this).find("img").animate({width:"54px",height:"60px",padding:"0"},100);
    });

    /*  Loading of the big image  */
    $(".imgCont").click(function(){
        $("#galRight #galHolder").empty();
        $("#galLoading").show();
        var url = $(this).find("img").attr("src").split("image=");
        var desc = $(this).find(".description").text();
        var img = $("<img />").attr("src",wpUrl+"/wp-content/themes/sanchez/image_res.php/gal-big.jpg?width=356&cropratio=1.1:1&image="+url[1]);
        $.preload(img,{
            onFinish:function(){
                $("#galLoading").hide();
                $("#galRight #galHolder").html(img);
                $("#galTextOver span#text").text(desc);
            }
            });
    })
    /* Slide to next Photos */
    $("#galDown").click(function(){
        if((step < steps)){
            marginTop = marginTop - 320;
            $("#galNav").animate({"marginTop": marginTop+"px"},1500,"easeOutCirc");
            step++;
        }else if(steps == step){
            marginTop = marginTop-lastStep;
            $("#galNav").animate({"marginTop": marginTop+"px"},1500,"easeOutCirc");
            step++;
            $("#galDown img").hide();
        }
        if(step > 1){
            $("#galUp img").show();
        }
    }); 

    /* Slide to last Photos */
    $("#galUp").click(function(){
        if((step > steps)){
            marginTop = marginTop+lastStep;
            $("#galNav").animate({"marginTop": marginTop+"px"},1500,"easeOutCirc");
            step--;
        }else if((step == steps)&&(step != 1)){
            marginTop = marginTop + 320;
            $("#galNav").animate({"marginTop": marginTop+"px"},1500,"easeOutCirc");
            step--;
        }

        if(step <= steps){
            $("#galDown img").show();
            if(step == 1){
            $("#galUp img").hide();
            }
        }
    })
});

$.fn.image = function(src, f){
    return this.each(function(){
        var i = new Image();
        i.src = ""
        this.appendChild(i);
    });
}

