// jQuery scripting for Campaign Homepage

var CHP = CHP || (function() {

	// List of plugins to install:
	CHPlugins = {
		
		CHPticker: function(options) {

			options = options || {}
			var tictoc = options.timer || 5000;
			var feed = options.feed;

			var items = [];
			var count = 0;
			var me = this;
			var cw = null;
			var paused = false;
			var _cw = function() {
				cw = window.setTimeout(_advance, tictoc);
			}

			var _advance = function() {
				if(!paused)	 me.fadeOut('fast', function() {	
					if(!paused) {
						me.html(items[++count]).fadeIn('fast');
						var temp=document.getElementById("fader");
						if(temp!=null)
						temp.style.filter='none';
						_cw();
					}
					else me.HTML(items[++count]);	
					if(count == items.length -1 ) count = -1;
				});
			}

			var _pause = function() {
				paused = true;	
				window.clearTimeout(cw);
			}
			
			var _play = function() {
				paused = false;	
				_cw();
			}

			// time to initialize things. we'll start with the feed:


				// we've gotten "feed", but it's written in RSS and we'll need to parse it into an array
				$.jGFeed('http://feeds2.feedburner.com/OSU-All-News',
				function(feeds){
				  // Check for errors
				  if(!feeds){
					// there was an error
					return false;
				  }
				  // do whatever you want with feeds here
				  for(var i=0; i<feeds.entries.length; i++){
					var entry = feeds.entries[i];
					// Entry title
					var pubDate = new Date(entry.pubDate);
					var strDate = pubDate.getMonth() + '/' + pubDate.getDate() + '/' + pubDate.getFullYear();
					var title = entry.title.replace(/’/g,"'").replace(/‘/g,"'").replace(/–/g,'-');
					
					if(title.length>70){
						title=title.substring(0,70)+"...";	
					}
					items[i] = '<a id="fader" href="'+entry.link+'"target="_blank">'+title+'</a>';
				  }
				  	me.mouseover(function() { _pause(); });
					me.mouseout(function() { _play(); });
				
				// display first story and start the clock:
				me.html(items[0]);
				_cw();
				}, 10);

				// now we can get down to business:


			return this;
		},
		CHPaccordion: function(options) {

			options = options || {}

			var a_h = this.children('li');

			var delay = options.delay || 0;
			var count = a_h.length;

			a_h.children('h3').mouseover(function() {
				var me = jQuery(this).parent();

				if(me.children('a').css('display') == 'block') {
					// do nothing if this one's already showing.
					return false;
				}
				else {
					// show this one, and hide the others (if they're open).
					var s = me.siblings();
					me.children('a').slideDown(delay);
					s.children('a').slideUp(delay);
					me.addClass('active');
					s.removeClass('active');
				}
			});
		
			return this;
		},

		/*
		*  CHP Features is the slideshow script used to manage stories on the campaign homepage
		*
		*/
		CHPfeatures: function(options) {

			options = options || {}
			var me = this;

			var _stories = options.stories;
			
			var _delay = options.interval;
			var _min = options.first || 0;
			var _max = options.last || (_stories.length - 1);
			
			var _index = 0;
			var _to = null;
			var _stopped = true;
			
			var _pause = function() {
				if(!_stopped) {	
					window.clearTimeout(_to);
					_to = null;
					_stopped = true;	
				}
			}
			
			var _showImage = function() {
				me.children('#story-image').html('<img src="'+ _stories[_index].image + '" width=572 height=323 />');
				$("#story-image").fadeIn("500");
			}
			var _showVideo = function() {
									var flashvars = {
						//autostart: 'true',
						controlbar: 'none',
						captions: _stories[_index].caption,
						file:_stories[_index].video,
						image:_stories[_index].image
					}
					var params = {wmode: 'transparent'};
				
					me.children('#story-image').html('<div id="preview"></div>');

					swfobject.embedSWF('/video/players/jwplayer/player.swf', 'preview', '572', '322', '9.0.0','expressInstall.swf', flashvars, params);
					
					jQuery('#preview').click(_pause);
				$("#story-image").fadeIn("500");
			}
			
			var _show = function() {
			
				// let's display some multimedia, depending on what type it is....
				
				if(video = _stories[_index].video) {
					
					// include a video


					$("#story-image").fadeOut("500");
					setTimeout(_showVideo,500);
					
					// done including el video.

				}
				else if(img = _stories[_index].image) {
					
					// include a picture:
					$("#story-image").fadeOut("500");
					setTimeout(_showImage,500);
					
					

				}
				else {
					throw('must spec multimedia (image/video) to display!');	
				}

				me.children('#story-caption').attr('href',_stories[_index].link);
				me.children('#story-caption').html(_stories[_index].caption + ' &raquo;');
				
				jQuery('#story-cat').html('<p>For ' + _stories[_index].cat + '</p>');
				
				jQuery('#story-cat').removeClass().addClass(_stories[_index].cat.replace('the ',''));
				me.children('#story-caption').removeClass().addClass(_stories[_index].cat.replace('the ',''));
				jQuery('#story-slides li').removeClass('active');
				jQuery('#story-slides li:nth-child('+(_index+1)+')').addClass('active');
			}
			
			var _advance = function(step) {
				if(step == null) step = 1;
				_index+=step;
			
				if(_index > _max) _index = _min;
				if(_index < _min) _index = _max;

				_show();
		
				if(!_stopped) _to = window.setTimeout(_advance, _delay, 1);
			}
			
			// set up slide controls
			
			var ctl_index = 0;
			
			//alert(this.children('#story-slides').children('li').children('a').length);
			
			jQuery('#story-slides li a').each(
			function(){
				var c = ctl_index;
				this.onclick = function(){ 
					me.FeatureStory.goto(c); 
					return false; 
				}
				ctl_index++;
			});

			// set up API
			this.FeatureStory = {		
				// clock management functions: pause
				pause: _pause,
				// clock management functions: play
				play: function() {
					_stopped = false;
					_advance(1);
				},
	
				// show management: go to a specified story
				goto: function(index) {
					this.pause();
					_index = index;
					_show();	
				},
	
				// show management: go to the next story
				advance: _advance
			};
			
			return this;
		}
	}

	// initialize jQueryAge
	for(x in CHPlugins) {
		if(typeof(jQuery.fn[x]) == 'undefined') {
			jQuery.fn[x] = CHPlugins[x];
		}
		else {
			throw('jQuery function ' + x + ' already exists!');	
		}
	}
	
	// initialization-type stuff
	jQuery(document).ready(function(){
		// do init
	});
})();

(function($){$.extend({jGFeed:function(url,fnk,num,key){if(url==null){return false;}var gurl="http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q="+url;if(num!=null){gurl+="&num="+num;}if(key!=null){gurl+="&key="+key;}$.getJSON(gurl,function(data){if(typeof fnk=="function"){fnk.call(this,data.responseData.feed);}else{return false;}});}});})(jQuery);
