/*
jquery.charts.js
A plugin that copies the content of a specific DIV and replaces the image with a larger version
Andrea Piernock, andrea.piernock@digitashealth.com
version 1.1 1/24/2011

1.1.2   add fix to ie bug
1.1		added functionality for chart ID
1.0.1	fixed image type
1.0		original chart plugin
*/

var chartWidths = '';

/* center a DIV on the page */

$.fn.center = function () {
    $(this).css('left', ( $(window).width() - $(this).width() ) / 2+$(window).scrollLeft() + "px");
	$(this).css('top', ($(window).scrollTop() + 100)+ "px");
    return $(this);
}

$.fn.setLargeImgWidth = function(largeWidth,largeDiv)
{    
    $(this).attr('largeWidth',largeWidth);
    $(largeDiv).css('width',largeWidth);    
    $(largeDiv).css('left', ( $(window).width() - $(largeDiv).width() ) / 2+$(window).scrollLeft() + "px");
}

/* charts customizable plugin */

$.fn.charts = function (options) {
	
	/* chart defaults, no user input */
	var defaults = {
		chartDirectory : '/images/charts/',
		chartImgSmall : '',
		chartImgLarge : '-large',
		chartImgType : '.jpg',
		chartClass : 'chart',
		largeClass : 'large',
		strEnlarge : 'Enlarge',
		strClose : 'Close',
		divOverlay : 'false',
		divOverlayOpacity : '0.5'
	}
	
	/* extend the options from user input */
	var options =  $.extend(defaults, options);
	
	/* variables used, based on plugin options */ 
	var chartDiv = '.' + options.chartClass;
	var largeDiv = '.' + options.chartClass + '.' + options.largeClass;
	var btnEnlarge = '<a class="enlarge">' + options.strEnlarge + '</a>';
	var btnClose = '<a class="close">' + options.strClose + '</a>';
	
	
	
	/* functions for the charts */
	return this.each(function() {		
		/* add the "enlarge button to regular charts only */
		$('.' + options.chartClass + ':not(.' + options.largeClass + ')').each(function(i) {
			$(this).prepend(btnEnlarge);
		});		
		/* bind the "close" function to the button in the layer */
		$('a.close').live('click', function() {
			$('body').find('.overlay').remove();
			$('body').find(largeDiv).remove();
		});		
		/* onclick for open/close */
		$('a.enlarge').click(function () {
			/* create the background/overlay */
			if ( options.divOverlay == 'true' ) {
				$('body').append('<div class="overlay"></div>');
				$('.overlay').css({ opacity: options.divOverlayOpacity })
			} else {
			/* remove any previous charts */			
			$('body').find('.overlay').remove();
			$('body').find(largeDiv).remove();
			return false;
			}		
			/* create the container for the large chart */
			$('body').append('<div class="' + options.chartClass + ' ' + options.largeClass + '"></div>');			
			/* copy the contents of the clicked chart div */
			$(largeDiv).css('visibility','hidden');
			$(largeDiv).html( $(this).parent(chartDiv).html() );			
			$(largeDiv + ' a.enlarge').remove();
			$(largeDiv).prepend(btnClose);			
			var largeWidth = $(chartDiv).attr('largeWidth');
			/* Find chart image, change to Large version */
			$(largeDiv + ' img[src*=' + options.chartDirectory + ']').each(function(i) {			    
				var imgSrc = $(this).attr('src'); // get the path to the original chart image
				var imgName = imgSrc.split(options.chartDirectory); // split at the given directory
				imgSrc = imgName[1]; // remove the directory
				imgName = imgSrc.split(options.chartImgType); // remove the file type
				chartName = imgName[0];
				if ( options.chartImgSmall != '' ) { // does the small chart have a different suffix?
					imgSrc = chartName;
					imgName = imgSrc.split(options.chartImgSmall);
				}
				imgSrc = options.chartDirectory + chartName + options.chartImgLarge + options.chartImgType; // piece it back together
				$(this).attr('src',imgSrc);		
				/*It force IE6 to load the large IMG each time that the largeWidth variable is not defined*/							
				if(largeWidth == undefined){
				    if(getIEVersion() == 6.0){
				        var d = new Date();
				        imgSrc+='?'+d.getTime();			
				    }
				}
			    $("<img/>").attr("src",imgSrc).load(function(){	
			        $(chartDiv).setLargeImgWidth(this.width+'px',largeDiv);
			    });		
			});					
			if(largeWidth != undefined){
			    $(largeDiv).css('width',largeWidth);			    
			}			
			/* position the large chart */
			if ( $(largeDiv).height() > $(window).height() ) { // height of layer is more than viewport
				$(largeDiv).show().center().css('top',$(window).scrollTop());
			} else { // height of layer is less than viewport
				$(largeDiv).show().center();
			}							
			/* add an ID to the large chart */
			var chartID = $(this).parent(chartDiv).attr('id');
			if ( chartID != '' ) {
				$(largeDiv).attr('id', chartID + '-' + options.largeClass);
			} else {
				$(largeDiv).attr('id', chartName);
			}	
			$(largeDiv).css('display','none');		
			$(largeDiv).css('visibility','visible');		
			$(largeDiv).fadeIn('slow');
		});					
	});  
	
}
