var sitemap_cleanup = {
    init: function() {
        var sitemap = jQuery(".ddsg-wrapper");
        jQuery("strong:contains(Category:)", sitemap).remove();
        jQuery("div p:containes(Plugin by)", sitemap).remove();
        jQuery(sitemap).show();
    }
}
var site_search = {
    init: function() {
        var searchBox = jQuery("form#site_search input#s");
        if (searchBox.attr("value") == "") {
            searchBox.attr("value", "Looking for something?");
        }
        jQuery(searchBox).click(function(i) {
            var sbv = searchBox.attr("value");
            if (sbv == "Looking for something?") {
                searchBox.attr("value","");
            }
        });
    }
}

var latest_photos = {
    init: function() {
        //initial large image
        var numImages = jQuery("#wow_latest_photos_list li").length;
        var randImg = Math.floor(Math.random() * numImages);
        var bigImage = jQuery('#wow_latest_photos_list li:eq(' + randImg + ') img').attr("src");

        jQuery(".flickrImageFull img").attr("src", bigImage);
        var currImgHeight = jQuery(".flickrImageFull img").height();
        if (currImgHeight < 215) {
            jQuery(".flickrImageFull img").attr("height", 215);
        } else {
            jQuery(".flickrImageFull img").attr("height", currImgHeight);
        }
        //thumbnail images
        var tnImages = jQuery("#wow_latest_photos_list li img");
        jQuery(tnImages).each(function(i) {
            var currImgHeight = jQuery(this).height();
            if (currImgHeight < 75) {
                jQuery(this).attr("height", 75);
            }
        });
        jQuery(".flickrImageFull img").attr("src", bigImage);
        var currImgHeight = jQuery(".flickrImageFull img").height();
        if (currImgHeight < 215) {
            jQuery(".flickrImageFull img").attr("height", 215);
        } else {
            jQuery(".flickrImageFull img").attr("height", currImgHeight);
        }
        jQuery('#wow_latest_photos_list').jcarousel({
            scroll: 2,
            wrap: 'last'
        });
        jQuery("#wow_latest_photos_list li a").click(function() {
            jQuery(".flickrImageFull img").attr("src", jQuery('img', this).attr("src"));
            var currImgHeight = jQuery(".flickrImageFull img").height();
            if (currImgHeight < 215) {
                jQuery(".flickrImageFull img").attr("height", 215);
            } else {
                jQuery(".flickrImageFull img").attr("height", currImgHeight);
            }
        });
    }
}

