$(document).ready(function(){
    
    $('.report').hide();
    $('.report_title').click(function(e){
        e.preventDefault();
        var open = $(this).nextAll('.report').first();
        $('.report').not(open).slideUp();
        open.slideToggle();
        return false;
    });
    $('.wpcf7-form').children('br').remove();
    
    if(location.hash && $('input[name^="reports"]').length > 0)
    {
        var hash = location.hash.substring(1);
        $("input[value='"+hash+"']").prop("checked", true);
    }
});

// launches a post inline
var launch = function(e)
{
    var isIe = (document.all)? true : false;
    
    e.preventDefault();




    if ($("#draggable").hasClass('noclick')) {
        $("#draggable").removeClass('noclick');
        
        
        $("#draggable a" ).preventDefault();
        
        return;
    }
    
    
    var $t = $(this),
        data = e.data || {};
    
    //log('launch ',$t);
    // force rolloff of the post
    if($t.attr('href')=='#')
    {
     return;
     }
    $t.append('<div id="loader"><span>Loading</span></div>').find('.images').mouseleave();
    
    //$('#grid').gridmap.deactivate();
    
    $.ajax({
        url: $t.attr('href'),
        //dataType:'html',
        data:{ajax:true},
        type:'GET',
        success: function(xml){
        
            $('#loader').remove();
            
            // fade out all tinted
            //tom comment
            //$('#grid .tint-inactive .tint').fadeIn();            
            //log(data);
            
            if(!data.substitute || true) // loading pod
            {
                // lightbox
                
                if(!isIe)
                {
                $('<div id="overlay-bg"></div>').insertBefore('#overlay').hide().fadeIn().click(function(e){
                    $('a.button-close').click();
                });
                }
                else
                {
                     $('<div id="overlay-bg"></div>').insertBefore('#overlay').click(function(e){
                    $('a.button-close').click();
                });
                }
                
                $(xml).hide();
                $('#overlay').empty().html($(xml).fadeIn());
                
                // set all links to open in new page
                $('#overlay a:not([href*="home"])').attr('target','_blank');
            }            
            else // load new page inline
            {
                //$('#overlay').empty();
//                if($('#mosaic-toggle').hasClass('closed')) $('#news-toggle').click();
                //Cufon.replace('#news-toggle', { fontFamily: 'Graphik Semibold' }); 
//                $('.pods_main_col').replaceWith($('.pods_main_col',xml).hide().fadeIn());
                // switch nav
//                if(data.switchSection){
//                    $('.current_page_item').removeClass('current_page_item');
//                    $(e.target).closest('li').addClass('current_page_item');
                    //Cufon.replace('#sidebar li>a', { fontFamily: 'Graphik Semibold', hover: true });
//                }
            } 
            /*
            if(!IS_IPHONE){
                Cufon.replace('h2', { fontFamily: 'Graphik Semibold', hover: true });
                Cufon.replace('h3', { fontFamily: 'Graphik Regular' , hover: true }); 
            }
            else // position item onscreen
            {
                $('#overlay').css({top:Math.max($(window).scrollTop()+6,80),left:12});
            }
            */
            
            // hide button closes & removes overlay
            if(!data.substitute) 
            $('<a class="button button-close" href="#"><span>X</span></a>').click(function(e){
                
                e.preventDefault();
                
                $('#overlay-bg').fadeOut('def',function(e){
                    $(this).remove();
                });
                
                // fade back in any tinted
                //$('#grid .tint-inactive .tint').fadeOut();
                
                // on single page check for the matching post
                //if(IS_SINGLE) $('#'+ $('.pods .post').attr('data-id') +' .tint').fadeOut();
                
                // fadeOut() was broken?!
                $('#overlay .pods').animate({opacity:0},'def',function(e){
                //$('#overlay .pods').fadeOut('def',function(e){
                    $(this).remove();
                    //$('#grid').gridmap.activate();
                    //log('closebt activate');
                
                });
                
            }).appendTo('#overlay .infotext, #overlay .page-pod');
            
            // next/prev buttons load another overlay
            //$('#overlay .pods .navigation a').bind('click',launch);
            
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //alert('error'+textStatus);
            //log('error',textStatus);
        }

    });

};

