try {

    var currentHeightTarget  = 0;
    var scale,hash;
    var processActive = false;
    var domain = 'http://'+document.domain.toString();
    var pathlink = document.location.toString().split('#');
    var path = document.location.pathname.toString();
    var pageStack = new Array();


    var blocks;
    var sourceBlock;
    var pageView = false;
    
    checkPath = function() {
        if(pathlink[1]) {
            
            $('content').set('opacity','0');            
            var req = new Request.HTML({

                            url:pathlink[1].replace('/view/','/xhr/'), 
                            urlEncoded: false,
                            onSuccess: function(html) {
                            $('content').set('text', '');
                            $('content').style.height = '0px';
                            //Clear the text currently inside the results div.
                            $('content').adopt(html);
                            $('loading').style.display = 'none';
                            checkResize();

                    },
            
            
                    onFailure: function() {
            
                        $('content').set('text', 'The request failed. try again?');
                        $('loading').style.display = 'none';
                        checkResize();
            
                    }
            
                }).send();
                pageStack.push(pathlink[1].replace('/view/','/xhr/'));

        }
        if(pathlink[0].indexOf('/contentitems/') > 0 || pathlink[0].indexOf('/projects/') > 0 ) {
            pageView = true;
        }
    }

    checkResize = function() {   
            setNavigation();
        //    $('content').set('opacity','1');   
            scale = $('content').get('tween', {
            property: 'opacity', 
            duration: '0'}).start(1);
                     
            processActive = false;
            
        
    }

    window.addEvent('domready', function() {
       setNavigation();   
       checkPath();
       milkbox = new Milkbox();

        
    }
    );
    
} catch(e) {
    alert(e);
}
function setNavigation() {
 
    //    var pageTracker = _gat._getTracker("UA-5635080-1");
    //    pageTracker._trackPageview();
        
   //     if(pathlink[1]) {$('content').set('text', '');}
        scale = $('content').get('tween', {
            property: 'opacity', 
            duration: '0'});

        $$('.thumb').each(function(el) { 
                el.addEvent('click',function(event) {
                    var thumb = el.getElementsByTagName('img');
                    var main =  document.getElementById('mainImage');
                    var showcase = main.getElementsByTagName('img');
                   showcase[0].src = thumb[0].src.replace('/240','/513');
                   showcase[0].src =  showcase[0].src.replace('/360','/540');
                   showcase[0].parentNode.href = showcase[0].src.replace('/513','/600/750');
                    milkbox.reloadGalleries();


                })
        })
        

        //CATEGORY NAVIGATION

        $$('.projectNavigation').each(function(el) { 
                
                el.addEvent('click',function(event) {
                
                event.preventDefault();
                if(processActive == false) {
                    processActive = true;
                    sourceBlock = el;
                    $('loading').style.display = 'block';

                    //remove the active class
                    var targetClassName;

                    $$('#navigation a').each(function(elNav) { 
                        elNav.removeClass('projectActive');
                        if(elNav.className == 'active') {
                            elNav.removeClass('active');
                            elNav.className = 'contentitemNavigation';
                        }
                    });
                    
                    if (el.className != 'contentitemNavigation') { 
                        targetClassName ='projectActive'; 
                    } else { 
                        targetClassName ='active'; 
                    }
                    
                    if(!event.target.href) {
                        event.target = event.target.parentNode;
                    }
                    
                    sourceBlock = event.target;
                     
                    el.className = targetClassName;
                    hash = event.target.href.replace(domain,''); 
                    document.location = path +'#'+hash;
                    
             
                    
                    if(event.target.href.indexOf('categories') > 0  && pageView == false) {
                        var t = event.target.href.replace('/view/','/json/');
                                        pageStack.push(t);

                        hideBlocks();
                        var req = new Request.JSON({
                    
                            url:t, 
                            urlEncoded: false,
                            onSuccess: function(responseJSON,responseText) {
                            
                            checkResize();
                            //Clear the text currently inside the results div.
                            handleJsonResponse(responseJSON);
                            
                         //   var pageTracker = _gat._getTracker("UA-5635080-1");
                          //  pageTracker._trackPageview(t);
            

                    },
            
            
                    onFailure: function() {
            
                        $('content').set('html', '<b>Can\'t connect to the server.</b><br/>');
                        $('loading').style.display = 'none';
                        setTimeout ( checkResize, 1000 );   
            
                    }
            
                }).send();    
                    
                    
                    } else {
                    $('content').set('opacity',0);
                    var t = event.target.href.replace('/view/','/xhr/');
                pageStack.push(t);

                   // $('content').get('tween', {property: 'opacity', duration: 'long'}).start(0);
                    var req = new Request.HTML({
                    
                            url:t, 
                            urlEncoded: false,
                            onSuccess: function(html) {
                            $('content').set('text', '');
                            
                            //Clear the text currently inside the results div.
                            $('content').adopt(html);
                            $('loading').style.display = 'none';
     
                            setTimeout ( checkResize, 500 );
                            
                         //   var pageTracker = _gat._getTracker("UA-5635080-1");
                          //  pageTracker._trackPageview(t);
            

                    },
            
            
                    onFailure: function() {
            
                        $('content').set('html', '<b>Can\'t connect to the server.</b><br/>');
                        $('loading').style.display = 'none';
                        setTimeout ( checkResize, 1000 );   
            
                    }
            
                }).send();
                

                if(event.target.href.indexOf('categories') > 0) {
                    pageView = false;
                } else {
                    pageView = true;
                }
                
                }
            }	
            })
            
        })
        
        
        ////ITEM NAVIGATION
        
        
            $$('.contentNavigation').each(function(el) { 
                    
                el.addEvent('click',function(event) {
                    event.preventDefault();
                pageView = true;
                if(processActive == false) {
                    processActive = true;
                    $('loading').style.display = 'block';
                    $('content').set('opacity',0);
                    //remove the active class
                    var targetClassName;

                    $$('#navigation a').each(function(elNav) { 
                        elNav.removeClass('projectActive');
                        if(elNav.className == 'active') {
                            elNav.removeClass('active');
                            elNav.className = 'contentitemNavigation';
                        }
                    });
                    
                    if (el.className != 'contentitemNavigation') { 
                        targetClassName ='projectActive'; 
                    } else { 
                        targetClassName ='active'; 
                    }
                    

                     
                    el.className = targetClassName;
                    hash = event.target.href.replace(domain,''); 
                    document.location = path +'#'+hash;
                    var t = event.target.href.replace('/view/','/xhr/');
                    pageStack.push(t);
                    
                    var req = new Request.HTML({
                    
                            url:t, 
                            urlEncoded: false,
                            onSuccess: function(html) {
                            $('content').set('text', '');
                            
                            //Clear the text currently inside the results div.
                            $('content').adopt(html);
                            $('loading').style.display = 'none';
     
                            setTimeout ( checkResize, 500 );
                            
                       //     var pageTracker = _gat._getTracker("UA-5635080-1");
                         //   pageTracker._trackPageview(t);
            

                    },
            
            
                    onFailure: function() {
                        $('loading').style.display = 'none';

                        $('content').set('html', '<b>Can\'t connect to the server.</b><br/>');
                        $('loading').style.display = 'none';
                        setTimeout ( checkResize, 1000 );   
            
                    }
            
                }).send();
            }	
            })
            
        })
    try {
    milkbox = new Milkbox();
        milkbox.reloadGalleries();
    } catch(exception) {}}


