$(function(){
	/*$('.galleries a.gImg').fancybox({
		'overlayColor' : '#000',
		'titlePosition' : 'inside'
	});
	*/
	$('#pagenav .txt').wrapInner('<span class="content"></span>').append('<span class="bg"></span>');
	
	
	$('.imageFader').each(function(){
		if ($(this).children().length > 1) fadeImages($(this));
	});
	
});
$(window).load(function(){
	$('.showroom').each(function(){
		showroom($(this));
	});
	
});

function fadeImages($fader) {
	$fader.children('img').hide().first().show();
	setInterval(function(){
		$fader.children('img').first().fadeOut(1000).next().fadeIn(1000).end().appendTo($fader);
	}, 3000);

	/*
	var fadeTimer;
	var index = 0;
	var maxi = fader.children().length-1; 
	var next;
	//fader.children().get();
	//setTimeout('fadeNext()', 1000);
	
	console.log('max: ' + maxi);
	
	fadeNext();
	function fadeNext(){
		next = (index < maxi) ? index+1 : 0;
		
		console.log('i:'+index + '  n:'+next);
		
		fadeTimer = setTimeout(function(){
			$(fader.children()[index]).fadeOut(300, function(){
				$(fader.children()[next]).fadeIn(300, function(){
					if (index < maxi) index++;
					else index = 0;
		
					console.log('ujra!');
		
					fadeNext();
				});
			});
		}, 3000);
	}*/
}

/*************************************************************/
//						Showroom START
/*************************************************************/

/*
 * Showroom beta
 * last modified 2011. 04. 26.
 *
 **/
 
var _inacThumbOp = 1.0;
var _acThumbOp = 0.3;
var _overThumbOp = 0.7;
var _rollOverFade = 200;
var _showHideFade = 400;
var _slideControlInact = 0.1;
var _slideControlAct = 0.6;
var _slideControlOver = 0.9;
var _slideControlDown = 1.0;
var _scrollSpeed = 200;
var _scrollStep = 100;

function showroom(showroom){
	
	showroom.identify('showroom');
	showroom.fadeIn();
	

	var showroom = showroom,
		showFrame = showroom.find('.showFrame'),
		thumbFrame = showroom.find('.thumbFrame'),
		thumbClip = showroom.find('.thumbClip'),
		thumbSlide = showroom.find('.thumbSlide'),
		thumbs = thumbSlide.children(),
		images = new Array(),
		scrollNext = showroom.find('.scrollNext'),
		scrollBack = showroom.find('.scrollBack');
	
	showroom.data({
		'showFrame' : showFrame,
		'thumbSlide' : thumbSlide
	});
	showFrame.data({
		'showroom' : showroom,
		'thumbSlide' : thumbSlide
	});
	thumbSlide.data({
		'showroom' : showroom,
		'showFrame' : showFrame,
		'thumbClip' : thumbClip,
		'scrollNext' : scrollNext,
		'scrollBack' : scrollBack
	});
	scrollNext.data({
		'thumbSlide' : thumbSlide,
		'thumbClip' : thumbClip
	});
	scrollBack.data({
		'thumbSlide' : thumbSlide,
		'thumbClip' : thumbClip
	});
	
	// collect images data from thumbs, set thumb opacity, events
	thumbs.each(function(i){
		var thumb =$(this);
		images[i] = { Id: showroom.attr('id')+'_img'+i, Source : thumb.attr('href')};
		thumb.attr('href', '#'+showroom.attr('id')+'_img'+i);
		//thumb.fadeTo(_rollOverFade, _inacThumbOp);
		bindThumb(thumb);
	});
	// create images from the collected data and the imageTemlpate, and appent them to showFrame
	$('#imageTemplate').tmpl(images).appendTo(showFrame);
	
	// activate and fade in the first thumb and image
	$('#'+showroom.attr('id')+'_img0').fadeIn(_showHideFade).load(function(){
		showFrame.animate({
			height: [$('#'+showroom.attr('id')+'_img0').height(), 'jswing']
		}, _showHideFade);
		$(thumbs[0]).unbind();
		$(thumbs[0]).addClass('active');
	});
	
	bindWheel(thumbSlide);
	setScrollControls(thumbSlide);
	
}
// bind mouse wheel for thumb slide
function bindWheel(slide){
	slide.data('thumbClip').mousewheel(function(event, delta){
		if(delta < 0) scrollNext(slide);
		if(delta > 0) scrollBack(slide);
		return false;
	});
};

// set and bind thumb slide controls
function setScrollControls(slide){
	slide.data('scrollNext').bind('click', function(){ scrollNext(slide); });
	slide.data('scrollBack').bind('click', function(){ scrollBack(slide); });
	
};

// scroll thumbs forward
function scrollNext(slide){
	slide.data('thumbClip').scrollTo('+='+_scrollStep+'px', 200, 'linear');
};

// scroll thumbs backward
function scrollBack(slide){
	slide.data('thumbClip').scrollTo('-='+_scrollStep+'px', 200, 'linear');
};

// bind thumb events
function bindThumb(thumb){
	thumb.bind('click', function(){
		showImage(thumb);
		return false;
	});
};


// change image in showFrame
function showImage(thumb){
	// fade out previously selected thumb and image
	var prevThumb = thumb.siblings('.active');
	bindThumb(prevThumb);
	prevThumb.removeClass('active');
	thumb.addClass('active');
	thumb.unbind();
	$(prevThumb.attr('href')).fadeOut(function(){
		// fade in selected thumb and image
		$(thumb.attr('href')).parent('.showFrame').animate({
			height: [$(thumb.attr('href')).height(), 'jswing']
		}, _showHideFade);
		$(thumb.attr('href')).fadeIn();
	});
};

/*************************************************************/
//						Showroom END
/*************************************************************/

