Cufon("h2");
Cufon("h3");
Cufon("h4");
Cufon("#verkkokauppa #tiedote-container a span");
Cufon("#product-menu ul li a");
Cufon(".eBtn a");



popupForm = {
  url: null,
  element: null,
  init: function(url,element){
    popupForm.url = url;
    popupForm.element = element;

    var src = popupForm.url + ' '+popupForm.element;
    $('#popUpHide').load(src, function(){
      $('#popUpHide').modal({
        closeHTML : "<a  href=\"#\">X</a>",
        containerCss: {
          backgroundColor: "#fff",
          borderColor: "#dedede",
          padding: 10,
          width: 300
        },
        modal: true,
        overlayClose: true,
        opacity: 50,
        overlayCss: {
          backgroundColor: "#000"
        },
        overClose: true,
        //overlayId: 'contact-overlay',
        //containerId: 'contact-container',
        //onOpen: popupForm.open,
        onShow: popupForm.show,
        onClose: popupForm.close
      });
      $('<div/>').appendTo('#popUpHide').addClass('virhe-msg');
    });
  },
  open: function(dialog){
  },
  show: function(dialog){
    $(popupForm.element).bind("submit", function(e){
          e.preventDefault();
          var form = $('#form1');
          var method  = form.attr('method') || 'GET';
          $(popupForm.element + ' h2').html('Lähetetään..');
          $(popupForm.element + ' p').fadeOut(200);
          form.fadeOut(200, function(){
            
            $(dialog.container).animate({
              height: '80px'
            });
          });

          $.ajax({
            type: method,
            url: form.attr('action'),
            data: form.serialize(),
            success: function(){
              $(popupForm.element+ ' h2').html('Kiitos Palautteestasi');
              $('.virhe-msg').html('Viesti Lähetetty!');
              //popupForm.close();
            },
            error: function(){
            $(popupForm.element+ ' h2').html('Virhe');
              $('.virhe-msg').html('Lähetyksessä tapahtui virhe');
            }
          });

        });
  },
  close: function(dialog){
    $('#simplemodal-container').fadeOut(1000, function(){
      $.modal.close();
      $('#popUpHide').empty();
    });
  }
};

$(document).ready(function(){
  $('.fancybox').fancybox({'type': 'image'});
  $(".youtube").click(function() {
	$.fancybox({
			'padding'		    : 0,
			'autoScale'		  : false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			    : this.title,
			'width' 		    : 680,
			'height'		    : 495,
			'href'			    : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			    : 'swf',
			'swf'			      : {
                        'wmode'		: 'transparent',
                        'allowfullscreen'	: 'true'
			}
		});

	return false;
});
  if($('.product').length != 0){
  $('<div></div>').appendTo('.product').addClass('extra-links');
  $('.extra-links').html('<a class="tarjous" href="/tarjouspyynto">Tarjouspyyntö</a>');
  //styling
  var moreLink = 151;
  var detailsHeight = 170;
  $('.product-details table tr:nth-child(2n) td').css({'background':'#e6e6e6'});
  var originHeight = $('.product-details').height();

  if(originHeight > detailsHeight){
  $('.product-details').css({
    'height':detailsHeight,
    'overflow':'hidden'
  });
  $('<a href="#">Lisätietoja...</a>').appendTo('.extra-links').addClass('more').css({
    'margin-left': '20px'
    /*'position':'absolute',
    'top': moreLink,
    'right':0,
    'text-decoration':'underline',
    'cursor':'pointer'*/
  });
  $('.product .more').click(function(e){
    e.preventDefault();
    if($(this).parent().siblings('.product-details').height() == detailsHeight){
    $(this).parent().siblings('.product-details').animate({'height': originHeight},1000);
   // $(this).animate({'top':originHeight-18},1000)
    }
    else{
      $(this).parent().siblings('.product-details').animate({'height': detailsHeight},1000);
      //$(this).animate({'top':moreLink},1000)
    }
  });
  }
  }
  $("#jcb-toimipisteet").hide();
  if($('#mateko-maanrakennus.yhteystiedot').length || $('.huolto').length){
    $(".kategoria").hide();
    $(".kategoria:first").show();
    var containerHeight = 600;
    $('#main').css('height',containerHeight);
      var titles = Array();
      $('h3').each(function(){
        var element = new Object();
        element['id'] = $(this).parent().attr('id');
        element['name'] = $(this).html();
        titles.push(element);
      });

      var container = $('<ul></ul>').addClass('controller');
      $('h2').after(container);
      $(titles).each(function(index, val){
         $('<li><a href="#'+val['id']+'">'+val['name']+'</a></li>').appendTo(container);

      });
      $(".controller li a").click(function(){
        var elementId = $(this).attr('href');
        $(".kategoria:visible").fadeOut(400,function(){
          $(elementId).fadeIn(400,function(){
            if($(elementId).height() > containerHeight){
              $('#main').css('height', $(elementId).height()+100);
            }
            else{
              $('#main').css('height', containerHeight);
            }
          });
        });
        return false;
      });

  }

  $('#palaute').click(function(e){
    e.preventDefault();
    popupForm.init($(this).attr('href'), '#palaute-lomake');
  });
  $('.tarjous').click(function(e){
    e.preventDefault();
    popupForm.init($(this).attr('href'), '.tarjouspyynto-lomake');
  });

});


