$(document).ready(function(){
	//menu fix for IE7-
	if($.browser.msie && parseInt($.browser.version)<8){
		$('ul.fullLengthMenu').each(function(){
			var $t=$(this),
				$newMenu=$('<table class="'+$t.attr('class')+'"><tr></tr></table>');
			$t.children().each(function(){
				$newMenu.find('tr').append('<td class="'+$(this).attr('class')+'">'+$(this).html()+'</td>');
			});
			$newMenu.find('a.cufon').each(function(){
				$(this).html($(this).text());
			});
			if($t.is('[id]')) $newMenu.attr('id',$t.attr('id'));
			$t.replaceWith($newMenu);
			Cufon.refresh();
		});
	}
	
	$('div.productCategoryHeader p').each(function(){
		var $t=$(this);
		$t.txt=$t.text();
		if($t.height()>55){
			var newTxt=$t.txt.slice(0,$t.txt.indexOf(' ', 270));
			if(newTxt!=$t.txt){
				if(newTxt.slice(-1)==',' || newTxt.slice(-1)=='-' || newTxt.slice(-1)==':' || newTxt.slice(-1)=='.') newTxt=newTxt.slice(0,-1);
				newTxt+='...';
				$t.text(newTxt);
			}
		}
	});
	
	$('a[id^=v_slidein]').each(function(){
		var $t=$(this).css({'display':'block','padding-bottom':'18px'});
			$t.block=$t.next('div').css({'display':'none','position':'relative','top':'-13px'});
			$t.hideLink=$t.block.find('a:last');
		$t.click(function(e){
			e.preventDefault();
			$t.block.slideDown(300);
		});
		$t.hideLink.click(function(e){
			e.preventDefault();
			$t.block.slideUp(300);
		});
	});
	
	$('a.notLink').click(function(e){
		e.preventDefault();
	});
	
	$('a.imgButtonPrint').click(function(e){
		e.preventDefault();
		window.print();
	});
	$('#menuTop li:has(ul) > a, #menuTop td:has(ul) > a').each(function(){
		var $t=$(this);
		/*$('body').append('<p>'+$t.parent().width()+'</p>')
		$('body').append('<p>'+Number($t.parent().width())/2+'</p>')
		$('body').append('<p>'+parseInt(Number($t.parent().width())/2)+'px'+'</p>')*/
		var n=parseInt(Number($t.parent().width())/2);
		$t.menu=$t.next('ul').css('padding-left',n+'px');
		if($.browser.msie && parseInt($.browser.version)<8) $t.menu.css('margin-left','-'+parseInt(Number($t.menu.width())/2)+'px');
		
		$t.bind('mouseenter',function(){
			$t.menu.fadeTo(350, 0.96);
		}).bind('mouseleave',function(){
			$t.menu.hide();
		});
		$t.menu.bind('mouseenter',function(){
			$t.menu.show();
		}).bind('mouseleave',function(){
			$t.menu.hide();
		});
	});
	
	$('#productList li.prodItem').each(function(){
		if($(this).find('span.oldPrice').length) $(this).addClass('withOldPrice');
		if($(this).find('div.optionSelect').length) $(this).find('div.pad').addClass('withOptions');
	}).filter(':odd').each(function(){
		var $t=$(this).addClass('odd').find('div.box:first'),
			$f=$(this).prev().find('div.box:first'),
			newH;
		$(window).load(function(){
			if(Number($t.height())<Number($f.height())) newH=Number($f.height());
			else newH=Number($t.height());
			$.each([$t,$f],function(){$(this).height(newH);});
		});
	});
	
	$('form: .submitLink').click(function(e){
		e.preventDefault();
		if(!$(this).parents('.outOfStock').length) $(this).parents('form').submit();
	});
	$('div.tab ul li').each(function(){
		$(this).prepend('<span class="imgTxt txtBullet"></span>');
	});
	
	$('#productTabs').pTabs()
	$('div.optionSelect').optionSelection();
	$('form .dynamicLabel').dynamicLabels();
	$('#content div.event h3').each(function(){
		var $t=$(this);
		$t.tempHTML=$t.html();
		if($t.tempHTML.charCodeAt(0)==8226){
			$t.tempHTML='<span class="bull">&bull;</span>'+$t.tempHTML.slice(1);
		}
		$t.html($t.tempHTML);
	});
	
	$('.customCheck').each(function(){
		var $t=$(this);
		$t.Label=$t.find('label:first').unbind();
		$t.Input=$t.find(':checkbox:first').unbind();
		if($t.Input.filter(':checked').length) $t.Label.addClass('checked');
		if($t.parents('tr.moduleRow').length){
			$t.Label.click(function(e){
				e.preventDefault();
			});
			$t.Input.click(function(e){
				e.preventDefault();
			});
			$t.parents('tr.moduleRow').find('td').click(function(){
				if($t.Input.filter(':checked').length){
					$t.Label.removeClass('checked');
					$t.Input.removeAttr('checked');
				}else{
					$t.Label.addClass('checked');
					$t.Input.attr('checked','checked');
				}
			});
		}else{
			$t.Label.click(function(e){
				e.preventDefault();
				if($t.Input.filter(':checked').length){
					$t.Label.removeClass('checked');
					$t.Input.click();
				}else{
					$t.Label.addClass('checked');
					$t.Input.click();
				}
			});
		}
	});
	
	/*$('.jumpSelect select').change(function(){
		location.href=location.protocol+'//'+location.host+'/'+$(this).attr('prefix')+$(this).val().replace;
	});*/
	
	$('#top5Carousel div.carousel').each(function(){
		$(this).jCarouselLite({
			btnPrev: $('#top5Carousel ul.carouselNavigation li.arrPrev a'),
			btnNext: $('#top5Carousel ul.carouselNavigation li.arrNext a'),
			auto:false,
			visible:1,
			speed:400,
			scroll:1
		});
	});

	//$("#homeSlider").scrollable({circular: true}).navigator({navi:'#homeSliderNav',idPrefix: 'btn'}).autoscroll({interval: 3000});
	$("#homeSlider").each(function(){
		var $t=$(this);
			$t.slides=$t.find('div.items > div');
			$t.nav=$('#homeSliderNav');
		var tmp=0, timer;
		$t.slides.each(function(){
			$t.nav.append('<a id="btn'+tmp+'" href="#'+tmp+'"></a>');
			tmp++;
		});
		$t.nav.find('a').click(function(e){
			e.preventDefault();
			$t.nav.find('.active').removeClass('active');
			$(this).addClass('active');
			$t.slides.fadeOut(1000);
			$t.slides.filter(':eq('+$(this).attr('href').slice(1)+')').fadeIn(1000);
		});
		$t.nav.find('a:first').click();
		timer=setInterval("if($('#homeSliderNav a.active').next().length){$('#homeSliderNav a.active').next().click()}else{$('#homeSliderNav a:first').click()}",5000);
		$t.bind('mouseenter',function(){
			clearInterval(timer);
		}).bind('mouseleave',function(){
			timer=setInterval("if($('#homeSliderNav a.active').next().length){$('#homeSliderNav a.active').next().click()}else{$('#homeSliderNav a:first').click()}",5000);
		});
		$t.nav.bind('mouseenter',function(){
			clearInterval(timer);
		}).bind('mouseleave',function(){
			timer=setInterval("if($('#homeSliderNav a.active').next().length){$('#homeSliderNav a.active').next().click()}else{$('#homeSliderNav a:first').click()}",5000);
		});
	});
	
	$('#hideMoreArticles').each(function(){
		if($(this).find('li').length>23){
			$(this).find('li:nth-child(n+24)').addClass('hidden');
			$('#showAll').click(function(e){
				e.preventDefault();
				$('#showAll').hide();
				$('#hideMoreArticles li.hidden').fadeIn(350).removeClass('hidden');
			}).show();
		}
	});
	
	$('select.autoRedirect').each(function(){
		if(location.pathname.length>1) $(this).find('option[value="'+location.pathname.slice(1)+'"]').attr('selected','selected');
	}).change(function(){
		if($(this).val().length>0) location.href=location.protocol+'//'+location.host + $(this).val();
	});
	
	$('div.simpleArticle > ul > li').prepend('<span class="imgTxt txtBullet"></span>');
	
	$('#accept_rules').each(function(){
		var $t=$(this);
		$t.click(function(){
			$t.parent().removeClass('inputRequirement');
		});
		$t.parents('form').submit(function(){
			if(!$t.filter(':checked').length){
				e.preventDefault();
				$t.parent().addClass('inputRequirement');
			}
		});
	});
	$('#content div.product ul.options > li:has(:radio:checked)').each(function(){
		if(Number($(this).find('span.product_qnty').text())<1) $(this).parents('div.product').addClass('outOfStock');
	});
	$('#content div.product ul.options > li').each(function(){
		var $t=$(this);
		$t.qty=Number($t.find('span.product_qnty').text());
		$t.prod=$t.parents('div.product');
		$t.find(':radio').click(function(){
			if($t.qty<1) $t.prod.addClass('outOfStock');
			else $t.prod.removeClass('outOfStock');
		});
	});
	
	$('.prodItem .price a, .relatedProducts .price span.submitLink, div.product a.addToCartButton').click(function(){
		if(!$(this).parents('.outOfStock').length){
			// old animation
			//$('<span class="effect"><!----></span>').appendTo($(this)).fadeOut(500,function(){$(this).remove()});
			
			// new animation
			var $t=$(this);
			
			var animationStyles={top:"-48px",marginLeft:"20px"};
			if($t.parents('ul.carousel').length /*|| $t.parents('div.relatedProducts').length*/){
				animationStyles={top:"-48px",marginLeft:"-20px"};
			}
			$t.animate(animationStyles,500,function(){
				$t.fadeOut(500,function(){
					$t.removeAttr('style');
				})
			});
		}
	});
	
	$('#content div.cartForm td.productListing-heading:first').addClass('first');
	$('#content div.cartForm td.productListing-data:has(:checkbox)').addClass('withCheckbox');
	$('#content div.cartForm td.productListing-data:has(:text)').addClass('withText');
	$('table.productListing td.productListing-data td.productListing-data:has(img)').addClass('withImage');
	$('table.productListing > tbody > tr:last').addClass('last');
	$('tr.infoBoxContents_button td.main:eq(1)').addClass('middle');
	$('div.standartForm input, div.standartForm select[name="country"]').each(function(){
		$(this).parent('td').parent('tr:not(.moduleRow)').addClass('withInput');
	});
	$('div.arrowTitles table td.main b').prepend('<span class="arr"></span>');
	$('td.main:has(input[name="sponsorship_email"])').addClass('sponsorMail');
	$('tr.withInput td:has(input[name="company"]) br:first,tr.withInput td:has(input[name="tva_intracom"]) br:first').hide();
	$('tr.withInput td:has(input[name="company"]) br:first,tr.withInput td:has(input[name="tva_intracom"]) br:first').hide();
	$('div.registrationForm td.main:has(:radio)').addClass('withRadio');
	$('tr.infoBoxContents_form > td:has(#defaultSelected)').addClass('infoBoxContents_td');
	$('textarea[name="comments"]').parent().addClass('pad6');
	$('tr.infoBoxContents_form td.main:has(:radio[name="payment"])').parent().parent().parent().parent().addClass('bottomPadding');
	$('table.planning').each(function(){
		var $toReplace=$(this).find('tr:first td:first');
		var colspan='';
		if($toReplace.attr('colspan')) colspan=' colspan="'+$toReplace.attr('colspan')+'"';
		$toReplace.replaceWith('<th'+colspan+'>'+$toReplace.html()+'</th>');
	});
	
	$('div.recipe div.pasteAtEnd').appendTo($('div.recipe'));

	if (typeof initialize == 'function') { // execute initialize function if defined
		initialize();
	}
});


