(function($){

var handle_tracker = function() {
	$("a[href^='http://'], a[href^='https://']").not('[href^="' + cms_systempath + '"]')
		.addClass("offSite")
		.attr('target', '_blank')
		.bind('click keypress', function(ev) {
			var code=ev.charCode || ev.keyCode;

			if((!code || (code && code == 13)) && pageTracker !== undefined) {
				pageTracker._trackEvent('Outbound', $(this).context.hostname, $(this).attr('href'));
			}

			return false;
		});
}

var handle_facebook = function() {
	if (!$('#fb-root').length)
		return;

	window.fbAsyncInit = function() {
		FB.init({
			appId: '223752184320128',
			status: true,
			cookie: true,
			xfbml: true
		});
	};

	var e = document.createElement('script'); e.async = true;
	e.src = document.location.protocol + '//connect.facebook.net/pl_PL/all.js';
	document.getElementById('fb-root').appendChild(e);
};

var handle_menu_width = function(){
	var menu = $('#menu');
	if (!menu.length) return;

	var width = $('#menu').width();
	var li = $('#menu ul.lvl_1 li.lvl_1');
	var links_width = 0;
	li.each(function(){
		links_width += $(this).outerWidth(true);
	});
	if(width > links_width){
		var space_width = width - links_width;
		var padding = parseInt((space_width/2)/li.length);
		$('#menu ul.lvl_1 li.lvl_1 a.lvl_1').css('padding', '0px '+padding+'px');
	}

	$('#menu li.lvl_1').mouseover(function() {
		$('ul', this).css('min-width', $(this).width());
	});
}

var handle_cycle_img = function(){
	var galleries_cycle = $('.img-container').cycle({ 
		fx:     'fade', 
		speed:	1000, 
		timeout: 3000, 
		pager:  $('.img-nav'),
		pagerAnchorBuilder: function(index){
			return $('.img-nav').find('a').get(index);
		},
		pauseOnPagerHover: 1,
		before: function(){
		
		},
		after: function(){
		
		},
		pause: 1
	});
}

var handle_cufon = function(){

//	.uc p, .uc a, .uc li, .uc h1, .uc h2, .uc h3, .uc h4, .uc h5, .uc h6

	Cufon.replace($('.cufonAller'), {
		hover: true,
		fontFamily: 'Aller'
	});
	
	Cufon.replace($('.cufonAllerIt'), {
		hover: true,
		fontFamily: 'AllerIt'
	});	
	
	Cufon.replace($('.cufonAllerBd'), {
		hover: true,
		fontFamily: 'AllerBd'
	});		
	
	Cufon.replace($('.cufonAllerShadow'), {
		hover: true,
		fontFamily: 'Aller',
		textShadow: '#605a5a 1px 1px'
	});	
	
	Cufon.replace($('.cufonAllerShadow1'), {
		hover: true,
		fontFamily: 'Aller',
		textShadow: '#000 1px 1px'
	});	
	Cufon.now();
}

var handle_contact = function() {
	var form = $('#offer-form');
	var alerts_container = $('#information-form-alerts');
	form.validate({
		errorPlacement: function(error, element) {
			error.insertAfter(element.parent('div'));
		}		
	});
	
	$('a.delete', form).click(function(ev){
		ev.preventDefault();
		form.resetForm();	
		return false;
	});
	
	$('a.submit', form).click(function(ev) {
		ev.preventDefault();

		if (!form.valid())
			return false;
		
		var submit = $(this);	
		var data = {
			action: $('input[name=action]', form).val(),
			company_name: $('input[name=company_name]', form).val(),
			company_address: $('input[name=company_address]', form).val(),
			person: $('input[name=person]', form).val(),
			store_average_pallets: $('select[name=store_average_pallets]', form).val(),
			store_max_pallets: $('input[name=store_max_pallets]', form).val(),
			average_pallet_height: $('select[name=average_pallet_height]', form).val(),
			average_pallet_weight: $('select[name=average_pallet_weight]', form).val(),
			sku_items: $('select[name=sku_items]', form).val(),
			receiving_goods_docs: $('select[name=receiving_goods_docs]', form).val(),
			wz_items: $('select[name=wz_items]', form).val(),
			sku_in_warehouse_release: $('select[name=sku_in_warehouse_release]', form).val(),
			transports_per_day: $('select[name=transports_per_day]', form).val(),
			customs_inspection_per_month: $('select[name=customs_inspection_per_month]', form).val(),
			good_specyfication: $('input[name=good_specyfication]', form).val(),
			url: location.href
		};
		
		$('p.status', alerts_container).hide();
		$('p.wait', alerts_container).show();
		submit.hide();
		form.hide();
			
		$.post(submit.attr('href'), data, function(resp) {
			if (resp == 'err') {
				$('p.status', alerts_container).hide();
				$('p.error', alerts_container).show();
			}
			else if (resp == 'ok') {
				$('p.status', alerts_container).hide();
				$('p.ok', alerts_container).show();
			}
		}); 
		
		return false;
	});		
}

$(function() {
	handle_tracker();
	handle_facebook();
	handle_cycle_img();
	handle_cufon();
	handle_menu_width();	
	handle_contact();
	
	$('#search-form input').focus(function(){
		$(this).val('');
	});

});

})(jQuery);

