jQuery(document).ready(function() {

// IF IE
	if ($.browser.msie){
		$('.project-list ul li:first').addClass('first');
	}

// Custom scrollbar
//------------------------------------------------------------------------------
	// make the content-text scrollable, but not if it contains the project list
		$('body:not(.homepage) .content-text:not(:has(.project-list))').jScrollPane();

// Auto show / hide content
//------------------------------------------------------------------------------

	$('.container').hoverIntent({
		over: showContainer,
		timeout: 1000,
		out: hideContainer
	});
		function showContainer(){
			$('.toggle').stop().css('visibility', 'visible').fadeTo('fast', 1);
		}
		function hideContainer(){
			$('.toggle').stop().fadeTo('fast', 0.1, function(){
				$(this).css('visibility', 'hidden')
			});
		}
	
	// set a cookie if it's the first visit
	if (!$.cookie('firsttime')){
		$.cookie('firsttime', 'no');
	}
	
	// if we're on the homepage hide the container, else wait for 5 seconds
	if ($('.homepage').length){
		$('.toggle').fadeTo(0, 0.1, function(){ $(this).css('visibility', 'hidden')});
	}else{
		$('.toggle').delay(5000).fadeTo(0, 'fast', function(){ $(this).css('visibility', 'hidden')});
	}
	
// Project navigation adjustment
	if ($('.tagged').length){
		// get the last bit of the url
		var ttag = document.location.href.split('/');
		var tag = ttag[ttag.length - 1];
		if ($('.sidebar ul li.' + tag).length){
			$('.sidebar ul li.current').removeClass('current');
			$('.sidebar ul li.' + tag).addClass('current');
		}
	}
	
// Projectslider
//------------------------------------------------------------------------------

	// let's build something animated that slides
	$('.project-list ul').projectSlider({
		// it should slide vertical
		orientation: 'vertical'
	})

	// same as above, animated slider. No orientation given so it's horizontal by default
	$('.thumbs-list ul').projectSlider();

	var i = [];
	$('.imagelist a').each(function(){
		i.push(this.href);
	});
	$('.fullscreen').bgStretcher({
				images: 	i,
				imageWidth: 1600, 
				imageHeight: 1050, 
				nextSlideDelay: 6000,
				slideShowSpeed: 1000,
				transitionEffect: 'fade',
				sequenceMode: 'normal',
				pagination: '#pagination',
				anchoringImg: 'center center', // right bottom center
				callbackfunction: checkForThumbs()
			});
	
	// build the imagelist captions
	var extra = ' active';
	$('.imagelist a').each(function(index){
		var tmp = document.createElement('div');
		tmp.className = 'image-details';
		if (index == 0){
			tmp.className += ' active';
		}
		tmp.id = 'image-details-' + index;
		// create a caption div with the text
		if ($(this).attr('title')){
			$('<div class="caption">' + $(this).attr('title') + '</div>').appendTo($(tmp));
		}
		// create description
		if ($(this).parent().find('.image-description').html()){
			$('<div class="description">' + $(this).parent().find('.image-description').html() + '</div>').appendTo($(tmp));
		}
		$(tmp).appendTo($('.fullscreen'));
	});
	
	function checkForThumbs(){
		// if you click on the image pause / play the slideshow
		$('.fullscreen').click(function(){
			// check if the slideshow is playing...
			if ($('#playpause').hasClass('pause')){
				$('#playpause').trigger('click');
				$('.fullscreen').bgStretcher.buttonSlide('next');
			}else{
				$('.fullscreen').bgStretcher.buttonSlide('next')
			}
		}).css('cursor', 'pointer');
		
		// make the playpause button work
		$('#playpause').click(function(e){
			e.preventDefault();
			if ($(this).hasClass('pause')){
				$(this).removeClass('pause').addClass('play').text('►');
				$('.fullscreen').bgStretcher.pause();
			}else{
				$(this).removeClass('play').addClass('pause').text('||');
				$('.fullscreen').bgStretcher.play();
			}
		});
		
		if($('.project-detail').length){
			$('.thumbs-list ul li a').click(function(e){
					e.preventDefault();
					var i = $('.thumbs-list a').index($(this));
					if ($('#slider-nav #pagination a').eq(i).length){
						$('#slider-nav #pagination a').eq(i).trigger('click');
					}
			});
		}
	}
	
});

// cookie stuff
jQuery.cookie = function (key, value, options) {
    
    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }
        
        value = String(value);
        
        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

