$(document).ready(function(){
  
  /* Twitter */
  setTimeout(checkTwitterDone, 100);
  
  /* Webshop */
  if($('.webshop-main').length > 0) {
    Cufon.replace('.webshop-main .item .name')('.webshop-main .item .price')('.webshop-main .order-form .title')('.webshop-main .order-form .subtitle');
    $('#header').css('background', 'transparent');
    $('.webshop-main .button').hover(function() {
      $(this).attr('src', $(this).attr('src').replace('.png', '-down.png'));
    }, function() {
      $(this).attr('src', $(this).attr('src').replace('-down.png', '.png'));
    });
    $('.webshop-main .order-form .form #order').unbind('mouseenter mouseleave').hover(function() {
      $(this).attr('src', $(this).attr('src').replace('-down.png', '.png'));
    }, function() {
      $(this).attr('src', $(this).attr('src').replace('.png', '-down.png'));
    });
    $('.webshop-main .item .button').click(function() {
      var info = $(this).attr('rel').split(';');
      $('.webshop-main .order-form ul').append($('<li>' + info[1] + ' - &euro;' + fixPrice(info[2]) + '</li>'));
      webshopTotal += Number(info[2]);
      $('.webshop-main .order-form .total').html('Total - &euro;' + fixPrice(webshopTotal));
      $('.webshop-main .order-form').fadeIn(600);
    });
    $('.webshop-main .order-form #order').click(function() {
      alert('send order');
    });
    $('.webshop-main .order-form #close').click(function() {
      $('.webshop-main .order-form').fadeOut(200);
    });
  }

  $('a.mail').each(function(i) {
        var text = $(this).text();
        var array = text.split(" ");
		var address = array[0] + '@' + array[2];
        $(this).attr('href', 'mailto:' + address);
		$(this).text(address);
	});
	
	$('.slider').slider();
	
	$(".ajaxformulier").each(function(i){
		$(this).unbind('submit').bind('submit',function(e){
			submitAjaxForm($(this));
			e.preventDefault();
		});
	});

	$('[rel^=land]:not(.active)').hide();	
	
	$("select[name=countrychoser]").bind('change',function(){
		$('[rel^=land].active').removeClass('active').hide('fast');	
		$('[rel=land_'+$(this).val()+']').addClass('active').show('fast');
	});
	
	
	
	
	$('.download_cat, .download_cat_titel').hide();
	$('.download_cat:eq(0), .download_cat_titel:eq(0)').show();
	$(".login-ingelogd ul li a:eq(0)").addClass('active');
	$(".login-ingelogd ul li a").bind('click', function(event){
		var rel = ('' == $(this).attr('rel') ? 'no_files' : $(this).attr('rel') );

		event.preventDefault();
		
		if($(this).parent().find('ul:not(.product)').length > 0) {
			$(this).parent().find('ul:not(.product)').slideToggle();
		}
		
		$(".login-ingelogd ul li a").removeClass('active');
		$(this).addClass('active');
    	$('.download_cat:not(.'+rel+')').removeClass('active').slideUp('fast');
		$('.'+rel).addClass('active').slideDown('fast');
    	$('.download_cat_titel:not(.'+rel+')').hide();
		$('.'+rel+'_titel').show();
	});
		
	$('.choctalk-post').bind('click',function(e){
		$('#choctalk .form').slideDown(300);
		e.preventDefault();
	});
	$('#choctalk  .cancel').bind('click',function(e){
		$('#choctalk .form').slideUp(300);
		e.preventDefault();
	});
	$('.maps').maps();
	
	$(".webshoplink").fancybox({
		'type'					: 'iframe',
		'autoDimensions'		: false,
		'width'					: 980,
		'height'				: 660,
		'hideOnContentClick'	: false,
		'overlayColor'			: '#FFFFFF',
		'overlayOpacity' 		: 0.7
	});
	
	$(".fancynews").fancybox({
		'scrolling'			: 'yes',
		'padding'			: 0,
		'margin'			: 0,
		'autoScale'			: false,
		'titleShow'			: false,
		'overlayColor'		: '#FFFFFF',
		'overlayOpacity' 	: 0.7
	});
	
	$('#tweet').tweet({
			username: "CHCOCOMPANY",
            join_text: "auto",
            avatar_size: 36,
            count: 20,
            loading_text: "loading tweets..."
	});
	
});
function checkTwitterDone() {
  if($('#tweet .tweet_list li').length > 0) {
    if($('#twitterscrollers').length > 0) {
      var w = $('#tweet .tweet_list').outerHeight();
      $('#scrollup').click(function() {
        if(Number($('#tweet .tweet_list').css('top').replace('px', '')) <= -210) {
          $('#tweet .tweet_list').stop(true, true).animate({top:'+=210px'}, 500);
        }
      });
      $('#scrolldown').click(function() {
        if(Number($('#tweet .tweet_list').css('top').replace('px', '')) > 210 - w) {
          $('#tweet .tweet_list').stop(true, true).animate({top:'-=210px'}, 500);
        }
      });
    }
  } else {
    setTimeout(checkTwitterDone, 100);
  }

}
var webshopTotal = 0;
function fixPrice(price) {
  if(Math.round(price) == price) {
    price = Math.round(price) + ',-';
  } else {
    price = String(price).replace('.', ',');
  }
  var bits = price.split(',');
  if(bits[1] != '-' && bits[1].length < 2) {
    price += '0';
  }
  return price;
}

function submitAjaxForm(form){
	var form = form;
	var data = form.serialize();
	var buttonlabel = form.find('input[type=submit]').val();
	form.find('input, textarea, checkbox, radio, select').each(function(){
		if($(this).attr('title')){
			ar = $(this).attr('name').split('[]');
			naam = ar[0];
			data += '&veldnamen['+encodeURIComponent(naam)+']=' + encodeURIComponent($(this).attr('title'))
		}
	});
	$.ajax({
		type: 	form.attr('method'),
		url: 	form.attr('action'),
		data: 	data,
		success: function(msg){
			if(form.attr('target')){
				form.find('input[type=submit]').attr('disabled',false).val(buttonlabel);
				$("#"+form.attr('target')).html(msg);
			}
		},
		error: function(xhr, ajaxOptions, thrownError){
			alert(xhr.status);
			alert(thrownError);
		}
	});
	
	form.find('input[type=submit]').attr('disabled',true).val('Bezig met verzenden..');
	return false;	
}


(function($) {
    $.fn.fadeIn = function(speed, callback) {
        return this.animate({opacity: 'show'}, speed, function() {
                if($.browser.msie){ $(this).get(0).style.removeAttribute('filter');}
                if ($.isFunction(callback) ) { callback.call(this) }
        });
    };

    $.fn.fadeOut = function(speed, callback) {
        return this.animate({opacity: 'hide'}, speed, function() {
                if ( $.browser.msie ){$(this).get(0).style.removeAttribute('filter'); }
                if ( $.isFunction(callback) ) { callback.call(this);}
        });
    };

    $.fn.fadeTo = function(speed, to, callback) {
        return this.animate({opacity: to}, speed, function() {
                if ( to == 1 && $.browser.msie ) { $(this).get(0).style.removeAttribute('filter');  }
                if ( $.isFunction(callback) )  { callback.call(this); }
        });
    };
})(jQuery);

