﻿function Depend()
{		
	//constructor
    this.Init = function() {
        SetBrowserType();
        InitiateSearchBox();
        InitiateNavigation();
        IE6PngFix();

        var self = this;

        $("a.target_blank").click
		(
			function() {
			    this.target = "_blank";
			}
		);

        $("img.seeProducts").hover(
			function() {
			    self.oldFilter = $(this).css("filter");
			    self.oldSrc = $(this).attr("src");
			    $(this).attr("src", "/images/see_products_rl.png").addClass("png");
			    IE6PngFix();
			},
			function() {
			    $(this).attr("src", self.oldSrc).addClass("png");
			    //IE6PngFix();
			    $(this).css("filter", self.oldFilter);
			}
		);

        /*Click to Chat JS*/
        var isChatServiceAvailable = false;

        testStatus = function() {
            if (chat.status == 'ready') {
                isChatServiceAvailable = true;
                return true;
            } else {
                isChatServiceAvailable = false;
                return false;
            }
        };

        /*Click to Chat*/
        $('a.cmdChat').click(function() {
            if (isChatServiceAvailable) {
                chat.start({
                    info: {
                        'product': 'Depend'
                    }
                });
            } else {
                var msgWindow = window.open('', 'chatWindow', 'top=100,left=100,height=375,width=375,menubar=0,resizable=0,location=0,status=0,scrollbars=0');
                var htmlString = "";
                htmlString += "<html><head><title>Popup</title><link rel='stylesheet' type='text/css' href='/css/chat.css' /></head>";

                var chatMessage = chat.msg;

                if (chatMessage == null || chatMessage == "" || chatMessage == null) {
                    chatMessage = "Live chat is currently unavailable.  Please check back with us later or you can call 1-877-413-3736.";
                }
                htmlString += "<body><div id='offTitle'><span><em>Sorry.</em></span></div><div id='offMessage'>" + chatMessage + "</div></body>";
                htmlString += "</html>";
                msgWindow.document.write(htmlString);
               
            }
        });

        var cssFilePath = "css/chat.css";
        var cssFile = "";

        if (document.location.host == "www.depend.com" || document.location.host == "www.us.depend.com") {
            cssFile = 'https://www.livechat.kimberly-clark.com/' + cssFilePath;
        } else {
            cssFile = 'https://www.qa.livechat.kimberly-clark.com/' + cssFilePath;
        }

        chat = Wt.chat.PublicChat;
        chat.init({
            site: 'www.depend.com',
            //site: 'test',
            statusCallback: testStatus,
            startOptions: {
                window: {
                    top: 100,
                    left: 100,
                    width: 375,
                    height: 545,
                    cssUrl: cssFile,
                    logoUrl: '',
                    headerHtml: '',
                    footerHtml: '<p>Your comments will be treated in confidence.</p>',
                    title: 'Chat with a Live Representative! '
                },
                initialText: 'init_text',
                info: {
                    'subject': 'Depend'
                }
            }
        });
    }
	
	this.oldSrc = "";
	this.oldFilter = ""
	
	this.pngFix = function ()
	{
		IE6PngFix();
	}
	
	//public properties
	this.BrowserType = "";
	
	
	//public methods
	this.setHomepageStory = function(storyID)
	{
		ognc.setCookie("storyID", storyID);
	}
	
	this.GetBrowserType = function()
	{
		return $("#wrapper").attr("class");
	}
	
	this.Bookmark = function(Anchor)
	{
		if (window.sidebar) //firefox
		{
			window.sidebar.addPanel(document.title, Anchor.location, "");
		}
		else if (window.opera && window.print) //opera
		{
			var elem = document.createElement("a");
			elem.setAttribute("href", Anchor.location);
			elem.setAttribute("title", document.title);
			elem.setAttribute("rel", 'sidebar');
			elem.click();
		} 
		else if(document.all) //ie
		{
			window.external.AddFavorite(Anchor.location, document.title);
		}
	}
	
	
	//private methods
	function SetBrowserType()
	{
		this.BrowserType = $("#wrapper").attr("class");
	}
	
	function InitiateSearchBox()
	{
		$("#search-box .text").focus
		(
			function()
			{
				if (this.value == "Search")
				{
					this.value = "";
				}
			}
		);
		
		$("#search-box .text").blur
		(
			function()
			{
				if (this.value == "")
				{
					this.value = "Search";
				}
			}
		);
	}
	
	function InitiateNavigation()
	{
		$("#get-samples-link a").append("<span class=\"dropshadow\">" + $("#get-samples-link a").html() + "</span>");
		$("#get-samples-link a").css("color", "#555555");
		
		if (this.BrowserType != "IE6")
		{
			$("#primary-navigation li a").each
			(
				function()
				{
					if (!$(this).parents(0).hasClass("selected"))
					{
						$(this).append("<span class=\"dropshadow\">" + $(this).html() + "</span>");
						$(this).addClass("dropshadow-color");
					}
					
					$(this).append("<span class=\"line\"></span>");
					$(this).children(".line").width(Math.round($(this).children(".line").width() / 4) * 4);
				}
			);
			
			$("#get-samples-link a").append("<span class=\"line\"></span>");
			$("#get-samples-link a .line").width(Math.round($("#get-samples-link a .line").width() / 4) * 4);
		}
		else
		{
			$("#primary-navigation li a").each
			(
				function()
				{
					if (!$(this).parents(0).hasClass("selected"))
					{
						$(this).append("<span class=\"dropshadow\">" + $(this).html() + "</span>");
						$(this).addClass("dropshadow-color");
					}
					
					$(this).append("<span class=\"line\" style=\"width:" + Math.round(($(this).parents(0).width() * 76) / 400) * 4 + "px\"></span>");
				}
			);
			
			$("#get-samples-link a").append("<span class=\"line\" style=\"width:" + Math.round(($("#get-samples-link a").parents(0).width() * 77) / 400) * 4 + "px\"></span>");
			
			$("#get-samples-link a").mouseenter
			(
				function()
				{
					$("#get-samples-link a span.dropshadow").css("color", "#cccccc");
				}
			);
			
			$("#get-samples-link a").mouseleave
			(
				function()
				{
					$("#get-samples-link a span.dropshadow").css("color", "#ffffff");
				}
			);
		}
	}
	
	function IE6PngFix()
	{
		if (this.BrowserType == "IE6")
		{
			$("img.png").each
			(
				function()
				{
					$(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $(this).attr("src") + "', sizingMethod='image')").attr("src", "/images/blank.gif").removeClass("png");
				}
			);
			
			$("div.png, span.png, ul.png, li.png, a.png, h3.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='scale')").css("background-image", "none").removeClass("png");
				}
			);
		}
	}
}