var launch1 = function(e)
{
    e.preventDefault();
    var $t = $(this),
        data = e.data || {};
    
    var isIe = (document.all)? true : false;
    //log('launch ',$t);
    // force rolloff of the post
    $t.append('<div id="loader"><span>Loading</span></div>').find('.images').mouseleave();
    
    //$('#grid').gridmap.deactivate();
    
       
       if($t.parent().parent().attr('id')=='topmenuul')
       {
           $("#topmenu a").attr("class","topmenudeactive");
         $t.attr('class','topmenuactive');
       }
    $.ajax({
        url: $t.attr('href'),
        //dataType:'html',
        data:{ajax:true},
        type:'GET',
        success: function(xml){
        
            $('#loader').remove();
            
            // fade out all tinted
            //tom comment
            //$('#grid .tint-inactive .tint').fadeIn();            
            //log(data);
            
            if(!data.substitute || true) // loading pod
            {
                // lightbox
               
                $('#overlay-bg').remove();
                
                if(!isIe)
                {
                $('<div id="overlay-bg"></div>').insertBefore('#overlay').hide().fadeIn().click(function(e){
                    $('a.button-close').click();
                });
                }
                else
                {
                     $('<div id="overlay-bg"></div>').insertBefore('#overlay').click(function(e){
                    $('a.button-close').click();
                });
                }
                
                
                $(xml).hide();
                $('#overlay').empty().html($(xml).fadeIn());
                
                // set all links to open in new page
                $('#overlay a:not([href*="home"])').attr('target','_blank');
            }            
            else // load new page inline
            {
                //$('#overlay').empty();
//                if($('#mosaic-toggle').hasClass('closed')) $('#news-toggle').click();
                //Cufon.replace('#news-toggle', { fontFamily: 'Graphik Semibold' }); 
//                $('.pods_main_col').replaceWith($('.pods_main_col',xml).hide().fadeIn());
                // switch nav
//                if(data.switchSection){
//                    $('.current_page_item').removeClass('current_page_item');
//                    $(e.target).closest('li').addClass('current_page_item');
                    //Cufon.replace('#sidebar li>a', { fontFamily: 'Graphik Semibold', hover: true });
//                }
            } 
            /*
            if(!IS_IPHONE){
                Cufon.replace('h2', { fontFamily: 'Graphik Semibold', hover: true });
                Cufon.replace('h3', { fontFamily: 'Graphik Regular' , hover: true }); 
            }
            else // position item onscreen
            {
                $('#overlay').css({top:Math.max($(window).scrollTop()+6,80),left:12});
            }
            */
            
            // hide button closes & removes overlay
            if(!data.substitute) 
            $('<a class="button button-close" href="#"><span>X</span></a>').click(function(e){
                
                e.preventDefault();
                
                $('#overlay-bg').fadeOut('def',function(e){
                    $(this).remove();
                });
                
                // fade back in any tinted
                //$('#grid .tint-inactive .tint').fadeOut();
                
                // on single page check for the matching post
                //if(IS_SINGLE) $('#'+ $('.pods .post').attr('data-id') +' .tint').fadeOut();
                
                // fadeOut() was broken?!
                $('#overlay .pods').animate({opacity:0},'def',function(e){
                //$('#overlay .pods').fadeOut('def',function(e){
                    $(this).remove();
                    //$('#grid').gridmap.activate();
                    //log('closebt activate');
                
                });
                
            }).appendTo('#overlay .infotext, #overlay .page-pod');
            
            // next/prev buttons load another overlay
            //$('#overlay .pods .navigation a').bind('click',launch);
            
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //alert('error'+textStatus);
            //log('error',textStatus);
        }

    });

};