var widget_wow_a_friend = {
    init: function() {
        jQuery("#wow_a_friend_buttons li a").click(function() {
            var whatElem = jQuery(this).text();
            jQuery('.wowFriendButton').removeClass("active");
            jQuery(this).addClass("active");
            jQuery(".steps").hide();
            jQuery('#wow_a_friend_list .step' + whatElem + '').show();
            if (whatElem == 2) {
                jQuery('#sidebar li.wow_a_friend #wow_a_friend_buttons_wrap').css('background-image', 'url("/wp-content/themes/rd2_wow_community_blog/images/bg_wowafriendbottom-step2.png")');
                jQuery('#sidebar #wow_a_friend_panel #wow_a_friend_list').css('margin-bottom', '14px');
            } else if (whatElem == 3) {
                jQuery('#sidebar li.wow_a_friend #wow_a_friend_buttons_wrap').css('background-image', 'url("/wp-content/themes/rd2_wow_community_blog/images/bg_wowafriendbottom-step3.png")');
                jQuery('#sidebar #wow_a_friend_panel #wow_a_friend_list').css('margin-bottom', '14px');
            } else {
                jQuery('#sidebar li.wow_a_friend #wow_a_friend_buttons_wrap').css('background-image', 'url("/wp-content/themes/rd2_wow_community_blog/images/bg_wowafriendbottom-step1.png")');
                jQuery('#sidebar #wow_a_friend_panel #wow_a_friend_list').css('margin-bottom', '52px');
            }
        });
        jQuery(".wow_a_friend_link a.wowFriendButton").click(function() {
            var whatElem = jQuery(this).attr("rel");
            jQuery('.wowFriendButton').removeClass("active");
            jQuery('#wow_a_friend_buttons #wow_a_friend_' + whatElem + '').addClass("active");
            jQuery(".steps").hide();
            jQuery('#wow_a_friend_list .step' + whatElem + '').show();
            if (whatElem == 2) {
                jQuery('#sidebar li.wow_a_friend #wow_a_friend_buttons_wrap').css('background-image', 'url("/wp-content/themes/rd2_wow_community_blog/images/bg_wowafriendbottom-step2.png")');
                jQuery('#sidebar #wow_a_friend_panel #wow_a_friend_list').css('margin-bottom','14px');
            } else if (whatElem == 3) {
            jQuery('#sidebar li.wow_a_friend #wow_a_friend_buttons_wrap').css('background-image', 'url("/wp-content/themes/rd2_wow_community_blog/images/bg_wowafriendbottom-step3.png")');
                jQuery('#sidebar #wow_a_friend_panel #wow_a_friend_list').css('margin-bottom', '14px');
            } else {
            jQuery('#sidebar li.wow_a_friend #wow_a_friend_buttons_wrap').css('background-image', 'url("/wp-content/themes/rd2_wow_community_blog/images/bg_wowafriendbottom-step1.png")');
                jQuery('#sidebar #wow_a_friend_panel #wow_a_friend_list').css('margin-bottom', '29px');
            }
        });
        jQuery("#sidebar li.wow_highest_latest").after('<li class="widgetSpacer afterWowHighestLatest"></li>');
        jQuery("#sidebar li.wow_a_friend").after('<li class="widgetSpacer afterWowFriend"></li>');
    }
}
var widget_polls_widget = {
    init: function() {
        jQuery("#sidebar li.widget_polls-widget").after('<li class="widgetSpacer"></li>');
    }
}
var wow_highest_latest = {
    init: function() {
        jQuery('.highestLatestNav span.highestRated a').click(function() {
            jQuery('.highestLatestNav span a').removeClass('active');
            jQuery(this).css('background-image', 'url("/wp-content/themes/rd2_wow_community_blog/images/button/btn_widgethighest-over.png")');
            jQuery(this).addClass('active');
            jQuery('.highestLatestNav span.latestComments a').css('background-image', 'url("/wp-content/themes/rd2_wow_community_blog/images/button/btn_widgethighest-latest.png")');
            jQuery('#sidebar li #wow_latest_panel').hide();
            jQuery('#sidebar li #wow_highest_panel').show();
        });
        jQuery('.highestLatestNav span.latestComments a').click(function() {
            jQuery('.highestLatestNav span a').removeClass('active');
            jQuery(this).css('background-image', 'url(/wp-content/themes/rd2_wow_community_blog/images/button/btn_widgethighest-latest-over.png)');
            jQuery(this).addClass('active');
            jQuery('.highestLatestNav span.highestRated a').css('background-image', 'url(/wp-content/themes/rd2_wow_community_blog/images/button/btn_widgethighest.png)');
            jQuery('#sidebar li #wow_highest_panel').hide();
            jQuery('#sidebar li #wow_latest_panel').show();
        });
    }
}
var sidebar_twitter_mods = {
    init: function() {
        jQuery('#sidebar li.widget_twitter h2').remove().insertAfter('#sidebar li.widget_twitter ul.twitter');
    }
}
var check_for_pagination = {
    init: function() {
        if (jQuery('.article_pagination a').is(':visible')) {
            jQuery('#footerTop').css("background-image", "url('/wp-content/themes/rd2_wow_community_blog/images/bg_footertop_wpages.png')");
        }
    }
}
var background_flip = {
    night : function(){
        jQuery('#wrapper').addClass('night');
    },
    day : function(){
        jQuery('#wrapper').addClass('day');
    }
}
var main_navigation_list = {
    init: function() {
        var currentUrl = document.URL;
        var catWidgetList = jQuery('#wow_category_nav li');
        jQuery(catWidgetList).each(function(i) {
            if (currentUrl == jQuery('a', this).attr('href')) {
                jQuery(this).addClass('current-cat');
            }
        });
        //alert(currentUrl);
        /*
        var section_links = jQuery('ul#navigation>li>a');
        section_links.each(function(i) {
        jQuery(this).click(function(e){
        var parent = jQuery(this).parent();
				
				var is_current = (parent.hasClass('current_page_item') ||
        parent.hasClass('current_page_parent') ||
        parent.hasClass('current_page_ancestor') )
				
				var child_list = jQuery(this).next('div.subpage_list_wrapper');
				
				if (child_list.length > 0 && !is_current) {
        e.preventDefault();
        child_list.toggle();
        return false;
        }
				
				if (is_current)
        {
        e.preventDefault();
        return false;
        }
        });
        });
        
        if (jQuery("ul#mainNav li:first").hasClass("current_page_item")) {
        jQuery("ul#wow_category_nav li.cat-item-all").addClass("current-cat");
        }
        */
    }
}

