/* bauer-woodcraft.de javascript */

// Do some stuff when DOM is ready
$(document).ready(function() {
	//$(".lightbox").lightbox();
	
	// Init fancybox
	$('.fancybox').fancybox();
});

// Do some stuff when window resizes
$(window).resize(function() {
	BWC.Window.getWindowInfo();
});


var BWC = {};

BWC.hostPath = '';


/* Gmap */
BWC.GMap = {};

// Init function
BWC.GMap.initialize = function() {
	if(GBrowserIsCompatible()) {
		// Init
		var map = new GMap2(document.getElementById("gmap"));
		//map.setCenter(new GLatLng(48.106502, 11.0361529), 16);
		map.setCenter(new GLatLng(48.056384, 11.963452), 13);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		
		// Set map type
		map.setMapType(G_NORMAL_MAP);
		
		// Show info window
		var marker = new GMarker(map.getCenter(), {draggable: false});
		map.addOverlay(marker);
		var html_insert = '<strong>Schreinerei Franz Bauer</strong><br />Wasserburger Strasse 33, D-85567 Grafing<br />Telefon +49 (0) 8092 85 08 79<br />Fax +49 (0) 8092 85 08 78<br />Mobil +49 (0) 177 6421138';
		marker.openInfoWindowHtml(html_insert);
	}
}



/* Window */
BWC.Window = {};

BWC.Window.width = 0;
BWC.Window.height = 0;

// Get dimension of browser window -> width & height
BWC.Window.getWindowInfo = function() {
	BWC.Window.width = $(window).width() - 80;
	BWC.Window.height = $(window).height() - 160;
	
	$.ajax({
		type: "POST",
		url: BWC.hostPath + "/image/storebrowserinfo",
		data: "width=" + BWC.Window.width + "&height=" + BWC.Window.height
		/*
		success: function(msg) {
			alert("Data Saved: " + msg );
		}
		*/
	});
}


/* Bigpic */
BWC.BP = {};

BWC.BP.pics = new Array();
BWC.BP.crnt = 0;
BWC.BP.next = 1;
BWC.BP.l = 0;
BWC.BP.intObj = null;
BWC.BP.interval = 6500;
BWC.BP.transition = 2300;

// Init
BWC.BP.init = function() {
	BWC.BP.pics = $("#page_bigpic_container img");
	BWC.BP.l = BWC.BP.pics.length - 1;
	
	// Set opacity to 0 exept the first pic in array
	for(var i=0; i<BWC.BP.pics.length; i++) {
		if(i != 0) {
			$(BWC.BP.pics[i]).css({
				opacity: 0,
				'display': 'block'
			});
		} else {
			$(BWC.BP.pics[i]).css({
				opacity: 1,
				'display': 'block'
			});
		}
	}
	
	//BWC.BP.ani();
	BWC.BP.intObj = window.setInterval("BWC.BP.ani()", BWC.BP.interval);
}

// Animation
BWC.BP.ani = function() {
	// Get next pic in array
	if(BWC.BP.crnt < BWC.BP.l) {
		BWC.BP.next = BWC.BP.crnt + 1;
	} else if(BWC.BP.crnt == BWC.BP.l) {
		BWC.BP.next = 0;
	}
	
	// Fade in next pic
	$(BWC.BP.pics[BWC.BP.next]).fadeTo(BWC.BP.transition, 1, function() {
		$(BWC.BP.pics[BWC.BP.crnt]).fadeTo(BWC.BP.transition - 1000, 0);
		
		// Set current
		if(BWC.BP.crnt == BWC.BP.l) {
			BWC.BP.crnt = 0;
		} else {
			BWC.BP.crnt++;
		}
	});
}



/* Gallery */
BWC.Gallery = {};

BWC.Gallery.imgCnt = 0;
BWC.Gallery.imgs = new Array();
BWC.Gallery.thumbContainers = new Array();
BWC.Gallery.containerID = 'gallery_container';
BWC.Gallery.placeholderID = 'gallery_loading_placeholder';
BWC.Gallery.itemSize = new Array(159, 160);
BWC.Gallery.imgsInRow = 5;
BWC.Gallery.winDimensions = new Array();

// Init
BWC.Gallery.init = function() {
	//console.log("gallery init");
	//console.log(BWC.Gallery.imgs);
	//console.log(BWC.Gallery.itemSize);
	
	// Arrange thumbnails outside the window
	//BWC.Gallery.arrangeThumbsOutside();
	
	// Get images count and calculate height of container
	BWC.Gallery.generateGalleryHeight();
}

