$(document).ready(function()
{	
	

	// Init pdBox
  $('.thickbox').pdBox({minWidth: 400});
  
	// Vertical sort
  $('.img-box').each(function(){ 
     var imgBoxH = $(this).height();
     $('img', this).each(function (){
       $(this).bind('load', function(){
        var imgH = $(this).height(); 
        $(this).css('margin-top', (imgBoxH - imgH) / 2 + 'px');
       }).attr('src', this.src);
     });
  });
  
  // Create zebra-style tables
	$('.col-content-top > table tr:nth-child(even), .col-content-top > .attached-file table tr:nth-child(even)').addClass('even');
	
	$('#content .col-content-top > table, #content .col-content-top > .attached-file table').wrap('<div class="table-help"></div>');
	
	/* FAKE SELECT */
  $('.light-box select').fakeSelect();
  
  var imgHeight = function(){
    this.onload = null;
  };
  var img1 = document.createElement('img');
  var img2 = document.createElement('img');
  var img3 = document.createElement('img');
  var img4 = document.createElement('img');
  var img5 = document.createElement('img');
  $(img1).bind('load', imgHeight).attr('src', '/img/bg-product-motive-asset-active.png');
  $(img2).bind('load', imgHeight).attr('src', '/img/bg-product-motive-computer-active.png');
  $(img3).bind('load', imgHeight).attr('src', '/img/bg-product-motive-h2-active.png');
  $(img4).bind('load', imgHeight).attr('src', '/img/bg-product-motive-helpdesk-active.png');
  $(img5).bind('load', imgHeight).attr('src', '/img/bg-product-motive-software-active.png');
  
  
	//AJAX submit calculation
	$('#pc-sum').keyup(function(){
		$('#calculationForm').submit();	
	});
	$('#margin').keyup(function(){
		$('#calculationForm').submit();
	});
	$('.moduly, #subscription').click(function(){
		$('#calculationForm').submit();	
	});
	
	$('#calculationForm').ajaxForm({ 
		target: '.table-calc', 
		success: function(xml) { 
			$('.table-calc').html(xml);	
		}
	});
	$("#calculationForm").attr("action", "/ajaxRecalculate/calc");
	
	// Bind click
	 $(".btn-download").click(function () {$("#down").click();	});
	 $(".buy-btn").click(function () {$("#buy").click();});
	 
	 $('.btn-submit .big-btn, .btn-submit .build').removeClass('out');
	 $('.table-calc .go-btn').addClass('out');
	 $('.download .go-btn').addClass('out');

    // Carousel	
	homeCarousel = new $.Carousel('#rot-position ul', {
		item: 'li',
		visible: 1,
		animation: 1000,
		htmlPager: '',
		timeout: 3000,
		auto: true
	});
	homeCarousel.init();	

	// SHOW BOX
	$('.open-box').each(function(){
        var box = {
            el: $(this),
            input: $('h2 :checkbox', this),
            box: $('#hide', this).wrap('<div class="js-help" style="overflow:hidden; position:relative; zoom:1;"></div>'),
            status: function(){
                return box.input.attr('checked') == true ? true : false;
            },
            init: function(first){
                var status = box.status();
                if(!first){   
                    if(status){$('.js-help', box.el).slideDown('normal');}
                    else{$('.js-help', box.el).slideUp('normal');}
                }
                else{
                    if(status){$('.js-help', box.el).show();}
                    else{$('.js-help', box.el).hide();}    
                }
            }
        };
        box.init(true);
        box.input.click(function(){box.init(false)});
    });
    
});
