var globalFunction={};


function flashCall() {
	globalFunction.disabling();
}		

$(document).ready(function(){
	$('#myslides').cycle();
	
	
	
	
	
	
	// DIV CORRECTOR
	var contentHeight=document.getElementById("content").offsetHeight;
	
	var myInterval = setInterval(function () {

		if ((contentHeight+253)<$(document).height()) {
			var difference=$(document).height()-(contentHeight+253);
			document.getElementById("content").style.height=(contentHeight-50)+difference+"px";
		}

		
	},100);
	
 
	

	window.onresize = function(event) { 
		contentHeight=document.getElementById("content").offsetHeight;
		
		if ((contentHeight+253)<$(document).height()) {
			var difference=$(document).height()-(contentHeight+253);
			
			document.getElementById("content").style.height=(contentHeight-50)+difference+"px";
		}
	} 

	
	
	
	
	
	
	
	
	

	// GOOGLE MAPS SETUP


	function initialize_google() {
	
	     var latlng = new google.maps.LatLng(60.169610, 24.929438);
	     var myOptions = {
			zoom: 14,
			center: latlng,
			mapTypeControl: false,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		
		
		
	
		var image = 'http://maps.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png';
		var infoWindow = new google.maps.InfoWindow();
		
		
		
		
	
		var marker_acm = new google.maps.Marker({
			position: new google.maps.LatLng(60.168082, 24.935368),
			map: map,
			icon: image,
			title: 'ACM'
		});
		
		var html_acm = '<p style="margin-bottom: 0px;"><img style="margin-top: 10px; margin-bottom: 10px;" src="/logo_acm.jpg" alt="" /><br/>Annankatu 31-33 C 45b<br/>00100<br/>Helsinki</p>';
		
		google.maps.event.addListener(marker_acm, 'click', function() {
			infoWindow.setContent(html_acm);
			infoWindow.open(map, marker_acm);
		});


		aco_link=document.getElementById("map_acm");
		google.maps.event.addDomListener(aco_link, 'click', function() {
			hideMarkers(1);
			infoWindow.setContent(html_acm);
			infoWindow.open(map, marker_acm);
		});


		it.setMap(map);
					
	};
		
		
		
		
		
		
		
		
		
		
		
		
			
	// EMAIL SPAM-PROTECTION
		
	$(".email").each(function(){

		var email = $(this).html();
		var parts = email.split(" (at-merkki) ");
		var re = /([a-z0-9\._-]+)/i;
		for(i in parts){
			parts[i] = $.trim(parts[i]);
		}
		$(this).html('<a href="mailto:'+ parts[0] +'@'+ parts[1] +'" title="Lähetä sähköpostia: '+ parts[0] +'@'+ parts[1] +'">'+ parts[0] +'@'+ parts[1] +'</a>');
	});		
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	// ROLLOVER FUNCTION
	
	var menuArray = document.getElementsByTagName('img');

	for (i=0;i<menuArray.length;i++) {
			
		if(menuArray[i].className=="rollOver") {
			menuArray[i].onmouseover = function()
			{
				var target=this.getAttribute('src').slice(0,-5)+"2."+this.getAttribute('src').slice(-3);
				this.setAttribute('src',target);
			};
			
			menuArray[i].onmouseout = function()
			{
				var target=this.getAttribute('src').slice(0,-5)+"1."+this.getAttribute('src').slice(-3);
				this.setAttribute('src',target);
			};
		}

	}











	// GOOGLE MAPS INIT

	var checkIfMapExists = document.getElementById("map_canvas");

	if (checkIfMapExists!=null) {
		initialize_google();
	}		
	
	
	
	
	
	
	// FLASH CHECK
	
	var checkIfRakennusalaFlashExists = document.getElementById("flashContent_rakennusala");

	if (checkIfRakennusalaFlashExists!=null) {
		var vars = {};
		var params = { scale:'noScale', salign:'lt', menu:'false' };
		var attributes = { name:'acm' }; // give an id to the flash object

		swfobject.embedSWF("http://www.acm.fi/core/wordpress/wp-content/themes/acm/header_video_rakennusala.swf", "flashContent_rakennusala", "851", "297", "9.0.0", "js/expressInstall.swf", vars, params, attributes );
	}
	
	
	
	
	
	
	
	/***************************/
	//@Author: Adrian "yEnS" Mato Gondelle
	//@website: www.yensdesign.com
	//@email: yensamg@gmail.com
	//@license: Feel free to use it, but keep this credits please!					
	/***************************/

	//SETTING UP OUR POPUP
	//0 means disabled; 1 means enabled;
	var popupStatus = 0;

	//loading popup with jQuery magic!
	function loadPopup(){
		//loads popup only if it is disabled
		if(popupStatus==0){
		
		
			var checkIfLemminkainenFlashExists = document.getElementById("flashContent_lemminkainen");

			if (checkIfLemminkainenFlashExists!=null) {
				var vars = {};
				var params = { scale:'noScale', salign:'lt', menu:'false', wmode:"transparent" };
				var attributes = { name:'acm' }; // give an id to the flash object

				swfobject.embedSWF("http://www.acm.fi/core/wordpress/wp-content/themes/acm/lemminkainen.swf", "flashContent_lemminkainen", "840", "510", "9.0.0", "js/expressInstall.swf", vars, params, attributes );
			}
			
			
			
			
			var checkIfKnaufFlashExists = document.getElementById("flashContent_knauf");

			if (checkIfKnaufFlashExists!=null) {
				var vars = {};
				var params = { scale:'noScale', salign:'lt', menu:'false', wmode:"transparent" };
				var attributes = { name:'acm' }; // give an id to the flash object

				swfobject.embedSWF("http://www.acm.fi/core/wordpress/wp-content/themes/acm/knauf.swf", "flashContent_knauf", "600", "510", "9.0.0", "js/expressInstall.swf", vars, params, attributes );
			}
			
			
			
			
			
			var checkIfKIMALDUCTFlashExists = document.getElementById("flashContent_kimalduct");

			if (checkIfKIMALDUCTFlashExists!=null) {
				var vars = {};
				var params = { scale:'noScale', salign:'lt', menu:'false', wmode:"transparent" };
				var attributes = { name:'acm' }; // give an id to the flash object

				swfobject.embedSWF("http://www.acm.fi/core/wordpress/wp-content/themes/acm/kim-al-duct.swf", "flashContent_kimalduct", "840", "510", "9.0.0", "js/expressInstall.swf", vars, params, attributes );
			}
			

		
		
			$("#backgroundPopup").css({
				"opacity": "0.7"
			});
			$("#backgroundPopup").fadeIn("slow");
			$("#popupContact").fadeIn("slow");
			popupStatus = 1;
		}
	}

	//disabling popup with jQuery magic!
	function disablePopup(){
		//disables popup only if it is enabled
		if(popupStatus==1){
		
			var checkIfLemminkainenFlashExists = document.getElementById("flashContent_lemminkainen");
			var checkIfKnaufFlashExists = document.getElementById("flashContent_knauf");
			
			if (checkIfLemminkainenFlashExists!=null) {
				swfobject.removeSWF("flashContent_lemminkainen");
				$("#flashContent_wrapper").prepend('<div id="flashContent_lemminkainen"></div>');
			}
			
			if (checkIfKnaufFlashExists!=null) {
				swfobject.removeSWF("flashContent_knauf");
				$("#flashContent_wrapper").prepend('<div id="flashContent_knauf"></div>');
			}
			
			
			$("#backgroundPopup").fadeOut("slow");
			$("#popupContact").fadeOut("slow");
			popupStatus = 0;
		}
	}
	
	
	globalFunction.disabling=function() {
		disablePopup();
	};
	


	//centering popup
	function centerPopup(){
		//request data for centering
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = $("#popupContact").height();
		var popupWidth = $("#popupContact").width();
		//centering
		$("#popupContact").css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
		});
		//only need force for IE6
		
		$("#backgroundPopup").css({
			"height": windowHeight
		});
		
	}



		
	//LOADING POPUP
	//Click the button event!
	$("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});


	
	
	
	
	
	
	
	
	
	



	//SLIDE SHOW INIT

	$(".slideshow").SlideShowManager();












	//LIGHTBOX SELECTOR
	
	// Use this example, or...
	//$('a[@rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel
	// This, or...
	$('#lightbox a').lightBox(); // Select all links in object with gallery ID
	// This, or...
	//$('a.lightbox').lightBox(); // Select all links with lightbox class
	// This, or...
	//$('a').lightBox(); // Select all links in the page
	// ... The possibility are many. Use your creative or choose one in the examples above



});
//DOCUMENT READY -FUNKTIO LOPPUU







