/* Center DIV on the page */
$.fn.center = function () {
    $(this).css("left", ( $(window).width() - $(this).width() ) / 2+$(window).scrollLeft() + "px");
	$(this).css("top", ( $(window).height() - $(this).height() ) / 2+$(window).scrollTop() + "px");
    return $(this);
}


/* pull information from chart to create larger div */
function copyChart() {
	
	$('div#content div.information div.chart').each(function(i) {
		
		/* find the ID of the chart */
		var chartID = $(this).attr("id");
		var largeID = $(this).attr("id") + '_large';
		var divID = 'div#' + largeID + '.chart.large';
		
		/* update increment counters to chart footnotes: formatting needed for AMA style guide */
		var arrCounter = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
		var footnotes = 'div#content div.information div#'+chartID+'.chart ol.footnotes.alpha';
		
		$(footnotes).css({"list-style":"none","padding-left":"0"});
		$(footnotes+" li").each(function(i) {
			$(this).html("<sup>"+arrCounter[(i)]+"</sup> "+$(this).html());
		});
		
		/* prepare copied chart div */
		$('div#seroquel-xr').before('<div class="chart large" id="' + largeID + '"></div>');
		
		/* copy the contents of the chart div */
		$(divID).html($(this).html());
		
		/* add the Close button */
		if (chartID == "depressive-episode-symptoms") {
			$(divID).html('<p class="close">Close <img src="/images/icon/close2.png" /></p>'+$(this).html());
		} else {
			$(divID).html('<p class="close">Close <img src="/images/icon/close.png" /></p>'+$(this).html());
		}
		
		/* Find chart image, change to Large version */
		var imgSrc = $(divID + ' img[src*=chart]').attr('src');
		var imageName = imgSrc.split('/charts/');
		imgSrc = imageName[1];
		imageName = imgSrc.split('.png');
		imgSrc = "/images/charts/"+imageName[0]+"_large.png";
		$(divID + ' img[src*=chart]').attr({ src: imgSrc });
		
		/* Enlarge Chart links in regular chart only */
		$('div#content div.information div#' + $(this).attr("id") + '.chart').html('<p class="enlarge"><a href="#" onclick="return false">Enlarge</a> <img src="/images/icon/enlarge.png" /></p>'+$(this).html());
		
	});
	
}



$(document).ready(function() {

	if ( $('div#content div.information div.chart').length ) {
	
		copyChart();
		
		/* onclick for open/close */
		$('p.enlarge').click(function () {
			var divID = 'div#' + $(this).parent("div").attr("id") + '_large.chart.large';
			
			var divH = $(divID).height();
			$(divID).center().show();
			
		});
		
		$('p.close').click(function () {
			$('div.chart.large').hide();
		});
		
	}

});

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}