Cufon.replace('.cufon',{hover:true, fontFamily:'GeosansLight'});

$.fn.pTabs=function(){
	return $(this).each(function(){
		var $t=$(this);
		if(!$t.find('.tabLinks').length){
			$t.tabs=$('<ul class="fullLengthMenu tabLinks nav"></ul>');
			var n=0;
			$('#content div.tab').each(function(){
				$t.tabs.append('<li><a href="#productTab_'+n+'">'+$(this).find('h2:first').html()+'</a></li>');
				$(this).find('h2:first').hide();
				$(this).attr('id','productTab_'+n++);
				$t.append($(this));
			});
			$t.prepend($t.tabs);
			if($.browser.msie && parseInt($.browser.version)<8){
				$('ul.fullLengthMenu').each(function(){
					var $t=$(this),
						$newMenu=$('<table class="'+$t.attr('class')+'"><tr></tr></table>');
					$t.children().each(function(){
						$newMenu.find('tr').append('<td class="'+$(this).attr('class')+'">'+$(this).html()+'</td>');
					});
					$newMenu.find('a.cufon').each(function(){
						$(this).html($(this).text());
					});
					if($t.is('[id]')) $newMenu.attr('id',$t.attr('id'));
					$t.replaceWith($newMenu);
					Cufon.refresh();
				});
			}
		}
		
		$t.tabs=$t.find('.tabLinks');
		$t.tabs.links=$t.tabs.find('a');
		$t.tabs.links.filter(':last').addClass('last');
		$t.tabs.links.each(function(){
			$t.show();
			if(Number($(this).height())>15) $(this).addClass('twoLineTab');
			$t.hide();
			var tabId=$(this).attr('href');
			$(this).attr('tab_id',tabId.slice(tabId.indexOf('#')));
		});
		$t.tabs.links.filter(':first').addClass('active first');
		$t.find($t.tabs.links.filter('.active').attr('tab_id')).addClass('activeTab');
		$t.tabs.links.click(function(e){
			e.preventDefault();
			if($(this).hasClass('active')) return $(this);
			$t.tabs.find('.active').removeClass('active');
			$t.find('.activeTab').removeClass('activeTab');
			$(this).addClass('active');
			$t.find($(this).attr('tab_id')).addClass('activeTab');
		});
		$t.show();
	});
};

