jQuery(function($){
	var smallHeight = 81; //px
	var animationTime = 800; //miliseconds
	
	var page = parseURL(window.location);
	var article = page.params.article;
	
	
	function collapse(collapsable){  //takes jquery element object ($())
		collapsable.animate({height:smallHeight + "px"},animationTime, function(){
			var text = collapsable.data('content').substring(0,280);
			var lastSpace = text.lastIndexOf(' ');
			collapsable.data('open', false).html(text.substring(0,lastSpace) + '...');
		});
	}
	
	$(".collapsing").each(function(){
		height = $(this).height() + 50;
		if (height > (smallHeight)){
			$(this).append("<a class='newsToggle' href='#'>Close News</a>")
			// $(this).append("<img src='images/close_news.gif' alt='Close News Article' class='article_closer'>")
			$(this).data('maxHeight',height).data('content',$(this).html()).data('open', true);
			var name = $(this).parent(".news_info").children("a").attr('name');
			if ( name != "article" + article ) {
				collapse($(this),$(this).html());
			}else{
				// $.scrollTo(this, 1000);
			} 
			// collapse($(this),$(this).html()); 
				// .after("<a class='newsToggle' href='#'>Open News Story</a>");
		}
	});
	
	setTimeout(function(){
		$.scrollTo($("a[name="+page.hash+"]"));
	}, 850);
	
	
	$(".newsToggle").click(function(e){
		e.preventDefault();
		var $container = $(e.target).parents(".news_info");
		var collapsable = $(".collapsing", $container);
		collapse(collapsable);
	});
	
	$(".newsHeader").click(function(e){
		e.preventDefault();
			
		var $container = $(e.target).parents(".news_info");
		var collapsable = $(".collapsing", $container);
		
		if(collapsable.data('open')){
			//close
			collapse(collapsable);
		}else{
			//open
			collapsable.animate({height: collapsable.data('maxHeight') + "px"},animationTime)
				.data('open', true)
				.html(collapsable.data('content'));
				
				$(".newsToggle").click(function(e){
					e.preventDefault();
					var $container = $(e.target).parents(".news_info");
					var collapsable = $(".collapsing", $container);
					collapse(collapsable);
				});
				
				Mediabox.scanPage();  //rescan recreated html
			
		}
	});
});

// This function creates a new anchor element and uses location
// properties (inherent) to get the desired URL data. Some String
// operations are used (to normalize results across browsers).
 //http://james.padolsey.com/javascript/parsing-urls-with-the-dom/
function parseURL(url) {
    var a =  document.createElement('a');
    a.href = url;
    return {
        source: url,
        protocol: a.protocol.replace(':',''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function(){
            var ret = {},
                seg = a.search.replace(/^\?/,'').split('&'),
                len = seg.length, i = 0, s;
            for (;i<len;i++) {
                if (!seg[i]) { continue; }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;
        })(),
        file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
        hash: a.hash.replace('#',''),
        path: a.pathname.replace(/^([^\/])/,'/$1'),
        relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
        segments: a.pathname.replace(/^\//,'').split('/')
    };
}
