var Site = {
	init: function () {		
		News.init();
		Lightbox.init();
		if($("#map").length){
			Map.init();
		}
	}
};
var News = {
	init : function(){
		$(".post a").bind("click",News.onClick);
		$(".post:not(:first-child)").find("p").hide();
		if($("#full").length) News.initBg();
	},
	initBg : function(){
		News.bg();
		$(window).resize(News.bg);
		$(window).load(function(){var interval = setInterval(News.switchBg,6000);});
		
	},
	onClick : function(e){
		e.preventDefault();
		$(".post p:visible").slideUp(600);
		$(e.currentTarget).parent().find("p").slideDown(600);
	},
	bg : function(){
		var aspect = 1680/1125, $window = $(window), $full = $("#full");
		if($window.width()/($window.height()-118)>aspect){
			$full.find("img").css({width: "100%", height:"auto"});
		}
		else {
			$full.find("img").css({height: "100%", width:"auto"});
		}
	},
	switchBg : function(){
		var $img = $("#full").find("img"), i = $("#full").find("img:visible").index(), l = $("#full").find("img").length;
		if(i+1<l) {
			$($img[i]).fadeOut(1500);
			$($img[i+1]).fadeIn(1000);	
		}
		else {
			$($img[i]).fadeOut(1500);
			$($img[0]).fadeIn(1000);				
		}
	}
};
var Lightbox = {
	current: "",
	count: "",
	li: "",
	code: "<div id=\"overlay\"></div><div class=\"lbcontainer\" id=\"imgcontainer\"><ul><li id=\"lbclose\"><a href=\"#\">close</a></li><li id=\"lbnext\"><a href=\"#\">next</a></li><li id=\"lbprev\"><a href=\"#\">prev</a></li></ul><img src=\"{SRC}\" alt=\"{NAME}\" /></div>",
	init: function () {
		if($(".photo-thumbs").length){
			$(".photo-thumbs").bind("mousemove",Lightbox.thumbsMove);
		}
		$("a[rel*=\"lightbox\"]").live("click", Lightbox.onclick);
		$(document).bind("keyup", Lightbox.keyup);
	},
	onclick: function (e) {
		e.preventDefault();
		Lightbox.li = $("a[rel^='lightbox']");
		if($(e.currentTarget).parent().parent().attr("class") == "photo-thumbs"){
			Lightbox.current = $(e.currentTarget).parent().index()+1;			
			Lightbox.count = Lightbox.li.length;
		}
		else {
			Lightbox.current = 0;
			Lightbox.count = Lightbox.li.length;
		}
		
		var w, h, src, parts;
		if ($(e.currentTarget).attr("rel") != "") {
			parts = $(e.currentTarget).attr("rel").split("|");
		} else {
			parts = Array("", 800, 800);
		}
		if (parts[3] == "html") {
			w = parts[1];
			h = parts[2];
			src = $(e.currentTarget).attr("href");
			Lightbox.display("", w, h, src, true);
		} else if (parts[3] == "json") {
			w = parts[1];
			h = parts[2];
			src = $(e.currentTarget).attr("href");
			Lightbox.display("", w, h, src, false, true);
		} else {
			var t = $(e.currentTarget).find("img").attr("alt");
			w = parts[1];
			h = parts[2];
			src = $(e.currentTarget).attr("href");
			Lightbox.display(t, w, h, src, false);
		}
		$("#overlay").height($(document).height()).hide().fadeIn(600);
	},
	display: function (t, w, h, src, html, json) {
		var tm, a;
		if (h < $(window).height()) {
			tm = ($(window).height() - h) / 2;
		} else {
			tm = 100;
		}
		if (json == true) {


			$("body").append(Lightbox.codejson);
			a = $("#imgcontainer li a");
			$(a[0]).bind("click", Lightbox.kill);
			$(a[1]).bind("click", function (e) {
				Lightbox.showimgjson(e, 1);
			});
			$(a[2]).bind("click", function (e) {
				Lightbox.showimgjson(e, -1);
			});

			$.getJSON(src + "?ajax=1", function (data) {

				var divs = [];

				$.each(data, function (key, val) {
					divs.push(Lightbox.jsontpl.replace("{SRC}", val[0]).replace(/{NAME}/g, val[1]));
				});
				Lightbox.count = divs.length;
				Lightbox.current = 0;

				$("#jsoncode").html(divs.join(""));
				$(".loader").remove();

			});
		} else if (html != true) {
			$("body").append(Lightbox.code.replace("{SRC}", src).replace(/{NAME}/g, t));
			a = $("#imgcontainer").find("a");
			if(Lightbox.count == 1){
				$(a[1]).remove();
				$(a[2]).remove();
			}
			
			$(a[0]).bind("click", Lightbox.kill);
			$(a[1]).bind("click", function (e) {
				Lightbox.showimg(e, 1);
			});
			$(a[2]).bind("click", function (e) {
				Lightbox.showimg(e, -1);
			});
		} else {
			$("body").append(Lightbox.codehtml);
			a = $("#htmlcontainer").find("a");
			$(a).bind("click", Lightbox.kill);
			$("#htmlcode").load(src);
		}
		$(".lbcontainer").css({
			'width': w + 'px',
			'height': h + 'px',
			'margin-left': -(w / 2) + 'px',
			'margin-top': ($(window).scrollTop() + tm) + 'px'
		});
		$(".lbcontainer").hide().fadeIn(600);
		$("#overlay").bind("click", Lightbox.kill);
	},
	showimg: function (e, dir) {
		e.preventDefault();
		Lightbox.current += dir;
		if (Lightbox.current >= Lightbox.count) {
			Lightbox.kill(e);
		} else if (Lightbox.current < 0) {
			Lightbox.kill(e);
		} else {
			var parts = $(Lightbox.li[Lightbox.current]).attr("rel").split("|"),
			w = parts[1],
			h = parts[2],
			t = $(Lightbox.li[Lightbox.current]).find("img").attr("alt"),
			src = $(Lightbox.li[Lightbox.current]).attr("href");
			$("#imgcontainer").stop(true, true).fadeOut(600, function () {
				$("#overlay, #imgcontainer").remove();
				Lightbox.display(t, w, h, src);
			});
		}
	},
	showimgjson: function (e, dir) {
		e.preventDefault();
		var divToHide = Lightbox.current;
		Lightbox.current += dir;
		if (Lightbox.current >= Lightbox.count) {
			Lightbox.kill(e);
		} else if (Lightbox.current < 0) {
			Lightbox.kill(e);
		} else {
			var divs = $("#jsoncode").find("div");
			$(divs[divToHide]).stop(true, true).fadeOut(600);
			$(divs[Lightbox.current]).stop(true, true).fadeIn(600);
		}
	},
	keyup: function (event) {
		if ($("#overlay").length) {
			if (event.keyCode == 27) {
				Lightbox.kill(event);
			} else if (event.keyCode == 37) {
				if ($("#jsoncode").length) {
					Lightbox.showimgjson(event, -1);
				} else {
					Lightbox.showimg(event, -1);
				}
			} else if (event.keyCode == 39) {
				if ($("#jsoncode").length) {
					Lightbox.showimgjson(event, 1);
				} else {
					Lightbox.showimg(event, 1);
				}
			}
		}
	},
	kill: function (e) {
		e.preventDefault();
		$("#overlay, .lbcontainer a").unbind();
		$("#overlay, .lbcontainer").stop(true, true).fadeOut(600, function () {
			$("#overlay, .lbcontainer").remove();
		});
	},
	thumbsMove : function(e){
		var tw = 0;
		$(e.currentTarget).find("li").each(function(a,b){
			var wid = $(b).width();
			if(!isNaN(wid)){
				tw += wid*1+2;
			}
			
		});
		
		var x = e.pageX - $(e.currentTarget).offset().left,
		y = e.pageY - $(e.currentTarget).offset().top;
		if(tw > $(e.currentTarget).parent().width()){
			var dif = tw - $(e.currentTarget).parent().width(),
			pos = x / $(e.currentTarget).parent().width();
			$(e.currentTarget).css({marginLeft:-dif*pos+"px"});
		}
	}
};
var Map = {
	activeWindow: null,
	map: null,
	points: null,
	mapContainer: "map",
	init: function () {
		console.log(points);
		Map.points = points;
		
		if ($("#" + Map.mapContainer).length) {
			Map.drawMap();
			Map.mapOptions["mapTypeControlOptions"] = {
				mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'mapstyle']
			};
			Map.mapOptions["mapTypeId"] = google.maps.MapTypeId.ROADMAP;
		}
	},
	mapOptions: {
		zoom: 4,
		mapTypeControlOptions: null,
		center: null,
		navigationControl: true,
		scaleControl: false,
		mapTypeControl: false,
		scrollwheel: false,
		mapTypeId: null
	},
	customStyle: {
		featureType: 'all',
		stylers: [
			{
			saturation: 0
		},
			{
			gamma: 0.5
		},
			{
			lightness: 0
		},
			{
			invert_lightness: false
		}
		]
	},
	applyMapStyle: function () {
		var customType = new google.maps.StyledMapType([Map.customStyle], {
			name: "mapstyle"
		});
		Map.map.mapTypes.set('mapstyle', customType);
		Map.map.setMapTypeId('mapstyle');
	},
	drawMap: function () {
		if (document.getElementById(Map.mapContainer)) {
			if (Map.points[Map.mapContainer].length > 0) {
				var gPoints = [];
				for (var i = 0; i < Map.points[Map.mapContainer].length; i++) {
					gPoints[i] = new google.maps.LatLng(Map.points[Map.mapContainer][i][0], Map.points[Map.mapContainer][i][1]);
				}
				Map.mapOptions["center"] = gPoints[0];
				Map.map = new google.maps.Map(document.getElementById(Map.mapContainer), Map.mapOptions);

				function createMarker(p, t, c, i) {
					var marker = new google.maps.Marker({
						position: p,
						map: Map.map,
						title: t
						,icon: '/images/marker.png'
					});
					var info = new google.maps.InfoWindow({
						content: c
					});
					google.maps.event.addListener(marker, 'click', function () {
						if (Map.activeWindow != null) Map.activeWindow.close();
						info.open(Map.map, marker);
						Map.activeWindow = info;
					});
					return marker;
				}
				for (var i = 0; i < gPoints.length; i++) {
					createMarker(gPoints[i], Map.points[Map.mapContainer][i][2], Map.points[Map.mapContainer][i][3], i);
				}
				if (Map.points[Map.mapContainer].length > 1) {
					//fit map to markers
					var bounds = new google.maps.LatLngBounds();
					for (var i = 0; i < gPoints.length; i++) {
						bounds.extend(gPoints[i]);
					}
					Map.map.setCenter(bounds.getCenter());
					Map.map.fitBounds(bounds);
				}
				Map.applyMapStyle();
			} else {
				document.getElementById(Map.mapContainer).style.display = 'none';
			}
		}
	}
};
$(document).ready(Site.init);
