var baseUrl = "";

$(window).load
(
	function()
	{
		if (jQuery.browser.msie && parseInt(jQuery.browser.version.substr(0, 1)) < 7)
		{
			$("img.png").each
			(
				function()
				{
					if($(this).attr("src") != "/na/images/blank.gif"){
						$(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $(this).attr("src") + "', sizingMethod='scale')").attr("src", baseUrl + "/images/blank.gif");
					}
				}
			);
			
			$("div.png, span.png").each
			(
				function()
				{
					var bg = $(this).css("background-image");
					bg.match(/^url[("']+(.*\.png)[)"']+$/i);
					bg = RegExp.$1;
					$(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bg + "', sizingMethod='crop')").css("background-image", "none");
				}
			);
		}
	}
);

$(document).ready
(
	function()
	{
		$(".search-box .text").focus
		(
			function()
			{
				if (this.value == "Search")
				{
					this.value = "";
				}
			}
		);

		$(".search-box .text").blur
		(
			function()
			{
				if (this.value == "")
				{
					this.value = "Search";
				}
			}
		);

		$(".left-navigation ul li ul").each
		(
			function()
			{
				var collapseMenu = true;

				if ($(this).children().length > 3)
				{
                    if ($(this).parent("#pyd-navigation").length >= 1) {
				        collapseMenu = false;
				    }
				    
					for (i = 0; i < $(this).children().length; i++)
					{
						if ($(this).children().eq(i).hasClass("active"))
						{
							collapseMenu = false;
							break;
						}
					}
				}
				else
				{
					collapseMenu = false;
				}

				if (collapseMenu)
				{
					$(this).css("height", $(this).children().eq(0).outerHeight() + $(this).children().eq(1).outerHeight() + $(this).children().eq(2).outerHeight() + 24);

					$(this).parents("li").append("<a href=\"javascript:void(0)\" class=\"view-more\">View More</a>");
					$(this).parents("li").append("<a href=\"javascript:void(0)\" class=\"view-less\">View Less</a>");
				}
			}
		);

		$(".view-more").click
		(
			function()
			{
				var i;
				var height = 0;

				for (i = 0; i < $(this).parents("li").children("ul").eq(0).children().length; i++)
				{
					height = height + $(this).parents("li").children("ul").eq(0).children().eq(i).outerHeight() + 8;
				}

				$(this).parents("li").children("ul").eq(0).animate({ "height": height }, 500);

				$(this).css("display", "none");
				$(this).parents("li").children(".view-less").css("display", "block");
			}
		);

		$(".view-less").click
		(
			function()
			{
				$(this).parents("li").children("ul").eq(0).animate({ "height": $(this).parents("li").children("ul").eq(0).children().eq(0).outerHeight() + $(this).parents("li").children("ul").eq(0).children().eq(1).outerHeight() + $(this).parents("li").children("ul").eq(0).children().eq(2).outerHeight() + 24 }, 500);

				$(this).css("display", "none");
				$(this).parents("li").children(".view-more").css("display", "block");
			}
		);
		
		$("a.target_blank").click
		(
			function()
			{
				this.target = "_blank";
			}
		);
		
		initiateShare();
		
	    // Pick Your Day - facts accordion
        $(".pyd-accordion-window").each(function() { // initialize
            if (! $(this).hasClass("pyd-accordion-window-selected")) {
                $(this).hide(0);
            }
        });
        
	    $(".pyd-accordion-click").click(function() { // change accordion display
	        if ($(this).parents(".pyd-accordion-section").find(".pyd-accordion-window").hasClass("pyd-accordion-window-selected")) {
	            $(".pyd-accordion-window").removeClass("pyd-accordion-window-selected");
	        } else {
	            $(".pyd-accordion-window").removeClass("pyd-accordion-window-selected");
	            $(this).parents(".pyd-accordion-section").find(".pyd-accordion-window").addClass("pyd-accordion-window-selected");
	        }
	        
	        $(".pyd-accordion-window").each(function() {
	        
	            var plusStr = '<img alt="+" src="' + baseUrl + '/images/pyd-facts-plus.png" />'
	            var minusStr = '<img alt="+" src="' + baseUrl + '/images/pyd-facts-minus.png" />'
	            
	            if ($(this).hasClass("pyd-accordion-window-selected")) {
	                if (! $(this).is(":visible")) {
	                    $(this).show("blind", {direction: "vertical" }, "slow", function() {
	                        // console.debug($(this).parents("div.pyd-accordion-section").position().top );
                	        $(window).scrollTop($(this).parents("div.pyd-accordion-section").position().top );
	                    }).parents(".pyd-accordion-section").find(".pyd-accordion-indicator").html(minusStr);
	                }
	            } else {
	                if ($(this).is(":visible")) {
	                    $(this).hide("blind", {direction: "vertical" }, "slow");
	                }
	                $(this).parents(".pyd-accordion-section").find(".pyd-accordion-indicator").html(plusStr);
	            }
	        });
	        // console.debug($(this).parents("div.pyd-accordion-header").position().top );
	    });
	    
	    // Pick Your Day - Questions page
	    
	    // Pagination
        initPagination();
	    
	    // More/Less maturity mini-accordions
	    // split out of document.ready into separate function for calendar page tabs? or, live click handler?
	    $("a.pyd-qa-expand-link").live('click', (function() {
	        var showStr, hideStr;
	        var navRoot = $(this).parents(".pyd-qa-expand");
	        var calendarPage = ($("#eventOverlay").length >= 1);

	        if ($(this).hasClass("pyd-qa-more-mature-link")) {
	            showStr = "more";
	            hideStr = "less";
	        } else {
	            showStr = "less";
	            hideStr = "more";
	        }
	        
	        if (! navRoot.find(".pyd-qa-" + showStr + "-mature-text").is(":visible")) {
	            if (calendarPage)
	                navRoot.find(".pyd-qa-" + showStr + "-mature-text").show(0);
	            else
	                navRoot.find(".pyd-qa-" + showStr + "-mature-text").show("blind", {direction: "vertical" }, "slow");
	            navRoot.find(".pyd-qa-" + showStr + "-mature-link .pyd-qa-indicator").html("-");
	        } else {
	            if (calendarPage)
    	            navRoot.find(".pyd-qa-" + showStr + "-mature-text").hide(0);
    	        else
	                navRoot.find(".pyd-qa-" + showStr + "-mature-text").hide("blind", {direction: "vertical" }, "slow");
	            navRoot.find(".pyd-qa-" + showStr + "-mature-link .pyd-qa-indicator").html("+");
            }
            
	        if (navRoot.find(".pyd-qa-" + hideStr + "-mature-text").is(":visible")) {
	            if (calendarPage)
	                navRoot.find(".pyd-qa-" + hideStr + "-mature-text").hide(0);
	            else
	                navRoot.find(".pyd-qa-" + hideStr + "-mature-text").hide("blind", {direction: "vertical" }, "slow");
	            navRoot.find(".pyd-qa-" + hideStr + "-mature-link .pyd-qa-indicator").html("+");
	        }
	        
	        if (calendarPage) {
	            updateOverlayTop();
	            // move buttons
	            if ( (navRoot.find(".pyd-qa-less-mature-text").is(":visible")) ||
	                 (navRoot.find(".pyd-qa-more-mature-text").is(":visible")) )
	                $('.pagination-section-button:visible').height($('.pagination-section-content:visible').height());
	            else
	                $('.pagination-section-button:visible').css('height', '');
	        }
	        
	        return false;
	    }));
		
	}
);

/*
 *  Pick Your Day - Calendar page
 *  Load dynamic content for paginated tabs
 */
function loadPopupContent(targetStr) {

	var params = new Object;
	params.sectionId = targetStr;

	switch (targetStr) {
        case "qaDay":
        case "iceBreaker":
        case "factDay":
            pathStr = "#eventOverlay .overlayContentBox";
            break;
		case "product":
		case "emailReminder":
		case "talkDay":
		case "shareDay":
            pathStr = "#eventOverlay .floodlightContentBox";
			break;
		default:
			return;
	}

	$.post(baseUrl + "/articles-info/my-daughters-period/pick-your-day/popups",
		params,
		function(data) {
		    var content = $(data).find("body").html();
			$(pathStr).html(data);
			initPagination();
			addthis.button('.addthis-share');
			if ( $('a#new-day-storage').attr('href') != '#') {
			    $('.pyd-footer-new-day:visible a').attr('href', $('a#new-day-storage').attr('href'));
			} else {
			    $('.pyd-footer-new-day:visible a').hide(0);
			}
		});

}


// Pick Your Day - Commit to Talk - pagination functions
/**
 * Callback function that displays the content.
 *
 * Gets called every time the user clicks on a pagination link.
 *
 * @param {int} page_index New Page index
 * @param {jQuery} jq the container with the pagination links as a jQuery object
 */
function pageselectCallback(page_index, jq){
    var itemsPerPage = paginationItemsPerPage();

    var new_content = Array();
    page_index = (itemsPerPage * page_index);
    for (i = 0; i < itemsPerPage; i++) {
        new_content[i] = jQuery('.pagination-wrapper:visible .pagination-data div.pagination-section:eq('+(page_index+i)+')').clone();
    }
    $('.pagination-wrapper:visible .pagination-content').empty();
    for (var i in new_content) {
        $('.pagination-wrapper:visible .pagination-content').append(new_content[i]);
    }
    
    // calendar popup-based code
    if ($("#eventOverlay").length >= 1)
    {
        // set pagination buttons
        var newHeight = Math.floor( $('.eventOverlayContent:visible .pagination-section-content').height() );
        // $('.eventOverlayContent:visible .pagination-section-button').height(newHeight);

        var popupButtonHideStr = false;    
        popupButtonHideStr = ( $('.pagination:visible span.prev').length >= 1) ? ".pagination-button-prev" : popupButtonHideStr;
        popupButtonHideStr = ( $('.pagination:visible span.next').length >= 1) ? ".pagination-button-next" : popupButtonHideStr;
        
        $('.eventOverlayContent:visible .pagination-content .pagination-button-prev-disabled').hide();
        $('.eventOverlayContent:visible .pagination-content .pagination-button-next-disabled').hide();
        
        if (popupButtonHideStr) {
            $('.eventOverlayContent:visible .pagination-content ' + popupButtonHideStr).hide();
            $('.eventOverlayContent:visible .pagination-content ' + popupButtonHideStr + '-disabled').show();
        }
        
        $('.pagination-section-button a').click(function() {
            if ($(this).hasClass('pagination-button-prev')) $(".pagination-wrapper:visible .pagination-pages").trigger('prevPage');
            if ($(this).hasClass('pagination-button-next')) $(".pagination-wrapper:visible .pagination-pages").trigger('nextPage');
        });
        
        // add class based on whether popup is extended up or down
        var popupPositionStr = ($('#eventOverlayArrow').hasClass('bottom')) ? 'pagination-section-bottom' : 'pagination-section-top';
        $('.pagination-content:visible').addClass(popupPositionStr);

        updateOverlayTop();
    }
    
    return false;
}

/**
 * Initialisation function for pagination
 */
function initPagination() {
    var itemsPerPage = paginationItemsPerPage();

    // count entries inside the hidden content
    var num_entries = jQuery(".pagination-wrapper:visible div.pagination-data div.pagination-section").length;
    // Create content inside pagination element
    if ((num_entries > 0) && ($(".pagination-wrapper:visible .pagination-pages").length > 0)) {
        $(".pagination-wrapper:visible .pagination-pages").pagination(num_entries, {
            callback: pageselectCallback,
            items_per_page: itemsPerPage,
            prev_text: "Previous"
        });
    }
 }

/**
 * Determine count by items on page
 */
function paginationItemsPerPage() {
    paginationItemsPerPage.itemsPerPage = ($(".pyd-qa-data .pyd-qa").length >= 1)
        ? 3
        : (($(".pyd-convo-data .pyd-convo").length >= 1)
            ? 4
            : 1);
    // override for calendar popups
    if ($("#eventOverlay").length >= 1)
    {
        paginationItemsPerPage.itemsPerPage = 1;
    }
    return paginationItemsPerPage.itemsPerPage;
}



// U by Kotex Tween - combo pack swap
function productSwap(featureStr)
{
    var showDiv, hideDiv;
    switch (featureStr)
    {
        case 'alt':
            showDiv = $('.product-features-alt');
            hideDiv = $('.product-features-base');
            break;
        case 'base':
        default:
            showDiv = $('.product-features-base');
            hideDiv = $('.product-features-alt');
    }
    
    showDiv.show(0);
    hideDiv.hide(0);
}

function resizeIframe(height)
{
	document.getElementById("content_ifr").style.height = height + 35 + "px";
}

function openNewWindow(url, name)
{
	var newWindow = window.open(url, name);
}

function initiateShare()
{
	
	addthis_disable_flash = true;
	
	if($(".addthis-share"))
	{
		addthis.button(".addthis-share");
		//Add In WT Tracking 
		$(".addthis-share").each
		(
			function()
			{
				if($(this).attr("addthis:url"))
				{
					$(this).attr("addthis:url", $(this).attr("addthis:url") + "&WT.mc_id=addthis");
				}
				else
				{
					$(this).attr("addthis:url", document.location + "?WT.mc_id=addthis");
				}
			}
		);
	}
}

//Base Sharing Configuration
var addthis_config =
{
	ui_hover_direction:1,
	services_compact: "facebook,email,twitter,favorites,more",
	//ui_cobrand: "Break The Cycle",
	//ui_header_background: "#000000",
	ui_offset_left: "-16",
	ui_offset_top: "130",
	//ui_header_color: "#ffffff",
	ui_click: true,
	data_track_linkback: true,
	data_ga_tracker: "ognc.tracking.pageTracker",
	ui_use_addressbook: true
}
