var bagHandler = function (event) {
	Enterprise.TopCart.hideCart();
	return false;
};
	
jQuery.noConflict();

jQuery.fn.anchorAnimate = function(settings) {
 	settings = jQuery.extend({
		speed : 500
	}, settings);
	
	return this.each(function(index, item){
		var caller = this;
		jQuery(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = jQuery(caller).attr("href")
			
			var destination = jQuery(elementClick).offset().top - 16;
			jQuery("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	});
};

jQuery.fn.productsScroll = function (where, settings) {
 	settings = jQuery.extend({
		speed : 500,
		visibleItems: 6
	}, settings);

 	if (jQuery(this).data('productscroll') == undefined) {
 		var el = jQuery(this).find('li:first-child');
 		var margin = (isNaN(parseInt(jQuery(el).css('marginLeft'))) ? 0 : parseInt(jQuery(el).css('marginLeft')));
 		margin += (isNaN(parseInt(jQuery(el).css('marginRight'))) ? 0 : parseInt(jQuery(el).css('marginRight')));
 		var width = jQuery(el).width() + margin;
 		jQuery(this).data('productscroll', {
 											'item': 0,
 											'items': jQuery(this).find('li').length,
 											'width': width, //outerWidth(true),
 											'ul': jQuery(this).find('ul'),
 											'prev': jQuery(this).find('.prev'),
 											'next': jQuery(this).find('.next'),
											'left': (isNaN(parseInt(jQuery(this).find('ul').css('left'))) ? 15 : parseInt(jQuery(this).find('ul').css('left'))) //+ (jQuery.browser.webkit ? margin : 0)
 											});
 	}
 	var data = jQuery(this).data('productscroll');
 	
 	if (where == -1 && jQuery(data.prev).hasClass('prev-inactive')) {
 		return this;
 	}
 	if (where == 1 && jQuery(data.next).hasClass('next-inactive')) {
 		return this;
 	}
 	
 	data.item += where;
 	if (data.item == 0) {
 		jQuery(data.prev).addClass('prev-inactive');
 	} else {
 		jQuery(data.prev).removeClass('prev-inactive');
 	}
 	if (data.item == (data.items-settings.visibleItems)) {
 		jQuery(data.next).addClass('next-inactive');
 	} else {
 		jQuery(data.next).removeClass('next-inactive');
 	}
 	
 	//alert(data.width);
 	data.left += (data.width*(-where));
 	//alert(data.left);
 	jQuery(data.ul).stop().animate({'left': data.left + 'px'}, settings.speed);

 	jQuery(this).data('productscroll', data);
 	return this;
}

var timers = [];
var timersi = 0;
var timerso = [];
function hover (item) {
	jQuery(timerso[item]).addClass('hover');
}

jQuery(document).ready(function($) {
	/*var hoverFix = '.header .nav-container #nav li';
	$(hoverFix).bind('mouseenter mouseleave', function() {
		$(this).toggleClass('hover');
	});*/
	
	// header
	$('#nav ul.level0').each(function (index, item) {
		var wrap = $('<div/>').addClass('level0').append($('<div/>').addClass('container'));
		$(item).wrap(wrap).after($('<div/>').addClass('clear'));
		
		var height = $(item).parent('.container').height();
		$(item).find('> li').height(height);
		var div0 = $(item).parents('div.level0');
		var li0 = $(item).parents('li.level0');
		if ($(div0).position().left > $(li0).position().left) {
			$(div0).css({'left': $(li0).position().left, 'right': 'auto'});			
		}
	});
	$('.header-container .header-panel ul.links:last-child').addClass('last');
	
	$('#nav li.level0').mouseenter(function () {
		timersi++;
		timerso[timersi] = this;
		timers[this] = setTimeout('hover(' + timersi + ');', 100);
	})
	$('#nav li.level0').mouseleave(function () {
		jQuery(this).removeClass('hover');
		clearTimeout(timers[this]);
	});
	$('#nav').addClass('processed');
	
	if (true) {

	$('#top-cart').click(function (event) {
		if ($(this).find('.block-title').hasClass('expanded')) {
			$('body').unbind('click', bagHandler);
			$('body').addClass('top-cart-bind').bind('click', bagHandler);
		} else {
			$('body').removeClass('top-cart-bind').unbind('click', bagHandler);
		}
		//if (jQuery(event.target).parents('#topCartContent').length) {
			$('body').removeClass('top-cart-bind').unbind('click', bagHandler);
			return true;
		//} else {
		//	return false;
		//}
	});
	
	
	// footer
	var el = $('.footer .block:first-child');
	var height = $('.footer').outerHeight() - (parseInt($(el).css('padding-top'))+parseInt($(el).css('padding-bottom')));
	$('.footer .block').height(height);
	
	
	// paragrafs
	$('.main p:last-child').addClass('last');
	

	// products-lists
	$('.products-list-big li.item:nth-child(2n)').addClass('item-lastColumn').parent('.products-list-big').addClass('processed');

	$('.products-list:not(.processed) li.item:nth-child(4n)').each(function (index, item) {
		 $(item).addClass('item-lastColumn').next('li').addClass('item-firstColumn');
	}).parent('.products-list').addClass('processed');
	$('.products-list li.item:last-child').prevUntil('.item-lastColumn').andSelf().addClass('item-lastRow');
	
	
	// product-view-tabs
	$('.collateral-tabs').each(function (index, item) {
		$(item).find('.tab').each(function (index, item) {
			$(item).click(function (event) {
				var index = $(this).prevAll('.tab').length;
				//$(this).parent('dl').find('.tab').not($(this)).removeClass('tab-active');
				var dl = $(this).parent('dl');
				var container = $(dl).find('.container');
				$(dl).find('.tab.active').removeClass('active');
				var dt = $(this).addClass('active');
				$(dl).find('.tab-container.active').removeClass('active');
				var dd = $(dl).find('.tab-container').eq(index).addClass('active');
				//$(container).animate({'height': ($(dt).outerHeight()+$(dd).outerHeight()) + 'px'});
				$(container).animate({'height': $(dd).outerHeight() + 'px'});
			});
		});
		$(item).find('.tab:first-child').addClass('tab-first').trigger('click');
		$(item).find('.tab:last-child').addClass('tab-last');
	});
	
	// product-view-read-more-link
	$('.product-view .product-shop a.more').anchorAnimate();
	$('.product-view .product-shop a.more').click(function () {
		$('#collateral-tabs .tab:first-child').trigger('click');
	});
	
	// product-view colorbox
	//$('.colorbox').colorbox({rel: 'product'});
	
	// price-currency-fix
	$('.price-box .price').each(function (index, item) {
		$(item).html($(item).html().replace(/,00/g, '').replace(/&nbsp;kr/g, ':-'));
	});
	
	// products-scroll
	$('.products-scroll').each(function (index, item) {
		var ul = $(item).find('ul');
		$(ul).wrap($('<div/>').addClass('container').height($(ul).outerHeight()));
		if ($(item).find('li').length > 6) {
			var prev = $('<div/>').addClass('prev prev-inactive').click(function () {
				$(this).parents('.products-scroll').productsScroll(-1);
				return false;
			});
			var next = $('<div/>').addClass('next').click(function () {
				$(this).parents('.products-scroll').productsScroll(1);
				return false;
			});
			$(ul).before($(prev));
			$(ul).after($(next));
		}
	});
	}
	
	
	// fade out the bag and hide cart-link in header, if no items found in it
	if ($('#top-cart .block-title').hasClass('no-items')) {
		$('.header-cart').addClass('no-items');
	}
	

	// statium-style-level2-js
	jQuery('#nav li.level1 > a').click(function (event) {
		window.location = jQuery(this).attr('href');
		window.location.reload();
		return true;
	});
	
	jQuery('#nav-left li.active > ul').addClass('open');
	
	jQuery('#nav-left li.level1').each(function (index, item) {
		if (jQuery(item).find('> ul').length > 0) {
			jQuery(item).find('> a').click(function (event) {
				var ul = jQuery(this).parent().find('ul.level1');
				jQuery('#nav-left ul.level1.open').not(ul).toggle('fast', 'linear').toggleClass('open');
				jQuery(ul).toggle('fast', 'linear').toggleClass('open');
				window.location.hash = jQuery(this).attr('href').replace(/http:\/\/.*?\//, '');
				return false;
			});
		}
	});
	
	if (window.location.hash.substring(1).length > 0) {
		jQuery('#nav-left li.level1 a[href$='+window.location.hash.substring(1)+']').each(function (index, item) {
			jQuery(item).trigger('click');
		});
	}

});

jQuery(window).load(function() {
	// slideshows
	jQuery('.slideshow').each(function (index, item) {
		jQuery(item)
			.append('<div id="slideshow-pager-' + index + '" class="slideshow-pager">')
			.append('<div id="slideshow-prev-' + index + '" class="slideshow-prev" style="display:none">')
			.append('<div id="slideshow-next-' + index + '" class="slideshow-next" style="display:none">')
			.cycle({
				slideExpr: 'img',
				speed: 1000,
				timeout: 6000,
				fx: 'fade',
				pager: '#slideshow-pager-' + index,
				prev: '#slideshow-prev-' + index,
				next: '#slideshow-next-' + index
			});
	});
	
	jQuery('.slideshow').mouseenter(function () { 
		jQuery('.slideshow-prev, .slideshow-next').each(function () {
			jQuery(this).fadeIn('fast');
		});
	});
	
	jQuery('.slideshow').mouseleave(function () { 
		jQuery('.slideshow-prev, .slideshow-next').each(function () {
			jQuery(this).fadeOut('fast');
		});
	});
	
	if (jQuery('#messages_product_view .messages li.success-msg').length > 0) {
		jQuery('body').unbind('click', bagHandler);
		jQuery('body').addClass('top-cart-bind').bind('click', bagHandler);
		Enterprise.TopCart.showCart(7);
	}
});

