/**
 * Define the Oprah.com namespace
 */

 var Odotcom = window.Odotcom || {};

/**
 * Application javascripts
 * dependancies: prototype & scriptaculous
 * @name Plugger
 * @type prototype & scriptaculous
 *
 * The plugger object is used to toggle an open/closed state of container elements.
 */

Odotcom.Plugger = {
  duration:     0.5,
  visible:      false,
  toggle: function(item,link) {
    $(link).down('span').innerHTML = (this.visible) ? '+' : '&ndash;';
    if(document.all) $(item).down(0).style.visibility = 'hidden';
    new Effect.toggle(item, 'blind', {duration: this.duration,
      afterFinish: function() {
        if(!this.visible) { new Effect.ScrollTo(item, {duration:.9, offset: -300}); }
        if(document.all) $(item).down(0).style.visibility = 'visible';
        this.visible = (this.visible) ? false : true;
      }.bind(this)
    });
  }
};

/**
 * @name Carousel
 * 
 * Directional links that trigger movement must define their direction via the hash (#back & #next || #up & #down).
 * All directional links should be joined under a common class name, such as .move_button (<a class="move_button"...)
 * 
 * @type prototype & scriptaculous
 * @param element -- Main element that actts as moving container of slide elements
 * @param links -- Link elements that trigger directional movement
 * @option vertical:bool -- Switch the carousels movement from horizontal to vertical
 * @option increment:number -- Use to override default slide amount, or fine tune slide amount
 * @option mark:number -- Use to set initial view (ie. 4 slides available, start on 1-4)
 * @option skip:number -- Use to slide more than one slide at a time - good for when elements are viewed 2+ elements at a time
 * @option duration:number -- Use to set (in seconds) the duration of the slide effect
 */
var Carousel = Class.create({  
  _moving:   false,
  vertical:  false,
  increment: null,
  duration:  0.5,
  mark:      1,
  skip:      1,
  initialize: function(elem,links,opts) {
    if (opts instanceof Object) {
      for (key in opts) this[key] = opts[key];
    } 
    this.element = $(elem);
    this.links  = $$(links);
    this.links.invoke('observe', 'click', this.slide.bind(this));
    try {
      this.nodes = this.element.childElements();
      this.total = Math.ceil(this.nodes.length / this.skip);
      this.orient(this.links,this.nodes);
      this.setLinks(this.mark);
    } catch(e) {
      if(console.firebug) {
        console.info('"' + elem + '" container has no content. See error below.');
        console.error(e);
      }
    }
    
    
  },
  orient: function(links,measure){
    if (this.vertical) {
      var original = (this.increment) ? this.increment : measure[0].getHeight();
      this.increment = (this.skip > 1) ? original * this.skip : original;
      this.element.style.height = ((this.increment + 5) * this.total) + 'px';
      
    } else {
      var original = (this.increment) ? this.increment : this.nodes[0].getWidth();
      this.increment = (this.skip > 1) ? original * this.skip : original;
      this.element.style.width = ((this.increment + 5) * this.total) + 'px';
    }
    links.each(function(e) {
      if (e.hash == '#back' || e.hash == '#up') {
        e.slideHeight = (this.vertical) ? this.increment : 0 ;
        e.slideWidth = (this.vertical) ? 0 : this.increment ;
      } else {
        e.slideHeight = (this.vertical) ? -1 * this.increment: 0 ;
        e.slideWidth = (this.vertical) ? 0 : -1 * this.increment ;        
      }
    }, this);
  },
  slide: function(evt) {
    Event.stop(evt);
    if (this._moving) return;
    this._moving = true;
    new Effect.MoveBy(this.element, Event.element(evt).slideHeight, Event.element(evt).slideWidth, {duration:this.duration, afterFinish:function() {this._moving = false;}.bind(this)});
    this.mark -= ( Event.element(evt).hash == '#back' || Event.element(evt).hash == '#up' ) ? 1 : -1;
    this.setLinks(this.mark);
  },
  setLinks: function(index) {
    this.links[0].style.visibility = (index == 1) ? 'hidden' : 'visible';
    this.links[1].style.visibility = (index == this.total) ? 'hidden' : 'visible';
  }
});

/* Internal Promo Ad Rotation */
Event.observe(window, 'load', function() {
	if ($('intpromoad')){
	var promo_timer = (function(){
	// grab list
	var list = document.getElementById('intpromoad').getElementsByTagName('li');
	// set inital variables
	var CURRENT = 0, TOTAL = list.length - 1;
	// init first element's display to block
	list[CURRENT].style.display = 'block';
	// start interval [currently set to 1250, or 1.25 seconds]
	return window.setInterval(function(){
	// hide currently visible
	list[CURRENT].style.display = 'none';
	// determine if current should be incremented or reset
	CURRENT = (CURRENT == TOTAL) ? 0 : CURRENT + 1;
	// show new 'current' panel
	list[CURRENT].style.display = 'block';
	}, 6000);
	})();
	}
});


