// dropin JS-library	

// TUOTEKUVAN VAIHTO
function vaihdacontent(uusicontent) {
  //hide all
  $("#tuotekuva, #tuotekuva1, #tuotekuva2, #tuotekuva3, #tuotekuva4").hide("");
  //show chosen
  $("#"+uusicontent).show();

}

// replace 
function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

// aletähti
function laskeale(vanhahinta,uusihinta) {
	vanhahinta = replace(vanhahinta,',','.');
	vanhahinta = replace(vanhahinta,'(','');
	vanhahinta = replace(vanhahinta,')','');
	vanhahinta = replace(vanhahinta,' €','');
	uusihinta = replace(uusihinta,',','.');
	uusihinta = replace(uusihinta,' €','');
	var prosentti = Math.round(((vanhahinta - uusihinta)/vanhahinta)*100);
	var aleprossa = '<div class="aletahti">-' + prosentti + '%</div>';
	var nothing = '';
	if (prosentti > 10) {
		return aleprossa;		
	} else {
		return nothing;
	}
}

// alen määrä
function laskealeteksti(tyyppi,vanhahinta,uusihinta) {
	if (tyyppi == "tarjous" || tyyppi == "outlet") {
		vanhahinta = replace(vanhahinta,',','.');
		vanhahinta = replace(vanhahinta,'(','');
		vanhahinta = replace(vanhahinta,')','');
		vanhahinta = replace(vanhahinta,' €','');
		uusihinta = replace(uusihinta,',','.');
		uusihinta = replace(uusihinta,' €','');
		var prosentti = Math.round(((vanhahinta - uusihinta)/vanhahinta)*100);
		var aleprossa = ' <b>Säästät <span style="font-size:14px;color:#d7620d;">' + prosentti + '%</span></b>';
		var nothing = '';
		if (prosentti > 10) {
			document.write(aleprossa);	
		} else {
			return nothing;
		}
	}
}


// checkload 
function checkLoad(img){
var pics=document.images
for (var i_tem = 0; i_tem < pics.length; i_tem++)
if (pics[i_tem]==img)
loads[i_tem]=img.src
}

var loads=new Array()

function isLoaded(){
var pics=document.images
for (var i_tem = 0; i_tem < pics.length; i_tem++)
if (loads[i_tem]==undefined&&pics[i_tem].lowsrc)
pics[i_tem].src=pics[i_tem].lowsrc
}

if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", isLoaded, false );
else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", isLoaded );
}
else {
    if ( window.onload != null ) {
        var oldOnload = window.onload;
        window.onload = function ( e ) {
            oldOnload( e );
            isLoaded();
        };
    }
    else
        window.onload = isLoaded;
}



// tiiserit 
var flvplayer=new Boolean(false);
var youtubeplayer=new Boolean(false);


if ( flvplayer == true ) {
  document.getElementById('flvplayer').innerHTML=document.getElementById('flvplayer-preload').innerHTML;
   } 


// toimitusaika
function showtoimitusaika(toimitusaika){
if (toimitusaika.match("2")) {
document.write('<span style="color:green;font-size:12px;font-weight:bold;">Heti</span>');
}
if (toimitusaika.match("viik")) {
document.write('<span style="color:blue;font-size:12px;font-weight:bold;">Toimitusaika 1 viikko</span>');
}
if (toimitusaika.match("tulos")) {
document.write('<span style="color:blue;font-size:12px;font-weight:bold;">Ennakkotilaustuote</span>');
}
if (toimitusaika.match("toimitukset")) {
document.write('<span style="color:blue;font-size:12px;font-weight:bold;">Ennakkotilaustuote</span>');
}
}

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};





/**
 * jQuery.Preload - Multifunctional preloader
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com
 * Dual licensed under MIT and GPL.
 * Date: 3/25/2009
 * @author Ariel Flesler
 * @version 1.0.8
 */
