//sIFR
var neuzeit = {
    src: '/sifr/neuzeit.swf'
};
var garamond = {
    src: '/sifr/garamond.swf'
};
sIFR.activate(neuzeit, garamond);
sIFR.replace(neuzeit, {
    selector: '#sidebar h2'
    ,
    wmode: 'transparent'
    ,
    css: [
    'a { color: #621a4b; text-decoration: none;}'
    ,'a:hover { color: #a92115; }'
    ]
});

sIFR.replace(garamond, {
    selector: '#content #section-header-landing h1.grantee-banner'
    ,
    wmode: 'transparent'
    ,
    css: [
    '.sIFR-root {color: #ffffff;}'
    ]
    ,
    ratios: [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
});


sIFR.replace(neuzeit, {
    selector: '.col h3'
    ,
    wmode: 'transparent'
    ,
    css: {
        '.sIFR-root': {
            'color': '#ffffff',
            'letter-spacing': 2,
            'text-transform': 'uppercase'
        }
    }
    ,
    filters: {
        DropShadow: {
            distance: 1
            ,
            strength: 1.3
        }
    }
});

function printWin(id) {
    printWinUrl = '/comatose/print?id=' + id;
    window.open(printWinUrl,"print","menubar,resizable,scrollbars,toolbar");
}


jQuery(document).ready(function() {

    jQuery('#show-comments').toggle(function(){
        jQuery('#comments').slideDown();
    }, function(){
        jQuery('#comments').slideUp();
    });
	
    /*drop down*/
    jQuery('#primary-nav').superfish();

    /*write current page indicator*/
    if(jQuery('#sidebar .current-page').length > 0) {
        var element = jQuery('#secondary-nav');//if current page is a sub-page
    } else {
        var element = jQuery('#sidebar');//if on top-level page
    }
    element.append('<div id="current-arrow"></div>');
       
    /*position current page indicator*/
    if(jQuery('#sidebar .current-page').length > 0) { //check to see if .current-page exists
        var current = jQuery('#sidebar .current-page');
        var offset = current.position();
        var topLevel = current.parent().attr('id');
        if (topLevel) {
            var posTop = offset.top + 2;
        } else {
            var posTop = offset.top - 4;
        }
        jQuery('#current-arrow').css({
            top:posTop
        });
    }

    /*print + email icons*/
    jQuery("#print-email li").hoverIntent(function() {
        jQuery(this).children('span').animate({
            textIndent : 0
        });
    }, function() {
        jQuery(this).children('span').animate({
            textIndent : -100
        });
    });
    //
    //    jQuery("#email li").hoverIntent(function() {
    //        jQuery(this).children('span').animate({textIndent : 0});
    //    }, function() {
    //        jQuery(this).children('span').animate({textIndent : -100});
    //    });


    /*hide + show email page form on sidebar partial*/
    jQuery("#email").click(function(){
        //jQuery(this).hide();
        jQuery("#email-form").slideDown();
    });
    
    jQuery("#email-form #hide").click(function(){
        jQuery("#email-form").slideUp();
        return false;
    });

    /*hide + show share page link/icon list on sidebar partial*/
    function shareDown() {
        //jQuery("#email").slideUp();
        jQuery("#email-form").slideUp();
        jQuery('#share-icon').children('span').animate({
            textIndent : 0
        });
        jQuery(".bookmarkaby").slideDown();
    }

    function shareUp() {
        //jQuery("#email").slideDown();
        jQuery('#share-icon').children('span').animate({
            textIndent : -100
        });
        jQuery(".bookmarkaby").slideUp()
    }

    var shareHoverConfig = {
        sensitivity: 10, // number = sensitivity threshold (must be 1 or higher)
        interval: 200, // number = milliseconds for onMouseOver polling interval
        over: shareDown, // function = onMouseOver callback (REQUIRED)
        timeout: 1000, // number = milliseconds delay before onMouseOut
        out: shareUp // function = onMouseOut callback (REQUIRED)
    };

    jQuery("#bookmark-wrapper").hoverIntent(shareHoverConfig);
	
    /* Email ajax form submit */
	
    jQuery("#email-submit").click(function(){
        var dataString = jQuery("form").serialize();
        jQuery.ajax({
            type: "POST",
            url: "/emailer/email",
            data: dataString += "&url=" + jQuery("#message").attr("rel"),
            success: function () {
                jQuery("#email-form").slideUp(function(){
                    jQuery("#email").show();
                });
                jQuery("#email-msg").fadeIn(5000);
                jQuery("#email-msg").fadeOut(5000);
            }
        });
        return false;
    });
});

/* select onchange update submit */
function update_filter(id, page, current_page, pagenum){
    var url = "";
	
    if(current_page.match("grantee-stories")) {
        url = "/" + page + "/update_filter";
    } else {
        url = "/admin/" +  page + "/update_filter";
    }

    jQuery.ajax({
        type: "POST",
        url: url,
        data: "id=" + id + "&current_page=" + current_page + "&pagenum=" + pagenum +"&authenticity_token=" + encodeURIComponent(AUTH_TOKEN),
        success: function (html) {
            jQuery('#filter-display').html(html);
            jQuery('#alphabet_alphabet_index option[@value=' + id + ']').attr("selected", "selected");
            jQuery('#program_area_program_area_id option[@value=' + id + ']').attr("selected", "selected");
            fb.tagAnchors(document);
        }
    });
}

function update_key_filter(id, keyword, page, current_page){
    jQuery.ajax({
        type: "POST",
        url: "/admin/" +  page + "/update_key_filter",
        data: "id=" + id + "&current_page=" + current_page + "&keyword=" + encodeURIComponent(keyword) + "&authenticity_token=" + encodeURIComponent(AUTH_TOKEN),
        success: function (html) {
            jQuery('#filter-display').html(html);
            fb.tagAnchors(document.getElementById(document));
        }
    });
    jQuery('#keyword_keyword_id').val(id)
}

function get_grant(id, area_id,page){
    if (jQuery('#grant-' + id + '-' + area_id).html() == "") {
        jQuery.ajax({
            type: "POST",
            url: "/admin/grants/get_grant",
            data: "id=" + id + "&page_type=" + page +"&authenticity_token=" + encodeURIComponent(AUTH_TOKEN),
            success: function(html){
                jQuery('#grant-' + id + '-' + area_id).hide();
                jQuery('#grant-' + id + '-' + area_id).html(html);
                jQuery('#grant-' + id + '-' + area_id).toggle("fast",function callback() {
                    if(jQuery('#h2-grant-' + id + '-' + area_id).attr("class") == ""){
                        jQuery('#h2-grant-' + id + '-' + area_id).addClass("shown");
                    } else {
                        jQuery('#h2-grant-' + id + '-' + area_id).removeClass("shown");
                    }
                }
                );
            }
        });
    }
    else {
        jQuery('#grant-' + id + '-' + area_id).toggle("fast",function callback() {
            if(jQuery('#h2-grant-' + id + '-' + area_id).attr("class") == ""){
                jQuery('#h2-grant-' + id + '-' + area_id).addClass("shown");
            } else {
                jQuery('#h2-grant-' + id + '-' + area_id).removeClass("shown");
            }
        }
        );
    }
}


function get_grantee_story(id, area_id,page){
    if (jQuery('#grant-' + id + '-' + area_id).html() == "") {
        jQuery.ajax({
            type: "POST",
            url: "/grantee_stories/get_grantee_story",
            data: "id=" + id + "&page_type=" + page +"&authenticity_token=" + encodeURIComponent(AUTH_TOKEN),
            success: function(html){
                jQuery('#grant-' + id + '-' + area_id).hide();
                jQuery('#grant-' + id + '-' + area_id).html(html);
                jQuery('#grant-' + id + '-' + area_id).toggle("fast",function callback() {
                    if(jQuery('#h2-grant-' + id + '-' + area_id).attr("class") == ""){
                        jQuery('#h2-grant-' + id + '-' + area_id).addClass("shown");
                    } else {
                        jQuery('#h2-grant-' + id + '-' + area_id).removeClass("shown");
                    }
                }
                );
            }
        });
    }
    else {
        jQuery('#grant-' + id + '-' + area_id).toggle("fast",function callback() {
            if(jQuery('#h2-grant-' + id + '-' + area_id).attr("class") == ""){
                jQuery('#h2-grant-' + id + '-' + area_id).addClass("shown");
            } else {
                jQuery('#h2-grant-' + id + '-' + area_id).removeClass("shown");
            }
        }
        );
    }
}


function get_story(id){
    if (jQuery('#grantee-' + id).html() == "") {
        jQuery.ajax({
            type: "POST",
            url: "/grantee_stories/get_grantee",
            data: "id=" + id + "&authenticity_token=" + encodeURIComponent(AUTH_TOKEN),
            success: function(html){
                jQuery('#grantee-' + id).hide();
                jQuery('#grantee-' + id).html(html);
                jQuery('#grantee-' + id).toggle("fast",function callback() {
                    if(jQuery('#h2-grantee-' + id).attr("class") == ""){
                        jQuery('#h2-grantee-' + id).addClass("shown");
                    } else {
                        jQuery('#h2-grantee-' + id).removeClass("shown");
                    }
                }
                );
            }
        });
    }
    else {
        jQuery('#grantee-' + id).toggle("fast",function callback() {
            if(jQuery('#h2-grantee-' + id).attr("class") == ""){
                jQuery('#h2-grantee-' + id).addClass("shown");
            } else {
                jQuery('#h2-grantee-' + id).removeClass("shown");
            }
        }
        );
    }
}

function show_all_grants(id, area_id, keyword, current_page){
    var all = false
    jQuery('#program-area-filter ul li div.area' + area_id).each(function(i){
        if(jQuery(this).html() == ""){
            all = true
        }
    });

    var url = ""
    if (id.toString().length == 1){
        url = "/admin/grants/update_filter"
    } else {
        temp = id
        id = keyword
        keyword = temp
        url = "/admin/grants/update_key_filter"
    }

    if(all){
        jQuery.ajax({
            type: "POST",
            url: url,
            data: "id=" + id + "&all=" + all + "&current_page=" + current_page + "&area_id=" + area_id  + "&keyword=" + keyword + "&authenticity_token=" + encodeURIComponent(AUTH_TOKEN),
            success: function (html) {
                jQuery('#filter-display').html(html);
                jQuery('#program-area-filter li div.area' + area_id).show();
                jQuery('#program-area-filter li.area' + area_id +' > h2').addClass("shown");
                jQuery('#area' + area_id + ' a').text("HIDE DETAILS");
                jQuery('#area' + area_id + ' a').addClass("show")
            }
        });
    } else {
        if(jQuery('#area' + area_id + ' a').text() == "SHOW ALL DETAILS"){
            jQuery('#program-area-filter li div.area' + area_id).show();
            jQuery('#program-area-filter li.area' + area_id +' > h2').addClass("shown");
            jQuery('#area' + area_id + ' a').text("HIDE DETAILS");
            jQuery('#area' + area_id + ' a').addClass("show")
        } else {
            jQuery('#program-area-filter li div.area' + area_id).hide();
            jQuery('#program-area-filter li.area' + area_id +' > h2').removeClass();
            jQuery('#area' + area_id + ' a').text("SHOW ALL DETAILS");
            jQuery('#area' + area_id + ' a').removeClass()
        }
    }
    jQuery('#alphabet_alphabet_index').val(id);
    jQuery('#program_area_program_area_id').val(id);
}

function show_all_grantee_stories(id, area_id, keyword, current_page){
    var all = false
    jQuery('#program-area-filter ul li div.area' + area_id).each(function(i){
        if(jQuery(this).html() == ""){
            all = true
        }
    });

    var url = ""
    if (id.toString().length == 1){
        url = "/grantee_stories/update_filter"
    }

    if(all){
        jQuery.ajax({
            type: "POST",
            url: url,
            data: "id=" + id + "&all=" + all + "&current_page=" + current_page + "&area_id=" + area_id  + "&keyword=" + keyword + "&authenticity_token=" + encodeURIComponent(AUTH_TOKEN),
            success: function (html) {
                jQuery('#filter-display').html(html);
                jQuery('#program-area-filter li div.area' + area_id).show();
                jQuery('#program-area-filter li.area' + area_id +' > h2').addClass("shown");
                jQuery('#area' + area_id + ' a').text("HIDE DETAILS");
                jQuery('#area' + area_id + ' a').addClass("show")
            }
        });
    } else {
        if(jQuery('#area' + area_id + ' a').text() == "SHOW ALL DETAILS"){
            jQuery('#program-area-filter li div.area' + area_id).show();
            jQuery('#program-area-filter li.area' + area_id +' > h2').addClass("shown");
            jQuery('#area' + area_id + ' a').text("HIDE DETAILS");
            jQuery('#area' + area_id + ' a').addClass("show")
        } else {
            jQuery('#program-area-filter li div.area' + area_id).hide();
            jQuery('#program-area-filter li.area' + area_id +' > h2').removeClass();
            jQuery('#area' + area_id + ' a').text("SHOW ALL DETAILS");
            jQuery('#area' + area_id + ' a').removeClass()
        }
    }
    jQuery('#program_area_program_area_id').val(id);
}

function get_publication(id,area_id){
    if (jQuery('#publication-' + id + '-' + area_id).html() == "") {
        jQuery.ajax({
            type: "POST",
            url: "/admin/publications/get_publication",
            data: "id=" + id + "&authenticity_token=" + encodeURIComponent(AUTH_TOKEN),
            success: function(html){
                jQuery('#publication-' + id + '-' + area_id).hide();
                jQuery('#publication-' + id + '-' + area_id).html(html);
                jQuery('#publication-' + id + '-' + area_id).toggle("fast",function callback() {
                    if(jQuery('#h2-publication-' + id + '-' + area_id).attr("class") == ""){
                        jQuery('#h2-publication-' + id + '-' + area_id).addClass("shown");
                    } else {
                        jQuery('#h2-publication-' + id + '-' + area_id).removeClass("shown");
                    }
                }
                );
                fb.tagAnchors(document);
            }
        });
    }
    else {
        jQuery('#publication-' + id + '-' + area_id).toggle("fast",function callback() {
            if(jQuery('#h2-publication-' + id + '-' + area_id).attr("class") == ""){
                jQuery('#h2-publication-' + id + '-' + area_id).addClass("shown");
            } else {
                jQuery('#h2-publication-' + id + '-' + area_id).removeClass("shown");
            }
        }
        );
    }
}

function show_all_publications(id, area_id, keyword, current_page, pagenum){
    var all = false
    jQuery('#program-area-filter ul li div.area' + area_id).each(function(i){
        if(jQuery(this).html() == ""){
            all = true
        }
    });

    var url = ""
    if (id.toString().length == 1){
        url = "/admin/publications/update_filter"
    } else {
        temp = id
        id = keyword
        keyword = temp
        url = "/admin/publications/update_key_filter"
    }
	
    if(all){
        jQuery.ajax({
            type: "POST",
            url: url,
            data: "id=" + id + "&all=" + all + "&pagenum=" + pagenum + "&current_page=" + current_page + "&area_id=" + area_id  + "&keyword=" + keyword + "&authenticity_token=" + encodeURIComponent(AUTH_TOKEN),
            success: function (html) {
                jQuery('#filter-display').html(html);
                jQuery('#program-area-filter ul li div.area' + area_id).show();
                jQuery('#program-area-filter ul li.area' + area_id +' > h2').addClass("shown");
                jQuery('#area' + area_id + ' a').text("HIDE DETAILS");
                jQuery('#area' + area_id + ' a').addClass("show")
                fb.tagAnchors(document);
            }
        });
    } else {
        if(jQuery('#area' + area_id + ' a').text() == "SHOW ALL DETAILS"){
            jQuery('#program-area-filter ul li div.area' + area_id).show();
            jQuery('#program-area-filter ul li.area' + area_id +' > h2').addClass("shown");
            jQuery('#area' + area_id + ' a').text("HIDE DETAILS");
            jQuery('#area' + area_id + ' a').addClass("show")
        } else {
            jQuery('#program-area-filter ul li div.area' + area_id).hide();
            jQuery('#program-area-filter ul li.area' + area_id +' > h2').removeClass();
            jQuery('#area' + area_id + ' a').text("SHOW ALL DETAILS");
            jQuery('#area' + area_id + ' a').removeClass()
        }
    }
    jQuery('#alphabet_alphabet_index').val(id);
    jQuery('program_area_program_area_id').val(id)
}

function show_all_publications_date(current_page, page_num){
    var all = false
    jQuery('#pub-date-wrapper ul li').find('div:first').each(function(i){
        if(jQuery(this).html() == ""){
            all = true
        }
    });

    if(all){
        jQuery.ajax({
            type: "POST",
            url: "/admin/publications/show_all_date",
            data: "all=" + all + "&current_page=" + current_page + "&pagenum=" + page_num + "&authenticity_token=" + encodeURIComponent(AUTH_TOKEN),
            success: function (html) {
                jQuery('#pub-date-wrapper').html(html);
                jQuery('#pub-date-wrapper ul li').find('div:first').show();
                jQuery('#pub-date-wrapper ul li > h2').addClass("shown");
                jQuery('#show_all_date').text("HIDE DETAILS");
                jQuery('#show_all_date').addClass("show")
                fb.tagAnchors(document);
            }
        });
    } else {
        if(jQuery('#show_all_date').text() == "SHOW ALL DETAILS"){
            jQuery('#pub-date-wrapper ul li').find('div:first').show();
            jQuery('#pub-date-wrapper ul li > h2').addClass("shown");
            jQuery('#show_all_date').text("HIDE DETAILS");
            jQuery('#show_all_date').addClass("show")
        } else {
            jQuery('#pub-date-wrapper ul li').find('div:first').hide();
            jQuery('#pub-date-wrapper ul li > h2').removeClass();
            jQuery('#show_all_date').text("SHOW ALL DETAILS");
            jQuery('#show_all_date').removeClass()
        }
    }
}