var pd = pd || {};

pd.init = function(){
	
};

pd.printTable = function(table, title){
	var w = window.open();
		w.document.write('<link rel="stylesheet" href="/Site/DefaultSite/Skins/poledivas/Skin.css" type="text/css"/>');
		w.document.write('<style type="text/css">@page land { size: landscape; } body { page: land; } #content table.timetable { width: 100%!important; page-break-after: always;" }</style>');
		w.document.write('<div id="content"><h2>'+title+'</h2>' + table + '</div>');
		w.print();
		if(!$.browser.msie){
			w.close();
		}
};

pd.printTables = function(table1, table2, title1, title2){
	var w = window.open();
		w.document.write('<link rel="stylesheet" href="/Site/DefaultSite/Skins/poledivas/Skin.css" type="text/css"/>');
		w.document.write('<style type="text/css">@page land { size: landscape; } body { page: land; } #content table.timetable { width: 100%!important; page-break-after: always;" }</style>');
		w.document.write('<div id="content"><h2>'+title1+'</h2>');
		w.document.write(table1);
		w.document.write('<h2>'+title2+'</h2>');
		w.document.write(table2);
		w.print();
		if(!$.browser.msie){
			w.close();
		}
};



var NOW = NOW || {};

//NOW.addMethod adds methods to objects and allows for additional properties like parent.
NOW.addMethod = function(parent, methodName, fn){
	parent.prototype[methodName] = fn;
	fn.parent = parent;
};

/* ---------------------------------------------------------------------------------
	NOW.Banner
	@type	constructor
	@param	options - 	available opitions:
						delay: controls delay between rotation in milliseconds (default: 6000)
						width: width of banners (default: 948)
						speed: sets speed of animation (default: 600)
	
	@return	null
------------------------------------------------------------------------------------ */
NOW.Banner = function(options){
	for(option in options){
		this.options[option] = options[option];
	}	
	
	this.setup();
	
};

/* -----------------------------------
	NOW.Banner.options default options 
-------------------------------------- */
NOW.Banner.prototype.options = {
	delay: 6000,
	width: 900,
	speed: 600,
	ul: function(){
		return $('#banners');
	},
	lis: function(){
		return $('#banners li');
	},
	controls: function(){ 
		return $('#hero-banner .controls')
	},
	xmlFilePath: '/banners/banners.xml',
	imageDirectoryPath: '/banners/'
};

/* -----------------------------------
	NOW.Banner.setup
-------------------------------------- */
NOW.addMethod(NOW.Banner, 'setup', function(){
	
	if(this.options.ul()){ //make sure the ul exists on this page before setting up
		var that = this;
		this.populateBanners(function(){
												
			that.options.lis().each(function(index){
				$this = $(this);
				
				// initially hide all items (except first)
				if(index == 0){
					$this.css('display', 'block');
				} else {
					$this.css('display', 'none');
				}
			});
			
			that.options.lis().eq(1).css({ left: that.options.width + 'px', display: 'block' });
			
			if(that.options.lis().length > 1){
				that.options.controls().css('display', 'block');
				that.bindControls();
				that.control.start();
			}
			
		});		
		
	}
	
});

/* -----------------------------------
	NOW.Banner.control
	@method	start
	@method	stop
	@method reset
-------------------------------------- */
NOW.addMethod(NOW.Banner, 'control', {
	_timer: null,
	start: function(){
		var that = this;
		this._timer = setInterval(function(){
					  that.parent.prototype.slide('right');
					}, that.parent.prototype.options.delay);
		return this;
	},
	stop: function(){
		var that = this;
		if(this._timer){
			clearInterval(that._timer);
		}
		return this;
	},
	reset: function(){
		if(this._timer){
			this.stop().start();
			return this;
		}
	}
});

