$(document).ready(function(){
    var swf_url = '/wp/wp-content/themes/gohave1/swf';
    var express_install = swf_url + '/expressInstall.swf';

    if(typeof sIFR == "function"){
        sIFR.replaceElement(".sifr", named({
            sFlashSrc: swf_url + '/dinneuzeitgrotesk.swf',
            sColor: "#887d75",
            sCase: "upper",
            sWmode: "opaque"
        }));
        sIFR.replaceElement(".sifr-sub", named({
            sFlashSrc: swf_url + '/dinneuzeitgrotesk.swf',
            sColor: "#ffffff",
            sBgColor: "#c3c947",
            sWmode: "opaque"

        }));
        sIFR.replaceElement(".sifr-sidebar", named({
            sFlashSrc: swf_url + '/dinneuzeitgrotesk.swf',
            sColor: "#ffffff",
            sBgColor: "#c3c947",
            sFlashVars: "textalign=center",
            sCase: "upper",
            sWmode: "opaque"
        }));
    };
    
    var headers = [
        [ 'common-fears', '1-CommonFear_header.swf' ],
        [ 'myths-and-facts', '2-Myths_and_Facts.swf' ],
        [ 'what-happens', '3-Whathappens.swf' ],
        [ 'where-2-have-1', '4-Where2have1.swf' ],
        [ 'mobile-mammograms', '5-MobileMammograms.swf' ],
        [ 'getting-your-results', '6-GettingYourResults.swf' ]
    ];
    
    var found_header = false;
    $(headers).each(function() {
        if ($('#main.' + this[0]).get(0)) {
            swfobject.embedSWF(swf_url + '/' + this[1], "header_swf", "985", "98", "7.0.0", express_install);
            found_header = true;
            return false;
        }
        return true;
    });
    
    if (!found_header) {
        swfobject.embedSWF(swf_url + '/CBCF_banner_header.swf', "header_swf", "985", "98", "7.0.0", express_install);
    }
    
    if ($('#main_video').get(0)) {
        swfobject.embedSWF(swf_url + '/homepage_video.swf', "main_video", "660", "364", "8.0.0", express_install, { vidpath : 'http://s3.amazonaws.com/CBCF/cbcf_nogenerationsend.flv' });
    }
    
    $('#video_promo').click(function() {
        swfobject.embedSWF(swf_url + '/video.swf', "video_promo", "325", "246", "8.0.0", express_install, { vidpath : swf_url + '/cbcf_underthebed.flv' });
        return false;
    });
    
    var flash_version = swfobject.getFlashPlayerVersion();
    if (flash_version.major) {
        $('#video_promo').show();
    }
    
    $('ul.comments div.more').each(function() {
        if ($(this).siblings('div.excerpt').get(0)) {
            $(this)
                .after('<p class="show_hide_button"><a href="#" class="show_hide">Show Entire Comment</a></p>')
                .hide();
        }
    });

    $('div.post div.more')
        .after('<p class="show_hide_button"><a href="#" class="show_hide">Show All</a></p>')
        .hide();
    
    $('a.show_hide').toggle(function() {
        $('#posting-guidelines, #subscribe-info').hide();

        $(this)
            .addClass('shown')
            .html('Hide')
            .parent().siblings('div.excerpt')
                .hide();
        
        $(this).parent().siblings('div.more')
            .slideDown(500);
    },
    function() {
        $('#posting-guidelines, #subscribe-info').hide();

        $(this)
            .removeClass('shown')
            .html('Show All')
            .parent().siblings('div.more')
                .slideUp(500, function() {
                    $(this).siblings('div.excerpt').slideDown(500);
                });
    });
    
    $('#view_all_comments').show();
    
    $('#view_all_comments a').click(Comments.toggle);
    
    var guidelines = $('#posting-guidelines').html();
    $('#posting-guidelines').remove();
    
    $('#main').addpopup('posting-guidelines', 'popup-small');
    $('#posting-guidelines').append(guidelines);
    $('#posting-guidelines').hide();

    $('#commentform a.guidelines').click(function() {
        $('#subscribe-info').hide();
        var pos = $('#commentform').offset();
        $('#posting-guidelines')
            .reposition(0, pos.top)
            .fadeIn();
        return false;
    });

    var subscribe_info = $('#subscribe-info').html();
    $('#subscribe-info').remove();
    
    $('#main').addpopup('subscribe-info', 'popup-small');
    $('#subscribe-info').append(subscribe_info);
    $('#subscribe-info').hide();

    $('#commentform a.subscribe-info-link').click(function() {
        $('#posting-guidelines').hide();
        var pos = $('#commentform').offset();
        $('#subscribe-info')
            .reposition(0, pos.top)
            .fadeIn();
        return false;
    });
    
    $('a.notify-me-button')
        .show()
        .click(function() {
            $('span.sifr-sidebar embed').eq(0).hide();
            
            $('#main').addpopup('notify-me', 'popup-notify', function() {
                $('span.sifr-sidebar embed').eq(0).show();
                $('#notify-me').fadeOut();
                return false;
            });

            var pos = $(this).offset();
            $('#notify-me')
                .hide()
                .reposition(pos.left - 290, pos.top - 15)
                .append('\
                    <h3>NOTIFY ME</h3>\
                    <div class="content">\
                        <p>\
                            Get email notifications when this page receives new\
                            comments.\
                        </p>\
                        <form id="notify-me-form" method="post" action="/notifyme/">\
                            <div class="field">\
                                <label for="youremail">Your Email:</label>\
                                <input id="youremail" type="text" value="" name="email" size="40"/>\
                            </div>\
                            <div class="submit-cancel">\
                                <input class="button" type="image" value="Submit" tabindex="5" src="/wp/wp-content/themes/gohave1/images/blank.gif" name="submit"/>\
                                <a class="cancel" onclick="$(\'#notify-me-form input[type=text]\').val(\'\');" href="javascript: ;">Reset</a>\
                            </div>\
                        </form>\
                    </div>\
                ')
                .fadeIn();
            
            return false;
        });
    
    
    $('a.send-friend-button').click(function() {
        if ($('#send-friend').get(0)) return false;
        $('span.sifr-sidebar embed').eq(0).hide();
        
        $('#main').addpopup('send-friend', '', function() {
            $('span.sifr-sidebar embed').eq(0).show();
            $('#send-friend').remove();
            return false;
        });
        
        var pos = $(this).offset();
        $('#send-friend')
            .reposition(pos.left - 270, pos.top - 15)
            .hide();
    
        $.ajax({
            type: "GET",
            url: this.href,
            data: 'ajax=1',
            success: function(data) {
                $('#send-friend')
                    .append(data)
                    .fadeIn();

                $('#send-friend a.close').click(function() {
                    alert('test');
                    $('span.sifr-sidebar embed').eq(0).show();
                    return false;
                });
            }
        });

        return false; 
    });

    if ($('#subscribe').get(0)) {
        if (!$('#subscribe').get(0).checked) {
            $('#commentform div.subscribe-email').hide();
        }
    }
    else {
        $('#commentform div.subscribe-email').hide();
    }
    
    $('#subscribe').click(function() {
        var div = $('#commentform div.subscribe-email');
        if (this.checked) {
            div.show();
        }
        else {
            div.hide();
        }
    });
    
    if (window.location.hash.indexOf('comment-') == -1) {
        Comments.hide(true);
    }
    else {
        Comments.show();
        $(window.location.hash + ' a.show_hide').trigger('click');
    }
    
    if (window.location.hash) {
        window.location.hash = window.location.hash;
    }
});