$.fn.optionSelection=function(){
	$.fn.extend({
		setCurrent:function($cur){
			var $t=this, opt=$cur.find('span.value').text(), price=$cur.find('span.price').text(),id=$cur.find('span.option_value_id').text();
			$t.curr.text(opt);
			$t.opt.text(opt);
			$t.curPrice.text(price);
			$t.hidId.val(id);
			if(Number($cur.find('span.product_qnty').text())<1) $t.prod.addClass('outOfStock');
				else $t.prod.removeClass('outOfStock');
			//if($cur.find('span.out').length && !$t.prod.find('div.addToCart div.out').length) $t.prod.find('div.addToCart').prepend('<div class="out">'+$cur.find('span.out').text()+'</div>');
			if($cur.find('span.old').length){
				if(!$t.prod.find('div.addToCart span.oldPrice').length) $t.prod.find('div.addToCart span.price').append('<span class="oldPrice">'+$cur.find('span.old').text()+'</span>');
					else $t.prod.find('div.addToCart span.oldPrice').text($cur.find('span.old').text()).show();
				$t.prod.addClass('withOldPrice');
			}else{
				$t.prod.removeClass('withOldPrice').find('div.addToCart span.oldPrice').hide();
			}
			
			
			$t.optList.hide().find('li.curr').removeClass('curr');
			$cur.parent().addClass('curr');
		}
	});
	return $(this).each(function(){
		var $t=$(this);
		$t.prod=$t.parents('li.prodItem');
		$t.optList=$t.find('ul');
		$t.opt=$t.prod.find('div.addToCart span.option');
		$t.curPrice=$t.prod.find('div.addToCart span.curPrice');
		$t.hidId=$t.prod.find('input.option_value_id');
		var $curOtion=$t.optList.find('li:first').addClass('curr');
		$t.curr=$t.find('a.current');
		$t.setCurrent($t.optList.find('li:first a'));
		$t.curr.click(function(e){
			e.preventDefault();
			$t.prod.css('z-index', '10');
			$t.optList.fadeIn(350);
		});
		$t.find('span.arrow').click(function(){
			if($t.optList.filter(':visible').length) $t.optList.hide();
			else $t.curr.click();
		});
		$t.optList.find('a').click(function(e){
			e.preventDefault();
			$t.setCurrent($(this));
		});
		$t.bind('mouseleave',function(){
			$t.optList.hide();
			$t.prod.removeAttr('style');
		});
	});
};

$.fn.dynamicLabels=function(){
	return $(this).each(function(){
		var $t=$(this);
		$t.label=$t.find('label');
		$t.input=$t.find('input');
		if($t.input.val()!='') $t.label.hide();
		$t.input.focus(function(){
			 $t.label.hide();
		});
		$t.input.blur(function(){
			if($t.input.val()=='') $t.label.show();
		});
	});
};

if(!window.console || !console.firebug){
	var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	window.console = {};
	for(var i = 0; i < names.length; ++i){
		window.console[names[i]]=function(){}
	}
}

function openLink(title, path, src) {

	summary = $('.description p').text();
	/*var fbPath = 'http://www.facebook.com/sharer.php?s=100'+
				'&p[title]=' + title +
				'&p[url]='+escape(path)+
				'&p[images][0]='+escape(src)+
				'&p[summary]='+summary;*/
	var fbPath = 'http://www.facebook.com/sharer.php?s=100'+
				'&p[title]=' + unescape( encodeURIComponent( title ) ) +
				'&p[url]='+escape(path)+
				'&p[images][0]='+escape(src)+
				'&p[summary]='+unescape( encodeURIComponent( summary ) );
	window.open(fbPath);
}
