$(function(){
	$('.jsshow').show();
	$('.jshide').hide();
	var $body = $('body:first');
	
	// all pages
	$('#footer_map').click(function (ev) {
		$('footer .map').slideToggle({
			duration: 400,
			step: function () { $body.scrollTop(99999); }
		});
		var map = new GMap2(document.getElementById("google_map"));
        map.setCenter(new GLatLng(54.294488,18.601913), 11);
		map.setUIToDefault();
		
		var mapIcon = new GIcon();
		mapIcon.image = "http://www.domynawzgorzu.pl/images/footer/map-marker.png";
		mapIcon.iconSize = new GSize(24,24);
		mapIcon.iconAnchor = new GPoint(12,24);
		mapIcon.infoWindowAnchor = new GPoint(12,0);
		
		var mapMarker = new GMarker(new GLatLng(54.269128,18.57743), { icon: mapIcon });
		GEvent.addListener(mapMarker, "click", function () {
			console.log('marker window', this, mapMarker);
			mapMarker.openInfoWindowHtml(
				'<div class="infoTip">'
				+'<strong>Domy na Wzgórzu</strong><br>'
				+'Ul. Jana Kasprowicza 31,<br>'
				+'Straszyn 83-010, Woj. Pomorskie<br><br>'
				+'<a href="http://www.domynawzgorzu.pl/">www.domynawzgorzu.pl</a>'
				+'</div>'
			);
		});
		
		map.addOverlay( mapMarker );
		
		$(document).unload(function () {
			GUnload();
		});
	});
	$('#footer_map_close').click(function (ev) {
		$('footer .map').slideUp(400);
	});
	
	// email validation
	$('form').submit(function () {
		var $this = $(this), valid = true, validstatus;
		
		// emails
		var emails = $this.find('input[type="email"]');
		emails.each(function (i) {
			var $this = $(this), value = $this.val(), validstatus;
			var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
			if (!value.length || !re.test(value)) {
				$this.removeClass('valid').addClass('invalid');
				/*
				if (validstatus = $this.data('validstatus')) {
					$(validstatus).removeClass('valid').addClass('invalid')
						.text('nieprawidłowe dane');
				}*/
				valid = false;
			} else {
				$this.removeClass('invalid').addClass('valid');
				/*
				if (validstatus = $this.data('validstatus')) {
					$(validstatus).removeClass('invalid').addClass('valid')
						.text('dziękujemy, email dodany');
				}*/
			}
		});
		
		// required fields
		var requireds = $this.find('input[required], textarea[required]');
		requireds.each(function (i) {
			var $this = $(this), value = $this.val();
			var re = /\w+/;
			if (!value.length || !re.test(value)) {
				$this.removeClass('valid').addClass('invalid');
				valid = false;
			} else {
				$this.removeClass('invalid').addClass('valid');
			}
		});
		
		// required checkboxes
		var checks = $this.find('input[type="checkbox"][required]');
		checks.each(function (i) {
			var $this = $(this), value = $this.val();
			if (!$this.is(':checked')) {
				$this.removeClass('valid').addClass('invalid');
				valid = false;
			} else {
				$this.removeClass('invalid').addClass('valid');
			}
		});
		
		// form status
		if (validstatus = $this.data('validstatus')) {
			if (valid) {
				//$(validstatus).removeClass('invalid').addClass('valid')
				//	.text('dziękujemy');
			} else {
				$(validstatus).removeClass('valid').addClass('invalid')
					.text('nieprawidłowe dane');
			}
		}
		
		return valid;
	});
	
	if (window.location.hash.indexOf('zadaj=complete') > -1) {
		new dnwSite.modal('<div class="result">'
				+'<h2>Dziekujemy.</h2>'
				+'<h3>Wiadomość została wysłana</h3>'
				+'<p>Zachęcamy również do odwiedzenia naszego domu modelowego<br>'
				+' w celu umówienia szczegułów inwestycji z naszym przedstawicielem</p>'
				+'</div>',
				[776, 368],
			function () {}, 'footer_zadaj');
	} else if (window.location.hash.indexOf('powiadomienia=complete') > -1) {
		$('.validator.powiadomienia').addClass('valid').text('dziękujemy, email dodany');
	}
	
	$('input').keyup(function (ev) {
		if (ev.which == 9 || ev.which == 13)
			return;
		
		$(this).removeClass('invalid valid');
	});
	
	// contact us modal
	$('footer .pytanie').click(function () {
		new dnwSite.modal($('#footer_zadaj').clone(true).attr('id',''), [776, 368],
			function () {}, 'footer_zadaj');
	});
	

	if ($body.hasClass('home')) {
		// "home" page
		$('a.planosiedla').click(function () {
			new dnwSite.modal('<img src="images/home/planosiedla.png" />',
							  [1062, 486], function(){}, 'planosiedla');
		});
		
		// day/night header
		var homeBg = $('.homebg'), header = $('header');
		$('.headerswitch .day').click(function () {
			if (header.hasClass('day')) return;
			
			homeBg.fadeOut(300, function () {
				header.removeClass('night').addClass('day');
			}).fadeIn(200);
		});
		$('.headerswitch .night').click(function () {
			if (header.hasClass('night')) return;
			
			homeBg.fadeOut(300, function () {
				header.removeClass('day').addClass('night');
			}).fadeIn(300);
		});
		// adjust for local time
		var now = new Date();
		if (now.getHours() < 4 || now.getHours() > 21) {
			header.removeClass('day').addClass('night');
		}
		
		
		var pb = 'images/gallery/'; // photo base
		new dnwSite.photoLayout('#home_featuregallery',
			[
			{ s: pb+'small/001.jpg', m: pb+'medium/001.jpg', l: pb+'large/001.jpg'},
			{ s: pb+'small/002.jpg', m: pb+'medium/002.jpg', l: pb+'large/002.jpg'},
			{ s: pb+'small/003.jpg', m: pb+'medium/003.jpg', l: pb+'large/003.jpg'},
			{ s: pb+'small/004.jpg', m: pb+'medium/004.jpg', l: pb+'large/004.jpg'},
			{ s: pb+'small/005.jpg', m: pb+'medium/005.jpg', l: pb+'large/005.jpg'},
			{ s: pb+'small/006.jpg', m: pb+'medium/006.jpg', l: pb+'large/006.jpg'},
			{ s: pb+'small/007.jpg', m: pb+'medium/007.jpg', l: pb+'large/007.jpg'},
			{ s: pb+'small/008.jpg', m: pb+'medium/008.jpg', l: pb+'large/008.jpg'},
			{ s: pb+'small/009.jpg', m: pb+'medium/009.jpg', l: pb+'large/009.jpg'},
			{ s: pb+'small/010.jpg', m: pb+'medium/010.jpg', l: pb+'large/010.jpg'},
			{ s: pb+'small/011.jpg', m: pb+'medium/011.jpg', l: pb+'large/011.jpg'},
			{ s: pb+'small/012.jpg', m: pb+'medium/012.jpg', l: pb+'large/012.jpg'},
			{ s: pb+'small/013.jpg', m: pb+'medium/013.jpg', l: pb+'large/013.jpg'},
			{ s: pb+'small/014.jpg', m: pb+'medium/014.jpg', l: pb+'large/014.jpg'},
			{ s: pb+'small/015.jpg', m: pb+'medium/015.jpg', l: pb+'large/015.jpg'},
			{ s: pb+'small/016.jpg', m: pb+'medium/016.jpg', l: pb+'large/016.jpg'},
			{ s: pb+'small/017.jpg', m: pb+'medium/017.jpg', l: pb+'large/017.jpg'},
			{ s: pb+'small/018.jpg', m: pb+'medium/018.jpg', l: pb+'large/018.jpg'},
			{ s: pb+'small/019.jpg', m: pb+'medium/019.jpg', l: pb+'large/019.jpg'},
			{ s: pb+'small/020.jpg', m: pb+'medium/020.jpg', l: pb+'large/020.jpg'},
			{ s: pb+'small/021.jpg', m: pb+'medium/021.jpg', l: pb+'large/021.jpg'},
			{ s: pb+'small/022.jpg', m: pb+'medium/022.jpg', l: pb+'large/022.jpg'},
			{ s: pb+'small/023.jpg', m: pb+'medium/023.jpg', l: pb+'large/023.jpg'},
			{ s: pb+'small/024.jpg', m: pb+'medium/024.jpg', l: pb+'large/024.jpg'},
			{ s: pb+'small/025.jpg', m: pb+'medium/025.jpg', l: pb+'large/025.jpg'},
			{ s: pb+'small/026.jpg', m: pb+'medium/026.jpg', l: pb+'large/026.jpg'},
			{ s: pb+'small/027.jpg', m: pb+'medium/027.jpg', l: pb+'large/027.jpg'},
			{ s: pb+'small/028.jpg', m: pb+'medium/028.jpg', l: pb+'large/028.jpg'},
			{ s: pb+'small/029.jpg', m: pb+'medium/029.jpg', l: pb+'large/029.jpg'},
			{ s: pb+'small/030.jpg', m: pb+'medium/030.jpg', l: pb+'large/030.jpg'},
			{ s: pb+'small/031.jpg', m: pb+'medium/031.jpg', l: pb+'large/031.jpg'},
			{ s: pb+'small/032.jpg', m: pb+'medium/032.jpg', l: pb+'large/032.jpg'},
			{ s: pb+'small/033.jpg', m: pb+'medium/033.jpg', l: pb+'large/033.jpg'},
			{ s: pb+'small/034.jpg', m: pb+'medium/034.jpg', l: pb+'large/034.jpg'},
			{ s: pb+'small/035.jpg', m: pb+'medium/035.jpg', l: pb+'large/035.jpg'},
			{ s: pb+'small/036.jpg', m: pb+'medium/036.jpg', l: pb+'large/036.jpg'}
			], 3, 'Straszyn i Okolice');
	
	} else if ($body.hasClass('standard')) {
		// "standard" page
		
		var standardPager = new dnwSite.pager('#standard_switcher .content, #standard_featurelist', '#standard_switcher .list');
		
		$('#standard_switcher .list').children().click(function () {
			if (standardPager.current == 2) {
				$('#standard_features').hide();
				$('#standard_techniczne').show();
			} else {
				$('#standard_features').show();
				$('#standard_techniczne').hide();				
			}
		});
		
		
		$('.button_switch.parter').click(function () {
			$('#standard_parter').show();
			$('#standard_pietro').hide();
		});
		$('.button_switch.pietro').click(function () {
			$('#standard_parter').hide();
			$('#standard_pietro').show();
		});
		
		var pb = 'images/standard/'; // photo base
		new dnwSite.photoLayout('#parter_photolayout',
			[
			{ s: pb+'small/001.jpg', m: pb+'medium/001.jpg', l: pb+'large/001.jpg'},
			{ s: pb+'small/002.jpg', m: pb+'medium/002.jpg', l: pb+'large/002.jpg'},
			{ s: pb+'small/003.jpg', m: pb+'medium/003.jpg', l: pb+'large/003.jpg'}
			], 3);
		new dnwSite.photoLayout('#pietro_photolayout',
			[
			{ s: pb+'small/004.jpg', m: pb+'medium/004.jpg', l: pb+'large/004.jpg'},
			{ s: pb+'small/005.jpg', m: pb+'medium/005.jpg', l: pb+'large/005.jpg'},
			{ s: pb+'small/006.jpg', m: pb+'medium/006.jpg', l: pb+'large/006.jpg'}
			], 3);
		


		var numbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven',
					   'eight', 'nine', 'ten', 'eleven', 'twelve'];
		for (i in numbers) {
			var num = numbers[i];
			(function (num) {
				$('.sprite_number.'+ num).each(function () {
					var $this = $(this);
					$this.mouseenter(function () {
						$('.sprite_number.'+ num).addClass('active');
					});
					$this.mouseleave(function () {
						$('.sprite_number.'+ num).removeClass('active');
					});
				})
			})(numbers[i]);
		}
		
	} else if ($body.hasClass('lokalizacja')) {
		// "location" page
		
		var pb = 'images/gallery/'; // photo base
		var a = new dnwSite.photoLayout('#lokalizacja_gallery',
			[
			{ s: pb+'small/001.jpg', m: pb+'medium/001.jpg', l: pb+'large/001.jpg'},
			{ s: pb+'small/002.jpg', m: pb+'medium/002.jpg', l: pb+'large/002.jpg'},
			{ s: pb+'small/003.jpg', m: pb+'medium/003.jpg', l: pb+'large/003.jpg'},
			{ s: pb+'small/004.jpg', m: pb+'medium/004.jpg', l: pb+'large/004.jpg'},
			{ s: pb+'small/005.jpg', m: pb+'medium/005.jpg', l: pb+'large/005.jpg'},
			{ s: pb+'small/006.jpg', m: pb+'medium/006.jpg', l: pb+'large/006.jpg'},
			{ s: pb+'small/007.jpg', m: pb+'medium/007.jpg', l: pb+'large/007.jpg'},
			{ s: pb+'small/008.jpg', m: pb+'medium/008.jpg', l: pb+'large/008.jpg'},
			{ s: pb+'small/009.jpg', m: pb+'medium/009.jpg', l: pb+'large/009.jpg'},
			{ s: pb+'small/010.jpg', m: pb+'medium/010.jpg', l: pb+'large/010.jpg'},
			{ s: pb+'small/011.jpg', m: pb+'medium/011.jpg', l: pb+'large/011.jpg'},
			{ s: pb+'small/012.jpg', m: pb+'medium/012.jpg', l: pb+'large/012.jpg'},
			{ s: pb+'small/013.jpg', m: pb+'medium/013.jpg', l: pb+'large/013.jpg'},
			{ s: pb+'small/014.jpg', m: pb+'medium/014.jpg', l: pb+'large/014.jpg'},
			{ s: pb+'small/015.jpg', m: pb+'medium/015.jpg', l: pb+'large/015.jpg'},
			{ s: pb+'small/016.jpg', m: pb+'medium/016.jpg', l: pb+'large/016.jpg'},
			{ s: pb+'small/017.jpg', m: pb+'medium/017.jpg', l: pb+'large/017.jpg'},
			{ s: pb+'small/018.jpg', m: pb+'medium/018.jpg', l: pb+'large/018.jpg'},
			{ s: pb+'small/019.jpg', m: pb+'medium/019.jpg', l: pb+'large/019.jpg'},
			{ s: pb+'small/020.jpg', m: pb+'medium/020.jpg', l: pb+'large/020.jpg'},
			{ s: pb+'small/021.jpg', m: pb+'medium/021.jpg', l: pb+'large/021.jpg'},
			{ s: pb+'small/022.jpg', m: pb+'medium/022.jpg', l: pb+'large/022.jpg'},
			{ s: pb+'small/023.jpg', m: pb+'medium/023.jpg', l: pb+'large/023.jpg'},
			{ s: pb+'small/024.jpg', m: pb+'medium/024.jpg', l: pb+'large/024.jpg'},
			{ s: pb+'small/025.jpg', m: pb+'medium/025.jpg', l: pb+'large/025.jpg'},
			{ s: pb+'small/026.jpg', m: pb+'medium/026.jpg', l: pb+'large/026.jpg'},
			{ s: pb+'small/027.jpg', m: pb+'medium/027.jpg', l: pb+'large/027.jpg'},
			{ s: pb+'small/028.jpg', m: pb+'medium/028.jpg', l: pb+'large/028.jpg'},
			{ s: pb+'small/029.jpg', m: pb+'medium/029.jpg', l: pb+'large/029.jpg'},
			{ s: pb+'small/030.jpg', m: pb+'medium/030.jpg', l: pb+'large/030.jpg'},
			{ s: pb+'small/031.jpg', m: pb+'medium/031.jpg', l: pb+'large/031.jpg'},
			{ s: pb+'small/032.jpg', m: pb+'medium/032.jpg', l: pb+'large/032.jpg'},
			{ s: pb+'small/033.jpg', m: pb+'medium/033.jpg', l: pb+'large/033.jpg'},
			{ s: pb+'small/034.jpg', m: pb+'medium/034.jpg', l: pb+'large/034.jpg'},
			{ s: pb+'small/035.jpg', m: pb+'medium/035.jpg', l: pb+'large/035.jpg'},
			{ s: pb+'small/036.jpg', m: pb+'medium/036.jpg', l: pb+'large/036.jpg'}
			], 4, 'Straszyn i Okolice');
	
	} else if ($body.hasClass('plandomow')) {
		// "house plan" page
		
		new dnwSite.toggle('.toggle_level', '.houseplan', 'level2');
		new dnwSite.toggle('.toggle_furniture', '.houseplan .furniture');
		
		var updateSun = function (ev, value) {
			value = (value-157)*0.01;
			sun.css({
				left: 450 + Math.sin(-value)*491,
				top: 145 - Math.cos(-value)*288,
				opacity: 1 - Math.abs(-value)/2
			});
		}
		
		var sun = $('.sun').fadeIn(1000);
		$(".slider_time input").rangeinput({ vertical: true })
			.bind('onSlide change', updateSun);
		
	
	} else if ($body.hasClass('ceny')) {
		// "pricing" page
		
		var houseTpl = '<strong>Pow. Domu:</strong> {house}m<sup>2</sup><br />'
					+'<strong>Pow. Działki:</strong> {plot}m<sup>2</sup><br />'
					+'<strong>Cena:</strong> {price}zł <em>(brutto)</em><br />'
					+'<em>{pricepm}zł m2 (netto)</em>';
		var notAvailable = 'Sprzedany';
		var notBuilt = 'Budowa planowana na 2012';
		
		$('.street1').one('mouseenter', function () {
			new dnwSite.callout('.house9', notBuilt);
			new dnwSite.callout('.house7', notBuilt);
			new dnwSite.callout('.house5', notBuilt);
			new dnwSite.callout('.house3', notBuilt);
			new dnwSite.callout('.house1', notBuilt);
		});
		$('.street2').one('mouseenter', function () {
			new dnwSite.callout('.house19', notAvailable);
			new dnwSite.callout('.house17', houseTpl.template({
				house: '150.5', plot: '282',
				price: '599.000', pricepm: '3.455'
				}));
			new dnwSite.callout('.house15', houseTpl.template({
				house: '150.5', plot: '282',
				price: '599.000', pricepm: '3.455'
				}));
			new dnwSite.callout('.house13', houseTpl.template({
				house: '150.5', plot: '282',
				price: '599.000', pricepm: '3.455'
				}));
			new dnwSite.callout('.house11', houseTpl.template({
				house: '151.9', plot: '435',
				price: '730.000', pricepm: '4.025'
				}));
		});
		$('.street3').one('mouseenter', function () {
			new dnwSite.callout('.house29', notAvailable);
			new dnwSite.callout('.house27', notAvailable);
			new dnwSite.callout('.house25', notAvailable);
			new dnwSite.callout('.house23', notAvailable);
			new dnwSite.callout('.house21', notAvailable);
		});
		$('.street4').one('mouseenter', function () {
			new dnwSite.callout('.house39', notAvailable);
			new dnwSite.callout('.house37', notAvailable);
			new dnwSite.callout('.house35', notAvailable);
			new dnwSite.callout('.house33', notAvailable);
			new dnwSite.callout('.house31', notAvailable);
		});
		$('.street5').one('mouseenter', function () {
			new dnwSite.callout('.house49', notAvailable);
			new dnwSite.callout('.house47', notAvailable);
			new dnwSite.callout('.house45', notAvailable);
			new dnwSite.callout('.house43', notAvailable);
			new dnwSite.callout('.house41', notAvailable);
		});
		
	} else if ($body.hasClass('aktualnosci')) {
		// "aktualnosci" page
		
		var pb = 'images/aktualnosci/'; // photo base

		new dnwSite.photoLayout('#aktualnosci_gallery2',
			[{ m: pb+'medium/005.jpg', l: pb+'large/005.jpg'}], 1);
		
		new dnwSite.photoLayout('#aktualnosci_gallery3',
			[
			{ s: pb+'small/001.jpg', m: pb+'medium/001.jpg', l: pb+'large/001.jpg'},
			{ s: pb+'small/002.jpg', m: pb+'medium/002.jpg', l: pb+'large/002.jpg'},
			{ s: pb+'small/003.jpg', m: pb+'medium/003.jpg', l: pb+'large/003.jpg'},
			{ s: pb+'small/004.jpg', m: pb+'medium/004.jpg', l: pb+'large/004.jpg'}
			], 4);
			
		new dnwSite.photoLayout('#aktualnosci_gallery4',
			[{ m: pb+'medium/006.jpg', l: pb+'large/006.jpg'}], 1);
	
	}
});

