

jQuery.fn.exists = function(){return jQuery(this).length>0;}

var MNM_DELAY = 140;
var PANEL_DELAY = 150;
var timeoutID;

var tallest = 'C';
var incenter_height = 0;
var leftcontent_height = 0;
var rightcontent_height = 0;
var space_below_ads = 20;
var left_space = 0;
var right_space = 0;
var redo_ads_threshold = 100;
var shortest_right_ad = 115;
var shortest_left_ad = 115;
var ad_display_delay = 500;


var rightad_1 = false;
var top_options = []; 
var box_options = ['ii_box_200x200_1'];
var hpm_options = ['ii_hpm_250x250_1'];


var left_options = [
'ii_left_160x600_1',
'ii_left_120x90_1',
'ii_left_160x320_1',
'ii_left_120x90_2',
'ii_left_160x320_2',
'ii_left_120x90_3',
'ii_left_160x600_2',
'ii_left_160x320_3'];



var right_options336 = [
'ii_right_336x125_1',
'ii_right_336x280_2g',
'ii_right_120x90_1',
'ii_right_300x250_1c',
'ii_right_336x45_1',
'ii_right_336x125_2',
'ii_right_300x250_3',
'ii_right_120x90_2',
'ii_right_300x600_1',
'ii_right_336x125_3',
'ii_right_300x600_2'].slice(0,-1);




function get_w_h(di){
    return di.replace('left', '').replace('right', '').replace('top', '').replace('box', '').replace('hpm', '').replace('ii__', '').split('_')[0].split('x');
}

var render_slots = []

function gatag(append_to, slot_name, classes){
    var w_h = get_w_h(slot_name);
    var w = parseInt(w_h[0]);
    var h = parseInt(w_h[1]);
    $('<div/>').attr('id', slot_name).addClass(classes).width(w).height(h).appendTo($(append_to));
    //$('<div/>').addClass(classes).width(w).height(h).append($('<div/>').attr('id', slot_name).width(w).height(h)).appendTo($(append_to));
    googletag.cmd.push(function() { 
        googletag.defineUnit('/1045383/ca-pub-7632572203492720/' + slot_name, [w, h], slot_name).addService(googletag.pubads());
    });
    render_slots.push(slot_name);
    return [w, h];
}

function fill_ad_space(){
    if( $("#box_ad_200x200").exists() ){
        for(boi in box_options){
            var bo = box_options[boi];
            gatag("#box_ad_200x200", bo, "ii_ad");
        }
    }

    if( $("#hpm_ad_250x250").exists() ){
        for(hoi in hpm_options){
            var ho = hpm_options[hoi];
            gatag("#hpm_ad_250x250", ho, "ii_ad");
        }
    }

    // right
    if( !$("body.no_right_ads").exists() ){
        for(roi in right_options336){
            var ro = right_options336[roi];
            var w_h = get_w_h(ro);
            var w = parseInt(w_h[0]);
            var h = parseInt(w_h[1]);
            if(right_space >= h + space_below_ads){
                if(roi == 0 && rightad_1 && $("#rightad_1").exists() ){
                    gatag("#rightad_1", ro, "ii_ad rightad");
                } else {
                    gatag("#rightads", ro, "ii_ad rightad");
                }
                right_space = right_space - h;
                right_space = right_space - space_below_ads;
            }
        }
    }

    // top
    for(x in top_options){
        w_h = gatag("#headbanner", top_options[x], "ii_ad topad");
    }

    // left
    for(loi in left_options){
        var lo = left_options[loi];
        var w_h = get_w_h(lo);
        var w = parseInt(w_h[0]);
        var h = parseInt(w_h[1]);
        if(left_space >= h + space_below_ads){
            gatag("#leftads", lo, "ii_ad leftad");
            left_space = left_space - h;
            left_space = left_space - space_below_ads;
        }
    }

}

function fill_slots(){
    for(target in targeting){
        googletag.cmd.push( function(){ googletag.pubads().setTargeting(target, targeting[target]);});
    }
    // uncommenting the following line breaks ad delivery.  Why?
    googletag.cmd.push(function() {googletag.pubads().enableSingleRequest();});
    googletag.cmd.push(function() {googletag.enableServices();});
    for(x in render_slots){ googletag.cmd.push(function() {googletag.display(render_slots[x]); }); }

    // following lines enable a fancy slide in effect.  Disabled while testing.
    //$(".rightad").each(function(index){$(this).delay(index*ad_display_delay).show('slide', {direction:'right'}, 1000);});
    //$(".leftad").each(function(index){$(this).delay(index*ad_display_delay).show('slide', {direction:'left'}, 1000);});
}

