// common.js
//
// Routines used by the blogs template
//
// author: Pau Santesmasses <santep@nytimes.com>
// author: Justin Kerr Sheckler <sheckler@nytimes.com>
//
// version: $Id: common.js,v 1.18 2008/02/11 16:41:03 patelj Exp $
//

CalendarPager = {
  init: function() {
    //console.log('initializing')
    if (document.getElementById('archive')) {
      CalendarPager.getbuttonrows();
    }
  },
  getbuttonrows: function() {
    this.archives = document.getElementById('archive')
    this.tables = this.archives.getElementsByTagName('table');
    this.calendars = new Array();
    for(var i = 0; i < this.tables.length; i++) {
      if (this.tables[i].className == "calendar") {
        this.calendars.push(this.tables[i]);
      }
    }
    //console.log(this.calendars.length)
    this.tableRows = this.archives.getElementsByTagName('tr');
    this.buttonrows = new Array();
    for(var i = 0; i < this.tableRows.length; i++) {
      if (this.tableRows[i].className == 'buttonrow'){
        this.buttonrows.push(this.tableRows[i]);
        //console.log(this.tableRows[i].className)
      }
    }
    //console.log(this.buttonrows.length)
    this.buttons = new Array();
    for(var i = 0; i < this.buttonrows.length; i++) {
      for(var j = 0; j < this.buttonrows[i].getElementsByTagName('a').length; j++) {
        this.buttons.push(this.buttonrows[i].getElementsByTagName('a')[j]);
        //console.log(this.buttonrows[i].getElementsByTagName('a')[j])
      }
    }
    //console.log(this.buttons.length)
    this.bind();
  },
  bind: function() {
    for(var i = 0; i < this.buttons.length; i++) {
      this.buttons[i].onclick = this.doPage;
      //console.log(  this.buttonrows[i].onclick )
    }
  },
  doPage: function() {
    //console.log(this.href)
    href  = this.href
    date  = href.replace(/\//g,'')
    month = date.substring(date.length-4,date.length-2);
    year  = date.substring(date.length-8,date.length-4);
    //console.log(month)
    //console.log(year)
    target = document.getElementById('cal_' + year + month);
    CalendarPager.show(target);
    return false;
  },
  show:  function(target) {
    this.hideAll();
    target.style.display = 'table';
  },
  hide: function(target) {
    target.style.display="none";
  },
  hideAll: function() {
    for(var i = 0; i < this.calendars.length; i++) {
      this.hide(this.calendars[i])
    }
  }
}

window.onload = CalendarPager.init;

// The following are used by the comments form

CommentsValidator = {

    // highlights a form field w/ a missing value
    highlightElement: function(element) {
        element.style.background = '#FFFFDD';
    },

    // removes highlighting
    resetElement: function(element) {
        element.style.background = '';
    },

    // adds a message saying that a field is invalid
    addWarning: function(message) {
        var element = document.getElementById('warnings');
        element.innerHTML = element.innerHTML + '<li>' + message + '</li>';
        element.style.display = 'block';
    },

    // removes all messages
    clearWarnings: function() {
        var element = document.getElementById('warnings');
        element.innerHTML = '';
        element.style.display = 'none';
    },

    // turnkey: checks the add a comment form for completeness
    validateForm: function() {
        var author = document.getElementById('author');
        var email = document.getElementById('email');
        var comment = document.getElementById('comment');
        var ret = true;

        // reset state from previous invokation
        this.clearWarnings();
        this.resetElement(author);
        this.resetElement(email);
        this.resetElement(comment);

        // author must have a value
        if (!author.value) {
            this.highlightElement(author);
            this.addWarning('Please enter your name');
            ret = false;
        }

        // email must have a value
        if (!email.value) {
            this.highlightElement(email);
            this.addWarning('Please enter your e-mail address');
            ret = false;
        }

        // email must be valid: defined as containing one dot (.) and one
        // at sign (@) with the at sign coming first.
        if (email.value) {
            var atpos = email.value.lastIndexOf('@');
            var dotpos = email.value.lastIndexOf('.');

            if (atpos < 0 || dotpos < atpos) {
                this.highlightElement(email);
                this.addWarning('That e-mail address is not valid');
                ret = false;
            }
        }

        // comments must have a value
        if (!comment.value) {
            this.highlightElement(comment);
            this.addWarning('Please enter your comment');
            ret = false;
        }

        // let the user know that it's normal not to see the comment
        // immediately
        if (ret == true) {
            alert('Your comment will appear once it has been approved.');
        }

        return ret;
    }

}

/* sharetool functions */

function showHideShareTool (id1, id2) {

	if (document.getElementById) {
		if (document.getElementById(id1).className == 'hide') {
			document.getElementById(id1).className = 'show';
			document.getElementById(id2).className = 'sharebox';
		} else {
	     	document.getElementById(id1).className = 'hide';
    		document.getElementById(id2).className = 'share';
		}
	}
	return false;
}

function blogPostShare(site, keywords, pubdate, theID) {

	var title;
	var description;
	var popUpUrl;
	var byline;
	var formCtl;
	var formID;
	var section;

	title = '';
	description = '';
	popUpUrl = '';
	byline = '';
	section = '';

	formID = 'emailThis_' + theID;

	if(document.getElementById) {
		formCtl = document.getElementById(formID);
		if(formCtl) {
			description = formCtl.description.value;
			title = formCtl.title.value;
			popUpUrl = formCtl.url.value;
			byline = 'By ' + formCtl.author.value;
			section = formCtl.section.value;
		}
	}

	switch (site) {
	case "newsvine":
		postPopUp('http://www.newsvine.com/_wine/save?ver=2&popoff=0&aff=nytimes&t=' + keywords + '&e=' + description + '&h=' + title + '&u=' + popUpUrl, 'newsvine', 'toolbar=0,status=0,height=445,width=650,scrollbars=yes,resizable=yes');
		break;
	case "facebook":
		postPopUp('http://www.facebook.com/sharer.php?u=' + popUpUrl + '&t=' + title, 'facebook', 'toolbar=0,status=0,height=436,width=646,scrollbars=yes,resizable=yes');
		break;
	case "digg":
		postPopUp('http://digg.com/remote-submit?phase=2&url=' + popUpUrl + '&title=' + title + '&bodytext=' + description, 'digg', 'toolbar=0,status=0,height=450,width=650,scrollbars=yes,resizable=yes');
		break;
	case "permalink":
		postPopUp('http://www.nytimes.com/export_html/common/new_article_post.html?url=' + popUpUrl + '&title=' + title+ '&summary=' + description + '&section=' + section + '&pubdate=' + pubdate + '&byline=' + byline, 'permalink', 'toolbar=0,status=0,height=410,width=490,scrollbars=yes,resizable=no');
		break;
	case "delicious":
		postPopUp('http://del.icio.us/post?v=4&partner=nyt&noui&jump=close&url=' + popUpUrl + '&title=' + title + '&bodytext=' + description, 'delicious', 'toolbar=0,status=0,height=400,width=700,scrollbars=yes,resizable=no');
		break;
	}
}

function postPopUp(url, name, params) {
	var win = window.open(url, name, params);
	if(win) {
		win.focus();
	}
}

/* sharetool functions end */

function blogPostPrint(keywords, pubdate, theID, printPostURL, blogImageURL) {

	var title;
	var description;
	var popUpUrl;
	var byline;
	var formCtl;
	var formID;
	var section;
	var full_text;

	title = '';
	description = '';
	popUpUrl = '';
	byline = '';
	section = '';
	full_text = '';

	formID = 'emailThis_' + theID;

	if(document.getElementById) {
		formCtl = document.getElementById(formID);
		if(formCtl) {
			description = formCtl.description.value;
			title = formCtl.title.value;
			popUpUrl = formCtl.url.value;
			byline = 'By ' + formCtl.author.value;
			section = formCtl.section.value;
			full_text = formCtl.full_text.value;
		}
	}

	postPopUp(printPostURL + '?ID=' + theID + '&full_text=parent_form_text', 'printthis', 'menubar=1,toolbar=0,status=0,height=445,width=650,scrollbars=yes,resizable=yes');

}