// JavaScript Document

var highlights = [],
          index = 0,
          width = 819,
          delay = 8000,
          suunta = 1,
	  interval;



$(document).ready(function(){ 

        

/* Get highlight images and links */
          highlights = $('#nostokaruselli .container').children('a,img,div,object');

          /* Animate, if there is more than one highlight */
          if (highlights.length > 1) {
			  $('#nostokaruselli .prev').show();
			  $('#nostokaruselli .next').show();
			  
			  $('#nostokaruselli .prev').click(function() {
				  clearTimeout(interval);
				  suunta = -1;
				  loopHighlights();
			  });
			  
			  $('#nostokaruselli .next').click(function() {
				  clearTimeout(interval);
				  suunta = 1;
				  loopHighlights();
			  });
              /* Clone first highlight as the last highlight */
              $('#nostokaruselli .container').append($(highlights[0]).clone());

              /* Get updated highlights and remove all other elements */
              highlights = $('#nostokaruselli .container').children('a,img,div,object');
              $('#nostokaruselli .container').html(highlights);

              /* Calculate container width */
              $('#nostokaruselli .container').css('width', highlights.length * width + 'px');

              /* Start animation */
              interval = setTimeout(loopHighlights, delay);
          }

 
});

function loopHighlights ()
      {
		  //suunta = suunta || 1;
		  index = index + suunta;
          /* Loops index between 0 and the amount of highlights */
		  if( index >= ( highlights.length ) ) {
			  index = 1;
			  $('#nostokaruselli .container').css({ left: '0px' });  
		  } else if ( index < 0 ) {
			 index = highlights.length -2;
			  $('#nostokaruselli .container').css({ left: '-' + ((highlights.length -1) * width) + 'px' }); 
		  }
          

          /* Do the slide animation */
          $('#nostokaruselli .container').animate({ left: '-' + (index * width) + 'px' } , 500);
		  suunta = 1;
		  interval = setTimeout(loopHighlights, delay);
      }



 
var currentSelectedVoteItem = '';
var currentSelectedVoteTitle = '';
 
$.ajaxSetup ({
    cache: false
});

