var Site = {
	title: "",
	loadFile: "",
	oldAddress: "",
	isiOs: false,
	large: "",
	oninit: function (event) {		
		if (navigator.userAgent.match(/like Mac OS X/i)) {
			Site.isiOs = true;
		};
		$("#content").css("display", "none");
		$('a:not([href^="http://"]):not([href^="mailto:"]):not([href="#"]):not([href="/artists"]):not([href=""]):not(.ftplink)').address(function () {
			return $(this).attr('href').replace(location.pathname, '');
		});
		$("#artists").find("a.menulink").bind("click",function(e){e.preventDefault();});
		$("body").append("<div id=\"loading\"><p>loading...<br /><br /><span>Tip: Use arrow keys to navigate photos</span><br /><img src=\"/images/arrows.gif\" alt=\"\" /></p></div>");
		LightBox.init();
		Chimp.init();
		$("a.artist").hover(Site.highlightCategories, Site.unhighlightCategories);
		$("a.category").hover(Site.highlightArtist, Site.unhighlightArtist);
		$(window).bind("resize", Photo.resizeFrame)
		.scroll(Site.onScroll);
		Site.setMenuHeight();
		$("#close").live("click", function(){
			$("#featured").slideUp(600);
		});
	},
	onScroll: function(){
		if($(window).scrollTop() > 0){
			$("#header").addClass("scrolled");
		}
		else {
			$("#header").removeClass("scrolled");
		}
	},
	checkResolution: function () {
		if (screen.width > 1300) {
			large = "&large=true";
		} else {
			large = "";
		}
	},
	blank: function () {
		return true;
	},
	fadeIt: function (e) {
		$(e.target).stop(true, true).animate({
			opacity: 0.7
		}, 200);
	},
	fadeBack: function (e) {
		$(e.target).stop(true, true).animate({
			opacity: 1
		}, 200);
	},
	highlightCategories: function (e) {
		$(".artist").removeClass("high");
		$(".category").removeClass("high");
		var cT = "";
		var cats = $(e.target).attr("class").split(" ");
		$(".category").removeClass("high");
		for (var c = 1; c < cats.length; c++) {
			cT = cats[c];
			$("#" + cT).addClass("high");
		}
	},
	unhighlightCategories: function (e) {
		$(".artist").removeClass("high");
		$(".category").removeClass("high");
	},
	highlightArtist: function (e) {
		$(".artist").removeClass("high");
		$(".category").removeClass("high");
		var cat = $(e.target).attr("id");
		$(e.target).addClass("high");
		$("." + cat).addClass("high");
	},
	unhighlightArtist: function (e) {
		$(".category").removeClass("high");
		$(".artist").removeClass("high");
	},
	slideDownArtists: function (e) {
		$("a.menulink").removeClass("open");
		$("#artistsmenu").stop(true, true).slideDown(600, "easeOutQuad");
		$("#artists").find("a.menulink").addClass("open");
		$('#artists').bind("mouseleave", Site.slideUpArtists);
	},
	slideUpArtists: function (e) {
		$('#artists').unbind("mouseleave");
		$("a.menulink").removeClass("open");
		$("#artistsmenu").stop(true, true).slideUp(400, "easeOutQuad");
		var ad = $.address.pathNames();
		$("li#" + ad[0] + " a.menulink").addClass("open");
	},
	slideToggleArtists: function (e) {
		e.preventDefault();
		if ($("#artistsmenu").is(':hidden')) {
			$("a.menulink").removeClass("open");
			$("#artistsmenu").stop(true, true).slideDown(600, "easeOutQuad");
			$("#artists").find("a.menulink").addClass("open");
			$('#artists').bind("mouseleave", Site.slideUpArtists);
		} else {
			$('#artists').unbind("mouseleave");
			$("a.menulink").removeClass("open");
			$("#artistsmenu").stop(true, true).slideUp(400, "easeOutQuad");
			var ad = $.address.pathNames();
			$("#" + ad[0]).find("a.menulink").addClass("open");
		}
	},
	onchange: function (event) {
		if (window.location.pathname.length > 1) {
				window.location = "http://" + window.location.host + "#" + window.location.pathname;
		}	
		var large = "";
		Site.checkResolution();
		var noChange = false;
		var parts = Site.oldAddress.split("/");
		$("li.menuitem a.menulink").removeClass("open");
		//if ((event.pathNames[0] == "news") && (event.pathNames[1] > 0) && (parts[1] == event.pathNames[0]) && ($("#newsitem" + event.pathNames[1]).length)) { // CHECK IF ONLY THE PHOTO NUMBER CHANGED && THIS NEWS ITEM EXISTS ON THE PAGE
		//	News.scrollNews(event.pathNames[1]);
		//} else
		if ((event.pathNames[0] == "artists") && (parts[1] == event.pathNames[0]) && (parts[2] == event.pathNames[1]) && (parts[4] == event.pathNames[3]) && (parts[5] == event.pathNames[4])) { // CHECK IF ONLY THE PHOTO NUMBER CHANGED
				Photo.scrollPhotos(event.pathNames[5]);

		} else {
			//options.hideOpt();

			Site.doLoad(event);
			$("#tip").fadeOut(400);
		}
	
	//	options.setFbk();
		$("li#" + event.pathNames[0] + " a.menulink").addClass("open");
		Site.oldAddress = event.value;
	},
	doLoad: function (e) {
		$("#content").fadeOut(300);
		$("#loading").fadeIn(200);
		PhotoNav.destroy();
		setTimeout(function () {
			
			if((e.pathNames[0] == "") || (e.pathNames[0] == undefined) || (e.pathNames[0] == "/")){
				e.pathNames[0]="home";
			}
					
			var dataObj = {p : e.pathNames[0], id : e.pathNames[1], category : e.pathNames[3], ajax : 1, album : e.pathNames[4], photo : e.pathNames[5], large : 1}, 
			url = "/index.php";
			$.ajax({
				url: url,
				global: false,
				type: "GET",
				data: dataObj,
				dataType: "html",
				async: false,
				success: function (data) {
					$("#content").html($("div",data).html()).fadeIn(600);
					var t = $("h1",data).html() + " - AF Photo";
					document.title = t.replace("-  -","-");
					$("#menu").find(".selected").removeClass("selected");
					$("#menu").find("a[href^='/"+ e.pathNames[0] + "']").parent().addClass("selected");
					Site.onLoadComplete();
				}
			});
		}, 300);
	},
	setMenuHeight: function () {
		$("#artistsmenu").css("height", Math.max($(window).height() - 45, $(document).height() - 45) + "px");
		$("#artistsmenu div").css("height", Math.max($(window).height() - 65, $(document).height() - 65) + "px");
	},
	onLoadComplete: function () {
		//Site.slideUpArtists();
		//$("#content").fadeIn(300, Site.setMenuHeight);
		$('a[href^="http://"]:not([href^="http://www.afphoto.pl"]):not([href^=["http://afphoto.pl"])').attr("target","_blank");
		LightBox.hideLink();
		$("#loading").fadeOut(200);
		$("#tip").fadeOut(400);
		Ftp.init();
		$(".thumbset img, .thumbs img").hover(Site.fadeIt, Site.fadeBack);
		if ($("#newscol").length) {
			News.init();
			//PhotoNav.create();
		}
		if ( ($("#photos").length) && (Site.isiOs == false)) {
			Photo.setup();
			PhotoNav.create();
		}
		if ((!($("#newsitems").length)) && (!($("#photos").length))) {
			PhotoNav.destroy();
		}
		if ($(".lightbox").length) {
			LightBox.draw();
		}
		if ($(".thumbset").length) {
			Photo.hideTooManyThumbs();
		}
	//	options.setFbk();
	},
	loadImage: function (img) {
		$(img).css({
			"opacity": "0"
		});
		$(img).bind('load', function (e) {
			Site.imageOnLoad(img)
		});
	},
	imageOnLoad: function (img) {
		$(img).hover(Site.fadeIt, Site.fadeBack);
		$(img).animate({
			opacity: 1
		}, 200)
	}
}
var Photo = {
	excess: Array(),
	setup: function () {

			Photo.hideExcess();
			$('#photos').wrap('<div id=\"wrapper\" />');
			Photo.resizeFrame();
			var photos = $('.photoitem'),
			getNr = $.address.value().split("/");
			Photo.scrollPhotos(getNr[6]);
			Site.slideUpArtists();
			$("#tip").fadeIn(400);
			Photo.updateNumber(getNr[6]);
			$(".photoitem").bind("click", Photo.removeFocus);
		
	},
        getIndex : function(){
                var p = $(".photoitem"),
          	getNr = $.address.value().split("/"),
                status;
                
		if($(p[getNr[6]-1]).length){
                    if($(p).length ==  getNr[6]){
                        status = -2; // last
                    }
                    else if(getNr[6] == 1){
                        status = -1; // first
                    }
                    else {
                        status = 1; // ok
                    }
                  
                    return status;
                }
                else {
                    return 0; // no 
                }
        },
	hideExcess: function () {
		if ($(".photoitem").length > 5) {
			p = $(".photoitem"),
			ad = $.address.value().split("/"),
			c = Number(ad[6]),
			nr = c + 4,
			l = $(".photoitem").length,
			i = 0;
			for (i = 0; i < l; i++) {
				if ($(p[i]).html() != "") {
					Photo.excess[i] = $(p[i]).html();
				}
				if (i > nr - 1) {
					$(p[i]).html("");
				}
				if (i < Math.min(l, c + 3)) {
					$(p[i]).html(Photo.excess[i]);
				}
			}
		}
	},
	resizeFrame: function () {
		$('#wrapper').css("width", ($(window).width() - 270) + "px");
	},
	removeFocus: function (e) {
		setTimeout(function () {
			$(document.documentElement).focus()
		}, 300);
	},
	hideTooManyThumbs: function () {
		var thumbSet = $(".thumbset");
		for (var t = 0; t < thumbSet.length; t++) {
			Photo.hideThumbs($(thumbSet[t]));
		}
		$(".showMore").click(Photo.showThumbs);
	},
	hideThumbs: function (v) {
		var thumbs = $(v).find("img"),
		show = "<p class=\"showMore\">more&nbsp;<img src=\"/images/arrow.gif\" alt=\"\" /></p>";
		if (thumbs.length >= 6) {
			for (var p = 5; p < thumbs.length; p++) {
				$(thumbs[p]).hide();
			}
			$(v).append(show);
		}
	},
	showThumbs: function (e) {
		$(e.target).fadeOut(200)
		.parent().find("img").fadeIn(200);
	},
	setLinks: function () {
		var photos = $('.photoitem');
		$('.photoitem').css("cursor", "pointer");
		var createFunction = function (i) {
			return function () {
				Photo.setAddress(i);
			};
		};
		for (var i = 0; i < photos.length; i++) {
			$(photos[i]).bind("click", createFunction(i + 1));
		}
	},
	lockIt: function (i) {
		return i;
	},
	setAddress: function (nr) {
		var ad = $.address.value().split("/");
		ad[6] = nr;
		var photos = $('.photoitem');
		ad = ad.join("/");
		if ((nr == photos.length) && (ad == $.address.value())) {
			Photo.setAddress(1);
		} else {
			$.address.value(ad);
		}
	},
	scrollPhotos: function (nr) {
		Photo.hideExcess();
		var photos = $('.photoitem'),
		tar = photos[nr - 1],
		offset = $(tar).offset(),
		off = offset.left,
		margin = Number($('#photos').css("marginLeft").replace("px", ""));
		$('#photos').stop(true, true).animate({
			marginLeft: 270 - (off - margin) + "px"
		}, 300);
		Photo.updateNumber(nr);
		LightBox.showLink();
	},
	jumpTo: function (direction) {
		Photo.hideExcess();
		var photos = $('.photoitem'),
		ad = $.address.value().split("/");
		nr = Number(ad[6]) + Number(direction);
		if ((nr > 0) && (nr <= photos.length)) {
			Photo.setAddress(nr);
		}
	},
	updateNumber: function (nr) {
		var photos = $('.photoitem'),
		ad = $.address.value().split("/");
		nr = Number(nr);
		ad[6] = "";
		ad = ad.join("/");
		if (nr <= 1) {
			var countHTML = "<p><span id=\"currentPhoto\">" + nr + "</span>/<span id=\"count\">" + photos.length + "</span><a href=\"#" + ad + (nr + 1) + "\"><img id=\"arNext\" src=\"/images/bigarrow.gif\" alt=\"\" /></a></p>";
		} else if (nr == photos.length) {
			var countHTML = "<p><a href=\"#" + ad + (nr - 1) + "\"><img id=\"arPrev\" src=\"/images/bigarrowback.gif\" alt=\"\" /></a><span id=\"currentPhoto\">" + nr + "</span>/<span id=\"count\">" + photos.length + "</span></p>";
		} else {
			var countHTML = "<p><a href=\"#" + ad + (nr - 1) + "\"><img id=\"arPrev\" src=\"/images/bigarrowback.gif\" alt=\"\" /></a><span id=\"currentPhoto\">" + nr + "</span>/<span id=\"count\">" + photos.length + "</span><a href=\"#" + ad + (nr + 1) + "\"><img id=\"arNext\" src=\"/images/bigarrow.gif\" alt=\"\" /></a></p>";
		}
		$("#photoNo").html(countHTML);
	}
}
var News = {
	hoverTime: null,
	init: function () {
		News.setHovers();
		var np = $.address.value(),
		c = np.split("/");
		if((c.length > 5)&&(c[3] == "category")){
			c.pop();
		}
		c = c.join("/");
		$(".news-col").find("a[href='"+c+"']").parent().addClass("selected");
	},
        getIndex : function(){          	
		var getNr = $.address.value().split("/"),             
                news = $("#newsitem" + getNr[2]),
                anews = $(".newsitem");
                if(getNr[2]==undefined){
                    news = $(anews[0]);
                }               
                if($(news).length){
             
                    if(($(".newsitem").length-1) == $(news).index()){
                        status = -2; // last                      
                    }
                    else if($(news).index() == 0){
                        status = -1; // first
                    }
                    else {
                        status = 1; // ok
                    }
                    return status;
                }
                else {
                    return 0;
                }
        },
	resizeFrame: function () {
		$('#wrapper').css("width", ($(window).width() - 270) + "px");
	},
	setHovers : function(){
		$('.thumbs').find('a.photolink').hover(News.onHover, News.showFirst);		
	},
	onHover : function(e){
		clearTimeout(News.hoverTime);
		//console.log($(e.currentTarget).index());
		var i = $(e.currentTarget).parent().parent().find(".newsphoto img");
		$(e.currentTarget).parent().parent().find(".newsphoto img:visible").stop(true,true).fadeOut(200);
		$(i[$(e.currentTarget).index()]).stop(true,true).fadeIn(200);
		
	},
	showFirst: function () {
		clearTimeout(News.hoverTime);
		News.hoverTime = setTimeout(function () {
			var otherimgs = $("img.otherimg");
			$(otherimgs).stop(true, true).fadeOut(100);
			var mainimgs = $("img.mainimg");
			$(mainimgs).stop(true, true).fadeIn(200);
		}, 50);
	},
	setLinks: function () {
		var news = $('.mainimg');
		$('.mainimg').css("cursor", "pointer");
		var createFunction = function (i) {
			return function () {
				News.setAddress(i);
			};
		};
		for (var i = 0; i < news.length; i++) {
			$(news[i]).bind("click", createFunction($(news[i]).attr("id").replace("newsitem", "")));
		}
	},
	setAddress: function (nr) {
		var ad = $.address.value().split("/");
		ad[2] = nr;
		var news = $('.newsitem');
		ad = ad.join("/");
		if ((nr == news.length) && (ad == $.address.value())) {
			News.setAddress(1);
		} else {
			$.address.value(ad);
		}
	},
	highlight: function (nr) {
		var newstitles = $("ul#newsmenu li a");
		$(newstitles).removeClass("high");
		$("#newslink" + nr).addClass("high");
	},
	scrollNews: function (nr) {
		var news = $('.newsitem');
		var tar = $("#newsitem" + nr);
		if (!($(tar).length)) {
			tar = $(news[0]);
			nr = $(tar).attr("id").replace("newsitem", "");
		}
		News.highlight(nr);
		var offset = $(tar).offset();
		var off = offset.left;
		var margin = Number($('#newsitems').css("marginLeft").replace("px", ""));
		$('#newsitems').stop(true, true).animate({
			marginLeft: 270 - (off - margin) + "px"
		}, 300);
	},
	jumpTo: function (direction) {
		var news = $('.newsitem');
		var ad = $.address.value().split("/");
		var ln = news.length;
		if (!($("#newsitem" + ad[2]).length)) {
			ad[2] = $(news[0]).attr("id").replace("newsitem", "");
		}
		for (var n = 0; n < ln; n++) {
			if ($(news[n]).attr("id").replace("newsitem", "") == ad[2]) {
				if ($(news[n + Number(direction)]).length) {
					News.setAddress($(news[n + Number(direction)]).attr("id").replace("newsitem", ""));
				}
			}
		}
	}
}
var options = {
	setFbk: function () {
		var base = "http://www.afphoto.pl"
		var url = base + $.address.value();
		var fbkLink = "http://www.facebook.com/sharer.php?u=" + $.URLEncode(url) + "&t=" + $.URLEncode(document.title);
		$("#share a").attr("href", fbkLink);
		$("#share a").click(options.handleFbkClick);
	},
	handleFbkClick: function (e) {
		e.preventDefault();
		window.open($("#share a").attr("href"), "Share on Facebook", "status=1,toolbar=1,scrollbars=1");
	},
	setTitle: function () {
		if ($(".artistsname").length) {
			document.title = $(".artistsname").html() + " - " + document.title;
		}
		if ($("#albumtitle").length) {
			document.title = $("#albumtitle").html() + " - " + document.title;
		}
	},
	hideOpt: function () {
		$("#options").fadeOut(300);
	},
	showOpt: function () {
		$("#options").fadeIn(300);
	}
}
$.extend($.easing, {
	easeOutQuad: function (x, t, b, c, d) {
		return -c * (t /= d) * (t - 2) + b;
	}
});
$.extend({
	URLEncode: function (c) {
		var o = '';
		var x = 0;
		c = c.toString();
		var r = /(^[a-zA-Z0-9_.]*)/;
		while (x < c.length) {
			var m = r.exec(c.substr(x));
			if (m != null && m.length > 1 && m[1] != '') {
				o += m[1];
				x += m[1].length;
			} else {
				if (c[x] == ' ') o += '+';
				else {
					var d = c.charCodeAt(x);
					var h = d.toString(16);
					o += '%' + (h.length < 2 ? '0' : '') + h.toUpperCase();
				}
				x++;
			}
		}
		return o;
	}
});
var KeyNav = {
	keyup: function (event) {
		if (event.keyCode == 37) {
			if ($("#newsitems").length) {
				News.jumpTo(-1);
			} else if ($("#photos").length) {
				Photo.jumpTo(-1);
			}
		} else if (event.keyCode == 39) {
			if ($("#newsitems").length) {
				News.jumpTo(1);
			} else if ($("#photos").length) {
				Photo.jumpTo(1);
			}
		}
	}
}
var PhotoNav = {
	fadeTime: null,
	lnav: "<div id='lnav' class='snav' style='opacity: 0.9; z-index: 9999; position: absolute; width: 60px; height: 18px; font-size: 11px; font-weight: bold; background: #000; color: #fff; display: block; text-align:center;'>PREV</div>",	
	rnav: "<div id='rnav' class='snav' style='opacity: 0.9; z-index: 9998; position: absolute; width: 60px; height: 18px; font-size: 11px; font-weight: bold; background: #000; color: #fff; display: block; text-align:center;'>NEXT</div>",
	isHovered: false,
	destroy: function () {
		if ($("#lnav").length) $("#lnav").remove(); 
		if ($("#rnav").length)	$("#rnav").remove();
		if($("#photoscontainer").length) $("#photoscontainer").unbind("click");
		if($("#newsitems").length) $("#newsitems").unbind("click");
	},
	hideNav: function () {
		$(".snav").stop(true, true).fadeOut(200);
	},
	showNav: function () {
		$(".snav").stop(true, true).fadeIn(200);
	},
	create: function () {
		PhotoNav.destroy();
		$("body").append(PhotoNav.lnav)
		$("body").append(PhotoNav.rnav);
		$(".snav").css("display", "none");
	},
	leftClick: function (e) {
		e.preventDefault();		
		e.stopPropagation();
		if ($("#newsitems").length) {
			News.jumpTo(-1);
		} else if ($("#photos").length) {
			Photo.jumpTo(-1);
		}
		PhotoNav.onMove(e);
	},
	rightClick: function (e) {		
		e.preventDefault();
		e.stopPropagation();
		if ($("#newsitems").length) {
			News.jumpTo(1);
		} else if ($("#photos").length) {
			Photo.jumpTo(1);
		}
		PhotoNav.onMove(e);
	},
	onMove: function (e) {
		var maxVal;
		if ($("#newsitems").length) {
			maxVal = 380;
		} else if ($("#photos").length) {
			maxVal = $("#photoscontainer").height();
		}
		if ($(".snav").length) {
			var pI = Number(Photo.getIndex()),
			nI = Number(News.getIndex()),
			bI = pI + nI;	
			$("#rnav").css({left : (30 + e.pageX) + "px", top: e.pageY + "px"});
			$("#lnav").css({left : (e.pageX - 90) + "px", top: e.pageY + "px"});
			if ((! $("#artistsmenu").is(":visible")) &&(e.pageX > $(window).width() / 2) && (e.pageY <= maxVal + 100) && (e.pageY >= 100)) {
			$("#lnav").hide();
                            if((bI==1)||(bI==-1)){
				$("#rnav").show();
				if($("#photoscontainer").length) $("#photoscontainer").unbind("click").bind("click",PhotoNav.rightClick);
				if($("#newsitems").length) $("#newsitems").unbind("click").bind("click",PhotoNav.rightClick);
				$("#photoscontainer, #newsitems").css({cursor: "pointer"});
                            }
                            else
                            {
                                $("#rnav").hide();
				$("#lnav").show();
				if($("#photoscontainer").length) $("#photoscontainer").unbind("click").bind("click",PhotoNav.leftClick);
				if($("#newsitems").length) $("#newsitems").unbind("click").bind("click",PhotoNav.leftClick);
				$("#photoscontainer, #newsitems").css({cursor: "pointer"});
                            }
                                
                            
			} else if (( ! $("#artistsmenu").is(":visible")) &&(e.pageX >= 270) && (e.pageY <= maxVal + 100) && (e.pageY >= 100)) {
				$("#rnav").hide();
                             if((bI==1)||(bI==-2)){
				$("#lnav").show();
				if($("#photoscontainer").length) $("#photoscontainer").unbind("click").bind("click",PhotoNav.leftClick);
				if($("#newsitems").length) $("#newsitems").unbind("click").bind("click",PhotoNav.leftClick);
				$("#photoscontainer, #newsitems").css({cursor: "pointer"});
                             }
                             else {
                                $("#lnav").hide();				
				$("#rnav").show();
				if($("#photoscontainer").length) $("#photoscontainer").unbind("click").bind("click",PhotoNav.rightClick);
				if($("#newsitems").length) $("#newsitems").unbind("click").bind("click",PhotoNav.rightClick);
				$("#photoscontainer, #newsitems").css({cursor: "pointer"});
                             }
				
			} else {
				if($("#photoscontainer").length) $("#photoscontainer").unbind("click");
				if($("#newsitems").length) $("#newsitems").unbind("click");
				$(".snav").hide();				
				$("#photoscontainer, #newsitems").css({cursor: "auto"});
			}		
		}		
	}
};
var LightBox = {
	cookieDate: "",
	cookieName: "lightbox",
	boxHtml: "",
	init: function () {
		$("body").append("<div id=\"add\"><p>+ add to lightbox</p></div>");
		$("#add").hide().click(LightBox.clickHandler);
		LightBox.cookieDate = new Date();
		LightBox.cookieDate.setTime(LightBox.cookieDate.getTime() + (1 * 60 * 60 * 1000));
		var contents = LightBox.readCookie();
		LightBox.updateCount(contents.length);
	},
	draw: function () {
		var contents = LightBox.readCookie();
		LightBox.boxHtml = "";
		$.each(contents, LightBox.toHtml);
		LightBox.boxHtml = "<ul id=\"lightboximg\">" + LightBox.boxHtml + "</div>";
		$("#boxcol").html(LightBox.boxHtml);
		$("#clearBox").click(LightBox.clear);
		$("#download").click(LightBox.download);
		$("#lightboximg").sortable();
		$("#lightboximg").disableSelection();
		$("#lightboximg").bind('sortstop', LightBox.onSortStop);
	},
	check: function (no) {
		var box = LightBox.readCookie();
		if ($.inArray(no, box) != -1) {
			return true;
		} else {
			return false;
		}
	},
	download: function (e) {
		e.preventDefault();
		var box = LightBox.readCookie();
		LightBox.createForm("pdf/index.php", box);
	},
	createForm: function (path, box) {
		method = "post";
		var form = document.createElement("form");
		form.setAttribute("method", method);
		form.setAttribute("action", path);
		form.setAttribute("target", "_blank");
		var hiddenField = document.createElement("input");
		hiddenField.setAttribute("type", "hidden");
		hiddenField.setAttribute("name", "box");
		hiddenField.setAttribute("value", box);
		form.appendChild(hiddenField);
		document.body.appendChild(form);
		form.submit();
		document.body.removeChild(form);
	},
	onSortStop: function (event, ui) {
		var result = $('#lightboximg').sortable('toArray');
		LightBox.saveCookie(result);
	},
	toHtml: function (id, val) {
		LightBox.boxHtml += "<li id=\"" + val + "\"><img src=\"/media/icon/" + val + ".jpg\" alt=\"\" /></li>";
	},
	readCookie: function () {
		if ($.cookie(LightBox.cookieName) != undefined) {
			contents = $.cookie(LightBox.cookieName).split("|");
		} else {
			contents = Array();
		}
		return contents;
	},
	clear: function (e) {
		e.preventDefault();
		$.cookie(LightBox.cookieName, null, {
			path: '/',
			expires: LightBox.cookieDate
		});
		$("#boxcnt").html(0);
		$("#lightboximg li").remove();
	},
	addContent: function (what) {
		var contents = LightBox.readCookie(), l;
		contents.push(what);
		LightBox.updateCount(contents.length);
		LightBox.saveCookie(contents);
	},
	updateCount: function(n){
		var l = $("#menu").find("a[href^='/lightbox']").html().split("(");
		$("#menu").find("a[href^='/lightbox']").html(l[0] + "(" + n + ")");
	},
	saveCookie: function (contents) {
		$.cookie(LightBox.cookieName, contents.join("|"), {
			path: '/',
			expires: LightBox.cookieDate
		});
	},
	clickHandler: function (e) {
		var getA = $.address.value().split("/"),
		photos = $(".photoitem"),
		src = $(photos[getA[6] - 1]).find("img").attr("src"),
		parts = src.split("/"),
		file = parts[parts.length - 1];
		file = file.replace(".jpg", "");
		if (LightBox.check(file)) {
			$.address.value("lightbox");
		} else {
			LightBox.addContent(file);
			LightBox.showGo();
		}
	},
	showLink: function () {
		var getA = $.address.value().split("/");
		var photos = $(".photoitem");
		var src = $(photos[getA[6] - 1]).find("img").attr("src");
		var parts = src.split("/");
		var file = parts[parts.length - 1];
		file = file.replace(".jpg", "");
		if (LightBox.check(file)) {
			LightBox.showGo();
		} else {
			LightBox.showAdd();
		}
	},
	showAdd: function () {
		var top = $("#photos").height();
		$("#add").html("<p>+ add to lightbox</p></div>")
		.css("top", (top + 100) + "px").fadeIn(300);
	},
	showGo: function () {
		var top = $("#photos").height();
		$("#add").html("<p>go to lightbox</p></div>")	
		.css("top", (top + 100) + "px").fadeIn(300);
	},
	hideLink: function () {
		$("#add").fadeOut(200);
	}
}
Ftp = {
	init: function () {
		if ($("#ftplogin").length) {
			$("#ftplogin").submit(Ftp.onsubmit);
		}
	},
	onsubmit: function () {
		var forms = $("#ftplogin input");
		var str = "username=" + $(forms[0]).val() + "&pass=" + $(forms[1]).val();
		// $("#loginbut").html('<img src="/img/ajax-loader.gif" alt="" />&nbsp;');
		$.post("ftp/login.php", str, Ftp.postftp);
		return false;
	},
	postftp: function (data, textStatus) {
		if (data == "OK") {
			$("#files").load("ftp/list.php", function () {
				$("#files ul ul").hide();
				//  $("#login").slideUp("fast");		  
				$("#files").fadeIn("fast");
			});
		} else {
			//   $("#loginbut").html('<a href="" onclick="ftplogin(); return false;">log in</a>');
			alert("Wrong username or password. Please try again.");
		}
	},
	toggle: function (i, f) {
		if (f != undefined) {
			$("#ul" + i + "_" + f).slideToggle();
		} else {
			$("#ul" + i).slideToggle();
		}
	}
}
var Chimp = {
	savedVal: "",
	defaultVal: "Subscribe: your e-mail",
	init: function () {
		var f = $("#menu").find("form"),
		i = $("#email");
		$(f).submit(Chimp.onSubmit);
		$(i).focus(Chimp.onFocus)
		.blur(Chimp.onBlur);
	},
	onFocus: function () {
		var i = $("#email");
		Chimp.savedVal = $(this).val();
		if ($(i).val() == Chimp.defaultVal) {
			$(i).val("");
		}
	},
	onBlur: function () {
		var i = $("#email");
		if ($(i).val().length > 0) {
			$(i).val(Chimp.savedval);
		} else {
			$(i).val(Chimp.defaultVal);
		}
	},
	onSubmit: function (e) {
		e.preventDefault();
		var f = $("#menu").find("form"),
		str = $(f).serialize();
		$.post("chimp/index.php", str, function (data, textStatus) {
			if (data == "SENT") {
				$(f).fadeOut("fast");
				alert("Thank you. Please confirm your subscription by clicking on the link in the e-mail we have just sent you.");
			} else {
				alert(data);
			}
		});
	}
}
$(document.documentElement).keyup(KeyNav.keyup);
$.address.init(Site.oninit).change(Site.onchange);
$(document).mousemove(PhotoNav.onMove);