// Get images count and calculate height of container
BWC.Gallery.generateGalleryHeight = function() {
	var imgsInColumn = Math.round((BWC.Gallery.imgCnt / BWC.Gallery.imgsInRow));
	var galleryContainerHeight = BWC.Gallery.itemSize[1] * imgsInColumn;
	$('#' + BWC.Gallery.containerID).animate({
		height: galleryContainerHeight + 'px'
	}, 1000, 'swing', function() {
		$('#' + BWC.Gallery.placeholderID).fadeOut(2000, function() {
			// Scroll to gallery viewport
			$.scrollTo('#' + BWC.Gallery.containerID, 1000, {
				//offset: '50',
				onAfter: function() {
					// Move thumbs to viewport
					BWC.Gallery.fadeInThumbs();
				}
			});
		});
	});
}

// Fade in thumbs
BWC.Gallery.fadeInThumbs = function() {
	// Get thumb container
	BWC.Gallery.thumbContainers = $('#' + BWC.Gallery.containerID + ' a.gallery_item');
	
	// Walk thru thumbs and fade them in
	BWC.Gallery.thumbContainers.each(function(i) {
		var max = 2000;
		var min = 100;
		var fader = Math.round(Math.random() * (max - min)) + min;
		$(this).fadeIn(fader, function() {
			BWC.Gallery.loadImage(i);
		});
	});
}

// Load image
BWC.Gallery.loadImage = function(i) {
		
	// Create empty img
	var img = new Image(BWC.Gallery.itemSize[0], BWC.Gallery.itemSize[1]);
	
	// Load img into it
	//img.src = BWC.Gallery.imgs[i].path;
	var imgPath = BWC.hostPath + '/image/thumbsforgallery?img=' + BWC.Gallery.imgs[i].path;
	img.src = imgPath;
	
	// Get corresponding container and append img
	var aContainer = $('#gallery_item_' + i);
	aContainer.html(img);
}







/*
// Move thumbs to viewport
BWC.Gallery.moveThumbsToViewport = function() {
	// Walk thru thumbs and move them to viewport
	BWC.Gallery.thumbContainers.each(function(i) {
		// Calc position of thumb object
		var pos = BWC.Gallery.calculatePositionOfThumbObj(i);
		
		$(this).animate({
			'left': pos[0] + 'px',
			'top': pos[1] + 'px'
		}, 2000, function() {
			// Set overflow of body to visible
			$('body').css({
				'overflow': 'visible'
			});
		});
	});
}

// Calculate position of thumb object
BWC.Gallery.calculatePositionOfThumbObj = function(i) {
	var res = new Array();
	
	// Check in which row and column we are
	var row = Math.floor(i / BWC.Gallery.imgsInRow);
	var column = i % BWC.Gallery.imgsInRow;
	
	//console.log(row, column);
	
	res[0] = row * BWC.Gallery.itemSize[0];
	res[1] = column * BWC.Gallery.itemSize[1];
	
	return res;
}

// Arrange thumbnails outside the window
BWC.Gallery.arrangeThumbsOutside = function() {
	// Set overflow of body to hidden
	$('body').css({
		'overflow': 'hidden'
	});
	
	// Get thumb container
	BWC.Gallery.thumbContainers = $('#' + BWC.Gallery.containerID + ' a.gallery_item');
	
	// Get dimensions of browser window
	BWC.Gallery.winDimensions[0] = $('body').width();
	BWC.Gallery.winDimensions[1] = $('body').height();
	
	
	// Place thumbs outside the visible viewport
	BWC.Gallery.thumbContainers.each(function(i) {
		var x, y;
		
		if(i % 2) {
			x = Math.round((Math.random() * ((BWC.Gallery.itemSize[0] + BWC.Gallery.winDimensions[0]) - (BWC.Gallery.itemSize[0] + BWC.Gallery.winDimensions[0]))) + (BWC.Gallery.itemSize[0] + BWC.Gallery.winDimensions[0]));
			y = Math.round((Math.random() * ((BWC.Gallery.itemSize[0] + BWC.Gallery.winDimensions[1]) - (BWC.Gallery.itemSize[0] + BWC.Gallery.winDimensions[1]))) + (BWC.Gallery.itemSize[0] + BWC.Gallery.winDimensions[1]));
			
			console.log(x, y);
			
			// Place it right or bottom
			$(this).css({
				'right': (x - 3000) + 'px',
				'bottom': (y - 3000) + 'px'
			});
		} else {
			x = Math.round((Math.random() * ((BWC.Gallery.itemSize[0] + 50) - (BWC.Gallery.itemSize[0] + 500))) + (BWC.Gallery.itemSize[0] + 50));
			y = Math.round((Math.random() * ((BWC.Gallery.itemSize[0] + 50) - (BWC.Gallery.itemSize[0] + 500))) + (BWC.Gallery.itemSize[0] + 50));
			
			// Place it left or top
			$(this).css({
				'left': '-' + (x + 3000) + 'px',
				'top': '-' + (y + 3000) + 'px'
			});
		}
		
		
	});
}
*/
