// Google analytics.
var _gaq = _gaq || [];

/**
 * Main DDB class.
 */
DDB = {
	constant: {
		LIVE_URL: "www.ddbuk.com",
		GA_ACCOUNT: "UA-20751842-1",

		KEY_ESCAPE: 27,

		ITEM_WIDTH: 238,
		ITEM_HEIGHT: 133,

		ITEM_EXPANDED_WIDTH: 720,
		ITEM_EXPANDED_HEIGHT: 401,

		ITEM_CENTER_OFFSET_X: 720/2,
		ITEM_CENTER_OFFSET_Y: 120,

		ITEM_MOBILE_EXPANDED_WIDTH: '95%',

		ITEM_ANIMATION_SPEED: 300,

		ITEM_ANIMATION_EASING: 'easeOutSine',

		MAP_ADDRESS: "12 Bishops Bridge Road<br>W2 6AA London<br>Tel: +44 (0) 20 7258 3979<br>Fax: 0207 402 4871",
		MAP_LAT: 51.51793,
		MAP_LNG: -0.181253,

		TOUCH_EVENT: 'click'
	},

	/**
	 * Common functionality.
	 */
	common: {
		init: function() {
			if (DDB.common.isMobile() && !DDB.common.isTablet()) {
				DDB.constant.ITEM_EXPANDED_WIDTH = DDB.constant.ITEM_MOBILE_EXPANDED_WIDTH;
			}

			if ('ontouchstart' in window || "ontouchend" in document) {
				DDB.constant.TOUCH_EVENT = 'touchstart';
			}
			try {document.createEvent("TouchEvent");DDB.constant.TOUCH_EVENT = 'touchstart';} catch(e){}

			DDB.divisions.init();
			DDB.search.init();
			DDB.nav.init();
			DDB.item.init();
			DDB.twitter.init();
			DDB.gallery.init();
			DDB.about.init();
			DDB.track.init();
                        DDB.gridCentrer.init();

			$("a[href^='http://']").attr("target", "_blank");

			if (!DDB.common.isMobile()) {
				$('#item-grid .item a').hover(function() {
					$('.hover', this).show();
				}, function() {
					$('.hover', this).hide();
				});
			}

			// Blog link fix
			if (!DDB.common.isLive())
			{
				$('#blog a').attr('href', 'http://blog.' + window.location.hostname);
			}
		},
		// END: init

		isLive: function() {
			return (window.location.hostname == DDB.constant.LIVE_URL);
		},
		// END: isLive

		isMobile: function() {
			if (!!(navigator.userAgent.match(/mobile/i))) {
				return true;
			}

			if (!!(navigator.userAgent.match(/iPad/i))) {
				return true;
			}

			if (!!(navigator.userAgent.match(/Android/i))) {
				return true;
			}

			if (!!(navigator.userAgent.match(/iPod/i))) {
				return true;
			}

			if (!!(navigator.userAgent.match(/BlackBerry/i))) {
				return true;
			}

			return false;
		},
		// END: isMobile

		isTablet: function() {
			if (!!(navigator.userAgent.match(/tablet/i))) {
				return true;
			}

			if (!!(navigator.userAgent.match(/iPad/i))) {
				return true;
			}

			if (!!(navigator.userAgent.match(/Android/i)) && !!!(navigator.userAgent.match(/mobile/i))) {
				return true;
			}

			return false;
		},
		// END: isMobile

		log: function(msg) {
			if (typeof console != "undefined" && typeof console.log == "function" && !DDB.common.isLive()) {
				console.log(msg);
			}
		}
	// END: log
	},
	// END: common

	/**
	 * Hover functions for the Division Tabs
	 *
	 * @todo sure this can be made a lot more elegantly, but in a time crunch.
	 */
	divisions: {
		init: function ()
		{
			var division = $('body').attr('id');

			if (division == 'tribalddb') {
				$('#ddbukNav').addClass('ddbukNav_hover');
				$('#tribalsparkNav').addClass('tribalsparkNav_hover');
			} else if (division == 'tribalspark') {
				$('#ddbukNav').addClass('ddbukNav_hover');
				$('#tribalddbNav').addClass('tribalddbNav_hover');
			}

			$(".navHover").hover(

				// IN - Change other divisions to greyed out.
				function ()
				{
					var ei = $(this).attr('id');

					if (ei == 'ddbukNav') {
						$('#ddbukNav').removeClass('ddbukNav_hover');
						$('#tribalddbNav').removeClass('tribalddbNav_hover');
						$('#tribalsparkNav').removeClass('tribalsparkNav_hover');
					} else if (ei == 'tribalddbNav') {
						$('#ddbukNav').addClass('ddbukNav_hover');
						$('#tribalddbNav').removeClass('tribalddbNav_hover');
						$('#tribalsparkNav').addClass('tribalsparkNav_hover');
					} else if (ei == 'tribalsparkNav') {
						$('#ddbukNav').addClass('ddbukNav_hover');
						$('#tribalddbNav').addClass('tribalddbNav_hover');
						$('#tribalsparkNav').removeClass('tribalsparkNav_hover');
					}
				},

				// OUT - Reset
				function ()
				{
					var ei = $(this).attr('id');

					if (ei = 'ddbukNav' && division == 'ddbuk') {
						$('#ddbukNav').removeClass('ddbukNav_hover');
						$('#tribalddbNav').removeClass('tribalddbNav_hover');
						$('#tribalsparkNav').removeClass('tribalsparkNav_hover');
					} else if (ei = 'tribalddbNav' && division == 'tribalddb') {
						$('#ddbukNav').addClass('ddbukNav_hover');
						$('#tribalddbNav').removeClass('tribalddbNav_hover');
						$('#tribalsparkNav').addClass('tribalsparkNav_hover');
					} else if (ei = 'tribalsparkNav' && division == 'tribalspark') {
						$('#ddbukNav').addClass('ddbukNav_hover');
						$('#tribalddbNav').addClass('tribalddbNav_hover');
						$('#tribalsparkNav').removeClass('tribalsparkNav_hover');
					}
				}
				);
		}
	},

	track: {
		init: function() {
			_gaq.push(['_setAccount', DDB.constant.GA_ACCOUNT]);
			_gaq.push(['_trackPageview']);

			var ga = document.createElement('script');
			ga.type = 'text/javascript';
			ga.async = true;
			ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
			var s = document.getElementsByTagName('script')[0];
			s.parentNode.insertBefore(ga, s);
		},
		// END: init

		event: function(category, action, label) {
			DDB.common.log('DDB.track.event(' + category + ', ' + action + ', ' + label + ')');

			if (DDB.common.isLive()) {
				_gaq.push(['_trackEvent', category, action, label]);
			}
		}
	},

	nav: {
		init: function () {
			$(".blog").hover(
				function () {
					$(".blog img").attr('src', '/assets/img/navigation/blog_rollover.png');
				},
				function () {
					$(".blog img").attr('src', '/assets/img/navigation/blog_standard.png');
				}
			);

			$("#nav a.current").parent().prepend('<div></div>');
			$("#nav div").css({
				width: $("#nav a.current").outerWidth()
			});
		}
	},

	twitter: {
		init: function() {
			$('#twitter-grid .tweet, #item-grid .tweet')
			.disableSelection()
			.find('a.twitter').click(function() {
				var tweetUser = $.trim(
					$(this).parent().parent()
					.children('h3')
					.clone()	// Clone the element
					.children()	// Select all the children
					.remove()	// Remove all the children
					.end()		// Again go back to selected element
					.text()		// Get the text of element
					);

				DDB.track.event('share', 'tweet', tweetUser);
			});
		}
	},
	// END: twitter

	item: {
		opened: false,

		init: function() {
			if ($.browser.msie && parseInt($.browser.version, 10) === 6) {
				return;
			}

			if (DDB.common.isTablet() || !DDB.common.isMobile()){
				$('#item-grid .item')
					.not('.item-expanded').disableSelection()
					.not('.tweet').click(DDB.item.click);

				$('#item-grid .item:not(.tweet) a').click(function () {
					$(this).parent().click();
					return false;
				});
			}

			// If we are viewing an item directly
			if ($('#content > .item-expanded')) {
				var itemTitle = $.trim(
					$('#content > .item')
						.children('h1')
							.clone()	// Clone the element
								.children()	// Select all the children
									.remove()	// Remove all the children
								.end()	  // Again go back to selected element
								.text()	 // Get the text of element
					);

				DDB.item._trackSharing($('#content > .item'), itemTitle);
			}
		},
		// END: init

		_trackSharing: function($item, itemTitle) {
			$item.find('a.facebook').click(function() {
				DDB.track.event('share', 'facebook', itemTitle);
			});

			$item.find('a.twitter').click(function() {
				DDB.track.event('share', 'twitter', itemTitle);
			});

			$item.find('a.email').click(function() {
				DDB.track.event('share', 'email', itemTitle);
			});
		},

		_open: function($item) {
			var animFinished = false;
			var ajaxFinished = false;

			var $img = $item.find('img');
			var offset = $img.offset();

			// Create the shaded overlay
			var $shadeOverlay = $('<div>', {
				id: 'shade-overlay',
				css: {
					position: 'fixed',
					background: '#fff',
					opacity: 0,
					width: '100%',
					height: $(document).height(),
					top: 0,
					right: 0,
					bottom: 0,
					left: 0
				}
			});

			if (!DDB.common.isMobile() || DDB.common.isTablet()) {
				$shadeOverlay.click(function() {
					DDB.item.close();
				});
			}

			// Create the expanded item container
			var $expandedItem = $('<div>', {
				id: 'item-expanded',
				css: {
					width: DDB.constant.ITEM_WIDTH,
					height: DDB.constant.ITEM_HEIGHT
				},
				html: '<div id="item-content">'
			});

			// Store a reverse lookup
			$expandedItem.data('item', $item);

			// Add the image to the container
			if(!DDB.common.isMobile()) {
				$img = $img.clone();
				$img.css({
					width: '100%',
					height: '100%'
				});
				$expandedItem.append($img);
			} else {
				$expandedItem.append('<p class="loading">Loading content...</p>');

			}

			// Get the current item offset to animate from
			$expandedItem.css({
				top: offset.top,
				left: offset.left
			});

			var destinationTop = offset.top;
			var destinationLeft = 10;

			// Center in viewport
			if (!DDB.common.isMobile() || DDB.common.isTablet()) {
				destinationTop = $(window).scrollTop() + DDB.constant.ITEM_CENTER_OFFSET_Y;
				destinationLeft = ($(window).width() * 0.5) - DDB.constant.ITEM_CENTER_OFFSET_X;
			}

			// Add the overlay and item to the body
			$('body').append($shadeOverlay, $expandedItem);

			var loadedContentAndDoneAnimation = function () {
				$expandedItem.children("img").remove();

				var height = $('#item-content', $expandedItem).height();

				if(DDB.common.isMobile()){
					$('#item-content', $expandedItem).show();
				} else {
					$expandedItem.fadeIn(1000, function(){
						$('#item-content', this).fadeIn(500);
					});
				}
/*
				if (height > DDB.constant.ITEM_EXPANDED_HEIGHT) {
					// Animate the height
					$expandedItem.animate({
						height: height
					}, {
						duration: DDB.constant.ITEM_ANIMATION_SPEED,
						easing: DDB.constant.ITEM_ANIMATION_EASING,
						queue: false,
						complete: function() {

							DDB.item.onOpen($expandedItem);
						}
					});
				} else*/ {
					DDB.item.onOpen($expandedItem);
				}
			};

			// Ajax load the content


			$.ajax({
				url: $item.find("a").attr("href"),
				data: {json: true},
				success: function(html) {
					if(DDB.common.isMobile()){
						$('p.loading', $expandedItem).remove();
					}
					$('#item-content', $expandedItem).html(html);
					$('#item-content', $expandedItem).append('<span class="close"></span>');
					$(".close").click(DDB.item.close);
					ajaxFinished = true;
					if (!DDB.item.opened && animFinished) {
						loadedContentAndDoneAnimation();
					}
				}
			});

			// Animate enlarge the expanded item
			if (DDB.common.isMobile()) {
				// Animate the overlay in
				$shadeOverlay.css({
					opacity: 0.8
				});

				$expandedItem.css({
					width: DDB.constant.ITEM_EXPANDED_WIDTH,
					height: DDB.constant.ITEM_EXPANDED_HEIGHT,
					left: destinationLeft,
					top: destinationTop
				});

				animFinished = true;
				if (!DDB.item.opened && ajaxFinished) {
					loadedContentAndDoneAnimation();
				}
			} else {
				// Animate the overlay in
				$shadeOverlay.animate({
					opacity: 0.8
				}, {
					duration: DDB.constant.ITEM_ANIMATION_SPEED,
					queue: false
				});

				$expandedItem.animate({
					width: DDB.constant.ITEM_EXPANDED_WIDTH,
					height: DDB.constant.ITEM_EXPANDED_HEIGHT,
					left: destinationLeft,
					top: destinationTop
				}, {
					duration: DDB.constant.ITEM_ANIMATION_SPEED,
					easing: DDB.constant.ITEM_ANIMATION_EASING,
					queue: false,
					complete: function() {
						animFinished = true;
						if (!DDB.item.opened && ajaxFinished) {
							loadedContentAndDoneAnimation();
						}
					}
				});
			}
		},
		// END: _open


		_close: function($expandedItem) {
			$item = $expandedItem.data('item');
			$img = $item.find('img');

			var offset = $img.offset();

			var destinationTop = offset.top;
			var destinationLeft = offset.left;
			var height = $('#item-content', $expandedItem).height();


			var itemTitle = $.trim(
				$expandedItem
				.find('.item > h1')
				.clone()	// Clone the element
				.children()	// Select all the children
				.remove()	// Remove all the children
				.end()		// Again go back to selected element
				.text()		// Get the text of element
				);

			// Add the image to the container
			$img = $img.clone();
			$img.css('width', '100%').css('height', '100%');

			var animateBack = function () {
				$('#item-content', $expandedItem).remove();
				$("#item-expanded").html($img);

				$expandedItem.animate({
					"width": DDB.constant.ITEM_EXPANDED_WIDTH,
					"height": DDB.constant.ITEM_EXPANDED_HEIGHT
				}, {
					duration: DDB.constant.ITEM_ANIMATION_SPEED,
					easing: DDB.constant.ITEM_ANIMATION_EASING,
					queue: false
				});

				$expandedItem.animate({
					"width": DDB.constant.ITEM_WIDTH,
					"height": DDB.constant.ITEM_HEIGHT
				}, {
					duration: DDB.constant.ITEM_ANIMATION_SPEED,
					easing: DDB.constant.ITEM_ANIMATION_EASING,
					queue: false
				});

				// Animate the expanded item to the center
				$expandedItem.animate({
					"left": destinationLeft,
					"top": destinationTop
				}, {
					duration: DDB.constant.ITEM_ANIMATION_SPEED,
					easing: DDB.constant.ITEM_ANIMATION_EASING,
					queue: false,
					complete: function() {
						DDB.item.onClose(itemTitle)
					}
				});

				// Remove the overlay
				$('#shade-overlay').stop().animate({
					"opacity": 0
				}, {
					duration: DDB.constant.ITEM_ANIMATION_SPEED,
					queue: false,
					complete: function() {
						$(this).remove();
					}
				});
			};



			if (DDB.common.isMobile()) {
				$('#item-content', $expandedItem).remove();
				$("#item-expanded").html($img);
				DDB.item.onClose(itemTitle);
				$('#shade-overlay').remove();
			} else {
				if (height > DDB.constant.ITEM_EXPANDED_HEIGHT) {
					$($expandedItem).animate({
						height: DDB.constant.ITEM_EXPANDED_HEIGHT
					}, {
						duration: DDB.constant.ITEM_ANIMATION_SPEED,
						complete: animateBack
					});
				} else {
					animateBack();
				}
			}

		},
		// END: _close


		close: function() {
			$expandedItem = $('#item-expanded');
			$(".videoPlayer").remove();
			DDB.item._close($expandedItem);
		},
		// END: close


		onOpen: function($expandedItem) {
			DDB.item.opened = true;
			$("a[href^='http://']").attr("target", "_blank");
			DDB.gallery.init();
			DDB.about.init();

			var itemTitle = $.trim(
				$expandedItem
				.find('.item > h1')
				.clone()	// Clone the element
				.children()	// Select all the children
				.remove()	// Remove all the children
				.end()		// Again go back to selected element
				.text()		// Get the text of element
				);

			DDB.track.event('item-click', 'open', itemTitle);

			DDB.item._trackSharing($expandedItem, itemTitle);
		},
		// END: onOpen


		onClose: function(itemTitle) {
			$expandedItem.remove();

			// Add extra events below
			DDB.track.event('item-click', 'close', itemTitle);

			DDB.item.opened = false;
		},

		// END: onClose

		click: function(event) {

			var $item = $(this);
			DDB.item._open($item);

		}
	// END: click

	},

	/**
	 * Autocomplete Search
	 * On Keyup after 2nd char it should present a list of available entries that match the input
	 */
	search:
	{
		init: function()
		{/*
			$('#search form').submit(function() {
				if ($('#search input').length > 0) {
					document.location = '/' + $('body').attr('id') + '?searchTerm=' + $('#search input').val();
				}

				return false;
			});

			if ($('#search input').length === 0) {
				return;
			}
		*/},

		keyup: function(event) { /*
			if (event.keyCode == DDB.constant.KEY_ESCAPE) {
				$(this).val('').blur();
			}
		*/}
	},

	gallery:
	{
		init: function(){
			$itemContainer = $('.item-expanded');

			if ($itemContainer.length > 0 && $itemContainer.is('.gallery')) {
				DDB.gallery.loadImages($itemContainer);
			}
		},
		// END: init

		loadImages: function($item) {
			$.ajax({
				url: '/gallery?id=' + $item.data('item-id'),
				dataType: 'json',
				success: function(json) {

					var $img = $(".item-img", $item);
					var width = $img.width();
					var offset = $img.position();

					if (json.length > 1) {

						// Add each gallery image to the item.
						$.each(json, function (foo) {
							var $galleryItem = $("<img/>", {
								src: "/content/item/" + this.url,
								className: "item-img",
								css: {
									opacity: 0,
									display: 'none'
								}
							});
							if (this.video) { // YouTube video
								$galleryItem.data("video", this.video);
								$galleryItem.addClass("video");
							}
							// Add the image to the item
							if (foo == 0) {
								$(".item-img", $item).replaceWith($galleryItem);
							} else {
								$(".item-img:last", $item).after($galleryItem);
							}
						});
						$(".item-img:first", $item).css({
							opacity: 1,
							display: "block"
						});

						var img = new Image();
						img.onload = function () {
							DDB.gallery.createPanel($item, $(".item-img:first", $item).height());
						};
						img.src = $(".item-img:first", $item).attr("src");

					}

					$(".item-img.video", $item).bind('click', function () { // show video

						var itemTitle = $.trim(
							$item
							.children('h1')
							.clone()	// Clone the element
							.children()	// Select all the children
							.remove()	// Remove all the children
							.end()		// Again go back to selected element
							.text()		// Get the text of element
							);
						DDB.track.event('gallery', 'video', itemTitle);

						var height = $(this).height();

						var $video = $("<div/>", {
							className: "videoPlayer",
							html: '<div></div><iframe src="http://www.youtube.com/embed/' + $(this).data("video") + '?autohide=1&autoplay=1&rel=0&egm=0&modestbranding=1&wmode=transparent" frameborder="0" allowfullscreen allowTransparency="true" width="80%" height="100%">',
							css: {
								width: width,
								height: height,
								top: offset.top + 10,
								left: offset.left
							}
						});
						$(this).after($video);
					});
				}
			});

		},
		// END: getGalleryImgs

		createPanel: function($item, height) {

			var $galleryImages = $(".item-img", $item);

			$galleryImages.eq(0).addClass('current');

			// Create the panel
			var $panel = $('<div>', {
				className: "panel",
				unselectable: "on",
				css: {
					top: (parseInt(height, 10) / 2)
				}
			//}).css({width: (DDB.constant.ITEM_EXPANDED_WIDTH-20)+'px',left:'10px'});
                        }).css({width: $('.item .current').width()+'px'}); 
			$('.image', $item).append($panel);

			var itemTitle = $.trim(
				$item
				.children('h1')
				.clone()	// Clone the element
				.children()	// Select all the children
				.remove()	// Remove all the children
				.end()		// Again go back to selected element
				.text()		// Get the text of element
				);

			// Create the dot container
			var $galleryPositionDisplay = $('<ul>', {
				className: "galleryPosition"
			});

			// Create the dots
			$galleryImages.each(function (i) {
				$galleryPositionDisplay.append('<li>' + i + '</li>')
			});

			$galleryPositionDisplay.find("li").bind(DDB.constant.TOUCH_EVENT, function () {
				DDB.track.event('gallery', 'dot', itemTitle);
				DDB.gallery.showImage($item, $(this).index());
				return false;
			}).eq(0).addClass("current");

			var $leftArrow = $('<span>', {
				className: "left",
				title: "View previous image"
			});
			$leftArrow.bind(DDB.constant.TOUCH_EVENT, function () {
				DDB.gallery.showImage($item, $(".item-img.current", $item).index() - 1);
				return false;
			});

			var $rightArrow = $('<span>', {
				className: "right",
				title: "View next image"
			});
			$rightArrow.bind(DDB.constant.TOUCH_EVENT, function () {
				DDB.gallery.showImage($item, $(".item-img.current", $item).index() + 1);
				return false;
			});


			$panel.append($galleryPositionDisplay).append($leftArrow).append($rightArrow);

			if (!DDB.common.isMobile()) {
				// Hover over Arrow
				$rightArrow.hover(
					// IN
					function ()
					{
						$(this).addClass("over");
					},
					// OUT
					function () {
						$(this).removeClass("over");
					}
					);

				// Hover over Panel
				$('.image', $item).hover(
					// IN
					function ()
					{
						$panel.fadeTo(500, 1);
						setTimeout(function() {
							$panel.stop().fadeTo(500, 0);
						}, 6000);
					},
					// OUT
					function () {
						$panel.stop().fadeTo(500, 0);
					}
					);

				var i = null;

				// Mousemove to regenerate it
				$('.image', $item).mousemove(function() {
					clearTimeout(i);
					$panel.stop().fadeTo(100, 1);
					i = setTimeout(function() {
						$panel.fadeTo(500, 0);
					}, 6000);
				}).mouseleave(function() {

					if (!$rightArrow.hasClass('over')) {
						clearTimeout(i);
						$panel.stop().fadeTo(500, 0);
					}
				});

				// Initial timeout to hide the panel
				if (!$rightArrow.hasClass('over')) {
					setTimeout(function() {
						$panel.fadeTo(500, 0);
					}, 1000);
				}

			} else {
				$panel.css('opacity', 1);
			}
		},
		// END: createPanel

		showImage: function($item, index) {

			if (index < 0) {
				index = $(".item-img", $item).length - 1;
			}

			if (index > $(".item-img", $item).length - 1) {
				index = 0;
			}

			$(".galleryPosition .current").removeClass("current");

			if (DDB.common.isMobile()) {
				$(".item-img.current", $item).css('opacity', 0).removeClass("current").hide();

				$(".item-img", $item).eq(index).css('opacity', 1).addClass("current").show();

				// Update the dot indicator
				$(".galleryPosition li").eq(index).addClass("current");
			} else {
				$(".item-img.current", $item).fadeTo(250, 0, function() {

					$(this).removeClass("current").hide();
					$(".item-img", $item).eq(index).show().addClass("current").fadeTo(250, 1);

					// Update the dot indicator
					$(".galleryPosition li").eq(index).addClass("current");

				});
			}

			$(".videoPlayer", $item).remove(); // remove YouTube vid if exists
		}
	// END: showImage

	},
	// END: gallery

	about: {
		init: function () {
			if ($(".item-expanded.about").length) {
				if (!DDB.common.isMobile() || DDB.common.isTablet()) {
					if (typeof google == "undefined" || typeof google.maps == "undefined") {
						$.getScript("http://maps.google.com/maps/api/js?sensor=false&callback=DDB.about.loadMap");
					} else {
						DDB.about.loadMap();
					}
				} else {
					$('#map').hide();
				}
			}
		},

		loadMap: function () {

			function DDBInfoWindow(latLng, content, map) {
				this.latLng_ = latLng;
				this.content_ = content;
				this.map_ = map;
				this.div_ = null;
				this.setMap(map);
			}

			DDBInfoWindow.prototype = new google.maps.OverlayView();

			DDBInfoWindow.prototype.onAdd = function() {
				var div = document.createElement("DIV");
				div.id = "infowindow";

				var img = document.createElement("IMG");
				if ($("body#tribalddb").length) {
					img.src = "/assets/img/logo-tribal.jpg";
				} else if ($("body#tribalspark").length) {
					img.src = "/assets/img/logo-spark.jpg";
				} else {
					img.src = "/assets/img/logo.jpg";
				}

				div.appendChild(img);

				div.innerHTML += "<p>" + this.content_ + "</p>";

				this.div_ = div;

				var panes = this.getPanes();
				panes.overlayLayer.appendChild(div);
			};

			DDBInfoWindow.prototype.draw = function() {
				var overlayProjection = this.getProjection();
				var point = overlayProjection.fromLatLngToDivPixel(this.latLng_);
				var div = this.div_;
				div.style.left = (point.x - 230) + 'px';
				div.style.top = (point.y - 165) + 'px';
			};

			var map = new google.maps.Map(document.getElementById("map"), {
				zoom: 17,
				center: new google.maps.LatLng(DDB.constant.MAP_LAT, DDB.constant.MAP_LNG),
				mapTypeId: google.maps.MapTypeId.SATELLITE
			});
			var infowindow = new DDBInfoWindow(new google.maps.LatLng(DDB.constant.MAP_LAT, DDB.constant.MAP_LNG), DDB.constant.MAP_ADDRESS, map);

		}
	},
        
        gridCentrer: {
            init: function(){
                if($('#item-grid').length > 0){
                    this.setupResizer(); 
                }
            },
            min_item_line: 4,
            min_width_line: 200,
            setupResizer: function(){ 
                DDB.gridCentrer.theeResizer();
                $(window).resize(DDB.gridCentrer.theeResizer);
            },
            theeResizer: function(){ 
                var parent = parseInt($('#content').width());
                var medium = (parseInt($('#item-grid .item').width())+parseInt($('#item-grid .item').css('margin-right')));
                var cluster = ((parent/medium));
                var item_line = Math.floor(cluster);
                if($('#item-grid .item').length < item_line){
                    item_line = parseInt($('#item-grid .item').length);
                }
                var set_width = (medium*item_line);
                if(item_line < DDB.gridCentrer.min_item_line){
                    set_width = (medium*DDB.gridCentrer.min_item_line);
                }
                var use_class = '#item-grid, #header, #nav';

                if(DDB.common.isMobile() && DDB.common.isTablet()){
                    use_class = '#content, #header, #nav';
                    set_width = (medium*3);
                     if($(window).width() < 321){
                        set_width = 320;
                    }else if($(window).width() < 534){
                         set_width = 520;
                    }
                }
                $(use_class).css({width:set_width+'px',margin:'0 auto'});
                $('#content, #nav').css('padding-left','0px');
            }
        }

};


$(document).ready(DDB.common.init);
