﻿function GetCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1)
        { 
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end==-1)
            {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}


    function switchClass(controlContainerId, controlList, elmContainerId, elmList, currentDisplayElmId, currentOnClickElmId)
    {
     this._controlContainerId = controlContainerId;
     this._controlList = controlList;
     this._elmContainerId = elmContainerId;
     this._elmList = elmList;
     this._currentDisplayElm = currentDisplayElmId;
     this._currentOnClickElm = currentOnClickElmId;
    }

     switchClass.prototype.getTagArray=function(id, tag)
    {
    var _id = document.getElementById(id)
    return _id.getElementsByTagName(tag)
    }
    

    switchClass.prototype.switchContent=function(index)
    { 
    var _controlCon = this.getTagArray(this._controlContainerId, this._controlList)
    var _elmCon = this.getTagArray(this._elmContainerId, this._elmList)
    
    for(i = 0 ; i < _elmCon.length; i++ )
    {
     _elmCon[i].id=null     
    }
    _elmCon[index].id = this._currentDisplayElm  
    }
    



   function getTagArray(id, tag)
    {
    var _id = document.getElementById(id)
    return _id.getElementsByTagName(tag)
    }
    
function mailboxOpen(target)
{
var controlList = getTagArray("mailbox-ans", "a");
var elmList = getTagArray("mailbox-ans", "div");

   for(i=0; i<controlList.length; i++)
   {
  if(controlList[i] == target && controlList[i].className=="open")
   {
   controlList[i].style.display="none";
 
   var divIndex = i/2
  if(i==0){divIndex=0}
   elmList[divIndex+1].style.display="block";
   }
   }
}
  function mailboxClose(target)
  {
    var controlList = getTagArray("mailbox-ans", "a");
    var elmList = getTagArray("mailbox-ans", "div");
    for(i=0; i<controlList.length; i++)
   {
   if(controlList[i] == target && controlList[i].className=="close")
   {
     controlList[i-1].style.display="block";

        var divIndex = (i-1)/2
    
    if(i==0){divIndex=0}
    elmList[divIndex+1].style.display="none";
   }
   }
  }

function switchContent(index, cssClass)
{

    var controlList = getTagArray("btnElement", "a");
     var elmList = getTagArray("swithElement", "table");
    
    for(i=0; i<elmList.length; i++)
    {
        elmList[i].style.display="none";
        controlList[i].className=cssClass.substr(0,cssClass.length-1)+i   
    }

    controlList[index].className=cssClass+"_current"; 
      
    elmList[index].style.display="block";
}


var menuids=new Array("verticalmenu") 
var submenuoffset=0;
function createcssmenu(){
for (var i=0; i<menuids.length; i++){

  var ultags=document.getElementById(menuids[i]).getElementsByTagName("ul")
    for (var t=0; t<ultags.length; t++){

    var spanref=document.createElement("span")
		spanref.className="arrowdiv"
		spanref.innerHTML="&nbsp;&nbsp;"
		ultags[t].parentNode.getElementsByTagName("a")[0].appendChild(spanref)
    ultags[t].parentNode.onmouseover=function(){
	isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1
	if (isIE6) {
		this.getElementsByTagName("ul")[0].style.left=(this.parentNode.offsetWidth+submenuoffset + 0 )+"px"
	} else {
		this.getElementsByTagName("ul")[0].style.left=(this.parentNode.offsetWidth+submenuoffset + 16 )+"px"
	}
	this.getElementsByTagName("ul")[0].style.position="absolute"
	this.getElementsByTagName("ul")[0].style.zIndex="1000"
    this.getElementsByTagName("ul")[0].style.display="block"
   
    }
    ultags[t].parentNode.onmouseout=function(){
   this.getElementsByTagName('ul')[0].style.display='none' 
    }
    var secondLvItem = ultags[t].parentNode.getElementsByTagName('ul')[0].getElementsByTagName("a")
        for(j=0; j<secondLvItem.length; j++)
        {
             var img = ".gif"
            var statusON = "_on"
             secondLvItem[j].onmouseover=function()
            {
            var src = this.childNodes[0].src
            var newSrc = src.split(img)[0]
            this.childNodes[0].src = newSrc+statusON+img
            }
            secondLvItem[j].onmouseout=function()
            {
            var src = this.childNodes[0].src
            var newSrc = src.split(img)[0]
            newSrc = newSrc.split(statusON)[0]
             this.childNodes[0].src = newSrc+img
            }
            
        }
    }
  }
}