//for ie noloading div
var launch2 = function(e)
{
    e.preventDefault();
    var $t = $(this),
        data = e.data || {};
        
 
    
    var isIe = (document.all)? true : false;
    //log('launch ',$t);
    // force rolloff of the post
   // $t.append('<div id="loader"><span>Loading</span></div>').find('.images').mouseleave();
    
    //$('#grid').gridmap.deactivate();
    
    $.ajax({
        url: $t.attr('href'),
        //dataType:'html',
        data:{ajax:true},
        type:'GET',
        success: function(xml){
        
            $('#loader').remove();
            
            // fade out all tinted
            //tom comment
            //$('#grid .tint-inactive .tint').fadeIn();            
            //log(data);
            
            if(!data.substitute || true) // loading pod
            {
                // lightbox
               
                $('#overlay-bg').remove();
                
                if(!isIe)
                {
                $('<div id="overlay-bg"></div>').insertBefore('#overlay').hide().fadeIn().click(function(e){
                    $('a.button-close').click();
                });
                }
                else
                {
                     $('<div id="overlay-bg"></div>').insertBefore('#overlay').click(function(e){
                    $('a.button-close').click();
                });
                }
                
                
                $(xml).hide();
                $('#overlay').empty().html($(xml).fadeIn());
                
                // set all links to open in new page
                $('#overlay a:not([href*="home"])').attr('target','_blank');
            }            
            else // load new page inline
            {
                //$('#overlay').empty();
//                if($('#mosaic-toggle').hasClass('closed')) $('#news-toggle').click();
                //Cufon.replace('#news-toggle', { fontFamily: 'Graphik Semibold' }); 
//                $('.pods_main_col').replaceWith($('.pods_main_col',xml).hide().fadeIn());
                // switch nav
//                if(data.switchSection){
//                    $('.current_page_item').removeClass('current_page_item');
//                    $(e.target).closest('li').addClass('current_page_item');
                    //Cufon.replace('#sidebar li>a', { fontFamily: 'Graphik Semibold', hover: true });
//                }
            } 
            /*
            if(!IS_IPHONE){
                Cufon.replace('h2', { fontFamily: 'Graphik Semibold', hover: true });
                Cufon.replace('h3', { fontFamily: 'Graphik Regular' , hover: true }); 
            }
            else // position item onscreen
            {
                $('#overlay').css({top:Math.max($(window).scrollTop()+6,80),left:12});
            }
            */
            
            // hide button closes & removes overlay
            if(!data.substitute) 
            $('<a class="button button-close" href="#"><span>X</span></a>').click(function(e){
                
                e.preventDefault();
                
                $('#overlay-bg').fadeOut('def',function(e){
                    $(this).remove();
                });
                
                // fade back in any tinted
                //$('#grid .tint-inactive .tint').fadeOut();
                
                // on single page check for the matching post
                //if(IS_SINGLE) $('#'+ $('.pods .post').attr('data-id') +' .tint').fadeOut();
                
                // fadeOut() was broken?!
                $('#overlay .pods').animate({opacity:0},'def',function(e){
                //$('#overlay .pods').fadeOut('def',function(e){
                    $(this).remove();
                    //$('#grid').gridmap.activate();
                    //log('closebt activate');
                
                });
                
            }).appendTo('#overlay .infotext, #overlay .page-pod');
            
            // next/prev buttons load another overlay
            //$('#overlay .pods .navigation a').bind('click',launch);
            
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //alert('error'+textStatus);
            //log('error',textStatus);
        }

    });

};

var setupLoadMore = function(getURL, preload, timeout, callback, nomore)
{
    var isLoading = false,
        to, 
        url = getURL();
    
    //log('first url', url);
    
    $(window).scroll(function(e)
    {
        if(!isLoading)
        {
            if($(window).scrollTop() == $(document).height() - $(window).height()) // at bottom of page
            {
                if(url)
                {
                    //log('getting', url);
                
                    isLoading = true;                    
                    preload();
                    
                    // slow loading msg after 10 secs
                    to = setTimeout(timeout, 10000);
                    
                    $.ajax({
                        url: url,
                        //dataType:'html',
                        data:{ajax:true, blog:true},
                        type:'GET',
                        success: function(xml)
                        {    
                            clearTimeout(to);
                            isLoading = false;
                            
                            //log(xml);
                            var $xml = $(xml);
                            callback( $xml );
                            
                            url = getURL($xml);
                            //log('next url', url);
                            if(!url)
                            {
                                $(window).unbind('scroll');
                                nomore();
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            log('error',textStatus);
                            isLoading = false; // try again
                        }
                    });
                    
                }
                else
                {
                    $(window).unbind('scroll');
                    nomore();
                }
                
            }
        }
    
    });
    
    // call first time
    $(window).scroll();

}