/**
 * @name inlineSlide
 * @extends Carousel
 *
 * Extends setlinks method to add functioality for showing counts (ie. 1 of 10).
 * To implement, create an element in the desired spot and set the id attribute
 * as sliding id (see Carousel @param elem) with _counter appended.
 */
 
var inlineSlide = Class.create(Carousel, {
  setLinks: function($super,index) {
    $super(index);
    this.setContent(index, this.total);
  },
  setContent: function(current, total) {
    if (total > 1) {
      $(this.element.id + "_counter").update('(' + current + ' of ' + total + ')');
    }
  }
});
/**
 * @name bookScroller
 * @extends inlineSlide
 *
 * this class extends the inlineSlide in order to add more information
 * outside of the scrolling object itself (book title, promeo, etc)
 * 
 */

var bookScroller = Class.create(inlineSlide, {
  setContent: function($super,current, total) {
    $super(current, total);
    current -= 1;
    $("recent_issues_title").update(this.nodes[current].down('div.title').innerHTML);
    $("recent_issues_footer").update(this.nodes[current].down('div.footer').innerHTML);
    $("recent_issues_promo").update(this.nodes[current].down('div.promo').innerHTML);
  }
});

/**
 * Startlist function is used to set state of package navigational element
 * and simulate :hover state for internet exploer
 *
*/
Odotcom.startList = function() {
  // count main tier list items and calculate width
  var items = $('pack_nav').childElements();
  var elemWidth = 500 / items.length;
  items.each(function(elem, index) {
    if (index == 0) elem.down('a').style.borderWidth = '0px';
    elem.style.width = elemWidth+'px';
    if(document.all) {
      elem.onmouseover=function() {this.className+=" over";};
      elem.onmouseout=function() {this.className=this.className.replace(" over", "");};
    } 
    try {
     var childs = elem.down('ul').childElements();
     var childsWidth = Math.floor((245 + (20 * childs.length)) / childs.length);
      childs.each(function(e) {
        e.down('a').style.width = childsWidth+'px';
     });
    } catch(e) {
    }
    if (elem.down('a').innerHTML.toLowerCase().indexOf('<br') == -1) {
      elem.down('a span').style.lineHeight = '30px';
    }
		if (elem.down('a').innerHTML.toLowerCase().indexOf('<br') > -1) {
      elem.down('a span').style.lineHeight = '15px';
    }
  });
  if (document.all) {
    // fix tiered nav rollovers in IE
    var sub = $$('#pack_nav .sub, #pack_nav .supersub');
    if(500 % items.length != 0) {
      items[0].style.width = (elemWidth + 2) + 'px';
    }
    var counter = 0;
    sub.each(function(a){
     a.onmouseover=function() {this.className+=" over";};
     a.onmouseout=function() {this.className=this.className.replace(" over", "");};
    });
  }
};

/**
 * Provides a simple interface for creating, retrieving and clearing cookies.
 * 
 */
