//Transform mailto-links
jQuery(function() {
	jQuery(".mailto_link").each(function() {
		var link = jQuery(this);
		var link_address = link.attr("href");
		link_address = link_address.replace(/!/g, ".");
		link_address = link_address.replace(/\^/g, "@");
		var link_text = link.text().replace(/\s\[at\]\s/, "@");
		if(link_address.substr(0,7) !== "mailto:") {
			link_address = "mailto:"+link_address;
		}
		link.attr("href", link_address);
		link.text(link_text);
	});
});

//Store information in class names
jQuery.fn.extend({
	getClassValue: function(classStart) {
		var defaultValue = arguments[1] || false;
		classStart+='-';
		var result = defaultValue;
		jQuery.each(this.get(0).className.toString().split(/\s+/), function(i, val) {
			if(val.indexOf(classStart) === 0) {
				result = val.substring(classStart.length);
				return false;
			}
		});
		return result;
	}
});

jQuery(function() {
	var overlay_image_wrappers = jQuery('.overlay_image_wrapper').each(function() {
		var overlay_image_wrapper = jQuery(this);
		if(overlay_image_wrapper.children().length < 2) {
			return;
		}
		
		// Init
		overlay_image_wrapper.children().addClass('bottom_element').filter(":first").
		addClass('top_element').removeClass('bottom_element').next().addClass('next_element').removeClass('bottom_element');
		
		var animation_type = overlay_image_wrapper.getClassValue('animation-type') ? overlay_image_wrapper.getClassValue('animation-type') : 'cover';
		var animation = OverlayAnimations[animation_type];
		animation.position_next_element(overlay_image_wrapper.children('.next_element'));
		
		overlay_image_wrapper.css({width: overlay_image_wrapper.width()});
		var animation_duration = parseInt(overlay_image_wrapper.getClassValue('animation-duration', 2500));
		var animation_curve = overlay_image_wrapper.getClassValue('animation-curve') ? overlay_image_wrapper.getClassValue('animation-curve') : 'easeOutExpo';
		
		// Run
		overlay_image_wrapper.bind('next-overlay', function() {
			if(overlay_image_wrapper.data('is_animating')) {
				return;
			}
			overlay_image_wrapper.data('is_animating', true);
			var top_element = overlay_image_wrapper.children('.top_element');
			var next_element = overlay_image_wrapper.children('.next_element');
			animation.position_next_element(next_element, true);
			overlay_image_wrapper.trigger('animate-next-overlay', {top_element: top_element, next_element: next_element});
			animation.animate(top_element, next_element, animation_duration, animation_curve, function() {
				top_element.removeClass('top_element').addClass('bottom_element');
				next_element.addClass('top_element').removeClass('next_element');
				var new_next_element = next_element.next();
				if(new_next_element.length === 0) {
					new_next_element = overlay_image_wrapper.children('.bottom_element:first');
				}
				new_next_element.removeClass('bottom_element').addClass('next_element');
				animation.cleanup(top_element, next_element, new_next_element);
				animation.position_next_element(new_next_element);
				
				overlay_image_wrapper.data('is_animating', false);
				overlay_image_wrapper.trigger('next-overlay-finished');
			});
		});
	});
});

jQuery(window).bind('load', function() {
	var delay_timeout = 2500;
	window.setTimeout(function() {
		jQuery('.overlay_image_wrapper.activate_after_delay').trigger('next-overlay');
	}, delay_timeout);
});

jQuery(document).ready(function() {
	var slideshows = jQuery('.slideshow');
	slideshows.each(function() {
		var slideshow = jQuery(this);
		var overlay_timeout = parseInt(slideshow.getClassValue("slideshow-timeout", 400));
		var overlay_trigger = function() {
			if(slideshow.data('stopped')) {
				return;
			}
			slideshow.trigger('next-overlay');
		};
		window.setTimeout(overlay_trigger, overlay_timeout);
		slideshow.bind('next-overlay-finished', function() {
			if(slideshow.data('stopped')) {
				return;
			}
			window.setTimeout(overlay_trigger, overlay_timeout);
		});
	});
});

jQuery(function() {
	jQuery('.load-ajax a').live('click', function() {
		var link = jQuery(this);
		if(link.is('.no-ajax, .no-ajax *, *[rel=document], *[rel=external], .service_links *')) {
			return true;
		}
		var load_ajax = link.parents('.load-ajax');
		var load_into = jQuery(load_ajax.getClassValue('load-into'));
		
		var load_handler = function() {};
		link.blur();
		load_into.load(link.attr('href'), {container_only: 'content'}, load_handler);
		return false;
	});
});