function handleJsonResponse(response) {
    $('loading').style.display = 'none';
    var stack = pageStack[pageStack.length -1].split('/');
    stack = stack[stack.length-1];
    
    var i = 0;
    blocks = document.getElementById('content').getElements('.block');
    
    for(var j = 0; j < blocks.length; j++ ) {
           if(blocks[j].getElements('a').length == 0 ||  blocks[j].getElements('h2').length > 0) {
         //       blocks[j].set('text', '');
           } else {
                var aS = blocks[j].getElements('a');
                    var elements = aS[0].href.split('/');
          //          alert(stack);
                    if(stack == elements[elements.length-1]) {

                    i = j;
                }
           }
    }
  
      for (var t = 0; t < response[0].Project.length; t++) {
      var project = response[0].Project[t];
        if(blocks[i] == null) {
            i = 0;
        }
        if(project.id != null) {

            if(blocks[i].getElements('a').length != 0 &&  blocks[i].getElements('h2').length == 0) {
                i++;
            }
      

                        
                blocks[i].set('html',
                    "<a href='/projects/view/"
                    +project.title
                    +"' class=\"projectNavigation\"><h2 style='display:none;'>"
                    +project.displaytitle
                    +"</h2></a>");
                    
       
            i++;
        }
    }
    setNavigation();
    setTimeout(showBlocks,300);
    try {
    milkbox = new Milkbox();
        milkbox.reloadGalleries();
    } catch(exception) {}
}

function showBlocks() {
     blocks = document.getElementById('content').getElements('h2');
     if(busy == true) {
        setTimeout(showBlocks,300);
     } else {
         for (var t = 0; t < blocks.length; t++) {
            var block =  blocks[t];
            if(block.style == null || block.style.display == 'none') {
                block.setStyle('display','block');
                setTimeout(showBlocks,300);
                break;
            }
         }
     }
}

var busy = false;

function hideBlocks() {
     blocks = document.getElementById('content').getElements('h2');
     busy = true;
     for(var k = 0; k < blocks.length; k++) {
        if(blocks[k].style == null || blocks[k].style.display != 'none') {
            blocks[k].setStyle('display','none');
            blocks[k].parentNode.parentNode.set('text', '');
            hideBlocks();
            return;
        }
     }
     busy = false;
}



