
/**************************************************************/
/***** Window onload Functions ********************************/
/**************************************************************/

window.addEvent(domready, function()
{
	/***** Corners *****/
	var addCorners = function(el)
	{
		var cl = {0: 't-l', 1: 't-r', 2: 'b-r', 3: 'b-l'};
		for(var p in cl)
			el.appendChild(new El('span', {className: cl[p] +' corners'}));
	};
	$(document.body).getByClass('round-corners').each(addCorners);
	/***** Corners *****/
	
	/***** Fix Navigation *****/
	ISDMain.fixNavigation();
	
	/***** Black/Gray Navigation *****/
	$('container').getByClass('tab-nav').each(ISDTabNav.addNavigation);
	
	/***** Calendar Special Sections *****/
	if($('r-c-calendars'))
		$('r-c-calendars').getByClass('hl', 'td').each(ISDMain.rateCardCalendar.init);
	
	/***** Share Buttons *****/
	if($('article'))
	{
		$('article').getByClass('isd-share').each(function(el)
		{
			$(el).addEvent('click', function()
			{
				ISDMain.share();
				return false;
			});
		});
	}
	
	/***** Test Links *****/
	/*$A(document.getElementsByTagName('a')).each(function(el)
	{
		if(el.href.indexOf('#') == el.href.length - 1)
		{
			el.onclick = function()
			{
				return false;
			};
		}
	});*/
});

window.addEvent('load', function()
{
	/***** Article Section Height *****/
	ISDMain.fixHeight();
});

/**************************************************************/
/***** ISD Main Functions *************************************/
/**************************************************************/

var SHARETHIS = 
{
	meta:
	{
		publisher : '10eb74f4-fdd7-4c73-94b8-8331580a709d',
		hostname  : location.host,
		location  : location.pathname
	},

	form: function(olist)
	{
		var field = function(k, v, brackets)
		{
			var f   = document.createElement("input");
			f.type  = "hidden";
			f.name  = (brackets == true ? k + "[]" : k);
			f.value = v;
			return f;
		}
		var normalize = function(s)
		{
			if (s == null)
				return '';
			return encodeURIComponent(s);
		}

		var f = document.createElement("form");
		f.method = "post";
		//f.action = "http://sharethis.com/share/lightbox";
		f.action = "http://wd.sharethis.com/share3x/lightbox.html";
		//f.action = "http://iowastatedaily.com/shareBroken.html";
		f.target = "stframe";
		for(var j in this.meta)
			f.appendChild(field("meta_" + j, normalize(this.meta[j])));
		for(var i = 0; i < olist.length; i++)
		{
			obj = olist[i];
			for(var j in obj.properties)
				f.appendChild(field(j, normalize(obj.properties[j]), true));
			for(var j in obj.query)
				f.appendChild(field("query_"+j, normalize(obj.options[j]), false));
			for(var j in obj.options)
				f.appendChild(field("opts_"+j, normalize(obj.options[j]), false));
		}
		return f;
	}
};

var ISDLoginBox = '<form action="" method="post" class="login" id="overlay-login">'
		+ '	<label>Email:<br><input type="text" name="data[isd-email]"></label>'
		+ '	<label>Password:<br><input type="password" name="data[isd-password]"></label>'
		+ '	<a href="http://www.iowastatedaily.net/admin/users/forgot.php" style="display: block; padding-bottom: 5px">Forgot Password?</a>'
		+ '	<input type="text" name="phone" value="" style="display: none">'
		+ '	<input type="hidden" name="action" value="login">'
		+ '	<input type="submit" value="Login">'
		+ '</form>'
		+ "<script> setTimeout(function(){$('overlay-login')['data[isd-email]'].focus()}, 600) </script>";

