//DON'T STEAL THIS SCRIPT

var imageList=new Array();
var currentLoading = 0;
var loadingImage = false;
var loadingTimer = 0;
var imageMaxHeight = 0;
var imgLinks = new Array();

function setImageBoxHeight(h){
    if(h>300){
        document.getElementById('image').style.height=h+'px';
        h+=250;
        document.getElementById('main').style.height=(h)+'px';
    }
}
function loadPhoto(){
    if(loadingImage){
        divimg[currentLoading].src = loadingImage.src;
        currentLoading++;
    }
    
    
    if(currentLoading<divimg.length){
        
        loadingImage=new Image();
        
        loadingImage.onload=loadPhoto;
        
        loadingImage.onerror=function(){
            
            var s=loadingImage.src.split('.');
            var t=s.pop();
            if(t=='jpg'){
                loadingImage.onload=loadPhoto;
                loadingImage.src=s.join('.')+'.jpeg';
            }
        };
        
        
        var s=divimg[currentLoading].src;
        s=s.split('-');
        if(s.length>1){
            s.pop();
        }
        s=s.join('-');
        s+='.jpg';
        loadingImage.src=s;
        
    }
    
}

var dots;
var photos;
var currentPhoto = 0;

function webkit1_movePhotos(x){
    photos.style['-webkit-transform']='translate('+x+'px, 0px)';
}

function webkit2_movePhotos(x){
    photos.style.webkitTransform='translate('+x+'px, 0px)';
}

function moz_movePhotos(x){
    photos.style.MozTransform='translate('+x+'px, 0px)';
}

function o_movePhotos(x){
    photos.style['-o-transform']='translate('+x+'px, 0px)';
}

function ms_movePhotos(x){
    photos.style['-ms-transform']='translate('+x+'px, 0px)';
}

function css3_movePhotos(x){
    photos.style.transform='translate('+x+'px, 0px)';
}

function jQuery_movePhotos(x){
    jQuery(photos).animate({left:x+'px'});
}

var movePhotos = false;

function motion_init(){
    if(photos.style.webkitTransform!=undefined)
        movePhotos=webkit2_movePhotos;
    else if(photos.style.MozTransform!=undefined)
        movePhotos=moz_movePhotos;
    else if(photos.style['-webkit-transform']!=undefined)
        movePhotos=webkit1_movePhotos;
    else if(photos.style['-o-transform']!=undefined)
        movePhotos=o_movePhotos;
    else if(photos.style['-ms-transform']!=undefined)
        movePhotos=ms_movePhotos;
    else if(photos.style.transform!=undefined)
        movePhotos=css3_movePhotos;
    else{
        load_jQuery();
        movePhotos=jQuery_movePhotos;
    }
}


function setCurrentPhoto(p){
    dots.childNodes[currentPhoto].className=null;
    currentPhoto=p;
    
    if(currentPhoto>=imageList.length){
        currentPhoto=0;
    }
    
    dots.childNodes[currentPhoto].className='current-dot';
    
    slideshowPosition=-currentPhoto*600;
    movePhotos(slideshowPosition);
}

function photos_press(){
    photos.className='auto-motion';
    
    setCurrentPhoto(currentPhoto+1);
    
}

var touchMoved=false;
var touchStartEvent=[0,0];
var touchStartEventObject = 0;
var slideshowPosition = 0;
var touchCanceled = false;

function photos_touchstart(e){
    touchCanceled=touchMoved=false;
    touchStartEventObject=slideshowPosition;
    //e.preventDefault();
    touchStartEvent[0]=e.touches[0].pageX;
    touchStartEvent[1]=e.touches[0].pageY;
}

var slideshow_drag_dif = 0;

function load_jQuery(){
    var fileref=document.createElement('script')
    fileref.setAttribute("type","text/javascript")
    fileref.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js");
    document.getElementsByTagName("head")[0].appendChild(fileref);
}

function photos_touchmove(e){
    if(touchCanceled)
        return;
    
    if(!touchMoved){
        touchMoved=true;
        photos.className=null;
    }
    
    
    var x=e.touches[0].pageX;
    var y=e.touches[0].pageY;
    
    if( Math.abs(x-touchStartEvent[0]) < Math.abs(y-touchStartEvent[1]) ){
        touchCanceled=true;
    }else{
        e.preventDefault();
        
        var dif=x-touchStartEvent[0];
        
        slideshow_drag_dif=(touchStartEventObject + dif)-slideshowPosition;
        
        slideshowPosition = touchStartEventObject + dif;
        
        if(slideshowPosition>0){
            slideshowPosition/=2;
        }else{
            var w = divimg[divimg.length-1].offsetParent.offsetLeft+divimg[divimg.length-1].offsetParent.offsetWidth;
            var ww = w-580;
            if(slideshowPosition<-ww){
                var s = -slideshowPosition;
                s-=ww;
                s/=2;
                slideshowPosition=-(ww+s);
            }
            
        }
        movePhotos(slideshowPosition);
    }
}