function showProductDetails( productNumber )
{
        $(".tuotteet_content").slideUp("slow", function ()
        {
            $(".tuotteet_content").load( "/tuotteet_ajax2011?id=" + productNumber, {}, function( responseText, textStatus, XMLHttpRequest )
            {
                if ( textStatus == 'success' )
                {
                    $(".tuotteet_content").slideDown()
                }
                else
                {
                    $(".tuotteet_content").html('<div class="product_info"><div class="header_left"></div><div class="header_center"></div><div class="header_right"></div><div class="product_content"><p>Tuotetietojen hakeminen epäonnistui.</p></div><div class="footer_left"></div><div class="footer_center"></div><div class="footer_right"></div><div class="clear"></div></div>');
                }
            });
        });
 
   
 
    return false;
}
$(document).ready(function ()
{
   
    $('html').css( 'min-height', $(window).height() + 'px' );
    $('body').css( 'min-height', $(window).height() + 'px' );
    $('#raster_top').css( 'min-height', $(window).height() + 'px' );
    $('#raster_bottom').css( 'min-height', $(window).height() + 'px' );
    
    var productsLenght = 0;
    $('#product_scroll .product_item').each(function()
    {
        productsLenght += $(this).outerWidth( true );
    });    
    $('#product_scroll .product_container').width( productsLenght );
    if ( ($('#product_scroll .product_container').outerWidth( true ) && $('#product_scroll .product_container').width()) <= $('#product_scroll').width() )
        $("#product_slider").hide();
    else
    {
        $("#product_slider .product_slider").slider({
            slide: handleSliderSlide
          });
    }
 
    var productsLenght = 0;
    $('#product_scroll_2 .product_item').each(function()
    {
        productsLenght += $(this).outerWidth( true );
    });    
    $('#product_scroll_2 .product_container').width( productsLenght );
    if ( ($('#product_scroll_2 .product_container').outerWidth( true ) && $('#product_scroll_2 .product_container').width()) <= $('#product_scroll_2').width() )
        $("#product_slider_2").hide();
    else
    {
        $("#product_slider_2 .product_slider").slider({
            slide: handleSliderSlide2
          });
    }
 
    var productsLenght = 0;
    $('#product_scroll_3 .product_item').each(function()
    {
        productsLenght += $(this).outerWidth( true );
    });    
    $('#product_scroll_3 .product_container').width( productsLenght );
    if ( ($('#product_scroll_3 .product_container').outerWidth( true ) && $('#product_scroll_3 .product_container').width()) <= $('#product_scroll_3').width() )
        $("#product_slider_3").hide();
    else
    {
        $("#product_slider_3 .product_slider").slider({
            slide: handleSliderSlide3
          });
    }
    
    var productsLenght = 0;
    $('#product_carousel .product_item').each(function()
    {
        productsLenght += $(this).outerWidth( true );
    });    
    $('#product_carousel .product_container').width( productsLenght );
    if ( ($('#product_carousel .product_container').outerWidth( true ) && $('#product_carousel .product_container').width()) <= $('#product_carousel').width() )
        $("#product_carousel_slider").hide();
    else
    {
        $("#product_carousel_slider .product_carousel_slider").slider({
            slide: handleSliderSlide4
          });
    }
 
    var productsLenght = 0;
    $('#prices_scroll .product_item').each(function()
    {
        productsLenght += $(this).outerWidth( true );
    });    
    $('#prices_scroll .product_container').width( productsLenght );
    if ( ($('#prices_scroll .product_container').outerWidth( true ) && $('#prices_scroll .product_container').width()) <= $('#prices_scroll').width() )
        $("#prices_slider").hide();
    else
    {
        $("#prices_slider .product_slider").slider({
            slide: handleSliderSlide6
          });
    }
 
    var productsLenght = 0;
    $('#recent_scroll .product_item').each(function()
    {
        productsLenght += $(this).outerWidth( true );
    });    
    $('#recent_scroll .product_container').width( productsLenght );
    if ( ($('#recent_scroll .product_container').outerWidth( true ) && $('#recent_scroll .product_container').width()) <= $('#recent_scroll').width() )
        $("#recent_slider").hide();
    else
    {
        $("#recent_slider .product_slider").slider({
            slide: handleSliderSlide7
          });
    }
 
    $("#accordion").accordion({autoHeight: false, collapsible: true, active: 1000});
 
});
 
 
function handleSliderSlide(e, ui)
{
    var maxScroll = $("#product_scroll").attr("scrollWidth") - $("#product_scroll").width();
    $("#product_scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
}
 
function handleSliderSlide2(e, ui)
{
    var maxScroll = $("#product_scroll_2").attr("scrollWidth") - $("#product_scroll_2").width();
    $("#product_scroll_2").attr({scrollLeft: ui.value * (maxScroll / 100) });
}
 
function handleSliderSlide3(e, ui)
{
    var maxScroll = $("#product_scroll_3").attr("scrollWidth") - $("#product_scroll_3").width();
    $("#product_scroll_3").attr({scrollLeft: ui.value * (maxScroll / 100) });
}
 
function handleSliderSlide4(e, ui)
{
    var maxScroll = $("#product_carousel").attr("scrollWidth") - $("#product_carousel").width();
    $("#product_carousel").attr({scrollLeft: ui.value * (maxScroll / 100) });
}
 
function handleSliderSlide5(e, ui)
{
    var maxScroll = $("#history_scroll").attr("scrollWidth") - $("#history_scroll").width();
    $("#history_scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
}
 
function handleSliderSlide6(e, ui)
{
    var maxScroll = $("#prices_scroll").attr("scrollWidth") - $("#prices_scroll").width();
    $("#prices_scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
}
 
function handleSliderSlide7(e, ui)
{
    var maxScroll = $("#recent_scroll").attr("scrollWidth") - $("#recent_scroll").width();
    $("#recent_scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
}
 
 
function fixIePng ()
{
    setTimeout("$('.iepng').supersleight({apply_positioning: false})",500);
}
 
var tempElementName = '';
var tempTimeOut = null;
 
function elementSlide ( elementName, effectType )
{
    if ( tempTimeOut )
        clearTimeout( tempTimeOut );
    tempElementName = elementName;
 
    if ( effectType == "piiloon" )
    {
        $(elementName).slideUp("slow");
    }
 
    if ( effectType == "esiin" )
    {
        $(elementName).show();
        $(elementName).hide();
        tempTimeOut = setTimeout("$(tempElementName).slideDown('slow', fixIePng)", 150);
    }
}
 
 
function clearFormValue ( formfield )
{
    if ( formfield.title == formfield.value )
        formfield.value = "";
}
 
function resetFormValue ( formfield )
{
    if ( formfield.value == "" )
        formfield.value = formfield.title;
}
 
var tempU = '';
var tempCommentElement = null;
 
function slideShare ( linkElement )
{
    if ( $( linkElement ).next().is( ':hidden' ) )
        $( linkElement ).next().slideDown( "slow" );
    else
        $( linkElement ).next().slideUp( "slow" );
    
    return false;
}
 