var ISDMain =
{
	fixHeight: function()
	{
		var rightColH = $('col-right').offsetHeight;
		var leftColH = $('col-left').offsetHeight;
		if(rightColH > leftColH)
		{
			var lastItem = $($('col-left').getByClass('round-corners').getLast());
			if($('staff-list') || $('isdFixHeightByPass'))
				lastItem = $('content');
			if(!lastItem)
				return;
			var lastItemH = this.minHeight || lastItem.getHeight();
			this.minHeight = (lastItemH < this.minHeight) ? lastItemH : this.minHeight;
			lastItem.style.height = (lastItemH + rightColH - leftColH) +'px';
		}
	},
	
	fixNavigation: function(sec)
	{
		if(!$('isd-menu'))
			return false;
		var ul = $('isd-menu').getByTag('ul')[0];
		var lis = $(ul).getByTag('li');
		var ulChilds = $A(ul.childNodes);
		var isSel = false;
		for(var i = 0; i < lis.length; i++)
		{
			$(lis[i]);
			if(ulChilds.contains(lis[i]))
			{
				if(sec)
				{
					if((lis[i].textContent || lis[i].innerText).toLowerCase().contains(sec.toLowerCase()))
						lis[i].addClass('sel');
					else
						lis[i].removeClass('sel');
				}
				if($(lis[i]).hasClass('sel'))
					isSel = true;
			}
		}
		if(!isSel && !sec)
			lis[0].addClass('sel');
	},
	
	hideMsg: function(el, name, value)
	{
		$(el).hide(
		{
			onComplete: function()
			{
				el.remove();
			}
		});
		var time = new Date().getTime();
		time += 2 * (24 * 60 * 60 * 1000);
		time = new Date(time).toGMTString();
		document.cookie = name +'='+ value +'; expires='+ time +'; path=/';
	},
	
	minHeight: 0,
	
	rateCardCalendar:
	{
		defn:
		{
			'A': { h: 'Daily Directory', d: ['August 19, 2008'] },
			'B': { h: 'Welcome Back', d: ['August 21, 2008'] },
			'C': { h: 'Gridirons', d: ['August 28, 2008', 'September 5, 2008', 'October 3, 17, 24, 2008', 'November 14, 2008'] },
			'D': { h: 'Fall Career Guide', d: ['September TBA, 2008'] },
			'E': { h: 'Clubs & Organizations', d: ['October 6, 2008'] },
			'F': { h: 'Family Weekend', d: ['October TBA, 2008'] },
			'G': { h: 'Homecoming', d: ['October 24, 2008'] },
			'H': { h: 'Hoops', d: ['November TBA, 2008'] },
			'I': { h: 'HoliDaily', d: ['December 3, 2008'] },
			'J': { h: 'Finals Edition', d: ['December 15, 2008'] },
			'K': { h: 'Games Books', d: ['January 12, 20, 26, 2008', 'February 2, 9, 16, 23, 2009'] },
			'L': { h: "Valentine's Day Tab", d: ['February 10, 2008'] },
			'M': { h: "Renter's Guide", d: ['February TBA, 2009'] },
			'N': { h: 'Spring Career Guide', d: ['February TBA, 2009'] },
			'O': { h: 'Spring Sports', d: ['March 9, 2009'] },
			'P': { h: 'Spring Tab', d: ['March 30, 2009'] },
			'Q': { h: 'Greek Beat', d: ['March TBA, 2009'] },
			'R': { h: 'Veishea Edition', d: ['April 13, 2009'] },
			'S': { h: 'Veishea Parade Edition', d: ['April 17, 2009'] },
			'T': { h: 'Finals Edition', d: ['May 4, 2009'] },
			'U': { h: 'Orientation Guide', d: ['June 2, 2009'] },
			'V': { h: '4th of July Edition', d: ['June 30, 2009'] }
		},
		
		init: function(el)
		{
			var sec = el.getAttribute('sec');
			el.sec = sec;
			$(el).addEvent('click', function()
			{
				var o = ISDMain.rateCardCalendar.defn[this.sec];
				var msg = ''
				+ '<div>'
				+ '	<span class="b">'+ o.h +'</span><br>'
				+ '	'+ o.d.join('<br>') +''
				+ '</div>';
				ISDMain.lightBox.lightBox(msg);
				console.log(ISDMain.rateCardCalendar.defn[this.sec]);
			});
		}
	},
	
	lightBox:
	{
		lightBox: function(msg, style)
		{
			var body = document.body, html = document.getElementsByTagName('html')[0];
			var topOff = html.scrollTop, totWidth = html.clientWidth;
			var viewHeight = html.clientHeight;
			var totHeight = (html.scrollHeight > body.scrollHeight) ? html.scrollHeight : body.scrollHeight;
		
			var overlay = document.createElement('div');
			$(overlay).id = '_lb-overlay';
			overlay.addEvent('click', ISDMain.lightBox.closeLightBox);
			overlay.setStyles(
			{
				height: totHeight +'px', opacity: 0
			});
			document.body.appendChild(overlay);
		
			var box = document.createElement('div');
			$(box).id = '_lb-box';
			//box.addEvent('click', ISDMain.lightBox.closeLightBox);
			box.innerHTML = '<div id="_lb-close"><span onclick="ISDMain.lightBox.closeLightBox()">x</span></div>';
			if(typeof msg != 'string')
				box.appendChild(msg);
			else
				box.innerHTML += msg;
			if(style)
				box.setStyles(style);
			box.style.opacity = 0;
			document.body.appendChild(box);
			var h = box.getHeight();
			box.style.top = (topOff + (viewHeight - h) / 2) +'px';
		
			new Anim(overlay,
			{
				duration: 200,
				onComplete: function(el)
				{
					new Anim(box, {duration: 400}).start({opacity: [1]});
				}
			}).start({opacity: [0.7]});
		},

		closeLightBox: function()
		{
			var overlay = $('_lb-overlay');
			var coupon = $('_lb-box');
			if(overlay)
				new Anim(overlay, {duration: 200, onComplete: function(){ overlay.remove(); }}).start({opacity: [0]});
			if(coupon)
				new Anim(coupon, {duration: 200, onComplete: function(){ coupon.remove(); }}).start({opacity: [0]});
		}
	},
	
	shareWindow: null,
	
	share: function()
	{
		//console.log($($('article').getByClass('article-outline')[0]).getByTag('h2')[0].textContent);
		var author = $('article').getByClass('byline')[0];
		author = (author.textContent || author.innerText).replace('By ', '');
		var art_date = $('article').getByClass('timestamp')[0];
		art_date = new Date(art_date.textContent || art_date.innerText).toGMTString();
		var art_title = $($('article').getByClass('article-outline')[0]).getByTag('h1')[0];
		art_title = art_title.textContent || art_title.innerText;
		var form = SHARETHIS.form([
		{
			properties:
			{
				//type:       '',
				title:      art_title,
				//summary:    '',
				//content:    '',
				url:        location.protocol+'//'+location.host+location.pathname,
				//icon:       '',
				//category:   '',
				updated:    art_date,
				//published:  '',
				author:     author
			},
			options:
			{
				tabs: 'web,post',
				charset: 'utf-8',
				style: 'default',
				publisher: '10eb74f4-fdd7-4c73-94b8-8331580a709d',
				linkfg: '#D42424',
				popup: 'true'
			},
			query:
			{
				tabs: 'web,post',
				charset: 'utf-8',
				style: 'default',
				publisher: '10eb74f4-fdd7-4c73-94b8-8331580a709d',
				linkfg: '#D42424',
				popup: 'true'
			}
		}]);
		if(this.shareWindow)
			this.shareWindow.close();
		var frame = document.createElement('iframe');
		frame.name = 'stframe';
		frame.src = 'http://sharethis.com/widget/loading';
		$(frame).setStyles(
		{
			width: '354px', height: '437px', border: 0
		});
		document.body.appendChild(frame);
		ISDMain.lightBox.lightBox(frame, {width: '354px'});
		document.body.appendChild(form);
		form.submit();
		$(form).remove();
		return false;
	},
	
	toggleLoginBox: function(el, box)
	{
		if(box)
		{
			var o = $(el).getCoords();
			box = $(box);
			box.setStyles(
			{
				top: o.t + 'px', left: o.l - 246 + 'px'
			});
		}
		else
			box = $(el);
		(box.style.display == 'block') ? box.hide({duration: 100, height: false, overflow: false}) : box.show({height: false, overflow: false});
	},
	
	parseEmail: function(t)
	{
		t = t.replace(/\$12v\$/g, '');
		document.write('<a href="mailto:'+ t +'">'+ t +'</a>');
	}
};

