var imgInd = 1;
var imgCnt = 0;
var timeout = false;
var animationTimes = 500;


$(function(){
	$('.colorbox').colorbox(
		{
			width:"50%",
			inline:true,
			href:"#contactcb",
			onComplete: function(){map.checkResize(); map.setCenter(new google.maps.LatLng(60.16201, 24.903871), 15);}
		}
	);
	
	$('.images li img').click(function(){
		var me = $(this);
		if(me.attr("id").length <= 2) return;
		var ind = getImgInd(me);
		imgInd = ind + 1;
		scrollToImg(ind + 1, true);
	});
	
	$('#ar_r').click(function(){
		nextImg();
	});

	$('#ar_l').click(function(){
		prevImg();
	});
	
	$('.images li:first img').load(function(){scrollToImg(imgInd, false);});
	resizeImagesToWindow();
});

function getImgInd(imgObj){
	if(imgCnt <= 0) return;
	return parseInt(imgObj.attr("id").replace("img", "")) - 1;
}

$('html,body').keydown(function(eve){
	if (eve.keyCode == '37') {
		prevImg();
	}else if(eve.keyCode == '39') {
		nextImg();
	}else if(eve.keyCode == '35') {
		lastImg();
	}else if(eve.keyCode == '36') {
		firstImg();
	}

});

$(window).resize(function() {
	if(timeout !== false) clearTimeout(timeout);
	timeout = setTimeout(resizeImagesToWindow, 50);
});




function resizeImagesToWindow(){
	var wH = $(window).height();
	//if(wH < 680){
	if(wH < 880){
		$('#ar_l').animate({"top" : $('.out').height() + 70 + "px"}, 200);
		$('#ar_r').animate({"top" : $('.out').height() + 70 + "px"}, 200);
	}else{
		$('#ar_l').css("top", "50%");
		$('#ar_r').css("top", "50%");
	}
	
	if(imgCnt <= 0) return;
	$('.imagecontainer').css("width", $(window).width() + "px");
	$('.images li img').css("height", (wH) + "px");
	scrollToImg(imgInd, false);
}

function nextImg(){
	if(imgCnt <= 0) return;
	if(imgInd >= imgCnt) return;
	imgInd++;
	scrollToImg(imgInd, true);
}

function prevImg(){
	if(imgCnt <= 0) return;
	if(imgInd <= 1) return;
	imgInd--;
	scrollToImg(imgInd, true);
}

function firstImg(){
	if(imgCnt <= 0) return;
	imgInd = 1;
	scrollToImg(1, true);
}

function lastImg(){
	if(imgCnt <= 0) return;
	imgInd = imgCnt;
	scrollToImg(imgCnt, true);
}


function scrollToImg(ind, anim){
	if(imgCnt <= 0) return;
	var img = $('.images li:nth-child(' + (ind) + ') img');
	var img2 = $('.images li:nth-child(' + (ind + 1) + ') img');
	
	var selLi = $('.images li:nth-child(' + (ind + 1) + ')');
	var msg = selLi.find('div').html();

	$('.images li img').removeClass('curimg');
	img2.addClass('curimg');

	if(ind <= 1){
		$('#ar_l').animate({opacity: "0"}, animationTimes);
	}else $('#ar_l').animate({opacity: "1"}, animationTimes);

	if(ind >= imgCnt){
		$('#ar_r').animate({opacity: "0"}, animationTimes);
	}else $('#ar_r').animate({opacity: "1"}, animationTimes);


//	if(!msg) msg = "";
	var w = Math.floor(($(window).width() / 2));

	var le = Math.floor(img.offset().left + $('.imagecontainer').scrollLeft());
	le -= w;
	le += Math.floor(img.width() / 2);
	le += img.width();
	
	//alert(img + '\n' + img.offset().left + '\n' + le + '\n' + ind);	
	
	
	if((!msg) || msg.length <= 0){
		$('#msg').hide(200);
		$('#msg').html("");
	}else{
		$('#msg').show(200);
		$('#msg').html(msg);
	}
	//$('.msg').html(ind);
	
	$('.images li img').each(function(){
		var me = $(this);
		var _ind = getImgInd(me);
		if(ind != (_ind + 1)){
			me.animate({opacity: "0.2"}, animationTimes);
		}else{
			me.animate({opacity: "1"}, animationTimes);
		}
	});
	
	if(anim){
		$('.imagecontainer').animate({scrollLeft : le}, animationTimes);
	}else{
		$('.imagecontainer').scrollLeft(le);
	}
}


/* MAP */
var points = [];
var map = null;
if (GBrowserIsCompatible()) { 


	google.load("maps", "2",{"other_params":"sensor=false"});
	function initialize() {
			map = new google.maps.Map2(document.getElementById("map"));
				map.enableScrollWheelZoom();
				map.setCenter(new google.maps.LatLng(60.16201, 24.903871), 15);
				map.addControl(new GLargeMapControl());
				map.addControl(new GMapTypeControl());
				
				var point = new GLatLng(60.16201, 24.903871);
				var marker = createMarker(point,'<strong>Studio Merikallio</strong><br>Cable Factory<br>Tallberginkatu 1 B, 3rd floor<br>00180 Helsinki<br>Finland');
				map.addOverlay(marker);
				points[points.length] = marker;
	}
	google.setOnLoadCallback(initialize);
}

function createMarker(point,html) {
      var blueIcon = new GIcon(G_DEFAULT_ICON);
	//blueIcon.image = "http://www.ifsociety.com/tommi/anttiicon.png";
	var markerOptions = { icon:blueIcon };
	    
	    var marker = new GMarker(point, markerOptions);
	    marker.html = html;
	    marker.bindInfoWindowHtml(html);
	GEvent.addListener(marker, "click", function() {
    	  //marker.openInfoWindow();
    	});
    	return marker;
		}
function goto(p){
	if(p > points.length) return;
	map.setCenter(points[p-1].getLatLng(), 15);
	points[p-1].openInfoWindow(points[p-1].html);
}
