var isiPad = navigator.userAgent.match(/iPad/i) != null;

(function($) {

function cssTrans(clas, props, duration) {
    var animdur = duration;
    var aminz = props;    

    $("style").append("."+clas+" { "+"transition-property: "+aminz+";transition-duration: "+animdur+"s; transition-timing-function: ease-out;-webkit-transition-property: "+aminz+";-webkit-transition-duration:"+animdur+"s;-moz-transition-property: "+aminz+";-moz-transition-duration: "+animdur+"s;-o-transition-property: "+aminz+";-o-transition-duration: "+animdur+"s;"+"}\n\n");
}

$.fn.thumbSlider = function(fotox, fld, path, thumbdim, hilightdim, loadf, clickf, inf, outf) {

    var animdur = 0.05;
    var aminz = "width opacity";

/*
    $("style").append(
    ".thumb { "+"opacity: 0.5;"
    + "position:relative;"
    + "float: left;"
    + "}"
+"\n\n.highlight_thumb {height: "+hilightdim.height+"px;width: "+hilightdim.width+"px;top: -"+((hilightdim.height - thumbdim.height)/2)+"px;opacity: 1;}"
    
    );*/
    //cssTrans("thumb", "width opacity", 0.05);
    //cssTrans("slider_inner", "left", 0.5);

    return $(this).each(function() {
        var loaded = 0;
        var amt = fotox.length;
        var slid = $(this);
    
        $(this).addClass("slider_outer");
        $(this).append('<div class="slider_inner"></div>');
        var slider_inner = $(this).find(".slider_inner");
        
        //slider_inner.width(thumbdim.width*(amt+1)+"px");
        
        var slider_outer = $(this);
        for (var x = 0; x < fotox.length; x++) {
            
            var foto = path + fotox[x][fld];
            
            
            var img = '<div class="thumbhold"><img src="'+foto+'" height="'+thumbdim.height+'" width="'+thumbdim.width+'" class="thumb" id="thumb'+x+'"></img></div>';
            //var ht = '<div class="thumbholder" id="thumbholder'+x+'">'+img+'</div>';
            var ht = img;
            $(this).find(".slider_inner").append(ht);
            //$('#thumbholder'+x).css("position", "relative").css("float", "left")
            //$('#thumb'+x).css("position", "relative").css("float", "left")
            //.css("left",thumbdim.width*x ).css("top", 0);
            $('#thumb'+x).load(function() {
                loaded++;
                if (loadf) 
                    if (loaded == amt)
                        loadf(1);
                    else
                        loadf(loaded*1.0/amt);
            });
            $('#thumb'+x).data("numex", x);
            $('#thumb'+x).click(function() {
                
                var zon =x;
                if (clickf)
                    clickf($(this).data("numex"));
            });
        } 
        $(".thumbhold").css("background-color", "black");
        
        var thumblen = $(".thumb").length;
        
        
        function highlight() {
            $(".thumb").removeClass("highlight_thumb");
            //$(".thumb").attr("style", "");
            //$(".thumb").css("top", 0).css("width", ""+152+"px").css("height",""+121+"px");
            
            //$(".thumbholder").removeClass("highlight_thumb");
            $(this).addClass("highlight_thumb");
           // $(this).parent().addClass("highlight_thumb");
        }
        
        function highlight2() {
            $(".thumb").removeClass("highlight_thumb");
            //$(".thumb").attr("style", "");
            $(".thumb").css("top", 0).css("width", ""+thumbdim.width+"px").css("height",""+thumbdim.height+"px");
            
            //$(".thumbholder").removeClass("highlight_thumb");
            $(this).addClass("highlight_thumb");
           // $(this).parent().addClass("highlight_thumb");
        }
        
        var t0 = (new Date).getTime();
        
        var v = 0;
        var a = 0;
        var x = 0;
        var x0 = 0;
        function process(t) {
            x = v*t + 0.5*a*t*t;
            a = x0 - x;
            
            v = x/t - 0.5*a*t;    
        }
        
        function processX() {
            var tt = (new Date).getTime();
            var elapse = tt - t0;
            t0 = tt;
            var t = (elapse*1.0)/1000;
            
            process(t);
            
            $(".kex").html("v = "+v + "  <br/> x = "+ x + " <br/>  a = "+a + " <br/>  t = "+t+ " <br/>  x0 = "+x0);
            setTimeout("processX()",1000/24);    
        }
        
        window.processX = processX;
        
        //setTimeout("processX()",1000/24);
        
        
        var hilight = -1;
  
        var acc = 0;
        function hilitrel(xx,x, bodyper,stripepos) {
                x = (-slider_inner.offset().left + xx) / thumbdim.width;


                x0 = bodyper;
                

                x = Math.floor(x);

                var difh = (hilightdim.height - thumbdim.height);
                var difw = (hilightdim.width - thumbdim.width);

                //var drobs = $("#thumb"+x);
                 
                //$(".kex").html(""+xop);

                //highlight.call(drobs);
                var durr = 100;
                //return;
                 
                if (hilight != x) {
                    if (hilight > -1) {
                        //$("#thumb"+hilight).attr("style", "").css("opacity", 0.5);
                        /*$("#thumb"+hilight).attr("style", "")
                            .width(thumbdim.width)
                            .height(thumbdim.height)
                            .css("top", 0)
                            .css("opacity", 0.5);
                        $("#thumb"+hilight).parent().css("top", 0);*/
                        
                        $("#clonexm").remove();

                        /*$("#clonexm").attr("id", "konixx"+acc).children(":first").attr("id", "konax"+acc);
                        var zorb = acc
                        $("#konixx"+acc).animate({top: 0,left: -( ( thumblen -hilight )*thumbdim.width) }, durr, function() {                        $(this).remove()});
                        $("#konax"+acc).animate({opacity: 0.5, width: thumbdim.width, height:thumbdim.height}, durr);
                        acc++;*/
                    }
                    /*$("#thumb"+x).parent().clone(true).attr("id", "clonexm")
                    .children(":first").attr("id","clonexk").unbind("load").removeClass("thumb").parent()                
                    .insertAfter($("#thumb"+x).parent());
                    $("#clonexm").css("position", "absolute");*/

                    var displ =  ( ( thumblen -x )*thumbdim.width);                    
                    $("#thumb"+x).parent().clone(true).attr("id", "clonexm")
                    .children(":first").attr("id","clonexk").unbind("load").removeClass("thumb").parent().css("position", "relative").css("left", -displ  )
                                        .appendTo(slider_inner);

                  

                    
                    $("#clonexk").animate({opacity: 1, width: hilightdim.width, height:hilightdim.height}, durr);
                    $("#clonexm").css("z-index", 1000).animate({top: - difh/2,left: -( ( thumblen -x )*thumbdim.width) - (difw/2) }, durr);                        
                }
                    
                hilight = x;
                
                
            
                
               /* if ($.browser.msie) {
                    $("#clonexk").animate({opacity: 1 }, durr);
                    $("#clonexk").css({width: hilightdim.width, height:hilightdim.height});
                    $("#clonexm").css("z-index", 1000).css("left",((hilight)*thumbdim.width)  - difw/2).css({top: - difh/2 });
                } else {
                
                         
                }*/
        }
        
        function hilit(xx,x, bodyper,stripepos) {
                x = (-slider_inner.offset().left + xx) / thumbdim.width;


                x0 = bodyper;
                

                x = Math.floor(x);

                var difh = (hilightdim.height - thumbdim.height);
                var difw = (hilightdim.width - thumbdim.width);

                var drobs = $("#thumb"+x);
                 
                //$(".kex").html(""+xop);

                //highlight.call(drobs);

                //return;
                if (hilight != x) {
                    if (hilight > -1) {
                        //$("#thumb"+hilight).attr("style", "").css("opacity", 0.5);
                        /*$("#thumb"+hilight).attr("style", "")
                            .width(thumbdim.width)
                            .height(thumbdim.height)
                            .css("top", 0)
                            .css("opacity", 0.5);
                        $("#thumb"+hilight).parent().css("top", 0);*/
                        
                        $("#clonexm").remove();

                        
                    }
                    $("#thumb"+x).parent().clone(true).attr("id", "clonexm")
                    .children(":first").attr("id","clonexk").unbind("load").removeClass("thumb").parent()                
                    .insertAfter($("#thumb"+x).parent());
                    $("#clonexm").css("position", "absolute");
                
                }
                    
                hilight = x;
                
                
                var durr = 100;
                
                /*if ($.browser.msie) {
                    $("#clonexk").animate({opacity: 1 }, durr);
                    $("#clonexk").css({width: hilightdim.width, height:hilightdim.height});
                    $("#clonexm").css("z-index", 1000).css("left",((hilight)*thumbdim.width)  - difw/2).css({top: - difh/2 });
                } else {*/
                
                    $("#clonexk").animate({opacity: 1, width: hilightdim.width, height:hilightdim.height }, durr);
                    $("#clonexm").css("z-index", 1000).css("left",((hilight)*thumbdim.width)  - difw/2).animate({top: - difh/2 }, durr);         
                //}
        }
        var fac = -1.0;
        //if (navigator.appVersion.indexOf("MSIE") != -1) fac = 1.0;
        
        var speedx = 0;
        var limin = 0;
        var limout = (($(".thumb").length * thumbdim.width)  - slider_outer.width());
        thumb = $(".thumb");
        
        var lastev = null;
        
        function mousemovesidez(ev) {
           
            if (ev)
                lastev = ev;
            else
                ev = lastev;
            var xx = ev.clientX;
            limout = ((thumb.length * thumbdim.width) - width);
            var relx = slider_outer.offset().left;
            var width = slider_outer.width(); 
                     
            xx = xx - relx;
            
            if (xx <0) xx = 0;
            if (xx > width) xx = width;
            
 
            
            
            var per = (xx*1.0) / width;
            
            var perx = per - 0.5;

            var tol = 0.1;

            if (Math.abs(perx) < 0.1  ) perx = 0;
            
            
            var strength = Math.abs(Math.cos(perx*3.1415));
            strength = strength*strength*strength;
            
            
            var minspeed = 1/120000;
            var maxspeed = 1; 
            
            var mindur = 8000;
            var maxdur = 200000;
            
            var dur = mindur + (strength*maxdur);
            

            
            var bodyper = 0;
            if (perx > 0) bodyper = 1;

            var widdist = (- slider_inner.offset().left +relx)/((thumb.length * thumbdim.width) - width);
            
            

            dur = dur * Math.abs(widdist-bodyper);
            
            //speedx = ((widdist-bodyper)*(($(".thumb").length * thumbdim.width) - width))/dur;
            
            if (bodyper == 1) {
                var poss = (- slider_inner.offset().left +relx) - ((thumb.length * thumbdim.width) - width); 
                speedx = poss/dur;
            } else {
                var poss = (- slider_inner.offset().left +relx); 
                speedx = poss/dur;
            }
            
            
            var stripepos =  ((thumb.length * thumbdim.width) - width)*bodyper;    
            var strikex = stripepos ;
                    
            stripepos= Math.floor(stripepos);
            //slider_inner.css("left", -stripepos); 

            var x = (stripepos + xx) / thumbdim.width;


            


            slider_inner.stop(true);
            
            if (perx != 0 )
                        slider_inner.animate({left: fac*stripepos}, {duration:dur, easing:'linear', step: function() {hilitrel(ev.clientX,x,bodyper,stripepos);}} );
            else {
                //hilit(ev.clientX,x,bodyper,stripepos);
                hilitrel(ev.clientX,x,bodyper,stripepos);
            }
            //$(".kex").html("x = "+xx + "  <br/> perc = "+ perx + " <br/>  widdist = "+ widdist + " <br/>  strength = "+strength+ " <br/> dur= "+dur+ " <br/> spid= "+speedx);

        }
        
        
        function mousemovex(ev) {
       
                //var bodyper = ev.clientX/slid.width();
                
                var xx = ev.clientX;

                xx = xx - (thumbdim.width/2); 

                if (xx < 0) xx=1;
                if (xx > $("body").width() - (thumbdim.width) ) xx=$("body").width() - (thumbdim.width);
                
                var lenn = $("body").width() - (thumbdim.width);
                
                var bodyper = (xx)/lenn;
                
                //$(".kox").html(".." +bodyper);
                //$(".thumb").removeClass("highlight_thumb");
                /*if (bodyper > 0.5) 
                    bodyper = Math.sqrt(bodyper);// * bodyper;    
                else*/
                    //bodyper = bodyper * bodyper * bodyper;
                var stripepos =  (($(".thumb").length * thumbdim.width) - $("body").width())*bodyper;    
                var strikex = stripepos ;
                    
                stripepos= Math.floor(stripepos);
                //slider_inner.css("left", -stripepos); 

                var x = (stripepos + xx) / thumbdim.width;

                slider_inner.stop(true).animate({left: fac*stripepos}, {duration:5000, easing:'easeOutQuad', step: function() {hilit(ev.clientX,x,bodyper,stripepos);}} );

                //hilit(ev,x, bodyper);
                return;

           
        }
        
       
        
        if (!isiPad)
            $(".thumb").mousemove(mousemovesidez);
        else {
            
            $("body").bind('touchmove', BlockMove);    
        }
        
        
        
        slider_outer.hover(function() {
            if (inf) inf();
        }, function() {
           
            $("#clonexm").remove();
            var stopin = 1000;
            var displax = 500;
            var xix = "-";
            var durre = Math.abs(speedx/4)*stopin;
            
            if (durre < limin)
                durre = limin;
            if (durre > limout)
                durre = limout; 
            if (speedx > 0) xix = "+";
            
            slider_inner.stop(true).animate({left: xix+"="+durre+"px"}, stopin, 'easeOutQuad', function() {if (outf) outf()});
             $(".kex").html("DUREX " + durre + "<br/>spid" + speedx );
        });
        
        
        $(window).resize(function() {
            if ( -(slider_inner).offset().left > ((thumb.length * thumbdim.width) - slider_outer.width()) )
                slider_inner.css("left", -((thumb.length * thumbdim.width) - slider_outer.width()) );
        });
        
    });   
}

//ontent.albuns[0].pasta_fotos + content.albuns[0].fotos[picno].file;
return;
$("#slidex").slidex(content.albuns[1].fotos, "file",content.albuns[1].pasta_thumbs, {width:121, height:96},{width:190, height:150} , function(xx) {$(".kox").html(xx)} );


})(jQuery);

var leftyy = 0;

 function onTouchMove(event) {

    if (!isiPad) return;
  // Tell Safari not to move the window.
    //window.mousemovex({clientX : event.targetTouches[0].clientX});
    var konak = event.targetTouches[0].clientX;
    var diff = konak - leftyy;
    leftyy = konak;
    var lefty =     $(".slider_inner").offset().left;
    $(".slider_inner").css("left", "-="+diff);
  event.preventDefault() ;
 }



