// jQuery


$(document).ready(function() {

	

  vaihdacontent('tuotekuva');

	$('#lisatiedot').hide();

	$('a#lisatiedot-toggle').click(function() {
	  $('#lisatiedot').toggle(400);
	  return false;
	});
	
$(".error").fadeOut(800).fadeIn(800).fadeOut(400).fadeIn(400)
    .fadeOut(400).fadeIn(400);

//create info links on product lists for some availabilities
$("span.saatavuusArvo").each(function(){
 var element = $(this);
 if(element.text().match(/Fråga/i)){
   //add thickbox clickable element after thickbox has been initialized
   element.html('<a href="#TB_inline?height=450&width=500&inlineId=fraga" class="thickbox">Fråga</a>');
   element.find("a").click(function(){ tb_show(this.title, this.href, this.reg); });
 }
});


/* pikkukuvan piilotus */
$('img.smallproductpic').preload({
    placeholder:'gfx/transparent.gif',
    notFound:'gfx/transparent.gif'
});

/* keskikokoisen piilotus */
$('img.mediumpic').preload({
    placeholder:'gfx/pienitulossa.gif',
    notFound:'gfx/pienitulossa.gif'
});

/* ison kuvan piilotus */
$('img.bigproductpic').preload({
    placeholder:'gfx/transparent.gif',
    notFound:'gfx/isotulossa.gif'
});


}); 







// TUOTEKUVAN VAIHTO
function vaihdacontent(uusicontent) {
  //hide all
  $("#tuotekuva, #tuotekuva1, #tuotekuva2, #tuotekuva3, #tuotekuva4").hide("");
  //show chosen
  $("#"+uusicontent).show();

}


// SIVUTUS
function initializeWSPagingLinks(){	
	$(".paging").each(function(){
		var paging = $(this);
		var baseUrl = document.location.href.split('&pagenr=')[0]; //TODO this can break if pagenr is not last parameter 
	    var currPage = parseInt( paging.find(".currpage").text() );
		var pageAmount = parseInt( paging.find(".pageamount").text() );
		var visiblePageLinks = 5;
		
		var paginghtml = '';
		if( currPage != 1 ){
			//if pages from the beginning are not visible, render a link to the beginning
			if( currPage > (visiblePageLinks+1) ){
			  paginghtml+=(' N&auml;yt&auml; lis&auml;&auml; sivuja: ');
			  paginghtml+=('<a href="' + baseUrl + '&pagenr=1"><<</a>');
			}
			paginghtml+=('<a href="' + baseUrl + '&pagenr=' + (currPage-1) + '"><</a>');
			// if pages from the beginning are not visible, print dots 
			if ( currPage > (visiblePageLinks+1) ) {
				paginghtml+=('<span class="dots">...</span>');
			}
		}
		//previous page links
		for(var i = 0; i<visiblePageLinks; i++){
			var pagenr = (currPage- (visiblePageLinks-i))
            if(pagenr>0){
			  paginghtml+=('<a href="' + baseUrl + '&pagenr=' + (pagenr) + '">' + (pagenr) + '</a>');
            }
		}
		//current page (if there are multiple pages
	    if (pageAmount != 1) {
			paginghtml+=('<span class="thisPage">' + currPage + '</span>');
		}
		//next page links
		for(var i = 1; i<=visiblePageLinks && (i+currPage)<=pageAmount; i++){
				paginghtml+=('<a href="' + baseUrl + '&pagenr=' + (i+currPage) + '">' + (i+currPage) + '</a>');
		}
		
		if( currPage != pageAmount ){
			// if pages until the end are not visible, print dots 
			if ( currPage < (pageAmount-visiblePageLinks) ) {
				paginghtml+=('<span class="dots">...</span>');
			}
			paginghtml+=('<a href="' + baseUrl + '&pagenr=' + (currPage+1) + '">></a>');
			
			//if pages until the end are not visible, render a link to the end
			if( currPage < (pageAmount-visiblePageLinks)  ){
			  paginghtml+=('<a href="' + baseUrl + '&pagenr='+pageAmount+'">>></a>');
			}
		}
		
		paging.html(paginghtml);
	});	
}