//SLIDESHOW SETUP


jQuery.fn.SlideShowManager = function() {
    return this.each(function(){
	new SlideShow(this);
  });
};
 
 
function SlideShow(base){
  var self = this;
 
  self.currentPosition = 0;
  self.slideWidth = 413;
  self.slides = $('.slide', base);
  self.numberOfSlides = this.slides.length;
  self.base = base;
  self.direction = 1;
  self.Timer = null;
 
 
  self.slides
    .wrapAll('<div class="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({
	    'float' : 'left',
	    'width' : self.slideWidth
    });
 
  $('.slidesContainer',base).css('overflow', 'hidden');
  $('.slideInner',base).css('width', self.slideWidth * self.numberOfSlides);
 
 
  //$('.slidesContainer',base).prepend ('<div class="autoplayControl">PLAY</div>');
 
  $(base)
  .prepend('<span class="leftControl">Move left</span>')
  .append('<span  class="rightControl">Move right</span>');
 
  $('.leftControl', base).bind('click', $.proxy(self.onLeftControlClick, self));
  $('.rightControl', base).bind('click', $.proxy(self.onRightControlClick, self));
  $('.autoplayControl',base).bind('click', $.proxy(self.onAutoPlayControlClick, self));
 
  self.manageControls();
}
 
	  SlideShow.prototype.manageControls = function(){
	    var self = this;
		if (self.currentPosition == 0){
		    $('.leftControl', self.base).hide();
		self.direction = +1;
		}
		else
		{
		    $('.leftControl', self.base).show();
		}
 
		if (self.currentPosition == (self.numberOfSlides - 1)){
		    $('.rightControl', self.base).hide();
		self.direction = -1;
		}
		else
		{
		    $('.rightControl', self.base).show();
		}
	  }
 
	  SlideShow.prototype.animate = function(){
		var self = this;
	    $('.slideInner', self.base).animate({ 'marginLeft' : self.slideWidth * (-self.currentPosition)});
	  }      
 
	  SlideShow.prototype.onLeftControlClick = function(){
	    //for( k in this)   console.log(k);
	    var self = this;
		self.currentPosition--;
		self.manageControls();
		self.animate();
	  }
 
	  SlideShow.prototype.onRightControlClick = function(){
	    //for( k in this)   console.log(k);
	    var self = this;
		self.currentPosition++;
		self.manageControls();
		self.animate();
	  }
 
	  SlideShow.prototype.autoAnimate = function(){
	    var self = this;
	    if (self.direction == 1){
		self.currentPosition++;
	    }
	    else
	    {
		self.currentPosition--;
	    }
	    //for (k in o) console.log(k);
	    self.manageControls();
	    self.animate();
	  }  
 
	SlideShow.prototype.onAutoPlayControlClick = function(){
	    var self = this;
 
	    if (self.Timer == null){
		self.Timer = window.setInterval(function(){
							self.autoAnimate();}
						,4000);
		$('.autoplayControl',self.base).html("STOP");
	    }else
	    {
		window.clearInterval(self.Timer);
		self.Timer = null;
		$('.autoplayControl',self.base).html("PLAY")
	    }
	}











