/* Author: Juxtaprose */

$(function() {
	var RG = {};
	$('#slidebox').append('<a class="prev scroll"></a><a class="next scroll"></a>');

	$('#slidebox').scrollable({
		circular: true, 
		speed: 900,
		onBeforeSeek: function() {
			$('.scroll').fadeOut('fast');
		},
		onSeek: function() {
			$('.scroll').fadeIn('slow');		
		}			
	});

	$('.scroll').fadeIn('slow');
	
	RG.cnt = 0;
	RG.fakeSrc = 'images/ajax-loader.gif';
	
	$.each($('.projthumb'), function() {
		RG.cnt++;	
		var ref = 'proj'+RG.cnt;
		$(this).find('img').first().wrap('<a href="#'+ref+'" />');
		var itxt = $('.intro',this).html();
		$('.intro',this).replaceWith('<a href="'+RG.fakeSrc+'">' + itxt + '</a>');
		$(this).children('p').first().append(' - <a href="#'+ref+'">view</a>');
		$('.projslides',this).attr('id', ref);
		$('a[href!="#'+ref+'"]',this).attr('rel',ref);		
		$('a[href="#'+ref+'"]',this).click(ref,function(e) {
			e.preventDefault();
			$('#'+ref+' a').first().trigger('click');
		});
		
		$('p',this).fadeIn(500);
	});

	RG.infoSub = '';
	$('.projslides a').colorbox({
		transition:"none", 
		width:"90%", 
		height:"90%", 
		initialWidth:"90%", 
		initialHeight:"90%", 
		scalePhotos: true,
		scrolling: true,
		title: function() {
			var href = $(this).attr('href');
			var info = $(this).html();
			if (href.indexOf(RG.fakeSrc) > -1 ) {
				$('img',this).addClass('introimg');
				RG.infoSub = $(this).html();
				info = '';							
			} else {
				RG.infoSub = '';
			}
			return info;
		},
		onComplete: function () {
			if (RG.infoSub != '') {	
				$('#cboxLoadedContent').html('<div>'+RG.infoSub+'</div>');
				var h = $('#cboxLoadedContent').height();
				var hd = $('#cboxLoadedContent div').height();
				if (h > hd) {
					var m = Math.floor((h-hd)/2);				
					$('#cboxLoadedContent div').css('margin-top', m+'px');
				}
			} else {
				if(!$(this).hasClass('noborder')) {				
					var img = $('#cboxLoadedContent img'), w = $(img).width()-2, h = $(img).height()-2;
					$(img).css({width: w, height: h, border: '1px solid #ccc'}).removeAttr('width').removeAttr('height');			
				}	
			}
		},
		onClosed: function() {
			RG.infoSub = '';
		}
	});
});