/**************************************************************/
/***** Featured Box *******************************************/
/**************************************************************/

var ISDFeatured =
{
	prev: 0,

	init: function()
	{
		var obj = this;
		this.tags = $('fp-items').getByTag('div');
		this.anims = [];
		this.tags.each(function(tag)
		{
			obj.anims.push(new Anim(tag, {duration: 300}));
		});
		this.links = $('fp-controls').getByTag('a');
		this.links.each(function(el, i)
		{
			el.onmouseover = function()
			{
				var prev = ISDFeatured.prev;
				if(i == prev)
					return false;

				if(ISDFeatured.anims[prev])
				{
					ISDFeatured.anims[prev].options.onComplete = function(tag)
					{
						tag.style.display = 'none';
					};
					ISDFeatured.anims[prev].start({opacity: [0]});
				}
				$(ISDFeatured.links[prev]).removeClass('sel');
				
				if(ISDFeatured.anims[i])
				{
					$(ISDFeatured.tags[i]).setStyles({display: 'block'});
					ISDFeatured.anims[i].options.onComplete = function(){};
					ISDFeatured.anims[i].start({opacity: [1]});
				}
				$(ISDFeatured.links[i]).addClass('sel');
				
				ISDFeatured.prev = i;
			};
			
			if(i == 0)
			{
				$(ISDFeatured.tags[i]).setStyles({display: 'block'});
				ISDFeatured.anims[i].start({opacity: [1]});
				$(ISDFeatured.links[i]).addClass('sel');
			}
		});
	}
};

