/*-----------------------------------------------------------------------------
 JS for fusion.fi
 version:    1.1
 date:       22/04/09
 author:     Fusion
 email:      florian.plank@fusion.fi
 website:    http://www.fusion.fi
 -----------------------------------------------------------------------------*/

// IMR
Cufon.replace('h2');
//Cufon.replace('h3');

var ENV = window.location.href.match(/\http:\/\/localhost:/) ? "development" : "production";

$(document).ready(function() {

	// Browser detection
	var isIE = (jQuery.browser.msie);
	var isIE6 = (jQuery.browser.msie && Number(jQuery.browser.version <= 6.0));
	var isIE7 = (jQuery.browser.msie && Number(jQuery.browser.version <= 7.0));

	// PNG fix for IE6
	if (isIE6) DD_belatedPNG.fix('ul#main-nav a, h1 img, #jump-to-content');

	// center images vertically
	if (isIE6) {
		$('.client-logos li img').each(function() {
			$(this).css('margin-top', (60 - $(this).height()) / 2);
		});
	}

	// replace email address
	// anti-spam measure
	$('span.email-address').each(function() {
		var obfAddress = $(this).text();
		var realAddress = obfAddress.replace(/\[at\]/g, '@').replace(/\[dot\]/g, '.');
		var emailLink = document.createElement('a');
		$(emailLink).attr({
			'href': 'mailto:' + realAddress,
			'title': 'Email: ' + realAddress,
			'class': 'email-address'
		}).text(realAddress);
		$(this).replaceWith(emailLink);

	});

	// Show JS elements
	$('.js').removeClass('js');
	$('.no-js').removeClass('no-js');

	// Clear form link
	$('a.clear-form').bind("click", function() {
		$(this).parents('form').find('input, textarea').val('');
		return false;
	});

	// Init slideshow on front page
	var slideShowFrontPage = new SlideShow('ul.slideshow-front', {
		cssClass: 'slideshow-front-js',
		duration: 5000

	});

	var summaryHeight = $('.case-summary').height();
	var $container = $('.case-description').parents('div:first').addClass('case-text-container').css('height', summaryHeight);
	var descriptionHeight = $('.case-description').height();
	var duration = 500;
	$('.case-description').css({
		'display': 'none',
		'top': '130px'
	});

	$('a#toggle-case-description').toggle(
	function() {
		$container.animate({
			'height': descriptionHeight
		},
		duration, "easeOutQuart");

		// for IE
		if (isIE) {
			$('.case-description').css({
				'display': 'block'
			}).animate({
				'top': '0'
			},
			duration);
			$('.case-summary').animate({
				'top': '130px'
			},
			duration, "easeOutQuart", function() {
				$(this).css('display', 'none');
			});

			// regular browsers
		} else {
			$('.case-description').css({
				'opacity': 0,
				'display': 'block'
			}).animate({
				'opacity': 1,
				'top': '0'
			},
			duration);
			$('.case-summary').animate({
				'opacity': 0,
				'top': '130px'
			},
			duration, "easeOutQuart");
		}

		$(this).addClass('opened').text('Show summary').attr('title', 'Minimize');
		return false;
	},
	function() {
		$container.animate({
			'height': summaryHeight
		},
		duration, "easeOutQuart");

		// for IE
		if (isIE) {
			$('.case-description').animate({
				'top': '130px'
			},
			duration, function() {
				$(this).css('display', 'none');
			});
			$('.case-summary').css('display', 'block').animate({
				'opacity': 1,
				'top': 0
			},
			duration, "easeOutQuart", function() {
				$('.case-description').css('display', 'none');
			});

			// for regular browsers
		} else {
			$('.case-description').animate({
				'opacity': 0,
				'top': '130px'
			},
			duration);
			$('.case-summary').animate({
				'opacity': 1,
				'top': 0
			},
			duration, "easeOutQuart", function() {
				$('.case-description').css('display', 'none');
			});
		}

		$(this).removeClass('opened').text('Read full case description').attr('title', 'Read on');
		return false;
	});

	// Toggle for processes section on home page
	var processHeight = $('#process').height();
	if (isIE) $('#process').css('height', 0);
	else $('#process').css({
		'opacity': 0,
		'height': 0
	});

	$('a#toggle-process').toggle(
	function() {
		var self = $(this);
		$('#process').css('display', 'block').animate({
			'height': processHeight,
			'opacity': 1
		},
		300, 'easeOutQuart', function() {
			self.addClass('opened').text('Close section "Design process"').attr('title', 'Minimize');
		});
	},
	function() {
		var self = $(this);
		$('#process').animate({
			'height': 0,
			'opacity': 0
		},
		300, 'easeOutQuart', function() {
			$(this).css('display', 'none');
			self.removeClass('opened').text('Learn more about our design process and services').attr('title', 'Maximize');
		});
	});

	// Init slideshow on cases page
	if ($('ul.slideshow').length > 0) {
		var slideShow = new SlideShow('ul.slideshow', {
			autoPlay: false
		});
		// Start/Pause button
		var slideshowPlayButton = $(slideShow.getPlayButton()).insertAfter('ul.slideshow');
		// Pagination
		$(slideShow.getPagination()).insertAfter(slideshowPlayButton);
	}


	// Load tweets
	$.jTwitter('fusion_fi', 5, function(tweets) {
		var tweetsString = '';
		if (tweets.length <= 0) {
			tweetsString = '<p class="info"><strong>Sorry! No tweets around here.</strong></p>';
		} else {
			tweetsString += '<ul class="clear">';
			$.each(tweets, function(i, tweet) {
				tweetsString += "<li>";
				tweetsString += tweet.text;
				tweetsString += "<br />";
				tweetsString += '<a href="http://twitter.com/fusion_fi/status/' + tweet.id + '">';
				tweetsString += "<small>&mdash; " + jQuery.timeago(new Date(Date.parse(tweet.created_at)));
				tweetsString += " via " + tweet.source + "</small>";
				tweetsString += "</a>";
				tweetsString += "</li>";
			});
			tweetsString += "</ul>";
			tweetsString += '<p>Follow us &mdash; <a href="http://www.twitter.com/fusion_fi">Fusion on Twitter</a></p>';
		}
		$('#tweets').removeClass('loading').html(tweetsString);
	});
	

	// Load blog entries via RSS
	var blogString = "";
	$.ajax({
		type: "GET",
		url: "http://www.fusion.fi/blog/feed/?cat=-133",
		dataType: "xml",
		success: function(rss) {
			if( $(rss).find('item').length > 0 ) {
				blogString += '<ul class="clear">';
				$(rss).find('item').each(function(i, item) {
					if (i < 2) {
						blogString += "<li>";
						blogString += '<strong><a href="' + $(item).find('link').text() + '">' + $(item).find('title').text() + '</a></strong><br />';
						blogString += "<small>Posted " + jQuery.timeago(new Date(Date.parse($(item).find('pubDate').text()))) + "</small>";
						blogString += $(item).find('description').text();
						blogString += "</li>";
					}
				});
				blogString += '</ul>';
				blogString += '<p>Read more at our <a href="/blog">blog</a> &raquo;</p>';
			} else {
				blogString += '<p class="info"><strong>Sorry! No posts around here.</strong></p>';
			}
			$("#blog-posts").removeClass('loading').html(blogString);
		},
		error: function() {
			$("#blog-posts").removeClass('loading').html('<p class="info"><strong>Sorry! Blog posts couldn\'t be loaded.</strong></p>');
		}
	});

});
