$(document).ready(function() {

	$('.imageBlur').click(function() {
		$(this).parent().find('img').click();
	});


	/* NAVIGATION über jQuery */

	$('#navSearch').click(function() {
		$('#search_block_top').toggle();
		$('#search_query_top').focus();
		return false;
	});

	checkUrl(location.href);

	function checkUrl(currentLink) {
		var valids       = new Array(['suche'], ['blog'], ['home']);
		var naviSections = new Array('#navSearch', '#navNews', '#navHome');
		var foundAt      = 666;

		limit = valids.length;

		//alert(currentLink);

		for(var i = 0; i < limit; i++) {
			elementSize = valids[i].length;
			for(var j = 0; j < elementSize; j++) {
				splits = currentLink.split(valids[i][j]);
				if(splits.length > 1) {
					$('#mainNavi').find('a.active').removeClass('active');
					$(naviSections[i]).addClass('active');
					foundAt = i;
				}
			}
		}

		if(foundAt == 666) {
			$('#mainNavi').find('a.active').removeClass('active');
			$('#navShop').addClass('active');
		}
	}



	var lastAct = false;
	$('#new-products_block_right li').hover(function() {
		$('#new-products_block_right .pDesc').hide();
		$('#new-products_block_right li').removeClass('active');
		actClass = $(this).attr('class');
		$('.'+actClass+'_text').show();
		$('.'+actClass).addClass('active');

	}, function() {

	});

	lastAct = $('#new-products_block_right li:first').attr('class');
	$('#new-products_block_right li:first').addClass('active');
	$('#new-products_block_right .products div:first').show();

	//to do not execute this script as much as it's called...
	if(!$('ul.tree.dhtml').hasClass('dynamized'))
	{
		//add growers to each ul.tree elements
		$('ul.tree.dhtml ul').prev().before("<span class='grower OPEN'> </span>");

		//dynamically add the '.last' class on each last item of a branch
		$('ul.tree.dhtml ul li:last-child, ul.tree.dhtml li:last-child').addClass('last');

		//collapse every expanded branch
		$('ul.tree.dhtml span.grower.OPEN').addClass('CLOSE').removeClass('OPEN').parent().find('ul:first').hide();
		$('ul.tree.dhtml').show();

		//open the tree for the selected branch
			$('ul.tree.dhtml .selected').parents().each( function() {
				if ($(this).is('ul'))
					toggleBranch($(this).prev().prev(), true);
			});
			toggleBranch( $('ul.tree.dhtml .selected').prev(), true);

		//add a fonction on clicks on growers
		$('ul.tree.dhtml span.grower').click(function(){
			toggleBranch($(this));
		});
		//mark this 'ul.tree' elements as already 'dynamized'
		$('ul.tree.dhtml').addClass('dynamized');

		$('ul.tree.dhtml').removeClass('dhtml');

		$('#categories_block_left span.OPEN').parent().find('a:first').addClass('active');
	}

	$('.thePrice, .shippingcosts').each(function() {
		price = $(this).html();
		tmp = price.split(',');

		if(tmp.length > 1) $(this).html(tmp[0] + ',' + tmp[1].substr(0, 2));
	});

	$('.shippingcosts, .taxcosts, .carrier_price .price').each(function() {
		/*
prechk = $(this + ':contains(",")').length;
		
		if(!prechk) {
			$(this).html($(this).html() + ',-');
		}
*/
		
		price = $(this).html();
		tmp = price.split(',');

		if(tmp[1].length > 2)	{
			$(this).html(tmp[0] + ',' + tmp[1].substr(0, 2));
		} else if(tmp[1].length < 2) {
			$(this).html(tmp[0] + ',' + tmp[1] + '0');
		}
	});
	
	$('.prodPrice, .carrier_price .price, .prodPriceSingle').each(function() {
		price = $(this).html();
		$(this).html(price.replace('.', ','));
	});


	$('.payment_module a img').attr('align', 'left');

	$('.taxInfoLink').click(function() {
		window.location = 'http://www.crazy-outfits.de/de/content/6-versandkosten';
	});

	$('#doCheckout').click(function() {
		$('#hiddenCheckout').slideDown();
		$('#cartButtonsWrap').hide();
	});

	$('.cart_availability img').hover(function(e) {
		$('#availLegend').css({
			'top': e.pageY - 80,
			'left': e.pageX + 20
		}).show();
	},function(e) {
		$('#availLegend').hide();
	});

	$('#product_list li:nth-child(4n)').css('margin-right', 0);

	if($('#thumbs_list_frame li').length == 8) $('#view_scroll_right').hide();


	// Currency Bug oder so wat weiss ich mir auch latte…
	priceText = $('.finalPrice').find('#our_price_display').html();
	if(priceText && priceText != 'NULL') {
		checkText = priceText.split('€');
		if(checkText.length < 2) $('.currencySign').show();
	}
});



//animate the opening of the branch (span.grower jQueryElement)
function openBranch(jQueryElement, noAnimation) {
		jQueryElement.addClass('OPEN').removeClass('CLOSE');
		if(noAnimation)
			jQueryElement.parent().find('ul:first').show();
		else
			jQueryElement.parent().find('ul:first').slideDown();
}
//animate the closing of the branch (span.grower jQueryElement)
function closeBranch(jQueryElement, noAnimation) {
	jQueryElement.addClass('CLOSE').removeClass('OPEN');
	if(noAnimation)
		jQueryElement.parent().find('ul:first').hide();
	else
		jQueryElement.parent().find('ul:first').slideUp();
}

//animate the closing or opening of the branch (ul jQueryElement)
function toggleBranch(jQueryElement, noAnimation) {
	if(jQueryElement.hasClass('OPEN'))
		closeBranch(jQueryElement, noAnimation);
	else
		openBranch(jQueryElement, noAnimation);
}
