var hoverslider;
$.fn.hoverslide = function(){
	function init(){
		$(this).mouseover(function(){
			$(this).addClass('hover');
			hoverslider = setInterval(function(){
				$('.section.hover').each(function(){
					if($('.bottoms img', this).length>1){
						$('.bottoms img:last', this).fadeOut(800, function(){
							$(this).prependTo($(this).parent());
						}).prev().fadeIn();
					}
				});
			}, 200);
		}).mouseout(function(){
			$(this).removeClass('hover');
			clearInterval(hoverslider);
		});
	}
	return this.each(init);
};
$.fn.carousel = function(){
	function init(){
		var list = $(this);
		var items = $('>*', list);
		var active = items.first().addClass('active');
		var itemwidth = active.width();
		var itemheight = active.height();
		list.css({'position':'relative', 'height': active.height()-0+100+'px'});
		items.css({'position':'absolute'});
		//active.css({'left':'50%', 'margin-left': -1*active.width()/2+'px'});
		centerActive();
		var prevlink = $('<a href="#">Previous</a>').click(slidePrev).insertAfter(list);
		var nextlink = $('<a href="#">Next</a>').click(slideNext).insertAfter(list);
		items.each(function(){
		
		});
		function slidePrev(){
		$('>:last', list).prependTo(list);
		active = active.removeClass('active').prev().addClass('active');
		centerActive();
		return false;
		}
		function slideNext(){
		$('>:first', list).appendTo(list);
		active = active.removeClass('active').next().addClass('active');
		centerActive();
		return false;
		}
		function centerActive(){
		var listlength = items.length;
		$('>*', list).each(function(i){
				if(i==0){
						$(this).animate({'right':'50%', 'margin-right':-1*itemwidth/2+'px', 'bottom': '0px', 'z-index':2});
				} else if(i == 1){      
				$(this).animate({'right':'0px','width':itemwidth*0.75+'px', 'height':itemheight*0.75+'px', 'bottom': '50px', 'z-index':1});
				} else if(i == listlength-1){
$(this).animate({'right':list.width()-itemwidth/2+'px','width':itemwidth*0.75+'px', 'height':itemheight*0.75+'px', 'bottom': '50px', 'z-index':1});
				} else {
						$(this).animate({'left': 'auto', 'right':i*list.width()/(listlength)-itemwidth/8+'px', 'width':itemwidth*0.5+'px', 'height':itemheight*0.5+'px', 'bottom': '75px', 'z-index':0});
				}
			});
		}
	}
	if ($('>*', this).length>1){
			return this.each(init);
	} 
	return this;
};
$.fn.lightbox = function(args, callback){
	var opt = jQuery.extend({
		lightbox : $('#lightbox').length ? $('#lightbox') : $('<div id="lightbox"></div>'),
		target : '#main >*'
	}, args); 
	function init(){
		if($(this).is('[type=submit]')){
			var url = $(this).closest('form').attr('action');
		} else {
			var url = $(this).attr('href');
		}
		$(this).click(function(e){
			if(url.split('#').length>1){
				window.location.href = window.location.href.split('#')[0]+'#'+url.split('#')[1];
			}
			e.preventDefault();
			$.get(url, function(data){
				opt.lightbox
					.html($(opt.target, data))
					.css({'top':$(window).scrollTop()+100+'px'})
					.fadeIn()
					.prepend($('<a href="#" class="close">Stäng</a>').click(function(){
						closeLightbox();
					}) 
					).appendTo('body');
				if(typeof(callback) != 'undefined'){
					callback();
				}
			});
			return false;
		});
	}
	function closeLightbox(){
		opt.lightbox.fadeOut(function(){
			$(this).remove();
		});
	}
	return this.each(init);
};
$.fn.clothSlider = function(){
function init(){
	var slider = $(this);
	slider.parent().css({'overflow':'hidden'});
	var nextlink = $('<a href="#" class="next">Nästa</a>').click(function(){
		var footers = slider.find('.footer').hide()
		slider.animate({'margin-left': '-=350px'}, 200, function(){
			update();
			footers.fadeIn();
		});
		return false;
	}).insertAfter(slider).hide();
	var prevlink = $('<a href="#" class="prev">Föregående</a>').click(function(){
		var footers = slider.find('.footer').hide();
		slider.addClass('animating').animate({'margin-left': '+=350px'}, 200, function(){
			update();
			footers.fadeIn();
		});
		return false;
	}).insertAfter(slider).hide();
	update();
	if(window.location.href.split('#bottom-').length>1){
		var bottid = window.location.href.split('#')[1];
		if($('#'+bottid, slider).length){
			$('.section:not(#'+bottid+')', slider).hide();
			nextlink.hide();
			prevlink.hide();
		}
	}
	function update(){
		if(slider.width()-350>slider.css('margin-left').match(/[0-9]*/g)[1]){
			nextlink.show();
		} else {
			nextlink.hide();
		}
		if(slider.css('margin-left').match(/[0-9]*/g)[1]>0){
			prevlink.show();
		} else {
			prevlink.hide();
		}
	}
}
return this.each(init);
};
//Createtabs of certain objects with the argument of the path to the element which text should be used in the tab
$.fn.createTabs = function(headerselector, args){
	var opt = jQuery.extend({
		history : false
	}, args);
	if($(this).length){
		if( !$('body').is('[role]') ){ $('body').attr('role','application'); }
		var sections = $(this);
		var tabbar = $('<ul class="tabs"></ul>').attr('role','tablist').insertBefore('.section.tab:first');
		$(sections).each(function(){
			var tab = $('<li class="tab"></li>').attr('role','tab').attr('tabindex', -1);
			var header = $(this).hide().find(headerselector).addClass('hidden').text();
			var tabid = header.replace(/ /, '_').toLowerCase();
			var tabsection = $(this).attr('role','tabpanel').attr('aria-hidden', true).attr('aria-labelledby', tabid+'-tab');
			$('<a href="#'+tabid+'" tabindex="-1"></a>').text(header).appendTo(tab.attr('id', tabid+'-tab'));
			if ($(this).is('.right')){
				tab.addClass('right');
			}
			tab.appendTo(tabbar).click(function(){
				$('div.tab.selected', tabsection.parent()).removeClass('selected').hide().attr('aria-hidden', true);
				$('.selected', tabbar).removeClass('selected');
				tab.addClass('selected').attr('tabIndex', '0');
				tabsection.addClass('selected').show().attr('aria-hidden', false);
				if (opt.history){
					window.location.href = window.location.href.split('#')[0]+'#'+tabid;
				}
				return false;
			}).keydown(function(e){
				switch (e.keyCode){
				case 16: 
					$(this).keydown(function(e2){
						if (e2.keyCode == 9){
							$(this).attr('tabindex', -1).prev().attr('tabindex', 0);
						}
					});
					break;
				case 9: 
					if($('a,input,select, textarea, [tabindex]', tabsection).not('[tabindex=-1]').length){
						tabsection.focus();
					} else {
						$(this).attr('tabindex', -1).next().attr('tabindex', 0);
					}
					break;
				case 37:
					$(this).attr('tabindex', -1).prev().attr('tabindex', 0).focus();
					break;
				case 39:
					$(this).attr('tabindex', -1).next().attr('tabindex', 0).focus();
					break;
				case 40:
				case 9:
					if($('a,input,select, textarea, [tabindex]', tabsection).not('[tabindex=-1]').length){
						$('a,input,select, textarea, [tabindex]', tabsection).get(0).focus();
					}
					break;
				default:
				break;
				}
			}).focus(function(){
				$(this).trigger('click');
			});
		});
		$(window).bind("hashchange", function(e) {
			if (window.location.href.split('#').length>1 && ($('#'+window.location.href.split('#')[1]).parents('.tab').length>0 || $('#'+window.location.href.split('#')[1]+'-tab').is('.tab'))){
				var id = window.location.href.split('#')[1];
				if ($('#'+id).parents('.tab').length){
					$('#'+$('#'+id).closest('.tab').attr('aria-labelledby')).trigger('click');
				}
				if ($('#'+id+'-tab').is('.tab')){
					$('#'+id+'-tab.tab').trigger('click');
				} 
				$('#'+id).focus();
			} else {
				//never set hashtag on first load, it messes up history
				var history = opt.history; 
				opt.history = false;
				$('li:first',tabbar).trigger('click');
				opt.history = history;
			}
		}).trigger('hashchange');
	}
	return this;
};
$.fn.initscripts = function(){
var imagepath = $('#logotype img').attr('src').split(/[a-z]*\.[a-z]*$/)[0];
	var container = $(this);
	$('.sizes label.info').each(function(){
		var img = new Image();
		img.src = imagepath+'measures/'+$(this).attr('for')+'.jpg';
		$(img).appendTo(this).wrap($('<div class="tooltip"></div>'));
	});
	$('.products .variations', container).hoverslide();
	$('.article.variations .bottoms', container).clothSlider();
	$('.products:not(.overview) .section a, .lightbox', container).lightbox({}, function(){
		$('#lightbox').initscripts();
	});
	$('.sizes .tab').createTabs('h4');
};
$(function(){
$(this).initscripts();
});
