//------------------------------------------------------------------------------
(function(window, document, undefined){
	function banner_scheduler(){
		var self = this;
		self.banners = [];
		self.days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
		self.months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'];
		self.show_banner = function(){
			//check to see if any banners were added
			if(self.banners.length === 0){
				return false;
			}
		
			//for now, George said only use the first result found			
			$('kwm_banner').innerHTML = self.banners[0][0];
		};
		
		var get_current_date = function(){
			var d = new Date();
			var obj = new Object();
			obj.day_num = d.getDate();
			obj.day_name = self.days[d.getDay()];
			obj.month_num = d.getMonth() + 1;
			obj.year_num = d.getFullYear();
			obj.minutes = d.getMinutes();
			obj.seconds = d.getSeconds();
			obj.hours = d.getHours();
			obj.timestamp = d.getTime();
			
			return obj;
		};
		
		var $ = function(div){
			if(document.getElementById(div)){
				return document.getElementById(div);
			}
			else{
				return false;
			}
		};
		
		self.create_banner = function(){
			//whenever this function is called, we will check the banner that is
			//being added to see if it will be shown right now
			var e = arguments;
			var d = self.date;
			var b = self.banners;
			
			//setup some tmp elements
			var start;
			var end;

			//grab the src of the element
			var src = e[0];
			
			//check the second argument to see if it is a recurring date
			if(e[1] === 'recurring'){
				//this banner is indeed is a recurring banner, now check to
				//see if the banner is recurring for our current date
				if((e[2].toLowerCase()) === d.day_name){
					//the banner is a recurring banner that shows today, so
					//now check the time to see if it is an all day event
					if(e[3] === '*'){
						//this banner shows all day, so we need to basically
						//add it to our list of banners to show
						b[b.length] = e;
					}
					else{
						//check the specific times for this recurring event to
						//see if the current time fits into the time range they
						//specified
						if(time_is_between(e[3], e[4]) === true){
							//this banner shows in the current timeframe, so
							//we need to basically add it to our list of banners
							//to show
							b[b.length] = e;
						}
						else{
							//discard this banner b.c it doesnt fit in the time
							return;
						}
					}
				}
			}
			
			//else the date is not recurring and is something specific or
			//an interval, check to see if the banner is an interval based
			//banner
			else if(e.length === 5){
				//interval date, check to see if the current date falls within
				//the target interval
				if(falls_into_interval(e[1], e[3], e[2], e[4]) === true){
					b[b.length] = e;
				}
				else{
					return;
				}
			}
			
			//the banner was not recurring or interval based, so it's
			//a specific date, check it
			else{
				//specific date, check to see if the date is today
				if(is_today(e[1]) === true && (time_is_between(e[2], e[3]) || e[2] === '*' )){
					b[b.length] = e;
				}
				else{
					//the date is not today, so discard it
					return;
				}
			}
		};
		
		var falls_into_interval = function(s, e, st, et){
			//this function will compare the two dates to make sure that the
			//current date falls in between or on them
			
			//now begin the checks
			s = s.split('/');
			s[0] = self.months[parseInt(s[0], 10) - 1];
			if(st !== '*'){
				s = s[0] + ' ' + s[1] + ', ' + s[2] + ' ' + st;
			}
			else{
				s = s[0] + ' ' + s[1] + ', ' + s[2];
			}
			e = e.split('/');
			e[0] = self.months[parseInt(e[0], 10) - 1];
			if(st !== '*'){
				e = e[0] + ' ' + e[1] + ', ' + e[2] + ' ' + et;
			}
			else{
				e = e[0] + ' ' + e[1] + ', ' + e[2] + ' 23:59:59';
			}
			var d = self.date;
			
			s = new Date(s).getTime();
			e = new Date(e).getTime();
			
			if(d.timestamp >= s && d.timestamp <= e){
				return true;
			}
			else{
				return false;
			}
		};
		
		var is_today = function(e){
			//this function will compare the current date with the date passed
			//to it to see if the dates are correct, if they are returns true
			//if not, returns false
				
			e = e.split('/');
			e[0] = parseInt(e[0], 10);
			e[1] = parseInt(e[1], 10);
			e[2] = parseInt(e[2], 10);
			var d = self.date;
			
			//now compare the dates
			if(e[0] === d.month_num && e[1] === d.day_num && e[2] === d.year_num){
				return true;
			}
			else{
				return false;
			}
		};
		
		var time_is_between = function(s, e){
			//this function will take a start time and end time to see if the
			//current time fits within the interval
			s = s.split(':');
			s[0] = parseInt(s[0], 10);
			s[1] = parseInt(s[1], 10);
			e = e.split(':');
			e[0] = parseInt(e[0], 10);
			e[1] = parseInt(e[1], 10);
			
			var d = self.date;
			
			//now we need to see if the current hour fits into the the interval
			if(d.hours >= s[0] && d.hours <= e[0]){
				//the banner fits within the hours, so check the minutes now
				if(d.hours === s[0] && d.hours === e[0] && (d.minutes >= s[1] && d.minutes <= e[1])){
					//the banner fits
					return true;
				}
				else if(d.hours === s[0] && d.hours < e[0] && (d.minutes >= s[1] && d.minutes <= e[1] + 60)){
					//example: current time = 11:17, start time = 11:16, end_time = 12:05

					return true;
				}
				else if(d.hours > s[0] && d.hours === e[0] && (d.minutes <= e[1])){
					return true;
				}
				else if(d.hours > s[0] && d.hours < e[0]){
					return true;
				}
				else{
					return false;
				}
			}
			else{
				//the banner didn't fit, so return false
				return false;
			}
		};
		
		self.date = get_current_date();
	};
	
	window.kwmbs = new banner_scheduler();
})(window, document);
//------------------------------------------------------------------------------

