/*
	CPC Mailer
	Main JavaScript classes

	Data being remembered:
	i admail 	addressed|unaddressed
	2 plan		budget|target
	3 budget	x
	4 havelist	y|n
	4 listtotal	x
	6 market	x
	8 ready
	8 piece
	8 inhouse
	10 region
	10 delivered a|b
*/

// load jquery into scoped section
(function($){

	function init (){
		// add a js class to add in JS specific styles
		// may not be ultimately relevant if js-only app but good practice
		$(document.body).addClass('js');

		// sidebar toggles
		$('.module h2').click(function(){
 			$(this).next().slideToggle('fast').each(function(){
 				var el = $(this.parentNode);
 				if (el.hasClass('closed')) {
 					el.removeClass('closed');
 				} else {
 					el.addClass('closed');
 				}
 			});

		});

		function addInfo(xmlNode, id) {
			var title = xmlNode.getElementsByTagName('title')[0].firstChild.data;
			var abstract = xmlNode.getElementsByTagName('abstract')[0].firstChild.data;
			var d = document.createElement('div');
			/*
				AUTHOR: Chris Murphy
				NOTE:	Revised Formatting for links

				// Previous formatting
				var html = "<h3>" + title + "</h3>" +
						   "<p>" + abstract + "</p>" +
						   "<a href=\"#\" title=\"" + (/\/fr\//.test(location.href) ? 'Plus':'More') + "\"><span>" + (/\/fr\//.test(location.href) ? 'Plus':'More') + "</span><strong>&nbsp;</strong></a>";
			*/

			var html = "<h3>" + title + "</h3>" +
					   "<p>" + abstract + "</p>" +
					   "<a href=\"#\" onClick=\"trackPopUp(this,'" + id.toLowerCase() + "','" + analyticsData.delimiter +"');\" title=\"" + (/\/fr\//.test(location.href) ? 'Plus':'More') + "\"><span class=\"more\">" + (/\/fr\//.test(location.href) ? 'Plus':'More') + "</span></a>";

			d.innerHTML = html;
			document.getElementById('resource-drop').appendChild(d);

			var m = document.createElement('div');
			m.id = 'modal' + id;
			m.title = title;
			document.body.appendChild(m);

			$.get('resources/content/' + id + '.html?' + (new Date()).getTime(), function(data){
				var d = document.createElement('div');
				d.className = 'inner';

				var pn = window.location.pathname;
				pn = pn.slice(0,pn.lastIndexOf('/' + (isFrench ? 'fr' : 'en') + '/')) + '/';
                
				d.innerHTML = data.replace(/src="/gm, 'src="' + pn);
				m.appendChild(d);
				$(m).hide();
			});

			var a = $( d.getElementsByTagName('a')[0] );
			a.click(function(evt){
				evt.preventDefault();
				var bb= $('#' + m.id).show().dialog({modal:true, overlay:{backgroundColor:'#000', opacity:.7}, width:'700px', height:'400px', resizable:false, open:
						function(a,b){
							/// needed for IE6 because
							if ( $.browser.msie && parseInt($.browser.version.substring(0,1)) < 7 ) {
								this.getElementsByTagName('div')[0].style.height = (this.offsetHeight - 50) + 'px';
							}
						}
					});
					return false;
			});
		}

		if (window.resources) {
			$.get("resources/resources.xml", function(doc){
				var a = doc.getElementsByTagName('article');

				for (var i=0;i < resources.length; i++) {
					for (var j=0; j < a.length; j++) {
						if (a[j].getAttribute('id') == resources[i]) {
							addInfo(a[j], resources[i]);
						}
					}
				}

			});
		}

		$('.resource').each(function(){
			var id = this.id.substring(1);
			var m = document.createElement('div');
			m.id = 'modalr' + id;
			m.title = this.innerHTML;
			document.body.appendChild(m);

			$.get('resources/content/' + id + '.html', function(data){
				var d = document.createElement('div');
				d.className = 'inner';
				var pn = window.location.pathname;
				pn = pn.slice(0,pn.lastIndexOf('/' + (isFrench ? 'fr' : 'en') + '/')) + '/';
                
				d.innerHTML = data.replace(/src="/gm, 'src="' + pn);
			//	d.innerHTML = data;
				m.appendChild(d);
				$(m).hide();
			});

		}).click(function(evt){
			evt.preventDefault();
			var id = this.id.substring(1);
			var bb= $('#modalr' + id).show().dialog({modal:true, overlay:{backgroundColor:'#000', opacity:.7}, width:'700px', height:'400px', resizable:false, open:
						function(a,b){
							/// needed for IE6 because
							if ( $.browser.msie && parseInt($.browser.version.substring(0,1)) < 7 ) {
								this.getElementsByTagName('div')[0].style.height = (this.offsetHeight - 50) + 'px';
							}
						}
					});
					return false;

		});

		// set page body to admail type
		var admail = $.cookie('admail');
		if (admail == 'addressed') {
			$(document.body).addClass('addressed');
		}else{
			$(document.body).addClass('unaddressed');
		}

		// start over button
		$('#startover').click(function(){
			window.location = 'default.asp';
		});
	}

	$(document).ready(init);

}(jQuery));
// end scoping

// Cyanide_7 (leo7278@hotmail.com)
// Web Site:  http://www7.ewebcity.com/cyanide7
	var isFrench = /\/fr\//.test(location.href);

	function formatDollars(num) {
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+ (isFrench ? ' ' : ',') +
		num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + (isFrench ? num + ' $' : '$' + num)  );
	}

//adding an image preloader for the close buttons 
if( document.images )
{
	preload_image_objects = new Array();

	// set image url
	image_urls = new Array();
	image_urls[ 0 ] = "../images/888888_11x11_icon_close.gif";
	image_urls[ 1 ] = "../images/454545_11x11_icon_close.gif";
	image_urls[ 2 ] = "../images/222222_11x11_icon_close.gif";

	for( var i = 0; i < image_urls.length; i++)
	{
		preload_image_objects[ i ] = new Image();
		preload_image_objects[ i ].src = image_urls[ i ];
	}
}