;(function($){var h=$.preload=function(c,d){if(c.split)c=$(c);d=$.extend({},h.defaults,d);var f=$.map(c,function(a){if(!a)return;if(a.split)return d.base+a+d.ext;var b=a.src||a.href;if(typeof d.placeholder=='string'&&a.src)a.src=d.placeholder;if(b&&d.find)b=b.replace(d.find,d.replace);return b||null}),data={loaded:0,failed:0,next:0,done:0,total:f.length};if(!data.total)return finish();var g=$(Array(d.threshold+1).join('<img/>')).load(handler).error(handler).bind('abort',handler).each(fetch);function handler(e){data.element=this;data.found=e.type=='load';data.image=this.src;data.index=this.index;var a=data.original=c[this.index];data[data.found?'loaded':'failed']++;data.done++;if(d.enforceCache)h.cache.push($('<img/>').attr('src',data.image)[0]);if(d.placeholder&&a.src)a.src=data.found?data.image:d.notFound||a.src;if(d.onComplete)d.onComplete(data);if(data.done<data.total)fetch(0,this);else{if(g&&g.unbind)g.unbind('load').unbind('error').unbind('abort');g=null;finish()}};function fetch(i,a,b){if(a.attachEvent&&data.next&&data.next%h.gap==0&&!b){setTimeout(function(){fetch(i,a,1)},0);return!1}if(data.next==data.total)return!1;a.index=data.next;a.src=f[data.next++];if(d.onRequest){data.index=a.index;data.element=a;data.image=a.src;data.original=c[data.next-1];d.onRequest(data)}};function finish(){if(d.onFinish)d.onFinish(data)}};h.gap=14;h.cache=[];h.defaults={threshold:2,base:'',ext:'',replace:''};$.fn.preload=function(a){h(this,a);return this}})(jQuery);



//sivutus

function paging(url,thisPage,lastPage) {
	var i = 0;
	var j = 0;

	// print div around the paging
	document.write('<div class="paging">');
	
	// print Previous page link if there are previous pages
	if (thisPage != 1) {
		var previousPage = thisPage - 1;
		// print link to first page if more than one previous pages available
		if (previousPage > 5) {
			document.write('<a href="' + url + '&pagenr=1' + '">&laquo; Alkuun</a>');
		}
		document.write('<a href="' + url + '&pagenr=' + previousPage + '">&laquo; Edellinen</a>');
		// print dots if more than one previous pages available
		if (previousPage > 5) {
			document.write('<span class="dots">...</span>');
		}
	}

	// print max 5 previous page numbers as links
	i = thisPage-5;
	while (i < thisPage) {
		if (i > 0) {
			document.write('<a href="' + url + '&pagenr=' + i + '">' + i + '</a>');
		}
		i++;
	}

	// print nothing if there is only one page
	if (lastPage != 1) {
		document.write('<span class="thisPage">' + thisPage + '</span>');
	}

	// print max 5 next page numbers as links
	i = thisPage+1;
	j = 0;
	while (i <= lastPage && j < 5) {
		document.write('<a href="' + url + '&pagenr=' + i + '">' + i + '</a>');
		i++;
		j++;
	}

	// print Next page link if there are previous pages
	if (thisPage != lastPage) {
		var nextPage = thisPage + 1;
		// print dots if more than more than one next page
		if ((lastPage - nextPage) > 5) {
			document.write('<span class="dots">...</span>');
		}
		document.write('<a href="' + url + '&pagenr=' + nextPage + '">Seuraava &raquo;</a>');
		// print link to last page if more than more than one next page
		if ((lastPage - nextPage) > 5) {
			document.write('<a href="' + url + '&pagenr=' + lastPage + '">Loppuun &raquo;</a>');
		}
	}

	// close the wrapping div
	document.write('</div>');
}


