var addthis_config =
{
	ui_language: 'sv'
}

$(function() {
	
	var menuTimers = new Array();
	
	$.scrollTo.defaults.axis = 'xy';
	
	$('#mainmenu > li').each(function(i) {
		$(this).hover( 
			function() { 
				$('#mainmenu > li').children('ul').hide();
				$(this).children('ul').show();
				clearTimeout(menuTimers[i]);
			}, function() {
				var obj = $(this).children('ul');
			
				menuTimers[i] = setTimeout(function() { $(obj).hide(); }, 500);
			} 
		);
	})
	
	new styleBrowsing();
	new newsscroller(5000);
	new prodhili(7000);
	new prodcat();
	
	if ($('#firstpage').size() == 0) {

		var contentHeight = $('#content').height();
		var columnHeight = $('#column-right').height();
		var sidebarHeight = $('#sidebar').height();
		
		if (contentHeight > columnHeight && contentHeight > sidebarHeight) {
			
			$('#sidebar').css('height', contentHeight);
			$('.box:last', $('#column-right')).css('border-bottom', 'none');
			var newColumnHeight = (contentHeight - columnHeight + 2)+'px';
			var newColumn = $('<div></div>').css({ 
										'background': '#fff', 
										'border': '1px solid #ddd',
										'border-top': 'none', 
										'height': newColumnHeight,
										'margin-top': '0px',
										'padding': '12px 0',
										'width': '326px' }).appendTo('#column-right');
										
		} else if (sidebarHeight > contentHeight && sidebarHeight > columnHeight) {
		
			$('#content').css('height', (sidebarHeight)+'px');
			var newColumnHeight = (sidebarHeight - columnHeight + 2)+'px';
			$('.box:last', $('#column-right')).css('border-bottom', 'none');
			var newColumn = $('<div></div>').css({ 
										'background': '#fff', 
										'border': '1px solid #ddd',
										'border-top': 'none', 
										'height': newColumnHeight,
										'margin-top': '0px',
										'padding': '12px 0',
										'width': '326px' }).appendTo('#column-right');
			
		
		} else {
			
			$('#sidebar').css('height', (columnHeight-26)+'px');
			$('#content').css('height', (columnHeight-26)+'px');
										
		}

	}
	
	$('.tooltip').css('cursor', 'help').tooltip({ delay: 0, showURL: false,	track: true, extraClass: "big" });
	$('img', $(('.product-category'))).tooltip({ delay: 0, showURL: false,	track: true });
	
});

var styleBrowsing = function() {
	sb = this;
	this.currentArticle = 0;
	this.articleCount = $('.style-article').size();
	this.autoScrolling = true;

	$('#style-articles').scrollTo(0, 0);

	var styleLinkList = $('<ul id="style-article-links" class="box-navi-links"></ul>');
	$(styleLinkList).appendTo('#style-articles');
	
	var bullets = "";
	var counter = 1;
	
	$('.style-article').each(function(i) {
		var article = this;
		var articleIndex = i;
		var bullet = (counter == 1) ? $('<li class="active"></li>') : bullet = $('<li></li>');
			
		var link = $('<a>link</a>').click(function() {
			sb.autoScrolling = false;
			$(this).parent().parent().children('li').removeClass('active');
			$(this).parent().addClass('active');
			$('#style-articles').scrollTo($(article), 500);
			sb.currentArticle = i;
		});
		
		$(link).appendTo(bullet);		
		$(bullet).appendTo('#style-article-links');
		
		counter++;
	});
	
	$(styleLinkList).appendTo('#style-wrapper');
	
	
	if (typeof(IE6) === 'undefined') {
		$('<div id="style-article-navi" class="png"></div>').appendTo('#style-wrapper');
		$('#style-article-navi').html('<img class="navi-left png" src="/images/arrow-left.png" alt="Föregående" /><img class="navi-right png" src="/images/arrow-right.png" alt="Nästa" />');
	} else {
		$('img', $('.style-article')).click(function() { sb.next(); }).css('cursor', 'pointer');
	}
		
	$('.navi-left').click( function() { sb.prev(); } );
	$('.navi-right').click( function() { sb.next(); } );
	
	$('#style-wrapper').hover(function() { $('#style-article-navi').show(); }, function() { $('#style-article-navi').hide(); });
	
	setTimeout(function() {
		sb.autoScroll();
	}, 4000);
	
	styleBrowsing.prototype.autoScroll = function() {
		if (!sb.autoScrolling)
			return false;
	
		sb.next();
		
		sb.autoScrolling = true;
		
		setTimeout(function() {
			sb.autoScroll();
		}, 4000);
	}
	
	styleBrowsing.prototype.next = function() {
		sb.autoScrolling = false;
	
		sb.currentArticle++;
		
		if (sb.currentArticle == sb.articleCount)
			sb.currentArticle = 0;
			
		$('#style-articles').scrollTo($('.style-article').eq(sb.currentArticle), 500);
		
		$('#style-article-links').children('li').removeClass('active').eq(sb.currentArticle).addClass('active');		
	}
	
	styleBrowsing.prototype.prev = function() {
		sb.autoScrolling = false;
	
		sb.currentArticle--;
		
		if (sb.currentArticle < 0)
			sb.currentArticle = sb.articleCount-1;
			
		$('#style-articles').scrollTo($('.style-article').eq(sb.currentArticle), 500);
		
		$('#style-article-links').children('li').removeClass('active').eq(sb.currentArticle).addClass('active');			
	}
}