String.prototype.template = function (replaces) {
	var orig = this.toString();
	for (var i in replaces) {
		orig = orig.replace('{'+ i +'}', replaces[i]);
	}
	return orig;
}

var dnwSite = {}

dnwSite.pager = function (content, list) {
	var thisPager = this;
	this.current = null;
	this.content = $(content);
	this.list = $(list).children();
	
	this.list.each(function (i) {
		$(this).click(function () {
			thisPager.change(i);
		})
	});
	
	this.content.children().hide();
	
	this.change(0);
}
dnwSite.pager.prototype.change = function (index) {
	var thisPager = this;

	if (this.current !== null) {
		this.content.each(function() {
			var $thisContent = $(this);
			$thisContent.children().eq(thisPager.current).fadeOut(300, function () {
				$thisContent.children().eq(index).fadeIn(400);
			});
		});
	} else {
		thisPager.content.each(function() {
			$(this).children().eq(index).fadeIn();
		});
	}
	
	this.list.eq(this.current).removeClass('active');
	this.list.eq(index).addClass('active');
	
	this.current = index;
}

dnwSite.modal = function (html, size, closeCallback, modalClass) {
	var thisModal = this;

	this.closeCallback = closeCallback;
	
	var closeLink = $('<a class="close">Zamknij</a>')
		.click(function (ev) { thisModal.close(); });
	
	this.elem = $('<div class="modalWindow">')
		.html(html).append(closeLink)
		.click(function (ev) { ev.stopPropagation(); });
	if (modalClass)
		this.elem.addClass(modalClass);
		
	if (size) {
		this.elem.css({
			width: size[0],
			height: size[1]
		});
	}
	
	this.shadow = $('<div class="modalShadow">').fadeIn()
		.click(function (ev) { thisModal.close(); })
		.append(this.elem).appendTo($('body'));
		
	dnwSite.placeholders(this.elem);
}
dnwSite.modal.prototype.close = function () {
	this.shadow.fadeOut(400, function () {
		$(this).remove();
	});
	if (typeof this.closeCallback == "function")
		this.closeCallback();
}