var Comments = {
    toggle: function() {
        if ($(this).hasClass('shown')) {
            Comments.hide();
        }
        else {
            Comments.show();
        }
        return false;
    },
    show: function(callback) {
        $('#posting-guidelines').hide();
        $('ul.comments.rest').slideDown(500, callback);
        $('#view_all_comments a')
            .addClass('shown')
            .children('span').html('HIDE COMMENTS');
    },
    hide: function(no_animate) {
        $('#posting-guidelines').hide();
        if (no_animate) {
            $('ul.comments.rest').hide();
        }
        else {
            $('ul.comments.rest').slideUp(500);
        }
        $('#view_all_comments a')
            .removeClass('shown')
            .children('span').html('VIEW ALL COMMENTS');
    }
}

jQuery.fn.addpopup = function(id, classes, close_func) {
    this.append('<div class="popup ' + classes + '" id="' + id + '"><a href="#" class="close">Close</a></div>');
    if (close_func) {
        $('#' + id + ' a.close').click(close_func);
    }
    else {
        $('#' + id + ' a.close').click(function() {
            $('#' + id).fadeOut();
            return false;
        });
    }
};

jQuery.fn.reposition = function(x, y) {
    this.css('top', y + 'px');
    this.css('left', x + 'px');
    return this;
}