$(document).ready(function()



{
	
	// enable scrolls. a simple magical one liner
	var all = $(".scroll").scrollable({size: 1, loop: true});
	
	
	// click event for scrollables
	all.click(function(e) {
	
		all.not(this).fadeTo("medium", 0.2);
		$(this).css("opacity", 1);
		
		// get previously active scroll and the current one
		var prev = all.filter(".active"), curr = $(this), api = curr.scrollable();
	
		// we are clicking on a different scrcollable than the current one
		if (prev.index(curr) == -1) {
	
			// toggle the class name
			prev.removeClass("active");
			curr.addClass("active");
	
			// optional: seek previous scroll to the beginning
			if (prev.length) { prev.scrollable().begin(); }
	
			// activates keyboard navigation for this scroll
			api.focus(); 
		}
		
		
		/** keyboard navigation **/
		
		// every time a scroll is clicked the keyboard action is activated
		$(document).unbind("keydown.my").bind("keydown.my", function(e) {
	
			// we are only interested in the up/down arrows
			if (e.keyCode == 38 || e.keyCode == 40) {
	
				// skip CTRL and ALT keys
				if (e.altKey || e.ctrlKey) { return; }
	
				// get current scroll
				var current = all.filter(".active");
	
				// get the next (or prev) scroll
				var next = e.keyCode == 40 ? current.next() : current.prev();
	
				// click on the next scroll
				if (next.is(".scroll")) {  next.click();	}
	
				// skip the default behaviour (scrolls the page a bit downwards)
				return e.stopImmediatePropagation();
			}
		});
		
		
		/** lazy loading **/ 
		api.onBeforeSeek(function(e, i) {
			var el = curr.find("a").eq(i);
			
			// loading indicator
			if (el.is(":empty")) {
				
				var image = new Image(), 
					 l = $("#loading").fadeIn(),
					 img = $("<img/>").attr("src", el.attr("href"));			
					 
				image.onload = function() { l.fadeOut(); }
				image.src = el.attr("href");
				
				el.append(img).click(function(e) { 
					return e.preventDefault(); 
				});
			}		
							
		});
	
		// cancel default click action
		return e.preventDefault();
		
	
	// mouseover/out functionality
	}).bind("mouseover mouseout", function(e)  {	
		var t = $(e.relatedTarget), isOver = e.type == 'mouseover';
	
		if (all.filter(".active").length) {
			var el = $(this);
			el.css("opacity", isOver ? 1 : 0.5);
		}
		
		if (!isOver && !t.is(".info") && !t.closest(".info").length && (!t.is("img") || t.is("body"))) {			
			all.fadeTo("medium", 1).removeClass("active");		
			$(document).unbind("keydown.my");
		}
	});
	
	
	// Expand Panel
	$("#open").click(function(){
		$("div#panel").slideDown("slow");
	
	});	
	
	// Collapse Panel
	$("#close").click(function(){
		$("div#panel").slideUp("slow");	
	});		
	
	// Switch buttons from "Log In | Register" to "Close Panel" on click
	$("#toggle a").click(function () {
		$("#toggle a").toggle();
	});
				
	slide("#sliding-navigation", 30, 15, 150, .8);
});

function slide(navigation_id, pad_out, pad_in, time, multiplier)
{
	// creates the target paths
	var list_elements = navigation_id + " li.sliding-element";
	var link_elements = list_elements + " a";
	
	// initiates the timer used for the sliding animation
	var timer = 0;
	
	// creates the slide animation for all list elements 
	$(list_elements).each(function(i)
	{
		// margin left = - ([width of element] + [total vertical padding of element])
		$(this).css("margin-left","-150px");
		// updates timer
		timer = (timer*multiplier + time);
		$(this).animate({ marginLeft: "15" }, timer);
		$(this).animate({ marginLeft: "0" }, timer);
		$(this).animate({ marginLeft: "0" }, timer);
	});

	// creates the hover-slide effect for all link elements 		
	$(link_elements).each(function(i)
	{
		$(this).hover(
		function()
		{
			$(this).animate({ paddingLeft: pad_out }, 150);
		},		
		function()
		{
			$(this).animate({ paddingLeft: pad_in }, 150);
		});
	});
	
	$("#accordion").tabs("#accordion div.pane", {tabs: 'h2', effect: 'slide', initialIndex: null});
}

