$.extend(Site, {
	
	Work: {
	
		slideshow: null,
		bookUrl: 'http://book.z6creation.net',
		
		currentWork: null,
		previousWork: 0,
		nextWork: 0,
		
		detailsHeight: {'normal':'680px', 'small': '480px'},
		detailsHeightCurrent: 'normal',
		heightLimitSmall: 800,
		readyChangeWork: true,
		durationChangeWork: 400,
		
		changeTypeNextWork: false,
		
		animateColor: function(work, callback) {
			if ((typeof work.bgcolor == 'string')) {
				if (work.bgcolor.length==6) { work.bgcolor = "#"+work.bgcolor; }
				var exp = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$";
				if( (work.bgcolor).match(exp)) { $('#details').animate({backgroundColor: work.bgcolor}, callback) }
			}	
		},
		
		closeWork: function() {
			if ($('#details').hasClass('opened') && Site.Work.currentWork != null) {
				if (Site.Work.slideshow != null) Site.Work.slideshow.cycle('destroy');
				$('#details').removeClass('opened');
				$('#details #close').fadeOut();
				$('html, body').animate({
					scrollTop : 0
				}, 300, function() {
					$('#title').hide();
					$('#details #slideshow').animate({
						height: 0
					}, 200, function() {
						if ($("#work_"+Site.Work.currentWork).length > 0) {
							$('html, body').animate({
								scrollTop : $("#work_"+Site.Work.currentWork).offset().top
							}, 500);
						}
					});
				});
				event.returnValue = false;
			}
		},
	
		getWork: function(workId, changeType) {
			if (typeof changeType=='undefined') changeType = false;
			if (changeType) {
				Site.Work.changeTypeNextWork = true;
			}
			if (Site.Work.readyChangeWork) {
				Site.Work.readyChangeWork = false;
				$('#work_'+workId+' a.picture').addClass('loading');
				Shin.script('work', 'task=getwork&id='+workId, Site.Work.showWork);
			}
		},
		
		init: function() {
			if (parseInt(Site.window['height']) < Site.Work.heightLimitSmall) {
				$('#details').addClass('small');
				Site.Work.detailsHeightCurrent = 'small';
			}
			if (parseInt((window.location.hash).replace('#',''))) {
				Site.Work.getWork(parseInt((window.location.hash).replace('#','')), true);
			}
			Site.Work.initEvents();
		},
		
		initEvents: function() {
			$('#details #title a#prevwork').click(function(event) {	Site.Work.getWork(Site.Work.previousWork); event.preventDefault(); });
			$('#details #title a#nextwork').click(function(event) { Site.Work.getWork(Site.Work.nextWork); event.preventDefault(); });
			$('li.item_work a.picture').click(function(event) { 
				Site.Work.getWork((event.currentTarget.id).replace('link_work_', '')); event.preventDefault();
			});
			$('#works a.picture').removeClass('loading');
			$(document).keyup(function(e) {
				  if (e.keyCode == 27) { Site.Work.closeWork(); e.preventDefault(); } 
			});
		},
		
		insertImages: function (work) {
			if (!Site.Work.readyChangeWork) {
				Site.Work.readyChangeWork = true;
				$('#details #slideshow div').html('');
				for(i=0;i<work.images.length;i++) {
					if (Site.Work.detailsHeightCurrent == 'normal') {
						$('#details #slideshow div').append('<img src="'+Site.Work.bookUrl+'/files/_thumbnails/zoomsite/'+work.images[i]['id']+'.'+work.images[i]['extention']+'" class="slideshow" style="display: none" />');					
					} else {
						$('#details #slideshow div').append('<img src="'+Site.Work.bookUrl+'/files/_thumbnails/zoomsitesmall/'+work.images[i]['id']+'.'+work.images[i]['extention']+'" class="slideshow" style="display: none" />');
					}
				}
				Site.Work.animateColor(work, function() { Site.Work.isDisplay(work);});
			}
		},
		
		isDisplay: function(work) {
			Site.Work.startSlideshow('#details #slideshow div');
			$('#work_'+work.id+' a.picture').removeClass('loading');
			Site.Work.setNextWork(work.id);
			Site.Work.setPreviousWork(work.id);
			Site.Work.showTitle(work);
			Site.Work.currentWork = work.id;
		},
		
		setNextWork: function(current) {
			var nextWork = $('ul#works li#work_'+current).next('li.item_work')
			if(nextWork.size() == 1) {
				Site.Work.nextWork = (nextWork.attr('id')).replace('work_','');
				$('#details #title a#nextwork').fadeIn();
			} else {
				Site.Work.nextWork = null;
				$('#details #title a#nextwork').hide();
			}	
		},
		
		setPreviousWork: function(current) {
			var previousWork =  $('ul#works li#work_'+current).prev('li.item_work');
			if(previousWork.size() == 1) {
				$('#details #title a#prevwork').fadeIn();
				Site.Work.previousWork = (previousWork.attr('id')).replace('work_','');
			} else {
				Site.Work.previousWork = null;
				$('#details #title a#prevwork').hide();
			}	
		},
		
		showWork: function(work) {
			var work = jQuery.parseJSON(work);
			var timeout = 0;
			if (Site.Work.changeTypeNextWork) {
				Site.Work.changeTypeNextWork = false;
				if (typeof work.type != 'undefined') {
					$('#filter_'+work.type).trigger('click');
				}
				timeout = 500;
			}
			setTimeout(function() {
				$('#details #slideshow .nav').hide();
				if (!isNaN(work.id)) {
					if ($('#details').hasClass('opened')) {	
						$('#details #slideshow .nav').hide();
						$('#details #slideshow div').fadeOut(Site.Work.durationChangeWork, function() {
							Site.Work.insertImages(work);
							$('#details #slideshow div').fadeIn(Site.Work.durationChangeWork, function(){
								$('html, body').animate({
									scrollTop : $("#details").offset().top
								}, 500);
							});
						});
					} else {
						$('html, body').animate({
							scrollTop : $("#details").offset().top
						}, 400, function() {
							Site.Work.insertImages(work);
							if (!$('#details').hasClass('opened')) {
								$('#details').addClass('opened');
								$('#details #slideshow').animate({
									height: Site.Work.detailsHeight[Site.Work.detailsHeightCurrent]
								}, 500);
							}
						});
					}
				}
			}, timeout);
		},
		
		showTitle: function(work) {
			$('#details #title').fadeIn();
			$('#title p span.client').html(work.client[0].name);
			if (typeof work.url != null && typeof work.url != 'object') {
				$('#title p span.job').html('<a href="'+work.url+'" class="link" target="_blank">'+(work.name)+'</a>');
			} else {
				$('#title p span.job').html(work.name);
			}
		},
	
		startSlideshow: function(element) {
			$(element+' img:first').fadeIn(500, function() {
				if ($(element+' img').size()>1) {
					Site.Work.slideshow = $(element).cycle({
						fx:      			'fade',
					    prev:    			'#prev',
					    next:    			'#next',
					    slideExpr:			'img',
					    onPrevNextEvent:	Site.Work.onPrevNextEventSlideShow,
					    before:				Site.Work.beforeSlideShow,
					    after:				Site.Work.afterSlideShow
					});
					$('#details #next').fadeIn();
					Site.Work.slideshow.cycle('pause');
				}
			});
			$('#details #close').fadeIn();
		},
		
		onPrevNextEventSlideShow: function(curr, next, opts) {
			Site.Work.slideshow.cycle('pause');
		},
		
		beforeSlideShow: function(curr, next, opts) {
			if (parseInt(opts.slideCount)>0) {
				if (opts.nextSlide>=opts.slideCount-1) { $('#details #next').hide();
				} else { $('#details #next').show(); }
				if (opts.currSlide == 1 && opts.nextSlide == 0) { $('#details #prev').hide();}
			}
		},
		
		afterSlideShow: function(curr, next, opts) {
			if (opts.currSlide>0) { $('#details #prev').show();
			} else { $('#details #prev').hide();}	
		}
	}
});