// tehdään kun sivu on latautunut
$(document).ready(function() {

//Tooltip



// create custom animation algorithm for jQuery called "bouncy"
$.easing.bouncy = function (x, t, b, c, d) {
    var s = 1.70158;
    if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
    return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
}

// create custom tooltip effect for jQuery Tooltip
$.tools.tooltip.addEffect("bouncy",

	// opening animation
	function(done) {
		this.getTip().animate({top: '+=15'}, 500, 'bouncy', done).show();
	},

	// closing animation
	function(done) {
		this.getTip().animate({top: '-=15'}, 500, 'bouncy', function()  {
			$(this).hide();
			done.call();
		});
	}
);


$("#teesit a[title]").tooltip({tip: '#tooltip', effect: 'bouncy'});
$("img.help[title]").tooltip({tip: '#tooltip', offset: [90, -180], effect: 'bouncy'});
 

//bookmark
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(7($){$.8.9=7(d){g e=$.v({},$.8.9.k,d);g f=e.l;2.w(7(){5(!$(2).3(\'4\')||$(2).3(\'4\')=="#"){$(2).3(\'4\',j.x.4)}5(!$(2).3(\'6\')){$(2).3(\'6\',y.6)}5($.h.z){$(2).3(\'m\',$(2).3(\'4\')).3(\'4\',\'\').3(\'4\',\'A:B(0)\').3(\'C\',\'$.8.9.n(2,"\'+$(2).3(\'6\')+\'","\'+$(2).3(\'m\')+\'")\')}i{$(2).o(p)}});7 p(a){g b=$(2).3(\'4\');g c=$(2).3(\'6\');b=b.E("q:///","F://q/");5($.h.G){j.H.I(b,c)}i 5($.h.J){j.r.K(c,b,\'\')}i 5(L.M.N().O(\'P\')>-1){s(f)}i 5($.h.Q){s(f)}a.R()};t 2};$.8.9.k={l:\'S "T + D" U "V + D" W X, u Y 2 Z u 10 11.\'};$.8.9.n=7(e,a,b){e.12="r";e.6=a;e.4="";e.4=b;e.o();t 13}})(14);',62,67,'||this|attr|href|if|title|function|fn|jFav|||||||var|browser|else|window|defaults|msg|link|OperaBookmark|click|addToFavorites|file|sidebar|alert|return|to|extend|each|location|document|opera|javascript|void|onClick||replace|http|msie|external|AddFavorite|mozilla|addPanel|navigator|userAgent|toLowerCase|indexOf|chrome|safari|preventDefault|Press|Ctrl|or|CMD|for|MAC|add|page|your|bookmarks|rel|false|jQuery'.split('|'),0,{}))

$('#bookmark').jFav();


//Kirjautuminen	
	// Expand Panel
	$("#open").click(function(){
		$("div#panel").slideDown("slow");
	
	});	
	
	// Collapse Panel
	$("#close").click(function(){
		$("div#panel").slideUp("slow");	
	});		
	
	// Switch buttons from "Log In | Register" to "Close Panel" on click
	$("#toggle a").click(function () {
		$("#toggle a").toggle();
	});
		
//Etusivun mainokset
  $('#slick-show').hide();
 // shows the slickbox on clicking the noted link
  $('a#slick-show').click(function() {
	$('#slickbox').slideDown('slow');
	$('a#slick-hide').show();
	$('a#slick-show').hide();
	return false;
  });
 // hides the slickbox on clicking the noted link
  $('a#slick-hide').click(function() {
	$('#slickbox').slideUp('fast');
	$('a#slick-hide').hide();        
	$('a#slick-show').show();  
	return false;
  });
 // toggles the slickbox on clicking the noted link
  $('a#slick-toggle').click(function() {
	$('#slickbox').toggle(400);
	return false;
  });
	




//pikahku

$(".search").keyup(function() {
	var searchbox = $(this).val();
	var dataString = 'action=search&searchString='+ searchbox;

	if(searchbox=='') {
		$("#display").empty();
	} else {
		$.ajax({
		type: "POST",
		url: "extra/search.groovy",
		data: dataString,
		cache: false,
		success: function(html) {
			$("#display").html(html).show();				
		}
		});
	}
	return false;    
});
});

jQuery(function($){
   $("#searchbox").Watermark("Syötä hakusana");
   $(".commenttextarea").Watermark("Kirjoita kommentti tuotteesta..");

//Karusellit      
jQuery('.carousel').jcarousel({
         }); 

vaihdacontent('tuotekuva');

/* pikkukuvan piilotus */
$('img.smallproductpic').preload({
    placeholder:'gfx/transparent.gif',
    notFound:'gfx/transparent.gif'
});



});


