var debug = true;
var address;
var latitude;
var longitude;

jQuery(document).ready(function(){
	boarea.init();
});

var boarea = new BoArea();


function BoArea(){
	this.map;
	
	this.init = function(){
		try{
			this._initMenu();
			this._initSlider();
			this._initColorBox();
			this._initContactForm();
		}catch(ex){ if(debug) alert(ex.message); }
	};
	
	this._initColorBox = function(){
		$(".colorbox-action").each(function(i, elm){
			elm = $(elm);
			elm.colorbox({initialWidth: 100, initialHeight:100, opacity:0,transition:'none',title:elm.attr('title') ,close:'x',  inline:!elm.hasClass("not-inline"), href:elm.attr('href')});
		});
		
		$('a[rel^=attachment]').each(function(i, elm){
			elm = $(elm);
			if(elm.find('img').length){
				try{
					elm.colorbox({initialWidth: 100, initialHeight:100, opacity:0,transition:'none',title:elm.find("img").attr('title') + "&nbsp;" ,close:'x', href:elm.find('img').attr('src').replace(/-[0-9]{2,3}x[0-9]{2,3}/i, '')});
				}catch(ex){ if(debug) alert(ex.message); }
			}
		});
	};
	
	this._initContactForm = function(){
		this._initHints('.mmf', true);
	};
	
	// Inits hints for input fields and textfields
	this._replaceDefaultValueToSpan = function(selector){
		try{
			jQuery(selector).find('input[type=text], textarea').not('input.dont-use-hints, textarea.dont-use-hints').each(function(i, element){
				element = jQuery(element);
				var value = jQuery.trim(element.val());
				var title = element.attr('title');
				element.parent().css("position", "relative");
				element.parent().prepend('<span class="default-value" style="position:absolute; width:100%; padding:0 4px; top:0; left:0;">' + value + '</span>');
				element.val("");
				element.parent().find('.default-value').click(function(){
					$(this).hide();
					element.focus();
				});
			});
		}catch(ex){ if(debug) alert(ex.message); }
	};
	this._initHints = function(selector, replaceDefaultValueToSpan){
		try{
			if(replaceDefaultValueToSpan == undefined)
				replaceDefaultValueToSpan = false;
				
			// Reset forms
			jQuery("form").each(function(i, elm){
				elm.reset();
			});
			
			if(replaceDefaultValueToSpan)
				this._replaceDefaultValueToSpan('.mmf');
			
			jQuery(selector).find('input[type=text], textarea').not('input.dont-use-hints, textarea.dont-use-hints').each(function(i, element){
				element = jQuery(element);
				var value = jQuery.trim(element.val());
				var title = element.attr('title');
				if(title.toLowerCase() == value.toLowerCase() || value == ""){
					var defaultspan = element.parent().find('.default-value');
					if(defaultspan.length){
						defaultspan.show();
						element.val("");
					}else
						element.val(title);
				}
				element.focus(function(){
					var target = jQuery(this);
					var value = jQuery.trim(target.val());
					var title = target.attr('title');
					if(title.toLowerCase() == value.toLowerCase()){
						var defaultspan = element.parent().find('.default-value');
						if(defaultspan.length){
							defaultspan.hide();
							element.val("");
						}else
							target.val("");
					}
				});
				element.click(function(){
					this.select();
				});
				element.blur(function(){
					var target = jQuery(this);
					var value = jQuery.trim(target.val());
					var title = target.attr('title');
					if(title.toLowerCase() == value.toLowerCase() || value == ""){
						var defaultspan = element.parent().find('.default-value');
						if(defaultspan.length){
							defaultspan.show();
							element.val("");
						}else
							target.val(title);
					}			
				});
			});
		}catch(ex){ if(debug) alert(ex.message); }
	};
	
	this._initMenu = function(){
		try{
			jQuery('#menu li ul').each(function(i, element){
				jQuery(element).closest('li').bind({
						mouseover: function(){
							jQuery('li.mouseover').not(this).removeClass("mouseover");
							jQuery(this).addClass("mouseover");
						},
						mouseout: function(){
							jQuery(this).removeClass("mouseover");
						}
					}
				);
			});
		}catch(ex){ if(debug) alert(ex.message); }
	};
	
	this._initSlider = function(){
		try{
			$('#slides').slides({
				preload: true,
				preloadImage: 'includes/images/loading.gif',
				play: 5000,
				pause: 1500,
				hoverPause: true,
				animationStart: function(current){
					$('.caption-wrapper .caption').animate({
						bottom:0
					},100);
					if (window.console && console.log) {
						// example return of current slide number
						console.log('animationStart on slide: ', current);
					};
				},
				animationComplete: function(current){
					current--;
					var clone = $('.slide .caption:eq('+(current)+')').clone();
					$('.caption-wrapper').empty().append(clone);
					
					$('.caption-wrapper .caption').animate({
						bottom:0
					},200);
					if (window.console && console.log) {
						// example return of current slide number
						console.log('animationComplete on slide: ', current);
					};
				},
				slidesLoaded: function() {
					$('.caption-wrapper').empty().append($('.slide .caption:first').clone());
					
					$('.caption-wrapper .caption').animate({
						bottom:0
					},0);
				}
			});
			if($('#slides .slide').length == 1){
				$('#slides .slides_container').show();
				jQuery('#slides').addClass('disabled');
			}
			
		}catch(ex){ if(debug) alert(ex.message); }
	};
	
	this.switchBetweenMapAndSlideShow = function(caller, map_label, slideshow_label){
		try{
			var scope = this;
			
			var slides = jQuery('#slides');
			if(slides[0].mapvisible != true){
				slides.addClass('disabled');
				jQuery('#slideshow #map').show();
				jQuery(caller).find('span').text(slideshow_label);
				
				if(this.map == undefined){
					var latlng = new google.maps.LatLng(60.128161,18.643501);
					var myOptions = {
					  zoom: 14,
					  scrollwheel: false,
					  center: latlng,
					  mapTypeId: google.maps.MapTypeId.ROADMAP
					}
					this.map = new google.maps.Map(document.getElementById("map"), myOptions);
					
					if(address != undefined){
						var geocoder = new google.maps.Geocoder();
						geocoder.geocode( { 'address': address}, function(results, status) {
						  if (status == google.maps.GeocoderStatus.OK) {
							scope.map.setCenter(results[0].geometry.location);
							var marker = new google.maps.Marker({
								map: scope.map, 
								position: results[0].geometry.location
							});
							setTimeout(function(){
								$.scrollTo(jQuery("#slideshow"), 500, {easing:'easeOutQuint', offset: {top:-20}});
							}, 300);
						  } else {
							/* Silent error */
							$.scrollTo(jQuery("#slideshow"), 500, {easing:'easeOutQuint', offset: {top:-20}});
						  }
						});
					}else{
						var latlng = new google.maps.LatLng(latitude, longitude);
						scope.map.setCenter(latlng);
						var marker = new google.maps.Marker({
							map: scope.map, 
							position: latlng
						});
						$.scrollTo(jQuery("#slideshow"), 500, {easing:'easeOutQuint', offset: {top:-20}});
					}
				}else{						
					$.scrollTo(jQuery("#slideshow"), 500, {easing:'easeOutQuint', offset: {top:-20}});	
				}
				slides[0].mapvisible = true;
			}else{
				slides.removeClass('disabled');
				jQuery('#slideshow #map').hide();
				jQuery(caller).find('span').text(map_label);
				slides[0].mapvisible = false;
				
				$.scrollTo(jQuery("#slideshow"), 500, {easing:'easeOutQuint', offset: {top:-20}});	
			}
		
		}catch(ex){ if(debug) alert(ex.message); }
	};
	
}




















