/**
 * Init.js
 * 
 * This contains code that should run when the dom is loaded and ready. 
 * 
 * If depending on this file, make sure it is included on the page:
 * <com:TClientScript ScriptUrl=<%~ ../lib/js/init.js %> />
 * If you use TClientScript, it should be placed at the bottom of the page (not in thead)
 * 
 * you can also register the script programmatically like this:
 * 
 * public function onPreInit($param){
 *    $this->Page->ClientScript->registerScriptFile("page_init", $this->publishAsset("../core-js/init.js"));
 * }
 * 
 */
document.observe("dom:loaded", function() {
	//Initialize shadowbox
	Shadowbox.init({
		players: ["html", "iframe"],
		skipSetup: true,
		viewportPadding: 50,
		resizeDuration: 0.5,
		useSizzle: false
	});
	
	//setup contact link to open in a shadowbox
	var contactLink = $("contact_link");
	if(contactLink){		
		Event.observe(contactLink, "click", function(e){
			var link = e.findElement("a");
			var url = link.rel;

			//open the shadowbox
			Shadowbox.open({
				content: url,
				player: "iframe",
				title: link.title,
			 	width: 800,
				height: 5000
			});
					
			e.stop();	
		});
	}
	
	//setup shadowboxes
	Site.initBoxes();
	
	//music show hide link. When the user clicks on the link, hide the playlist
	var playlist_link = $('show_hide_player');
	if(playlist_link){
		Event.observe(playlist_link, "click", function(e){
			e.stop();				
			
			var player = $('music_player');
			if(player){
				player.fade({duration: 2});
			}
		});
	}
	
	//keep music player from going above the posts heading
	window.onscroll = document.documentElement.onscroll = Site.positionMusicPlayer;
	window.onresize = Site.positionMusicPlayer;
	
});

//namespace for utility methods for this site
Site = {
	/*
	 * Go through the page for everything with class shadowbox, and set an onclick handler to open a shadowbox.
	 */
	initBoxes: function(){
		//get all the links with class name shadowbox
		var links = $$(".shadowbox");
		Seed.debug(links);

		//loop through each link and define an onclick handler. This handler will open up a shadowbox with the
		//contents from the url in the link's rel attribute.
		for(var x=0; links[x]; x++){

			//onclick handler
			Event.observe(links[x], "click", function(e){
				//get the link that was clicked
				var link = e.findElement("a");
				Seed.debug(link);

				//stop the link's default action
				e.stop();

				//get the url for the ajax content. this is stored in name so that the href can be used like normal if js is disabled.
				var url = link.rel;

				new Ajax.Request(url, {
					method:'get',
				 	onSuccess: function(transport){
				   		var content = transport.responseText;

						//open the shadowbox
						Shadowbox.open({
							content: content,
							player: "html",
							title: link.title,
						 	width: 800,
							height: 5000
						});

				   	},
				 	onFailure: function(){
					 	window.href = link.href; //if there's a problem, redirect user to the original href of the link
					}
				}); //end ajax request			
			}); //end event.observe
		} //end for loop		
	},
	
	/*
	 * Keep the music player from going above the posts heading. If there isn't enough width for the player
	 * then change the display from fixed to static.
	 */
	positionMusicPlayer: function(){
		var player = $('music_player');
		var heading = $('posts_heading');
		var content = $("content");
		var posts = $("posts");
		
		//take care of top offset
		var scrollOffset = document.documentElement.scrollTop || document.body.scrollTop; // body for Safari
		
		var headingOffsetTop = heading.cumulativeOffset()[1];
		var headingOffset = headingOffsetTop + heading.offsetHeight;
				
		var newOffsetTop = headingOffset - scrollOffset + 3; //3 for padding
		
		if(newOffsetTop < 32){
			newOffsetTop = 32;
		}
		
		if (newOffsetTop != parseInt(player.style.top, 10)){
			player.style.top = newOffsetTop + 'px';
		}
		
		//take care of left offset
		var postsWidth = posts.getWidth();
		var playerWidth = player.getWidth();
		var contentWidth = content.getWidth();
		
		var spaceLeft = contentWidth - (playerWidth + postsWidth);
		if(spaceLeft < 0){
			player.style.position = "static";
		}
		else{
			player.style.position = "";			
		}
	}
};
