$(document).ready(function(){
	
	
	//	if #slideshow, init cycle
	if ($('#slideshow').length != 0) {
		initCycle();
	}
	
	//	if #map, init google map
	if($('#map').length != 0) {
		initMap();
	}
	
	if ( $("form").length > 0 ) {
		initForm();
	}
	
});



/*
	jQuery Cycle
*/

function initCycle(data) {

	$("#slideshow .left").addClass("js");
	$("#slideshow .right").addClass("js");
	$('#slideshow #slides').cycle({ 
  		fx : 'scrollHorz'
  	,	speed : 900
  	,	timeout : 8000             
  	,	easing: 'easeOutCubic'
  	, prev:    '#slideshow .left' 
    , next:    '#slideshow .right' 
  	, pager:      '#slideshow .footer'
    , pagerEvent: 'click'

	});
}






/* 
	GOOGLE MAPS
*/

var _map;
var _marker;
var _latlng = new google.maps.LatLng(60.155069,24.878895);
var _infowindow = new google.maps.InfoWindow();

function initMap() {

	var myOptions = {
		zoom: 12,
		center: _latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		sensor:false
	};
	_map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	setTimeout('markify()', 1000);
}

function markify() {
	_marker = new google.maps.Marker({
    map:_map,
    draggable:false,
    animation: google.maps.Animation.DROP,
    position: _latlng,
    title:'We Are Here'
  });
}



var _form = {};
var _required = ['name', 'email'];

function initForm () {
	$("form .text").each(function(){
		_form[$(this).attr("name")] = $(this).val();
		$(this).focus(function() {
			if ($(this).val() == _form[$(this).attr("name")]) 
				$(this).val('');
		});
		
		$(this).blur(function() {
			if ($(this).val() == '') 
				$(this).val(_form[$(this).attr("name")]);
		});
		
	});
	
	$("form").submit(function(e){
		
		$(this).find(".text").each(function() {
			if ($(this).attr("name") in oc(['name', 'email']))  {
				if ( $(this).val() == _form[$(this).attr("name") || $(this).val()==''] ) {
					//alert("Please fill Name and E-Mail");
					$('#content .column form .error').html('Please fill Name and E-Mail');
					e.preventDefault();
					return false;
				}
				
				if ($(this).attr("name")=='email' && !validEmail($(this).val())) {
					//alert("Not a valid e-mail address");
					$('#content .column form .error').html('Not a valid e-mail address');
					e.preventDefault();
					return false;
				}	
				
				$('#content .column form .error').html('');	
			}
		});
	});
	
}


function oc (a) {
  var o = {};
  for( var i=0, l=a.length; i<l; i+=1 ) {
    o[a[i]]='';
  }
  return o;
}

// php checks, in more detail, this is jut to see it's approx the right format..
// code taken from w3school, and modified a bit
function validEmail (email) {
	var atpos = email.indexOf("@");
	var dotpos = email.lastIndexOf(".");
	if (atpos<1 || dotpos<atpos+2 || dotpos+2 >= email.length) {
		return false;
	}
	
	return true;
}