/* -----------------------------------
	NOW.Banner.populateBanners
	@desc populates banners from XML file via AJAX
-------------------------------------- */
NOW.addMethod(NOW.Banner, 'populateBanners', function(callback){
	var that = this;
	$.ajax({
		type: "GET",
		url: that.options.xmlFilePath,
		dataType: "xml",
		success: function(xml) {

			var ban = $('banner', xml);

			/* preload our images first*/
			if($.preload){
				$(xml).find('banner').each(function() {
					$.preload([$(this).find('background').text()], {
						base: that.options.imageDirectoryPath,
						ext: ''
					});
				});
			}

			$(xml).find('banner').each(function() {

				// on page load, this loads in first node into the odd banner
				var title = $(this).find('title').text();
				var content = $(this).find('content').text();
				var background = $(this).find('background').text();				
				var link = $(this).find('link').text();
				
				//create li
				var parent = that.createHtmlNode(that.options.ul(), '<li></li>');
				
				//if background exists, add it to li
				if(background) {
					background = background.replace(/\s/, '');
					parent.css({'background-image': "url(/banners/" + background + ")"});
				}
				
				
				//if a link exists, make this the new parent (so it wraps all the content)
				//also add class padding to the link, otherwise add it to the li
				if(link){
					parent = that.createHtmlNode(parent, '<a href="' + link + '" class="padding"></a>');
				} else {
					parent.addClass('padding');
				}
				
				//append elements and insert their values
				if(title)
				that.createHtmlNode(parent, '<h2></h2>', title);
				
				if(content)
				that.createHtmlNode(parent, '<p></p>', content);
							
				

			});	
			
			//run callback once this is done
			callback();
			
		}
	});
});

/* -----------------------------------
	NOW.Banner.createHtmlNode
	@desc creates a HTML no
-------------------------------------- */
NOW.addMethod(NOW.Banner, 'createHtmlNode', function(on, type, text){
	var that = this;
	
	//appends a html element (type) on a parent element (on)
	var el = $(type);
	on.append(el);
	
	//if there is text then insert it
	if(text){
		el.html(text);	
	}
	
	return el;
	
});

/* -----------------------------------
	NOW.Banner.bindControls
-------------------------------------- */
NOW.addMethod(NOW.Banner, 'bindControls', function(){
	var that = this;
	
	this.options.controls().children('.back').click(function(e){		
		e.preventDefault();
		that.control.reset();
		that.slide('back');
	});
	this.options.controls().children('.next').click(function(e){
		e.preventDefault();
		that.control.reset();
		that.slide('next');
	});
});

/* -----------------------------------
	NOW.Banner.unbindControls
-------------------------------------- */
NOW.addMethod(NOW.Banner, 'unbindControls', function(){
	var that = this;
	this.options.controls().children('.back').unbind('click').click(function(e){
		e.preventDefault();
	});
	this.options.controls().children('.next').unbind('click').click(function(e){
		e.preventDefault();
	});
});

/* -----------------------------------
	NOW.Banner.slide
-------------------------------------- */
NOW.addMethod(NOW.Banner, 'slide', function(dir){
	
	var that = this;
	this.unbindControls();
	
	var lis = this.options.lis();
	
	/* Direction: back */
	if(dir == 'back'){
		
		lis.eq(0).animate({ left: this.options.width + 'px' }, this.options.speed, 'easeInOutExpo', function(){
			/*$(this).prepenTo( NOW.Banner._data.lis.eq() ).css({ display: 'none', left: 0 });*/
			if(lis.length == 2){
				lis.eq(0).css({ left: that.options.width + 'px', display: 'block' });
			} else {
				lis.eq(2).css({ left: that.options.width + 'px', display: 'block' });
			}
			
			that.bindControls();
		});
		lis.eq(lis.length - 1).prependTo( this.options.ul() ).css({ left: '-' + this.options.width + 'px', display: 'block' }).animate({ left: '0' }, this.options.speed, 'easeInOutExpo');
		
	} else {
		
		// move current banner to -948px, on callback append to parent
		lis.eq(0).animate({ left: '-' + this.options.width + 'px' }, this.options.speed, 'easeInOutExpo', function(){
			$(this).appendTo( that.options.ul() ).css({ display: 'none', left: 0 });
			if(lis.length == 2){
				lis.eq(0).css({ left: that.options.width + 'px', display: 'block' });
			} else {
				lis.eq(2).css({ left: that.options.width + 'px', display: 'block' });
			}
			
			that.bindControls();
		});
		
		// move offscreen banner to 0
		lis.eq(1).animate({ left: '0' }, this.options.speed, 'easeInOutExpo');
		
	}
	
});

$(document).ready(function(){
	
	var Banner = new NOW.Banner();
	
});