var login_widget = {
    init: function() {
        var login_button = jQuery('span#log_in_link a');
        var login_close_button = jQuery('#login_form a.closeButton');
        var login_form = jQuery('form#login_form');
        var register_form = jQuery('form#register_form');

        login_button.click(function(e) {
            login_form.toggle();
            register_form.hide();
            login_button.parent().toggleClass('log_in_link_active');

            e.preventDefault();
            return false;
        });
        login_close_button.click(function(e) {
            login_form.hide();
            e.preventDefault();
            return false;
        });
    },

    show_form: function(opts) {
        var login_button = jQuery('span#log_in_link a');
        var login_form = jQuery('form#login_form');
        login_button.parent().removeClass('log_in_link_active');
        login_form.show();

        if (opts.animate) {
            login_form.animate({ "opacity": ".6" });
            login_form.animate({ "opacity": "1" });
        }
    }
}

var widget_sidebar = {
    init: function() {
        // initialization code for the widget sidebar
        jQuery('.widget_tag_cloud').addClass('collapse');
        jQuery('.widget_archive').addClass('collapse');
        jQuery('.widget_links').addClass('collapse');
        jQuery('li.collapse h2').next().addClass("widgetContent");
        jQuery('li.collapse > *:not(h2)').hide();

        jQuery('.collapse h2').bind('click', function() {

            if (jQuery(this).next().is(':hidden')) {
                jQuery('.widgetContent').slideUp('fast');
                jQuery('h2.widgettitle').removeClass('active');
                jQuery(this).addClass('active');
                jQuery(this).next().slideDown('fast');
            }
        });

    }
}

jQuery(document).ready(function($) {
    main_navigation_list.init();
    login_widget.init();
    widget_sidebar.init();
    check_for_pagination.init();
    sidebar_twitter_mods.init();
    widget_wow_a_friend.init();
    widget_polls_widget.init();
    wow_highest_latest.init();
    latest_photos.init();
    site_search.init();
    sitemap_cleanup.init();
    jQuery('#ribbon').supersleight({ shim: '/wp-content/themes/rd2_wow_community_blog/js/transparent.gif' });
    jQuery('#flickrImageFullMask').supersleight({ shim: '/wp-content/themes/rd2_wow_community_blog/js/transparent.gif' });
    jQuery('.flickrImageItemMask').supersleight({ shim: '/wp-content/themes/rd2_wow_community_blog/js/transparent.gif' });
    jQuery('#footerTop').supersleight({ shim: '/wp-content/themes/rd2_wow_community_blog/js/transparent.gif' });
    //jQuery('#wow_a_friend_buttons_wrap').supersleight({ shim: '/wp-content/themes/rd2_wow_community_blog/js/transparent.gif' });
    jQuery('#footer ul#socialSites').supersleight({ shim: '/wp-content/themes/rd2_wow_community_blog/js/transparent.gif' });
    jQuery('#wow_latest_photos_panel .jcarousel-prev').supersleight({ shim: '/wp-content/themes/rd2_wow_community_blog/js/transparent.gif' });
    jQuery('#wow_latest_photos_panel .jcarousel-next').supersleight({ shim: '/wp-content/themes/rd2_wow_community_blog/js/transparent.gif' });
    jQuery("#wowMomentsCarouselWrap ul ul").attr("id", "wowMomentsCarousel");

    var numMoments = jQuery("ul#wowMomentsCarousel li").length;
    var randomMoments = jQuery("ul#wowMomentsCarousel li").get().sort(function() {
        return Math.round(Math.random()) - 0.5
    }).slice(0, numMoments);

    jQuery("#wowMomentsCarouselWrap ul#wowMomentsCarousel li").remove();

    //put moments into final random order
    jQuery(randomMoments).each(function() {
        jQuery(this).appendTo("ul#wowMomentsCarouselOrig");
    });

    jQuery("#wowMomentsCarouselWrap ul#wowMomentsCarouselOrig li a").attr('target', '_blank');
    jQuery("#wowMomentsCarouselWrap ul#wowMomentsCarouselOrig li a.rsswidget").attr('title', '');

    jQuery('#wowMomentsCarousel').jcarousel({
        scroll: 1,
        wrap: 'circular',
        itemVisibleInCallback: { onBeforeAnimation: mycarousel_itemVisibleInCallback },
        itemVisibleOutCallback: { onAfterAnimation: mycarousel_itemVisibleOutCallback }
    });
    jQuery('#wowMomentsCarouselMask').fadeOut();
    jQuery('.article_pagination').remove().appendTo("#contentWrap");
    jQuery('.flickrImageGalleryItem img').css('border', '0');

});
function wowMomentsCarouselItems() {
    var items = [];
    jQuery('ul#wowMomentsCarouselOrig li').each(function() {
        items.push(jQuery(this).html());
    });
    return items;
}
function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt) {
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var mycarousel_itemList = wowMomentsCarouselItems();
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
    carousel.remove(i);
};
function mycarousel_getItemHTML(item) {
    return '<div class="item">' + item + '</div>';
};
