// Generic
function roll_over(img_name, img_src) {
   document[img_name].src = img_src;
}

// Voting
var max_amount = 5;
var vote_cache = new Array()
var img_cache = new Array()

function fiddle(id, amount) {
    var up_img = new Array()
    amount =  parseInt(amount)

    for(i=0;i<max_amount+1;i++) {
        eval("up_img[i]=document.images.i"+id+"i"+i)
    }
    for(i=1;i<max_amount+1;i++) {
        eval("up_img[-i]=document.images.i"+id+"in"+i)
    }    
    

    if (amount == 0) {
        wipe(up_img)
        up_img[0].className="a_vote"
    }
    else if (amount > 0) {
        wipe(up_img)
        for(i=1;i<amount+1.0;i++) {
            up_img[i].className="a_vote"
        }
    }
    else if (amount <0) {
        wipe(up_img)
        for(i=amount;i<0;i++) {
            up_img[i].className="a_vote"
        }    
    }
}

function wipe(img_array) {
    for(i=-max_amount;i<max_amount+1;i++) {
        img_array[i].className="i_vote"
    }
}


function vote(id, amount) {

    if (vote_cache[id] == amount) {
        return 1
    }
    vote_cache[id] = amount

    junk = Math.round(Math.random()*100000)

    new Ajax.Request('/'+id+'/vote/'+type+'/'+amount+'/'+junk+"/", {
        method:'get',
        onSuccess:function(transport){
            response = transport.responseText
            eval(response)
            amount = data['score']
        }
    })
    fiddle(id, amount)
}

function redirect(url) {
    window.location=url
}

function make_voting(id) {
    junk = Math.round(Math.random()*100000)


    for(i=max_amount;i>0;i--) { 
        document.write('<img src="http://www.imageboard.net/assets/votedown.png" class = "i_vote" name="i'+id+'in'+i+'" onClick="vote('+id+', '+-i+')">')
    }
    document.write('<img src="http://www.imageboard.net/assets/cancel.png" class = "i_vote" name="i'+id+'i0" onClick="vote('+id+', 0)">')
    for(i=1;i<max_amount+1;i++) { 
        document.write('<img src="http://www.imageboard.net/assets/voteup.png" class = "i_vote" name="i'+id+'i'+i+'" onClick="vote('+id+', '+i+')">')
    }
    amount = 0
    new Ajax.Request('/'+id+'/rank/'+type+'/'+junk+"/", {
        method:'get',
        onSuccess:function(transport){
            response = transport.responseText
            eval('amount = '+response)
            fiddle(id, amount)
            vote_cache[id] = amount
        }
    })    
}

function fake_voting(id) {
    for(i=max_amount;i>0;i--) { 
        document.write('<img src="http://www.imageboard.net/assets/votedown.png" class = "i_vote" onClick="redirect(\'/accounts/login/\')">')
    }
    document.write('<img src="http://www.imageboard.net/assets/cancel.png" class = "i_vote" onClick="redirect(\'/accounts/login/\')">')
    for(i=1;i<max_amount+1;i++) { 
        document.write('<img src="http://www.imageboard.net/assets/voteup.png" class = "i_vote" onClick="redirect(\'/accounts/login/\')">')
    }
}

// Latest Page
var limit = 5
var reload_period = 30000
var last_id = 1

function update() {
    var response = ""
    new Ajax.Request('/latest/pull.js/'+limit+'/'+last_id+'/', {
        method:'get',
        onSuccess:function(transport){
            response = transport.responseText
            response = "var list = " + response
            eval(response)
            last_id = list[0]["pk"]
            
            var img_str = "";
            var href_str = "";
            for (var i=0; i < limit; i++) {
                var j = parseInt(i)+5
                href_str += 'document.links['+j+'].href = "/" + list["'+i+'"]["pk"] + "/";'             
                img_str += 'document.images.i'+i+'.src = list["'+i+'"]["fields"]["url"];'
            }
            
            eval(img_str);
            eval(href_str);

        }
    })

    window.setTimeout(update,reload_period)
}

// Slideshow 
var posts = new Array()
var preloads = new Array()
var period = 1000
var counter = 0

function init() {
    for (i=0; i < posts.length;i++) {
     preloads[i] = new Image()
     preloads[i].src = posts[i]
    }
}

function step() {  

 if (counter <0 ) {
  counter = posts.length-1
 }

 document.images.slideshow.src=posts[counter]
 counter--
 window.setTimeout(step,period)
}

function change_speed(delta) {
    period += delta
}