dnwSite.toggle = function (toggle, target, onClass) {
	toggle = $(toggle);
	target = $(target);
	if (onClass == undefined) onClass = 'on';
	
	toggle.click(function () {
		var $this = $(this);
		if ($this.hasClass('on')) {
			$this.removeClass('on');
			target.removeClass(onClass);
		} else {
			$this.addClass('on');
			target.addClass(onClass);
		}
	});
}

dnwSite.callout = function (element, html) {
	var elem = $(element);
	var callout = $('<div class="callout"><div class="arrow"></div>'
					+ html
					+'</div>');
	elem.after(callout);
	
	elem.mousemove(function (ev) {
		var pOffset = callout.offsetParent().offset(),
			offX = ev.pageX - pOffset.left,
			offY = ev.pageY - pOffset.top;
		callout.css({
			left: offX - 15,
			top: offY - callout.outerHeight() - 42,
			display: 'block'
		});
	});
	elem.mouseleave(function () {
		callout.css('display', 'none');
	});
}


// photoLayout
// l: large, m: medium, s: small
dnwSite.photoLayout = function (elem, photos, max, title) {
	var thisPhotos = this;
	this.elem = $(elem).empty();
	this.photos = photos;
	this.max = max;
	this.title = title;
	this.current = ~~((photos.length-1) * Math.random());
	if (this.photos.length == this.max)
		this.current = 0;
	this.currentElem = 0;
	this.modalCurrent = null;
	this.photoHistory = [];
	
	for (var i=0; i < max; i++) {
		this.elem.append(this.nextPhoto(i));
	}
	
	if (photos.length > this.max) {
		setInterval(function () {
			thisPhotos.currentElem = ++thisPhotos.currentElem % (thisPhotos.max);
			
			var newPhoto = thisPhotos.nextPhoto(thisPhotos.currentElem);
			var oldPhoto = thisPhotos.elem.children().eq(thisPhotos.currentElem);
			oldPhoto.find('img').fadeOut(600, function () {
					oldPhoto.replaceWith(newPhoto);
					newPhoto.find('img').hide().fadeIn(400);
				});
		}, 4500);
	}
}
dnwSite.photoLayout.prototype.nextPhoto = function (currentIndex) {
	if (this.max == this.photos.length) {
		this.current = ++this.current % (this.photos.length);
	} else {
		while ($.inArray(this.current, this.photoHistory) > -1) {
			this.current = ~~(Math.random() * this.photos.length);
		}
		this.photoHistory[currentIndex] = this.current;
	}
	
	if (isNaN(this.current)) this.current = 0;
	
	var thisPhotos = this;
	var title = this.title;
	var bigImage = this.photos[this.current].l;	
	var photo = $('<div class="photolayout">'
				  +'<img src="'+ this.photos[this.current].m +'" />'
				  +'<div class="zoom"></div></div>');
	var thisI = ~~(this.current);
	photo.click(function () {
		var popup = $('<div class="photomodal">');
		var width = 645;
		thisPhotos.modalCurrent = thisI;
		
		popup.append(title?'<h4 class="photolayout">'+ title +'</h4>':'');
		if (thisPhotos.photos.length > 1) {
			popup.append(thisPhotos.switcherSidebar());
			width += 140;
		}
		popup.append('<div class="bigimage"><img src="'+ bigImage +'" /></div>');
		
		new dnwSite.modal(popup, [width, 428 + (title?50:0)]);
	});
	return photo;
}
dnwSite.photoLayout.prototype.switcherSidebar = function (current) {
	// ul > li*photos
	// active photo .active
	// javascript for scrollup/scrolldown buttons
	// javascript so ul is initially scrolled to center active photo
	var list = $('<ul></ul>');

	var thisPhotos = this;
	var photos = this.photos;

	for (i in photos) {
		if (photos[i].s) {
			(function (photo) {
				var item = $('<li'
							+(thisPhotos.modalCurrent == i ?' class="active">':'>')
							+'<img src="'+ photo.s +'" /></li>');
				item.click(function () {
					$('.photomodal .bigimage').replaceWith(
						'<img src="'+ photo.l +'" class="bigimage" />'
					);
					var $this = $(this);
					$this.siblings('.active').removeClass('active');
					$this.addClass('active');
				})
				
				list.append(item);
			})(photos[i]);
		}
	}
	list.mousewheel(function (ev, delta) {
		var $this = $(this);
		$this.scrollTop($this.scrollTop() - delta*15);
		
		ev.stopPropagation();
		ev.preventDefault();
	});
	
	var prev = $('<div class="prev"></div>'),
		next = $('<div class="next"></div>');
	var prevInterval, nextInterval;
	var prevDist = 0, nextDist = 0;
	prev.click(function () {
		prevDist += list.innerHeight() * 2;
		
		clearInterval(prevInterval);
		prevInterval = setInterval(function () {
			var curTop = list.scrollTop();
			if (prevDist < 2 || curTop <= 0) {
				clearInterval(prevInterval);
				prevDist = 0;
				return;
			}
			list.scrollTop(curTop - prevDist/3);
			prevDist /= 3;
		}, 32);
	});
	next.click(function () {
		nextDist += list.innerHeight() * 2;
		
		clearInterval(nextInterval);
		nextInterval = setInterval(function () {
			var curTop = list.scrollTop();
			if (nextDist < 2 /*|| curTop >= 0*/) {
				clearInterval(nextInterval);
				nextDist = 0;
				return;
			}
			list.scrollTop(curTop + nextDist/3);
			nextDist /= 3;
			console.log(curTop, nextDist, list.innerHeight());
		}, 32);
	});
	
	var alignThumbs = function () {
		var offset = list.find('li.active:first').position();
		if (offset.top - 176 > list.scrollTop())
			list.scrollTop(offset.top - 176);
	};
	setTimeout(alignThumbs, 200);
	$('img', list).load(alignThumbs);
	
	var thumbs = $('<div class="thumbs">');
	thumbs.append(prev).append(list).append(next);

	return thumbs;
	
}

dnwSite.placeholders = function (context) {
	if ('placeholder' in document.createElement('input'))
		return;
	
	$('[placeholder]', context).focus(function() {
		var input = $(this);
		if (input.val() == input.attr('placeholder')) {
			input.val('');
			input.removeClass('placeholder');
		}
		}).blur(function() {
		var input = $(this);
		if (input.val() == '' || input.val() == input.attr('placeholder')) {
			input.addClass('placeholder');
			input.val(input.attr('placeholder'));
		}
		}).blur().parents('form').submit(function() {
		$(this).find('[placeholder]').each(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
				input.val('');
			}
		})
	});
}