/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Copyright notice and license must remain intact for legal use
 * jFade
 * Version: 1.0 (Jun 30, 2008)
 * Requires: jQuery 1.2.6+
 *
 *
 * Original Code Copyright (c) 2008 by Michael Leigeber
 * Website: http://www.leigeber.com
 *
 *
 */

(function($) {

	$.fn.jFade = function(options) {
		// merge users option with default options
		var opts = $.extend({}, $.fn.jFade.defaults, options);
		var startrgb,endrgb,er,eg,eb,rint,gint,bint,step;
		var target = this;
		//var obj = this;
	
		var init = function() {
			var tgt = target;
			opts.steps = opts.steps || 20;
			opts.duration = opts.duration || 20;
			//clear everything + reset
			clearInterval(tgt.timer);
			endrgb = colorConv(opts.end);
			er = endrgb[0];
			eg = endrgb[1];
			eb = endrgb[2];
		
			if(!tgt.r) {
				//convert to usable rgb value
				startrgb = colorConv(opts.start);
				r = startrgb[0];
				g = startrgb[1];
				b = startrgb[2];
				tgt.r = r;
				tgt.g = g;
				tgt.b = b;
			}
			//process red
			rint = Math.round(Math.abs(tgt.r-er)/opts.steps);
			//process green
			gint = Math.round(Math.abs(tgt.g-eg)/opts.steps);
			//process blue
			bint = Math.round(Math.abs(tgt.b-eb)/opts.steps);
			if(rint == 0) { rint = 1 }
			if(gint == 0) { gint = 1 }
			if(bint == 0) { bint = 1 }
		
			tgt.step = 1;
			tgt.timer = setInterval( function() { animateColor(tgt,opts.property,opts.steps,er,eg,eb,rint,gint,bint) }, opts.duration);
		
			function animateColor(obj,property,steps,er,eg,eb,rint,gint,bint) {
				var tgt = obj;
				var color;
				if(tgt.step <= steps) { // for each loop
					var r = tgt.r;
					var g = tgt.g;
					var b = tgt.b;
					if(r >= er) {
						r = r - rint;
					}
					else {
					r = parseInt(r) + parseInt(rint);
					}
					if(g >= eg) {
						g = g - gint;
					}
					else {
						g = parseInt(g) + parseInt(gint);
					}
					if(b >= eb) {
					b = b - bint;
					}
					else {
						b = parseInt(b) + parseInt(bint);
					}
					color = 'rgb(' + r + ',' + g + ',' + b + ')';
					
					$(obj).css(property, color);
					
					tgt.r = r;
					tgt.g = g;
					tgt.b = b;
					tgt.step = tgt.step + 1;
				}
				else {// last loop
				
					clearInterval(tgt.timer);
					color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
					$(obj).css(property, color);
				}
			}
			
			// convert the color to rgb from hex
			function colorConv(color) {
				//covert 0-2 position hex into decimal in rgb[0]
				//covert 2-4 position hex into decimal in rgb[1]
				//covert 4-6 position hex into decimal in rgb[2]
				var rgb = [parseInt(color.substring(0,2),16),
				parseInt(color.substring(2,4),16),
				parseInt(color.substring(4,6),16)];
				//return array containing rgb[0], rgb[1], rgb[2]
				return rgb;
			}
		};
		if (opts.trigger == "load")
			init();
		else
			$(this).bind(opts.trigger, function(){
				target = this;
				init();
			});
		
		return this;
	};

	$.fn.jFade.defaults = {
		trigger: "load",
		property: 'background',
		start: 'FFFFFF',
		end: '000000',
		steps: 5,
		duration: 30
	};
})(jQuery);