/**************************************************************/
/***** Black/Gray Navigations *********************************/
/**************************************************************/

var ISDTabNav =
{
	addNavigation: function(el)
	{
		var table = $(el).getByClass('nav');
		if(!table)
			return;
		table = $(table[0]);
		var lists = el.getByClass('tab-nav-item');
		$(lists[0]).getByTag('a').each(ISDTabNav.parseText);
		var links = table.getByTag('a');
		var addHook = function(link, n)
		{
			$(link).addEvent('click', function(e)
			{
				$stopEvent(e);
				ISDTabNav.selectTab(links, lists, n);
				ISDMain.fixHeight();
				return false;
			});
		};
		links.each(addHook);
	},
	
	parseText: function(el, i)
	{
		if(!el.title)
		{
			var m = el.innerHTML.match(/^([^<]+)/);
			if(m)
				el.title = m[1];
		}
		
		var n = -1;
		while(1)
		{
			var t = el.innerHTML;
			var h = el.offsetHeight;
			if(h > 20 && n < 20)
			{
				if(t.toLowerCase().contains('</span>'))
					el.innerHTML = t.replace(/\s([^\s\<]+)\s\<span/i, '... <span');
				else
					el.innerHTML = t.replace(/\s([^\s]+)$/, '...');
			}
			else
				break;
			n++;
		}
	},
	
	selectTab: function(links, lists, n)
	{
		lists.each(function(list, i)
		{
			$(list)[(i == n) ? 'addClass' : 'removeClass']('show');
			$(list).getByTag('a').each(ISDTabNav.parseText);
		});
		links.each(function(a, i)
		{
			(i == n) ? $(a.parentNode).addClass('sel') : $(a.parentNode).removeClass('sel');
		});
	}
};

var ISDArticleTab =
{
	els:
	{
		'main': '',  'video': '', 'audio': '',  'discussion': ''
	},

	load: function()
	{
		var t = window.location.hash;
		this.selectTab(t);
	},

	selectTab: function(t)
	{
		if(t.contains('comment-') || t.contains('respond'))
			t = '#discussion';
		if(t == '')
			t = '#main';
		for(var el in this.els)
		{
			var curEl = $('isd-article-tab-'+ el);
			if(curEl)
			{
				curEl.style.display = ('#'+ el == t) ? 'block' : 'none';
				var fn = ('#'+ el == t) ? 'addClass' : 'removeClass';
				if($('isd-article-nav-'+ el)[fn])
					$('isd-article-nav-'+ el)[fn]('sel');
			}
		}
		$('isd-article-tab-main').parentNode.style.height = '';
		ISDMain.fixHeight();
	}
};



