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

 $(window).bind('load', function() {
     $('.image_container').css("background-image","none");
 })

$(document).ready(function()
{
    $("#thumb_scroller").css("position", "relative");
   
   /*$('.image_container img').each(function(){
        $(this).bind('load readystatechange',function(e){
            if (this.complete || (this.readyState == 'complete' && e.type == 'readystatechange')) {
                alert("hello");
            }
        } );


    });*/
    var thumbs = new Array();
    var clicked = 0;
    var xpos = 0;
    ypos = 0;
    var pos_count = 0;
    var distance = 100

    $(".image_container").each(function(){
        thumbs.push(new thumb($(this),xpos,ypos));
        xpos += distance;
    });
    for(x=thumbs.length-1;x>-1;x--)
    {
        thumbs[x].showMe(x);
        thumbs[x].ele.click(function(){
            showItem(thumbs,this);
        });
    }
    var rand = Math.floor(Math.random()*thumbs.length);
    thumbs[rand].showProject();
    if(rand < thumbs.length-4)
    {
        pos_count = rand;
        call_scroll_left();
    }else
    {
        pos_count = thumbs.length-4;
        call_scroll_left();
    }

    $('img').unbind();
    $("#right_arrow").click(function(){
            pos_count++;
            newxpos = thumbs[pos_count%thumbs.length].ele.position().left+(distance*3);
            $(thumbs[(pos_count+3)%thumbs.length].ele).css("left", newxpos);
            call_scroll_left();
    });

    $("#left_arrow").click(function(){
        newxpos = thumbs[((pos_count % thumbs.length) + thumbs.length) % thumbs.length].ele.position().left-distance;
            pos_count--;
            
            
            $(thumbs[((pos_count % thumbs.length) + thumbs.length) % thumbs.length].ele).css("left", newxpos);
            call_scroll_left();
    });

    function call_scroll_left()
    {
        $("#thumb_scroller").animate({left:-(pos_count*distance)},1000,function(){});
    }
});

function showItem(thumbs,i)
{
    src = $(i).children("img").attr("src");
    for(x=thumbs.length-1;x>-1;x--)
    {
        thumbs[x].moveMe(x*100);
        if(src == thumbs[x].src)
        {
            thumbs[x].showProject();
        }
    }
}

function openAni(thumbs)
{
    /*$(this).fadeIn(1500);
    $(this).animate({left:xpos}, 1000,function(){
        $(this).children(".title").slideDown("slow");
    });
    xpos += 100;*/
}

function thumb(ele, xpos, ypos)
{

    this.ele = ele;
    this.src = ele.children("img").attr("src");
    this.type = ele.children("img").attr("alt");
    this.link = String(ele.children("img").attr("src")).split("/");
    this.link[3] = "large";
    this.link = this.link.join("/");
    this.par = this.ele.parent();
    this.ypos = ypos;
    this.xpos = xpos;
    this.ele.children(".title").css("display","none");
    //this.ele.css("top",ypos);
    //alert($(ele).children("img").get(0));
    //DD_belatedPNG.fixPng( $(ele).children("img").get(0));
    this.showMe = function(ypos)
    {
        $(this.ele).css("left", xpos);
    }
    this.moveMe = function(newX)
    {

        /*this.ele.animate({top:290,left:newX,duration:3000}, function(){
            $(this).children(".title").slideUp("slow");
            $(this).css("background-image","none");
            $("#media_stage").css("background-image","url(../../../images/thumb_bottom_holder.jpg)");

        });*/
    }

    this.showProject = function()
    {
        if(this.type == "image")
        {
            $(".media_large").html("<img class='unitPng' src='"+this.link+"' />");
        }
        else
        {
            $(".media_large").load(this.link);
        }
    }

    return true;
}



function scroll_left(pos)
{
    
}