jQuery(function() {
	jQuery('.project_thumbnail_info_overlay').each(function() {
		var overlay = jQuery(this);
		overlay.parents('.thumbnail_item').hover(function() {
			overlay.animate({marginTop: "-50px", height: "50px", opacity: 1}, {queue: false});
		}, function() {
			overlay.animate({marginTop: 0, height: 0, opacity: 0}, {queue: false});
		});
	});
});

jQuery(function() {
	var fade_ins = jQuery('.sequential_fadein');
	var index = 0;
	if(fade_ins.length == 0) {
		return;
	}
	window.fade_in_interval = window.setInterval(function() {
		var fade_in = jQuery(fade_ins.get(index));
		index++;
		fade_in.css({display:'none'}).css({visibility: 'visible'});
		fade_in.fadeIn('fast');
		if(index == fade_ins.length) {
			window.clearInterval(window.fade_in_interval);
		}
	}, 150);
});

var OverlayAnimations = {
	cover: {
		position_next_element: function(next_element, is_immediate) {
			next_element.css({opacity: 0, display: is_immediate ? '' : 'none'});
		},
		animate: function(top_element, next_element, animation_duration, animation_curve, callback) {
			next_element.animate({
				left: 0
			}, {duration: animation_duration, easing: animation_curve, complete: callback, queue: false});
		},
		cleanup: function(top_element, next_element, new_next_element) {
			top_element.css({left: null});
		}
	},
	fadeWhite: {
		position_next_element: function(next_element) {
			next_element.css({opacity: 0, display: 'none'});
		},
		animate: function(top_element, next_element, animation_duration, animation_curve, callback) {
			top_element.animate({opacity: 0}, {duration: animation_duration/2, easing: animation_curve.replace(/InOut/, "In"), complete: function() {
				next_element.animate({opacity: 1}, {duration: animation_duration/2, easing: animation_curve.replace(/InOut/, "Out"), complete: callback});
			}});
		},
		cleanup: function(top_element, next_element, new_next_element) {}
	},
	dissolve: {
		position_next_element: function(next_element, is_immediate) {
			next_element.css({opacity: 0, display: is_immediate ? '' : 'none'});
		},
		animate: function(top_element, next_element, animation_duration, animation_curve, callback) {
			next_element.animate({opacity: 1}, {duration: animation_duration, easing: animation_curve, complete: callback});
		},
		cleanup: function(top_element, next_element, new_next_element) {}
	}
}

OverlayAnimations.push = {
	position_next_element: OverlayAnimations.cover.position_next_element,
	animate: function(top_element, next_element, animation_duration, animation_curve, callback) {
		OverlayAnimations.cover.animate(top_element, next_element, animation_duration, animation_curve, callback);
		top_element.animate({
			marginLeft: "-"+(top_element.width()-1)+"px"
		}, {duration: animation_duration, easing: animation_curve, queue: false, complete: function() {
			top_element.css({marginLeft: null});
		}});
	},
	cleanup: OverlayAnimations.cover.cleanup
};

/**
* Stylesheet toggle variation on styleswitch stylesheet switcher.
* Built on jQuery.
* Under an CC Attribution, Share Alike License.
* By Kelvin Luck ( http://www.kelvinluck.com/ )
**/

(function($) {
	var createCookie = function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	};
	
	var readCookie = function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++)
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	};
	
	var eraseCookie = function(name) {
		createCookie(name,"",-1);
	};
		
	// To loop through available stylesheets
	$.stylesheetSave = function() {
		var foundStyle = false;
		$('link[rel*=style][title]').each(function(i) {
			if(this.disabled) {
				return;
			}
			createCookie('style', this.getAttribute('title'), 365);
			foundStyle = true;
			return false;
		});
		if(!foundStyle) {
			eraseCookie('style');
		}
	};
	
	$.stylesheetSwitch = function(styleName) {
		$('link[@rel*=style][title]').each(function() {
			this.disabled = true;
			if(this.getAttribute('title') == styleName) {
					this.disabled = false;
			}
		});
		createCookie('style', styleName, 365);
	};
	
	// To initialise the stylesheet with it's 
	$.stylesheetInit = function() {
		var c = readCookie('style');
		if (c) {
			$.stylesheetSwitch(c);
		}
	};
})(jQuery);

jQuery(jQuery.stylesheetInit);
jQuery(window).bind('unload', jQuery.stylesheetSave);

jQuery(function() {
	var pageTracker = _gat._getTracker("UA-10691002-1");
	pageTracker._trackPageview();
});