function determine_heights(){
    var bump_height = $("#above_center_and_left").height();
    incenter_height = $('.center .incenter').height() + bump_height;
    var home_left = $('.homecolumn.homeleft').height();
    var home_right = $('.homecolumn.homeright').height();
    if(home_left > 0 || home_right > 0){
        incenter_height = home_left;
        if(home_right > home_left) incenter_height = home_right;
    }
    leftcontent_height = $('#leftcontent').height() + bump_height;
    rightcontent_height = $('#rightcontent').height() + 30;
    if($("#rightad_2").exists()){rightcontent_height = rightcontent_height + $("#rightad_2").height();}
    if(leftcontent_height > incenter_height && leftcontent_height > rightcontent_height) tallest = 'L';
    if(rightcontent_height > incenter_height && rightcontent_height > leftcontent_height) tallest = 'R';
    if(tallest == "L"){
        left_space = 0;
        right_space = leftcontent_height - rightcontent_height;
    }
    if(tallest == "C"){
        left_space = incenter_height - leftcontent_height;
        right_space = incenter_height - rightcontent_height;
    }
    if(tallest == "R"){
        right_space = 0;
        left_space = rightcontent_height - leftcontent_height;
    }
    
}
function display_heights(){
    $('#leftads').html(leftcontent_height);
    $('#rightads').html(rightcontent_height);
    $('#rightads').append(tallest);
    $('#centerads').html(incenter_height);
}
function reassess_ads_after_resize(){
    var old_incenter_height = incenter_height;
    determine_heights();
    if(Math.abs( old_incenter_height - incenter_height ) > redo_ads_threshold ){
        fill_ad_space();
    }
}

function mnmOpen(mnmp){
    mnmp.onmouseout = new Function("clearTimeout(timeoutID);")
    var mnmpid = mnmp.getAttribute('id');
    var mnmliid = mnmpid.replace('mnmp', 'mnmli');
    timeoutID = setTimeout("mnmOpenClose('" + mnmliid + "')", MNM_DELAY);
}
function mnmOpenClose(mnmliid){
    var mnmli = document.getElementById(mnmliid);
    var c = mnmli.className;
    if( c.indexOf('open') == -1 ) mnmli.className = 'open ' + c;
    else{mnmli.className = c.replace('open', '')}
}

function prevNextSetup(element, url){
    element.onclick = new Function("document.location = '" + url + "';");
    var prevNex = document.getElementById('previousnext');
    var code = "var p = document.getElementById('previousnext');" + 
        "toggleClass(p, 'replaceme');";
    switch(element.id){
        case "hometarget": {
                toggleClass(element, "home");
                element.onmouseout = new Function("toggleClass(this, 'home');");
                break;
            }
        case "previoustarget": {
                toggleClass(prevNex, 'previous');
                element.onmouseout = new Function(code.replace('replaceme', 'previous'));
                break;
            }
        case "nexttarget": {
                toggleClass(prevNex, 'next');
                element.onmouseout = new Function(code.replace('replaceme', 'next'));
                break;
            }
    }
}
function toggleClass(element, className){
    if(element != null){
        var currentClass = element.className;
        if(currentClass.indexOf(className) == -1){
            //not found, so add it
            element.className = currentClass + " " + className;
        } else {
            //found it, so remove it
            element.className = currentClass.replace(className, "");
        }
    }
}

function slideblue(el){
    $(el).addClass('open');
}
function prepMNM(){
    if(those_to_bullet.length > 0){
        slideblue( $(those_to_bullet[0]) );
        $(those_to_bullet[0]).addClass('bul');
        if(those_to_bullet.length > 1){
          $(those_to_bullet[1]).addClass('selected');
        } else {
          $(those_to_bullet[0]).children().first().children().first().addClass('selected');
        }
    }
    $("#mnm2 a").each(function(i){var cur = $(this); $(this).parent().parent().click(function(){window.location = cur.attr("href");}); })
}

function addSandBags(){
    $('.pull-quote').each(
        function(){
            prev = $(this).prev();
            if(prev.attr('class') != 'sandbag-2-lines' ){
                //only add a sandbag if not already sandbagged
                $(this).before('<div class="sandbag-2-lines"></div>');
            }
        }
    );
}

function mainPhoto(){
    // "z-index:0;margin:0;height:263px;position:absolute;top:-270px;"
    $('#large-image-holder').css('z-index', 0).css('position', 'absolute').css('top', -270);
}

//setInterval(reassess_ads_after_resize, 5000);

$(mainPhoto);
$(prepMNM);
$(determine_heights);
$(fill_ad_space);
$(fill_slots);
$(addSandBags);





