/*
 * DubinskyGalleryHome 1.0
 * Copyright by chriswuersch.com
 * 
 */

var img_left = new Image();
img_left.src = 'media/arrow_left.gif';

var img_left_a = new Image();
img_left_a.src = 'media/arrow_left_hover.gif';

var img_right = new Image();
img_right.src = 'media/arrow_right.gif';

var img_right_a = new Image();
img_right_a.src = 'media/arrow_right_hover.gif';

var img_pause = new Image();
img_pause.src = 'media/pause.gif';

var img_pause_a = new Image();
img_pause_a.src = 'media/pause_hover.gif';

$(document).ready(function() {
	var img_amount = $('#noJavascript_hiddenGallery #DynPG_Gallery_Main a').length;
	if(img_amount != 0){
		$('#noJavascript_hiddenGallery a').each(
			function(z, lookbook_image)
			{
				imgSize = $(lookbook_image).attr('rel');
				imgWidth = imgSize.match(/^([0-9]+)x/);
				imgWidth = parseInt(imgWidth[1]);
				imgHeight = imgSize.match(/x([0-9]+)$/);
				imgHeight = parseInt(imgHeight[1]);
				imgAlt = $(lookbook_image).attr('alt');
				imgTitle = $(lookbook_image).attr('title');
				gallery_image = new Image(imgWidth, imgHeight);
				gallery_image.src = $(lookbook_image).attr('href');
				
				$('#middleContainer').append(
					$('<div id="lookbook_wrapper_'+z+'" />').css({
						'position':'relative',
						'display':'none',
						'width':'910px',
						'height':'480px'
					}).append(
						$('<div />').css({
							'margin':'0 auto 0 auto',
							'width':imgWidth,
							'height':((480-imgHeight-40)/2)
						}),
						$('<div id="lookbook_img_'+z+'" />').css({
							'cursor':'pointer',
							'margin':'0 auto 0 auto',
							'width':imgWidth,
							'height':imgHeight
						}).bind({
							click: function(){
								$("[id*='lookbook_wrapper_']").stop();
								start_lookbook(z,'r',true,500,false);
							}
						}).append(
							gallery_image
						),
						$('<div />').css({
							'margin':'0 auto 0 auto',
							'width':imgWidth,
							'height':'15px'
						}),
						$('<div />').css({
							'margin':'0',
							'width':'910px',
							'height':'25px'
						}).append(
							$('<div id="lookbook_caption_'+z+'" />').addClass('caption').css({
								'width':'910px'
							}).append(
								$('<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td align="center"><span><b>'+imgAlt+'</b> '+imgTitle+'</span></td></tr></table>')
							)
						),
						$('<div />').css({
							'position':'absolute',
							'bottom':0,
							'right':0,
							'width':'90px',
							'height':'12px'
						}).append(
							$('<div />').addClass('right_arrow').css({
								'cursor':'pointer',
								'height':'11px',
								'width':'6px',
								'padding-left':'5px',
								'padding-right':'5px'
							}).bind({
								mouseover: function(){
									$(this).removeClass('right_arrow').addClass('right_arrow_active');
								},
								mouseout: function(){
									$(this).removeClass('right_arrow_active').addClass('right_arrow');
								},
								click: function(){
									$("[id*='lookbook_wrapper_']").stop();
									start_lookbook(z,'r',true,500,false);
								}
							}),
							$('<div id="lookbook_pause_'+z+'" />').addClass('pause').css({
								'height':'11px',
								'width':'5px',
								'padding-left':'5px',
								'padding-right':'5px'
							}).bind({
								mouseover: function(){
									$(this).removeClass('pause').addClass('pause_active');
								},
								mouseout: function(){
									$(this).removeClass('pause_active').addClass('pause');;
								}
							}),
							$('<div />').addClass('left_arrow').css({
								'cursor':'pointer',
								'height':'11px',
								'width':'6px',
								'padding-left':'5px',
								'padding-right':'5px'
							}).bind({
								mouseover: function(){
									$(this).removeClass('left_arrow').addClass('left_arrow_active');
								},
								mouseout: function(){
									$(this).removeClass('left_arrow_active').addClass('left_arrow');;
								},
								click: function(){
									$("[id*='lookbook_wrapper_']").stop();
									start_lookbook(z,'l',false,500,false);
								}
							}),
							$('<div class="img_number"><span>'+(z+1)+'/'+img_amount+'</span></div>')
						)
					)
				);
			}
		);
		$('#noJavascript_hiddenGallery').remove();
		start_lookbook(-1,'r',true,0,false);
	}else{
		$('#middleContainer').append($('#noJavascript_hiddenGallery').html());
		$('#noJavascript_hiddenGallery').remove();
	}
});

var timeoutId = new Array();

function start_lookbook(activeImgId, direction, doTimeout, fadingTime, doStop){
	clearAllTimeouts();
	if(!doStop)
		change_lookbook_image(activeImgId, direction, doTimeout, fadingTime);
}

function change_lookbook_image(activeImgId, direction, doTimeout, fadingTime){
	if(direction == 'r'){
		var newImgId = activeImgId + 1;
	}else{
		var newImgId = activeImgId - 1;
		if(newImgId < 0){
			newImgId = $('#middleContainer img').length - 1;
		}
	}
	if(!$('#lookbook_wrapper_'+newImgId).attr('id')){
		newImgId = 0;
	};
	if(activeImgId != -1){
		$("#lookbook_pause_"+activeImgId).css({'cursor':'auto'}).unbind('click');
		$("#lookbook_caption_"+activeImgId).css({'display':'block'});
		$("#lookbook_wrapper_"+activeImgId).css({'display':'block'}).animate({
		    opacity: 0
		  },fadingTime, function(){
			  $("#lookbook_wrapper_"+activeImgId).css({'display':'none'});
			  $("#lookbook_wrapper_"+newImgId).animate({opacity: 0},0).css({'display':'block'});
			  $("#lookbook_wrapper_"+newImgId).animate({
			    opacity: 1.0
			  },fadingTime, function(){
				  $("#lookbook_caption_"+newImgId).css({'display':'inline'});
				  $("#lookbook_wrapper_"+newImgId).css({'clear':'both'});
				  if(doTimeout)
					  timeoutId[timeoutId.lenght] = window.setTimeout('start_lookbook('+newImgId+',\'r\',true,1500)',6000);
				  $("#lookbook_pause_"+newImgId).css({'cursor':'pointer'}).bind({
					  click: function(){
						  start_lookbook(newImgId,'l',false,0,true);
					  }
				  });
			});
		});
	}else{
		$("#lookbook_caption_"+newImgId).css({'display':'inline'});
		$("#lookbook_wrapper_"+newImgId).css({'display':'block','clear':'both'});
		if(doTimeout)
			timeoutId[timeoutId.lenght] = window.setTimeout('start_lookbook('+newImgId+',\'r\',true,1500)',6000);
		$("#lookbook_pause_"+newImgId).css({'cursor':'pointer'}).bind({
			click: function(){
				start_lookbook(newImgId,'l',false,0,true);
			}
		});
	}
	return false;
}

function clearAllTimeouts(){
	if (timeoutId) for (var i in timeoutId) if (timeoutId[i]) window.clearTimeout(timeoutId[i]);
	timeoutId = new Array();
}
