$(document).ready(function() {
    
    $("#imgPreview li img").center({
  	  vertical: true,
      horizontal: true 
    });	
    
    
    $("#sitemap").hide();
    $("#open_sitemap").click(function () { 
      $(this).toggleClass("activetab");
      $("#sitemap").toggle();
    });
    $("#xbasket #loginform input").focus(function () {
         $(this).attr("value","");
    });
    
    
    // FEATURE NAVIGATION = ETUSIVUJEN BOKSIT    
     $(".featureNavigation ul li a").hover(
      function () {
        $(this).removeClass('inactive');
      }, 
      function () {
      	if(!$(this).parent().hasClass('active')){
          $(this).addClass('inactive');
      	}
      }
    );
       
    $(".featureNavigation ul li a").click(function () {
      // palautetaan alkutilat
      $(".featureNavigation ul li a").addClass('inactive');
      $(this).removeClass('inactive'); 
      $(".featureNavigation ul li").removeClass('active');
      $(this).parent().addClass('active');
      // vaihdetaan oikea sisältö
      $(".featureBox").hide();
      var target = $(this).attr("href");
      $(target).show();
      return false;            
    });
    $(".featureNavigation ul li:first").addClass('active');
    $(".featureNavigation ul li:not(:first) a").addClass('inactive');
    $(".featureBox:not(:first)").hide();    






   // Artikkelit
    $("#article_container .article:not(:first)").hide();
    $("#articlelist_container .articlelist:not(:first)").hide();
    $("#articlelist_container .articlelist:first ul li:first").addClass('selected');
    $("#article_areas li:first").addClass('selected');
    
    $("#article_areas li a").click(function () {
    	
    	$("#article_areas li[class='selected']").removeClass('selected');
    	$(this).parent().addClass('selected');
    	
        $("#article_container .article").hide(); 
        var article = ".article[rel='" + $(this).attr('rel') + "']:first"; 
    	$(article).show();
    	$(".articlelist").hide();    	
    	var list = ".articlelist[rel='" + $(this).attr('rel') + "']";
    	$(list).show();
        $("#articlelist_container .articlelist ul li[class='selected']").removeClass('selected');
        var li = ".articlelist[rel='" + $(this).attr('rel') + "'] ul li:first";
        $(li).addClass('selected');
    	return false;
    });
        
    $(".articlelist ul li a").click(function () {
       $("#article_container .article").hide(); 	 
       $( $(this).attr('href') ).show();
       $("#articlelist_container .articlelist ul li[class='selected']").removeClass('selected');
       $(this).parent().addClass('selected');
       return false; 
    });

   
    // contact page toggle
    $(".contactHead a.toggle").click(function () { 
      $(this).parent().next().toggle();
      if( $(this).text()=='Avaa'){ 
      	$(this).text('Sulje');
      	$(this).addClass('close');  
      }else{
      	$(this).text('Avaa');
      	$(this).removeClass('close');  
      }
      return false;
    });
 
 
    // gallery plugin
    $('.gallery').addClass('gallery_view'); // adds new class name to maintain degradability
    //$('.galleryNav').css('display','none'); // hides the nav initially
    
    $('ul.gallery').galleria({
            history   : false,     // deactivates the history object for bookmarking, back-button etc.
            clickNext : false,     // helper for making the image clickable. Let's not have that in this example.
            insert    : undefined, // the containing selector for our main image. 
                                                       // If not found or undefined (like here), galleria will create a container 
                                                       // before the ul with the class .galleria_container (see CSS)
            onImage   : function() { $('.galleryNav').css('display','block'); } // shows the nav when the image is showing
    });

    //varmistus kuvan kokoon
	window.setTimeout(function() {
      //alert( parseInt($(".replaced").width() ) );
	  if( parseInt($(".replaced").width()) > '513' ){ 
	   	 $(".replaced").css({ height: 'auto', width: '513px'  });
	   	 $(".replaced").center({
  	       vertical: true,
           horizontal: true 
         });	
	  }
	}, 500);	
	window.setTimeout(function() {
      //alert( parseInt($(".replaced").width() ) );
	  if( parseInt($(".replaced").width()) > '513' ){ 
	   	 $(".replaced").css({ height: 'auto', width: '513px'  });
	   	 $(".replaced").center({
  	       vertical: true,
           horizontal: true 
         });	
	  }
	}, 5000);     
});