function photos_touchend(e){
    if(touchCanceled)
        return;
    var move=true;
    
    if(!touchMoved){
        // photos_press();
    }else{
        if(slideshowPosition>0){
            slideshowPosition=0;
            photos.className='correct-motion';
        }else{
            
            var w = divimg[divimg.length-1].offsetParent.offsetLeft+divimg[divimg.length-1].offsetParent.offsetWidth;
            var ww = w-580;
            if(slideshowPosition<-ww){
                photos.className='correct-motion';
                slideshowPosition=-(ww);
            }else{
                photos.className='correct-motion';
                if(slideshow_drag_dif<-5){
                    if(currentPhoto<(divimg.length-1)){
                        setCurrentPhoto(currentPhoto+1);
                        move=false;
                    }else{
                        slideshowPosition=-currentPhoto*600;
                    }
                }else if(slideshow_drag_dif>5){
                    if(currentPhoto>0){
                        setCurrentPhoto(currentPhoto-1);
                        move=false;
                    }else
                        slideshowPosition=0;
                }else{
                    var i=-Math.round(slideshowPosition/600);
                    setCurrentPhoto(i);
                    move=false;
                }
                
            }
        }
    }
    if(move)
        movePhotos(slideshowPosition);
    e.preventDefault();
}

var divimg=new Array();

function init(){
    if(category){
        var n=document.getElementById('nav');
        var nlst=n.getElementsByTagName('LI');
        //alert(nlst.length);

        for(var i=0;i<nlst.length;i++){
            if(nlst[i].firstChild.firstChild.nodeValue==category){
                nlst[i].className+=' current-menu-item';
            }
        }
    }
    dots=document.getElementById('dots');
    photos = document.getElementById('photos');
    var removes=new Array();
    var divs=document.getElementsByTagName('A');
    // if(photos){
    for(var i=0;i<divs.length;i++){
        if(photos && divs[i].rel && divs[i].rel.match(/attachment/gi)){
            var small_img=divs[i].firstChild;
            
            var nimg=new Image();
            nimg.src=small_img.src;
            
            
            var t=small_img.src;
            t=t.split('-');
            if(t.length>2){
                t.pop();
                t = t.join('-');
                t=t+'.jpeg';
            }
            imageList.push(t);
            
            
            if(dots){
                
                var img=new Image();
                img.src=wp_theme+'/dot.png';
                if(imageList.length==1){
                    img.className='current-dot';
                }
                dots.appendChild(img);            
            }
            
            var d=document.createElement('DIV');
            d.className='slideshow-image';
            d.style.left=((divimg.length*600))+'px';
            d.appendChild(nimg);
            
            var w=nimg.width;
            var h = Math.floor((580/w)*nimg.height);
            if(h>imageMaxHeight){
                imageMaxHeight=h+70;
                setImageBoxHeight(imageMaxHeight);
            }
            nimg.style.height=Math.floor(h)+'px';
            
            photos.appendChild(d);
            imgLinks.push(divs[i].href);
            divimg.push(nimg);
            removes.push(divs[i]);
            
        }else if(divs[i].className && divs[i].className.match(/postlink/gi)){
            while(divs[i].childNodes.length){
                divs[i].removeChild(divs[i].childNodes[0]);
            }
            var d=document.createElement('DIV');
            d.className='hit';
            divs[i].appendChild(d);
        }
        
    }
    if(dots && dots.childNodes.length<2){
        dots.parentNode.removeChild(dots);
    }
    // }
    for(var i=0;i<removes.length;i++){
        removes[i].parentNode.removeChild(removes[i]);
    }
    if(photos){
        if(photos.addEventListener){
            
            photos.parentNode.addEventListener('click',photos_press,false);
            photos.addEventListener('touchstart',photos_touchstart,false);
            photos.addEventListener('touchmove' ,photos_touchmove,false);
            photos.addEventListener('touchend'  ,photos_touchend,false);
        }else{
            photos.onclick=function(){photos_press();};
        }
    }
    motion_init();
    //loadPhoto();
    setTimeout('loadPhoto()',100);
}