function openNewWin(path, width, height)
{
var newWin = window.open(path, "JohnsonsBaby", "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width="+width+",height="+height+"");
newWin.focus();
}
function openBetterSleep()  
{	
     var path = "/bettersleep/default.aspx";
    openNewWin(path,'623','620');
}
function openBabyAlbumDisclaimer()  
{	
     var path = "/babyalbum/disclaimer.aspx";
    openNewWin(path,'514','220');
}
function openBedTime()
{	
     var path = "/expert/faq.aspx";
	//var newWin = window.open(path, "JohnsonsBabyFAQ", "toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,fullscreen=yes");
	var newWin = window.open(path, "JohnsonsBabyFAQ");
	newWin.focus();
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function topsearch_cleartext(me) {
if (me.value == "請輸入關鍵字") {
    me.value="";
}
}
function topsearch_defaulttext(me) {
if (me.value == "") {
    me.value="請輸入關鍵字";
}
}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        if (window[movieName] != null)
            return window[movieName];
        else
            return document[movieName];
    } else {
        return document[movieName];
    }
}
function VideoTracking(label) {
    this.ACTION_PLAY = "PLAY";
    this.ACTION_PAUSE = "PAUSE";
    this.ACTION_UNLOAD = "UNLOAD";
    this.GA_EVENT_DURACTION = "DURATION";
    this.CATEGORY_VIDEOS = "Videos";
    this.action = "";
    this.label = label;
    this.play_time = 0;
    this.pause_time = 0;
    this.last_playing_dt = 0;
    this.overall_duration = 0;
    this.TrackPlay = function(video_label, play_time) {
        if (this.IsPlaying()) {
            var duration = this.GetPlayDuration();
            // play -> play, track pause for last playing video, use timer duration
            this.TrackGAEvent(this.ACTION_PAUSE, this.play_time + duration);
            this.overall_duration += duration;
        }
        this.action = this.ACTION_PLAY;
        this.play_time = play_time;
        this.pause_time = 0;
        this.label = video_label;
        this.TrackGAEvent(this.ACTION_PLAY, this.play_time);
        this.last_playing_dt = (new Date()).getTime();
    }
    this.TrackPause = function(video_label, pause_time) {
        this.pause_time = pause_time;
        if (this.IsPlaying()) {
            // play -> pause
            this.overall_duration += this.GetPlayDuration();
            this.TrackGAEvent(this.ACTION_PAUSE, this.pause_time);
        }
        this.action = this.ACTION_PAUSE;
    }
    this.TrackUnload = function(video_label, unload_time) {
        if (this.IsPlaying()) {
            // play -> pause
            this.pause_time = unload_time;
            debug_write("TrackUnload-play_time: " + this.play_time);
            var duration = this.GetPlayDuration();
            this.overall_duration += duration;
            if (this.pause_time == 0) {
                this.pause_time = this.play_time + duration;
                debug_write("TrackUnload-pause_time: " + this.play_time);
            }
        }
        debug_write("TrackUnload-overall_duration: " + this.overall_duration);
        this.TrackGAEvent(this.ACTION_UNLOAD, this.pause_time);
        // this.TrackGAEvent(this.GA_EVENT_DURACTION, this.overall_duration);
        this.action = this.ACTION_UNLOAD;
        this.Reset();
    }
    this.Reset = function() {
        this.play_time = 0;
        this.pause_time = 0;
        this.last_playing_dt = 0;
        this.overall_duration = 0;
        this.label = "";
    }
    this.IsPlaying = function() {
        return this.action == this.ACTION_PLAY;
    }
    this.GetPlayDuration = function() {
        var duration = 0;
        if (this.pause_time != 0) {
            var duration = this.pause_time - this.play_time;
            duration = (duration > 0 ? duration : 0);
        } else {
            var stop_playing_dt = (new Date()).getTime();
            var duration = parseInt((stop_playing_dt - this.last_playing_dt) / 1000);
            // max video length is 600 second
            duration = ((duration > 0 && duration <= 600) ? duration : 0);
        }
        // debug_write("duration = " + duration);
        return duration;
    }
    this.TrackGAEvent = function(action, time) {
        debug_write("GA: action = " + action + ", label = " + this.label + ", time = " + time);
        TrackGAEvent(this.CATEGORY_VIDEOS, action, this.label, time);
    }
}

function facebookShare() {
	TrackVisit('FacebookShareClick');
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