Odotcom.cookie = {
  OPERA: (navigator.userAgent.toLowerCase().indexOf("opera") != -1),
  get: function(name) {
    var result = null;
    document.cookie.split('; ').each(function(cookies) {
      var crumb = cookies.split('=');
      if (crumb[0] == name && crumb[1] != null) {
        result = crumb[1];
      }
    });
    return (this.OPERA && result != null) ? result.replace(/%22/g, '"') : result;
  },
  set: function(name, value, days) {
    if (this.OPERA) value = value.replace(/"/g, "%22");
    if (days) {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
  },
  clear: function(name) {
    this.setCookie(name, "", false);
  }
};

/*
  TODO remove scrollbar class (will not be used)
*/
Odotcom.Scrollbar = Class.create({
  initialize: function(target) {
    this.target    = $(target);
    this.track     = $(target + "_track");
    this.handle    = $(target + "_handle");
    this.container = $(target + "_container");
    this.content   = $(target + "_content");
    this.initScroller();
  },
  initScroller: function() {
    this.track.style.height = this.container.getHeight() + 'px';
    new Control.Slider(this.handle, this.track, { axis: 'vertical', onSlide: function(val) {
      this.scrollTo(val);
    }.bind(this) });
  },
  scrollTo: function(val) {
    val = parseInt(this.container.getHeight() - this.content.getHeight()) * val;
    this.content.setStyle( {top: val + 'px' });
  }
});


 // MINITWIX DATE FUNCTIONS HAVE BEEN REMOVED  (GO TO REV NO. < 1819 TO GET THEM BACK)


/* 
Function to open Move Player Popup 
*/
function popMovePlayer(url,support) {
if ((screen.width >=1024) && (screen.height>=768)) {
newwindow=window.open(url,'name','width=980,height=665,resizable=no,location=no,scrollbars=no,toolbar=no');
window.name='oprah_main';
} else {
//send them to support page
window.document.location=support;
}
}

/* 
Function to open DC Map Popup 
*/
function popupDCPlayer(url) {
newwindow=window.open(url,'name','width=980,height=665,resizable=no,location=no,scrollbars=no,toolbar=no');
newwindow.document.close();
window.name='oprah_main';
}

/* 
Function to open Interactive Biography Popup 
*/
function popBio(url) {
newwindow=window.open(url,'name','toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,width=640,height=610');
newwindow.document.close();
window.name='oprah_main';
}

/* 
Function to open Printable Page Popup 
*/
function popPrintable(url) {
newwindow=window.open(url,'name','width=780,height=674,toolbar=yes,menubar=yes,status=no,locationbar=no,scrollbars=yes,resizable=yes,top=100,left=100');
newwindow.document.close();
window.name='oprah_main';
}

/*
Function to open slidepopup.jsp Popup
*/
function popSlide(url) {
newwindow=window.open(url,'slidepopup','width=810,height=600,scrollbars=no,resizable=no,toolbar=no,location=no,directories=no,menubar=no');
newwindow.document.close();
window.name='oprah_main';
}

/*
Function to open Audio Player Popup
*/
function popAudioPlayer(url) {
newwindow=window.open(url,'name','width=300,height=125,scrollbars=no,resizable=no,toolbar=no,status=no,location=no,directories=no,menubar=no');
newwindow.document.close();
window.name='oprah_main';
}

/*
Generic Popup Trigger
*/
function triggerPopupPage(url, w, h) {
	newwindow=window.open(url,'name','width='+w+',height='+h+',scrollbars=no,resizable=no,toolbar=no,status=no,location=no,directories=no,menubar=no');
	window.name='oprah_main';
}

/* 
Function to open Family Fun Calendar Popup 
*/
function popFamFunCal(url) {
newwindow=window.open(url,'name','width=1070,height=905,resizable=yes,location=no,scrollbars=yes,toolbar=no');
window.name='oprah_main';
}

/*
Placeholder function to correct error from commented out comments feature
*/
function loadComments(){}


/*
 * Send to a Friend scripts - added 10/3/08 from code version as of 8/8/08
 */
Event.observe(window, 'load', function() {
  if (document.getElementById('sendToaFriendID')) {
    var saf = new sendFriend('sendToaFriendID');
    var params = window.top.location.href.toQueryParams();     
    if (params.send) {
      var url = window.top.location.href.split('?');
      saf.display(url[0]);
    }
  }
});

/*
 * Send a Friend Class
 * @param form id
 * @contact bmetzger@harpo
 **/
 
var sendFriend = Class.create({
  initialize: function(form) {
    this.form = $(form);
    $('par_page').value = window.top.location.href;
    this.form.observe('submit', function(evt) {Event.stop(evt)});
    $('btnSend').observe('click', this.validate.bind(this));
    $$('a.send_toggle').invoke('observe', 'click', function(evt) {
      //this.form.reset();
      toggle_forms();
      new Effect.toggle('send_a_friend', 'appear', {duration:.4, afterFinish: function() {this.form.focusFirstElement();}.bind(this)});
      Event.stop(evt);
    }.bind(this));
  },
  display: function(val) {
    $('par_page').value = val;
    new Effect.toggle('send_a_friend', 'appear', {duration:.4, afterFinish: function() {this.form.focusFirstElement();}.bind(this)});
  },
  validate: function(evt){
    Event.stop(evt);
    var ret = true;
    var errors = "Please revise the highlighted fields<br />";
    this.form.getElements().each(function(e) {
      this.errorStyle(e,"#6A6A6A", "1px", '1px');
      $w('required email alpha alpha_num').each(function(t) {
        if(e.hasClassName(t)) {
          if(!Validator[t](e).valid) {
            this.errorStyle(e,"#E03434","2px",'0');
            errors += Validator[t](e).msg + "<br />";
            ret = false;
          }
        }
      },this);
    },this);
    if (ret) this.runSend();
    else {
      if($('errors')) $('errors').remove();
      this.form.down(3).insert({before: "<p style='color:#E03434;padding-left:60px' id='errors'>" + errors + "</p>"});
    }
  },
  reset: function(){
    //this.form.reset();
    $('recipientID').value = '';
    toggle_forms();
    this.form.show().up('div').next('div').remove();
    if($('errors')) $('errors').remove();
  },
  runSend: function() {
    // the message below should be put into a hidden input field
  	//$('messageID').value = $('messageID').value + " " + window.top.location.href;
    new Ajax.Request(this.form.action, {
      method: 'post',
      parameters: this.form.serialize(true),
      onSuccess: this.success.bind(this),
      onFailure: this.failure.bind(this)
    });
  },
  success: function(){
    var insides  = '<div style="padding:0 0 180px 0;"><div style="margin:7px 0;"><strong>E-mail Sent Successfully!</strong></div>';
        insides += '<div>Your friend(s) should be receiving your e-mail shortly.</div></div>';
    this.form.hide().up('div').insert({after: insides});
    new Effect.Fade('send_a_friend', {delay:3.5,duration:1, afterFinish: this.reset.bind(this) });
  },
  failure: function(transport){
    if($('errors')) $('errors').remove();
    var err = transport.responseText.evalJSON();
    var errmsg = '';
    if(!err.validTo) {
      // highlight to field
      this.errorStyle($('recipientID'),"#E03434","2px",'0');
      errmsg += "- Recipient's E-mail Address is not valid.<br />";
    }
    if(!err.validFrom) {
      // highlight from field.
      this.errorStyle($('fromID'),"#E03434","2px",'0');
      errmsg += "- Your E-mail Address is not valid.<br />";
    }
    //console.log(err);
    this.form.down(3).insert({before: "<p style='color:#E03434;padding-left:60px' id='errors'>Please revise the highlighted fields<br />"+errmsg+"</p>"});
  },
  errorStyle: function(elem, color, border, padding) {
    elem.setStyle({
      borderColor: color,
      borderWidth: border,
      padding: padding
    });
  }
});

Validator = {
  required: function(elem){
    return {valid: elem.value.length != 0, msg: "- " + elem.title + " is required."};
  },
  email: function(elem){
    if(!this.required(elem).valid) return {valid: true };
    var ret;
    elem.value.split(/,\s*/).each(function(e) {
      if (!/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(e)) {
        ret = false;
        throw $break;
      } else {
        ret = true
      }
    });
    return {valid: ret, msg: '- ' + elem.title + " is not valid." };
  },
  alpha: function(elem){
    if(!this.required(elem).valid) return {valid: true };
    return {valid: /^[a-zA-Z\s\'\-\.]+$/.test(elem.value), msg: '- ' + elem.title + ' contains invalid characters'};
  },
  alpha_num: function(elem) {
    if(!this.required(elem).valid) return {valid: true };
    return {valid: /^[a-zA-Z0-9\-\.\?\!\s\,*]+$/.test(elem.value), msg: "- Use only letters &amp; numbers in " + elem.title};
  }
};

Odotcom.embedHearst = function(url, size, id) {
  if (url == null) return false;
  if (metas['sectionname'].content == 'oprahshow' && metas['mediatype'].content == 'Article') return false;
  
  var content = $$('meta[name="contentsource"]')[0].content.toLowerCase();
  
  
  if(content == 'omag' || content == 'oah') {
    if (section == 'oathome' || section == 'omagazine') {
      return false;
    }
  } else {
    if(Odotcom.cookie.get('subscriptionAdViewed') == 'viewed') {
      return false;
    } else {
      Event.observe(window, 'unload', function() {
        Odotcom.cookie.set('subscriptionAdViewed', 'viewed');
      });
    }
  }
  if(url != 'ARTICLE_PAGE' && url != 'SLIDESHOW_PAGE') {
    var html = '';    
    html += '<div id="'+id+'" class="x'+size+'">';
    //html += '<div class="TL"><div class="TR"></div></div><div class="box"><div class="CT">';
    html += '<script type="text/javascript" src="'+url+'"><\/script>';
    //html += '</div></div><div class="BL"><div class="BR"></div></div>';
    html += '</div>';
    document.write(html);
  }
  return true;
};

/*
Google Analytics
*/
   var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
   document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

   var trackerID = (("www.oprah.com" == document.location.hostname) ? "UA-8224552-1" : ("static.oprah.com" == document.location.hostname) ? "UA-8224552-2" : ("images.oprah.com" == document.location.hostname) ? "UA-8224552-3" : ("media.oprah.com" == document.location.hostname) ? "UA-8224552-4" : "");
       
   try {
      var pageTracker = _gat._getTracker(trackerID);
      pageTracker._trackPageview();
      }catch(err) {}

