$(function() {
	if ($(".gmap").length > 0) loadGmap();
    if ($(".show").length > 0) {
        $(".shownavibutton").click(function() {
            var buttonindex = $(".shownavibutton").index(this) + 1;
            console.log(buttonindex);
            showFrame(buttonindex, true);
        });
        nextShowFrame();
    }
	fixHeight();
});

function loadGmap()
{
	$('.gmap').gMap(
	{
         latitude: 60.252754,
         longitude: 24.936173,
	     maptype: 'ROADMAP',
	     zoom: 10,
	     controls: {
	         panControl: false,
	         zoomControl: true,
	         mapTypeControl: false,
	         scaleControl: false,
	         streetViewControl: false,
	         overviewMapControl: false
	     },
	     markers:[
	      		{
                    latitude: 60.192754,
                    longitude: 24.936173,
					html: "<div>Alppilan Autohuolto Oy</div><div>Karjalankatu 2</div><div>puh 075 326 5510</div>"
	      		},
				{
                    //<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.fi/maps?f=q&amp;source=s_q&amp;hl=fi&amp;geocode=&amp;q=takkatie+7,+Helsinki&amp;aq=&amp;sll=60.192754,24.936173&amp;sspn=0.008842,0.01929&amp;vpsrc=0&amp;ie=UTF8&amp;hq=&amp;hnear=Takkatie+7,+00370+Helsinki&amp;t=m&amp;z=14&amp;ll=60.222909,24.854695&amp;output=embed"></iframe><br /><small><a href="http://maps.google.fi/maps?f=q&amp;source=embed&amp;hl=fi&amp;geocode=&amp;q=takkatie+7,+Helsinki&amp;aq=&amp;sll=60.192754,24.936173&amp;sspn=0.008842,0.01929&amp;vpsrc=0&amp;ie=UTF8&amp;hq=&amp;hnear=Takkatie+7,+00370+Helsinki&amp;t=m&amp;z=14&amp;ll=60.222909,24.854695" style="color:#0000FF;text-align:left">Näytä suurempi kartta</a></small>
	      			latitude: 60.222909,
	      			longitude: 24.854695,
					html: "<div>Alppilan Autohuolto Oy</div><div>Takkatie 7, Pitäjänmäki</div><div>puh 075 326 5515</div></div>"
				},
                {
                    //<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.fi/maps?f=q&amp;source=s_q&amp;hl=fi&amp;geocode=&amp;q=ansatie+2,+vantaa&amp;aq=&amp;sll=60.222909,24.854695&amp;sspn=0.008834,0.01929&amp;vpsrc=0&amp;ie=UTF8&amp;hq=&amp;hnear=Ansatie+2&amp;t=m&amp;z=14&amp;ll=60.295871,24.930422&amp;output=embed"></iframe><br /><small><a href="http://maps.google.fi/maps?f=q&amp;source=embed&amp;hl=fi&amp;geocode=&amp;q=ansatie+2,+vantaa&amp;aq=&amp;sll=60.222909,24.854695&amp;sspn=0.008834,0.01929&amp;vpsrc=0&amp;ie=UTF8&amp;hq=&amp;hnear=Ansatie+2&amp;t=m&amp;z=14&amp;ll=60.295871,24.930422" style="color:#0000FF;text-align:left">Näytä suurempi kartta</a></small>
                    latitude: 60.295871,
                    longitude: 24.930422,
                    html: "<div>Vistema Oy</div><div>Ansatie 2, Vantaa</div><div>puh 075 326 5525</div></div>"
                }
	      	]
	});
}

function fixHeight()
{
    if ($(".vasen").length > 0 && $(".keski").length > 0 && $(".oikea").length > 0) {
        var oh = $(".oikea").height();
        var vh = $(".vasen").height();
        if (oh > vh) $(".vasen").height(oh);
        else $(".oikea").height(vh);
    }
}

function nextShowFrame()
{
    $(".show").oneTime(3000, function() {
       var currentindex = $(".showframe").not(".hidden").index() + 1;
       var nextindex = currentindex+1 > $(".showframe").length ? 1 : currentindex + 1;
       showFrame(nextindex);
       nextShowFrame();
    });
}

function showFrame(frameindex, removetimer)
{
   if (removetimer) $(".show").stopTime();
   $(".showframe").addClass("hidden");
   $(".showframe:nth-child(" + frameindex + ")").removeClass("hidden");
   $(".shownavibutton").removeClass("active");
   $(".shownavibutton:nth-child(" + frameindex + ")").addClass("active");
}
