$(document).ready(function() {


	//tutorial
	$('#tuts .tut').hover(
	
		function () {
		
			$(this).stop(false, false).children('a.cap').show();
			
		},
		function (){

			$(this).stop(false, false).children('a.cap').hide();	
		}
	);

	//slider

	var items = $('#slider .item').length;
	var page = Math.ceil(items/6);

	$('#page a').click(function () {
	
		var pos = $(this).attr('rel') * $('#slider').height() - 2;
	
		$('#slider').scrollTo({top:pos, left:'0'}, 800 );
		$('#page a').removeClass('active');
		$(this).addClass('active');
		return false;
	
	});


	$('#tuts').height($('#tuts .tut').outerHeight() * 3);

	$('#tutpage a').click(function () {
	
		var pos = $(this).attr('rel') * $('#tuts').outerHeight();
	
		$('#tuts').scrollTo({top:pos, left:'0'}, 800 );
		$('#tutpage a').removeClass('active');
		$(this).addClass('active');
		return false;
	
	});


	/* custom modal window */
	$('#slider .item').click(function () {
		$('#mask').mask();
		$('#window').center();	
		
		setData($(this));
		
		return false;
	});

	$(window).resize(function () {
		if ($('#window').is(':visible')) {
			$('#mask').mask();
			$('#window').center();
		}
	});
	
	$('a#close, #mask').click(function () {
		$('#mask, #window').hide();	
		return false;
	});
	
	var speed = 5000;
	
	
	//Mouse over, pause it, on mouse out, resume the slider show
	$('.gallery').hover(
	
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('newsslider(0)', speed);	
		}
	);		
	
	 var run = setInterval('newsslider(0)', speed);
	 
	 
});


function newsslider(prev) {
 
    //Get the current selected item (with selected class), if none was found, get the first item
    var current_image = $('.gallery img.selected').length ? $('.gallery img.selected') : $('.gallery img:first');
 
    //if prev is set to 1 (previous item)
    if (prev) {         
    
        //Get previous sibling
        var next_image = (current_image.prev().length) ? current_image.prev() : $('.gallery img:last');
        
    //if prev is set to 0 (next item)
    } else {
         
        //Get next sibling
        var next_image = (current_image.next().length) ? current_image.next() : $('.gallery img:first');
    }

    //clear the selected class
    $('.gallery img').removeClass('selected');
     
    //reassign the selected class to current items
    next_image.addClass('selected');
 
    //Scroll the items
    $('.gallery').scrollTo(next_image, 800);      

     
}

function setData(i) {

	var url = i.find('a').attr('href');
	var i = i.find('img');
	
	var title = i.attr('alt');
	var desc = i.attr('rel');
	var data = i.attr('data').split('#');


	var roles = data[0].split('|');
	var tech = data[1].split('|');	
	var img = data[2].split('|');


	
	$('.gallery, ul.roles, ul.tech').empty();

	$('#window .details h3').html(title);
	$('#window .details p').html(desc);
	
	/* get images */
	
	content = '';
	$.each(img, function (key, val) {
		content += '<img src="images/clients/folio/' + val + '"/>';
	});	
	
	$('.gallery').append(content);

	/* get roles and tech list */
	
	content = '';
	$.each(roles, function (key, val) {
		content += '<li>' + val + '</li>';
	});
	
	$('ul.roles').append(content);

	content = '';
	$.each(tech, function (key, val) {
		content += '<li>' + val + '</li>';
	});
	
	$('ul.tech').append(content);
	
	$('a.sitelink').attr('href', url);
	
}

jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	this.show();
	return this;
}

jQuery.fn.mask = function () {
	this.css("position","absolute");
	this.css("top","0");
	this.css("left","0");        
	this.width($(window).width());
	this.height($(document).height());
	this.show();	
	return this;
}