function flagPost(postid) {
    $.post("/Pages/Shared/FlagPost.aspx", { postid: postid }, function(data) {
        deactivateFlag(postid,data);
    });
}

function deactivateFlag(postid, data) {
    //alert("Post ID: " + postid + " Data Loaded: " + data);
    if (data == "True") {
        var element = document.getElementById(postid);
        element.innerHTML = "<strong>Thank You for Flagging</strong>";
    }

}

var self = this;
var video_id = "";
var content_id = "";
var video_title = "";
var end_frame = "";
var destoryData = true;
var contentType = "video";
var playerColor = "green";

//Show video overlay
function showVideoFragment(argColor,contentID, videoID, videoTitle, divID, fragmentPath, endFrame) {
    content_id = contentID;
    video_id = videoID;
    video_title = videoTitle;
    end_frame = endFrame;
    playerColor = argColor;
    showFragment(divID, fragmentPath);
}

//Show gen overlay
function showFragment(id, fragmentPath) {
    // clear out any previous content
    $("#overlay_content_inner").empty();

    // create the div		
    $('<div id="' + id + '"></div>').appendTo("#overlay_content_inner");

    // load the fragment
    $("#" + id).load(fragmentPath, '',

    // when complete fade in
			function() {
			    createOverlay();
			});
}

//Overlay template
function createOverlay() {
    var windowHeight = $(document).height();
    $('#overlay').css('height', windowHeight);
    $('#overlay').fadeIn("med");

    $("#page_container select").hide() /* IE6 Fix: for the select z-index bug */

    moveOverlay();
    window.setTimeout(moveOverlay, 100);
    window.setTimeout(moveOverlay, 500);
    window.onresize = moveOverlay;
    window.onscroll = moveOverlay;
}

//Adjust overlay based on browser window
function moveOverlay() {
    var yPos;
    var contentHeight = $("#overlay_content_container").outerHeight();

    // check for mission footer
    if ($("#mission_footer").css('display') == 'none') {
        yPos = $(window).height() / 2 - contentHeight / 2 + $(window).scrollTop();
    } else {
        yPos = ($(window).height() - $("#mission_footer").outerHeight()) / 2 - contentHeight / 2 + $(window).scrollTop();
    }

    // if the item is larger then available do not center
    if ($("#mission_footer").css('display') == 'none') {
        if (contentHeight > $(window).height()) {
            yPos = $("#overlay_content_container").css('top');
        }
    } else {
        if (contentHeight > $(window).height() - $("#mission_footer").outerHeight()) {
            yPos = 20;
            console.log(yPos);
        }
    }
    if (yPos <= 0) { yPos = 0; }
    //$("#overlay_content_container").css("top", yPos);
    $("#overlay_content_container").stop();
    $("#overlay_content_container").animate({ top: yPos }, 200);
}

//Remove overlay from view
function closeOverlay() {

    if ($('#video_embed_flash').length) {
        $('#video_embed_flash').remove();
    }

    $('#overlay').fadeOut("med", 'linear', function() {

        if (destoryData == true) {
            $("#overlay_content_inner").empty();
        }
    });
    $("#page_container select").show() /* IE6 Fix: for the select z-index bug */
    window.onresize = '';
    window.onscroll = '';
}

var depend = new Depend();

$(document).ready
(
	function()
	{
		depend.Init();
	}
);

	//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: "0",
    ui_offset_top: "25",
    //ui_header_color: "#ffffff",
    ui_click: true,
    data_track_linkback: true,
    data_ga_tracker: "ognc.tracking.pageTracker",
    ui_use_addressbook: true
}
