$(window).load(function()
{
  load_images();
})

load_images = function ()
{
  $('img.delay').each(function(){
    var img_src = $(this).attr('name');
    $(this).attr('src', img_src);
    $(this).removeAttr('name');
  });
}


$(document).ready(function(){

$("a.external").click(function(){
  window.open($(this).attr('href'),'newwin');
  return false;
});

// Accordion

$("#accordion a.head, #accUpper a.head, #accLower a.head").click(function(){
        if ($(this).hasClass('unselected')) {
            $(this).removeClass('unselected').addClass('selected').next().fadeSliderToggle();
//            $(".activeNews").removeClass('activeNews').fadeSliderToggle();
//            $(this).next().addClass('activeNews');
        } else {
            $(this).removeClass('selected').addClass('unselected').next().fadeSliderToggle();
        } 
});

$('#widgets #technische,  #freefieldThree h3').click(function(){
        if ($('#freefieldThree').hasClass('unselected')) {
            $('#freefieldThree').removeClass('unselected').addClass('selected').children().fadeSliderToggle();
            location.hash = ("#ff3");
        } else {
            $('#freefieldThree').removeClass('selected').addClass('unselected').children().fadeSliderToggle();
        }
});
$('#widgets #reinigung,  #freefieldFour h3').click(function(){
        if ($('#freefieldFour').hasClass('unselected')) {
            $('#freefieldFour').removeClass('unselected').addClass('selected').children().fadeSliderToggle();
            location.hash = ("#ff4");
        } else {
            $('#freefieldFour').removeClass('selected').addClass('unselected').children().fadeSliderToggle();
        }
});

// Accordion Galerie

var galerieHeight = Math.round($('#galerie').children('.img').size()/4);

$('#galerie h3').toggle(function(){
		$('#galerie').animate({height: galerieHeight*115 + 21})
		$(this).removeClass('unselected').addClass('selected');
	}, function() {
		$('#galerie').animate({height: 21})
		$(this).removeClass('selected').addClass('unselected');
});

// Sidebar Galerie

var sidebarGalerieHeight = Math.ceil($('#sidebarImageGalerie.productImages').children('.img').size()/3);

$('#sidebarImageGalerie.productImages h3').toggle(function(){
		$('#sidebarImageGalerie.productImages').animate({height: 20 + sidebarGalerieHeight*73})
		$(this).removeClass('unselected').addClass('selected');
	}, function() {
		$('#sidebarImageGalerie.productImages').animate({height: 239})
		$(this).removeClass('selected').addClass('unselected');
});

// Colorbox

	jQuery("a[rel=\'layingpatterns\']").colorbox({
			inline: true,
			href: function(){return "#laying-patterns .calculator#pattern-" + $(this).parent().attr('id').substr(8);},
	 		onComplete: function() {
				var that = $.fn.colorbox.element();
				pattern_switch('#laying-patterns .accblock', '.calculator#pattern-' + $(this).parent().attr('id').substr(8));
			}
		},
		function() {
			var that = $.fn.colorbox.element();
			pattern_switch('#laying-patterns .accblock', '.calculator#' + that.attr('id'));
		});
	jQuery("a[rel=\'gallery\']").colorbox();
	jQuery("a[rel=\'productcolors\']").colorbox();
	jQuery("a[rel=\'SidebarImageGalerie\']").colorbox();
	jQuery("a[rel=\'reference\']").colorbox();

  // Drucken von Verlegemustern
  jQuery(".print_button").click(function() {
    //var title = $('#cboxTitle').html();
    //var baseTag = $('base').attr('href');
    //$('#cboxContent').printElement({pageTitle: title, /* leaveOpen:true,*/ printMode:'iframe', overrideElementCSS: ['themes/metten/css/layout.css', 'themes/metten/css/example2/colorbox.css', 'themes/metten/css/typography.css', 'themes/metten/css/print.css'],  base:baseTag});
    $('#cboxContent').jqprint({extraCSS: ['themes/metten/css/print.css']});
   });

   // Verlegemuster-PDF
   jQuery("#laying-patterns .calculator .pdf_button").click(function() {
       var id = $(this).attr('id').substr(4); // input id="pdf-ID"
       var form = $('#pdfform-' + id);
       var title = $(this).attr('title');
       $('#pdf_filename', form).val(fill_pdf_name(id));
       $('#html', form).val(fill_tpl(id, title));
       form.submit();
       return false;
   });
   
   function pdf_tpl(id) { 
       return ('\
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\
<html>\
    <head>\
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">\
        <base href="_base" />\
        <title>_title</title>\
        <link rel="stylesheet" type="text/css" href="http://metten.dev.schaper-design.com/themes/metten/css/layout.css">\
        <link rel="stylesheet" type="text/css" href="http://metten.dev.schaper-design.com/themes/metten/css/typography.css">\
        <link rel="stylesheet" type="text/css" href="http://metten.dev.schaper-design.com/themes/metten/css/example2/colorbox.css">\
        <link rel="stylesheet" type="text/css" href="http://metten.dev.schaper-design.com/themes/metten/css/print.css">\
    </head><body class="pdf">\
        <div class="calculator active_calc" id="pattern-' + id + '">\
        _logo-in-laying \
        _img \
        _desc \
        _patternForm \
        _patternFootnote \
        </div><div class="clear"></div>\
        _address-in-laying \
        <script type="text/javascript" src="/dospuntoceroCMS/javascript/jquery-1.3.2.min.js"></script>\
        <script type="text/javascript" src="/themes/metten/javascript/json2min.js"></script>\
        <script type="text/javascript" src="/mysite/javascript/verlegebedarf.js"></script>\
        <script type="text/javascript">_values</script>\
    </body>\
</html>');
   }
   
   function current_calculator_values(id) { 
       // Copy vars patterns, current_input and current_id and include verlegebedarf.js;
       // calling it restores input values and calculates the results without further ado.
       $.map(units, function(unit, i) {current_input[unit] = Input(unit);});
       return('var patterns  = ' + JSON.stringify(patterns) + ';'
             +'current_input = ' + JSON.stringify(current_input) + ';'
             +'$(document).ready(function(){'
               +'$.map(units, function(unit, i) { Input(unit, current_input[unit]); });'
               +'pattern_switch("body", ".calculator#pattern-' + id + '"); });');
   } 
   
   function fill_tpl(id, title) {
       var tpl = pdf_tpl(id);
       var pat = $('#pattern-' + id);
       var html = tpl.replace(/_base/,  $('base').attr('href'))
                     .replace(/_title/, title)
                     .replace(/_logo-in-laying/,    $('.logo-in-laying', pat).outer())
                     .replace(/_img/,               $('.img', pat).outer())
                     .replace(/_desc/,              $('.desc', pat).outer())
                     .replace(/_patternForm/,       $('.patternForm', pat).outer())
                     .replace(/_patternFootnote/,   $('.patternFootnote', pat).outer())
                     .replace(/_address-in-laying/, $('.address-in-laying', pat).outer())
                     .replace(/_values/, current_calculator_values(id));
        return html;
   }
   function fill_pdf_name(id) {
       var qm  = $('#input-qm-' + id).val();
       var lfm = $('#input-lfm-' + id).val();
       if (! qm)  {qm = '0';}
       if (! lfm) {lfm = '0';}
       return 'METTEN-' + URLSegment + '-' + id + '-' + qm + ',' + lfm + '.pdf';
   }


  // Drucken von News
  jQuery(".news_ul .news_print_button").click(function() {
      var li_id = $(this).attr('id'); // <input id="knopp-$ID"
      var id = li_id.substr(6);
      var toPrint = '#acc-' + id + ' .accblock';
      $(toPrint).addClass('printed_news');
      $(toPrint).jqprint({extraCSS: ['themes/metten/css/print.css']});
      $(toPrint).removeClass('printed_news');
  });
    // Blog
    jQuery(".blogEntry .news_print_button").click(function() {
        var toPrint = '.blogEntry';
        $(toPrint).addClass('printed_news');
        $(toPrint).jqprint({extraCSS: ['themes/metten/css/print.css']});
        $(toPrint).removeClass('printed_news');
    });
   
   // PDF-Download von News
   jQuery('.news_ul .pdf_button').click(function() {
      var id = $(this).attr('id').substr(4); // input id="pdf-$ID"
      var pdf = '/pdf/store/' + $('#pdf-name-' + id).val(); // need to have the same path in /pdf/wkhtmltopdf.inc
      window.open(window.location.protocol + '//' + window.location.hostname + pdf);
   });
   // Blog
   jQuery('.blogEntry .pdf_button').click(function() {
      var id = $(this).attr('id').substr(4); // input id="pdf-$ID"
      var pdf = '/pdf/store/' + $('#pdf-name-' + id).val(); // need to have the same path in /pdf/wkhtmltopdf.inc
      window.open(window.location.protocol + '//' + window.location.hostname + pdf);
   });
});