var prodcat = function() {

	pc = this;
	this.activeCat = new Array();
	this.catSize = new Array();

	$('.product-category-wrapper').each(function(index) {
		
		var wrapper = this;
		var prodcatCount = $('.product-category', this).size();
		pc.catSize[index] = prodcatCount;
		
		if (prodcatCount > 1) {
		
			pc.activeCat[index] = 0;
		
			var list = $('<ul id="product-category-navi-'+(index+1)+'" class="box-navi-links"></ul>');
		
			list.appendTo(wrapper);
			
			var items = '';
			for (i=0; i<prodcatCount; i++)
				items = items + '<li><a>'+(i+1)+'</a></li>';
					
			list.html(items).children('li').eq(0).addClass('active');
			list.children('li').each(function(i) {
				$(this).click(function() {
					list.children('li').removeClass('active').eq(i).addClass('active');
					$('.product-category', wrapper).hide().eq(i).show();
					pc.activeCat[index] = i;
				});
			});
			
			$('img', wrapper).css('cursor', 'pointer').each(function(i) {
				$(this).click(function() {
					pc.activeCat[index]++;
					
					if (pc.activeCat[index] == pc.catSize[index])
						pc.activeCat[index] = 0;
					
					list.children('li').removeClass('active').eq(pc.activeCat[index]).addClass('active');
					$('.product-category', wrapper).hide().eq(pc.activeCat[index]).show();
				});
			});
		}
		
	});

}

var prodhili = function(interval) {

	if (typeof(interval) == 'undefined')
		interval = 2000;
		
	ph = this;
	this.activeProd = new Array();
	this.timerIds = new Array();
	this.prodHiliCount = new Array();
	this.customInterval = false;
		
	$('.product-highlight-wrapper').each(function(i) {
	
		ph.customInterval = false;
	
		var classList = $(this).attr('class');
		classList = classList.split(' ');
		$.each(classList, function(index, value) {
			if (value.search('time') >= 0)
				ph.customInterval = value.split('-')[1];
		});
	
		if ($('.product-highlight', this).size() > 1) {	
	
			var context = this;
			
			ph.activeProd[i] = 0;
			if (!ph.customInterval)
				ph.timerIds[i] = setInterval(function() { ph.nextProdHili(context, i) }, interval);
			else
				ph.timerIds[i] = setInterval(function() { ph.nextProdHili(context, i) }, ph.customInterval);
			ph.prodHiliCount[i] = $('.product-highlight', context).size();
			
			prodhili.prototype.nextProdHili = function(context, index) {
			
				$('.product-highlight', context).eq(ph.activeProd[index]).fadeOut(function() {
					ph.activeProd[index]++;
					
					if (ph.activeProd[index] == ph.prodHiliCount[index])
						ph.activeProd[index] = 0;
					
					$('.product-highlight', context).eq(ph.activeProd[index]).fadeIn();	
				});
					
			}
		
		}
	
	});

}

var newsscroller = function(interval) {

	if (typeof(interval) == 'undefined')
		interval = 2000;

	this.newsScrollers = new Array();
	ns = this;

	$('.news-scroller').each(function() {
		var scroller = this;
		
		$('.stories > h3 > a', scroller).each(function(i) {
			$(this).hover(function() {
				// Stop loop
				var scrollerId = $(scroller).attr('id');
				clearInterval(ns.newsScrollers[scrollerId][3]);
			
				$('.image > img', scroller).hide().eq(i).show();
				$('.stories > h3 > a', scroller).removeClass('active');
				$(this).addClass('active');
			}, function() {
				//$(this).removeClass('active');
				//$('.image > img', scroller).hide().eq(0).show();
				//$('.stories > h3 > a', scroller).eq(0).addClass('active');
			});
		});
	});
	
	$('.news-scroller').each(function(i) {
		var scroller = this;
		var scrollerId = 'newsscroller-'+(i+1);
		var storyCount = $('.stories > h3', this).size();
		
		$(this).attr('id', scrollerId);
		
		var t = setInterval(function() { ns.nextScrollerStory(scroller); }, interval);
		
		ns.newsScrollers['newsscroller-'+(i+1)] = new Array(scrollerId, 1, storyCount, t);
		
	});
	
	newsscroller.prototype.nextScrollerStory = function(scroller) {
		var id = scroller.id;
		var nextItem = this.newsScrollers[id][1];
		var itemCount = this.newsScrollers[id][2];

		$('.image > img', scroller).hide().eq(nextItem).show();
		$('.stories > h3 > a', scroller).removeClass('active').eq(nextItem).addClass('active');
		
		ns.newsScrollers[id][1]++;
		
		if (ns.newsScrollers[id][1] == itemCount)
			ns.newsScrollers[id][1] = 0;
	}

}