$(function(){
    // Collapse
    
    openImg();
      
    $('#portfolio section dl dd a').click(function(e){
        e.preventDefault(); e.stopPropagation();
        
        if( history.pushState ){
        
            var stateObj = { foo: "bar" };
            history.pushState( stateObj , $( this ).text() , $( this ).attr('href') );
            
        }
        
        $('#portfolio section dl dd a,#portfolio section dl dt a').css('color','');

        $(this).parents('dl').find('dt a').css('color','#115D82');
        $(this).css('color','#115D82');

        imgUrl = $(this).attr('href').replace('http://www.wellington.art.br/desenhos/','').replace('.html','');
        texto = $(this).text();

        imgName = imgUrl;
        
        $('div#contentJob').html('<p id="loadAll">Carregando...</p>');
		$('div#content h1').html('Desenhos - ' + texto);
        $.ajax({
            type: "GET",
            url: "desenhos.html",
            data: "img=true&imgName=" + imgName + "&imgTitle=" + texto,
            success: function(msg){
                $('div#contentJob').html(msg);
                $('div#imgThumbs a').click(function(e){
                    e.preventDefault(); e.stopPropagation();
                    imgUrl = $(this).attr('href').replace('http://www.wellington.art.br/desenhos/','').replace('.html','');
                    texto = $(this).text();
                    imgName = imgUrl;
                    $('div#imgMaster').html('<p id="loadAllBig">Carregando...</p>');
                    $.ajax({
                        type: "GET",
                        url: "desenhos.html",
                        data: "imgBig=true&imgUrl=" + imgUrl + "&imgTitle=" + texto,
                        success: function(msg){
                            $('div#imgMaster').html(msg);
                        }
                    });
                })
            }
        })
    })
    
})

function openImg(){

    urlPlace = window.location;
    if( urlPlace == "http://www.wellington.art.br/desenhos.html" || urlPlace == "http://wellington.art.br/desenhos.html"){
        urlPlace = "alien";
    }else{
        urlPlace = urlPlace.toString().replace('http://www.wellington.art.br/desenhos/','').replace('.html','');
    }

    $('#portfolio section dl dd').hide();
    
    img = { imgName : urlPlace, imgTitle : "Alien" };

    if(urlPlace){
        img.imgTitle = img.imgName;
    }else{
        img.imgName = 'alien';
        img.imgTitle = 'Alien';
        $('#portfolio section dl dt a').eq(0).addClass('open').css('color','#115D82');$('#portfolio section dl dd a').eq(0).css('color','#115D82');
        $('#portfolio section dl').eq(0).find('dd').show();
    }
    
    $('#portfolio section dl dt a').click(function(e){
        e.preventDefault(); e.stopPropagation();
        
        $('#portfolio section dl dd').hide(); 
        $('#portfolio section dl dt a').addClass('close').removeClass('open');
        
        if( $(this).attr('class') == "close" ){
            $(this).addClass('open').removeClass('close');
            $(this).parents('dl').find('dd').show();
        }else if( $(this).attr('class') == "open" ){
            $(this).addClass('close').removeClass('open');
            $(this).parents('dl').find('dd').hide();
        }
    })
    
    $.ajax({
        type: "GET",
        url: "desenhos.html",
        data: "img=true&imgName=" + urlPlace + "&imgTitle=" + img.imgTitle,
        success: function(msg){
            $('div#contentJob').html(msg);
            $('div#imgThumbs a').click(function(e){
                e.preventDefault(); e.stopPropagation();
                    imgUrl = $(this).attr('href').replace('http://www.wellington.art.br/desenhos/','').replace('.html','');
                    texto = $(this).text();
                    imgName = imgUrl;
                $('div#imgMaster').html('<p id="loadAllBig">Carregando...</p>');
                $.ajax({
                    type: "GET",
                    url: "desenhos.html",
                    data: "imgBig=true&imgUrl=" + imgUrl + "&imgTitle=" + texto,
                    success: function(msg){
                        $('div#imgMaster').html(msg);
                    }
                });
            })
        }
    });

}

window.onpopstate = function(event) {

    //console.info( "state: " + JSON.stringify( event.state ) );
    $('div#content h1').html('Portfolio / Desenhos');
    openImg();
    
};
