﻿$(window).load(function () {
	$('.valign').vAlign();
//}); 

//$(document).ready(function(){
	$('form#login fieldset input').clearOnFocus();
	
	equalHeight($('.eqheight'));
	var footerpos = $('footer').offset().top + 230;
	var windowheight = $(window).height();
	
	if (footerpos < windowheight)
	{
	
		$('footer').css({
			'position' : 'absolute',
			'bottom' : '0'
		});
	}
	
	
	var pricebefore = $('.pricebox strong:first').text();
	pricebefore = pricebefore.replace('.','');
	
	var prevcolor = $('.pricebox strong:first').css('color');
	
	// ## CHANGE PRICE 
	$('.pricebox .buy select').change(function () {
	
		var price_element = $('.pricebox strong:first');
		var price = pricebefore;
		
		price = price.substr(0, price.length-2);
		
		$('.pricebox .buy select').each(function (index) {
			var addprice = $(this).children('option:selected').text().split('(');
			
			if (addprice.length > 1)
			{
				addprice = addprice[1];
				var mathsign = addprice.substr(0,1);
				addprice = addprice.substr(1, addprice.length-4);
				addprice = addprice.replace('.','');
				
				if (mathsign == '+')
				{
					price = parseFloat(price) + parseFloat(addprice);
				}
				else
				{
					price = parseFloat(price) - parseFloat(addprice);
				}
			}
			
			price_element.text(CurrencyFormatted(price) + ' SEK');
			
			price_element.animate({
				color: '#f2b9b2'
			}, function () {
				price_element.animate({
					color: prevcolor
				});
			});
			
		});
		
		
	});
	
	// ## SELECT LANGUAGE
	$('#language > span.selected').click(function (event) {
		if ($('#language > ul.list').css('display') == 'block')
		{
			$(this).css('background-color', 'transparent');
			$('#language > ul.list').slideUp(100);
			$('#selectarrow').animate({rotate: '-=180deg'}, 100);
		}
		else
		{
			$(this).css('background-color', '#fff');
			$('#language > ul.list').slideDown(100);
			$('#selectarrow').animate({rotate: '+=180deg'}, 100);
		}
		
		$(this).siblings('.list').children('li').click(function () {
			$('#language > span.selected > span').html($(this).html());
		});
		
		$(document).one('click', function () {
			if ($('#language > ul.list').css('display') == 'block')
			{
				$('#language > span.selected').css('background-color', 'transparent');
				$('#language > ul.list').slideUp(100);
				$('#selectarrow').animate({rotate: '-=180deg'}, 100);
			}
		});
		
		event.stopPropagation();
	});
		
	// ## EXPAND PLUS (HEADER)
	$('.plus > img').click(function () {
		$('.expand').fadeOut();
		$('.plus > img').attr('src', 'images/gfx/header_plus.png');
	
		var expand = $(this).siblings('.expand');
		
		if (expand.css('display') != 'block')
		{
			$(this).attr('src', 'images/gfx/header_minus.png');
			expand.fadeIn();
		}
		else
		{
			$(this).attr('src', 'images/gfx/header_plus.png');
			expand.fadeOut();
		}
	});
	
	// ## SLIDER

	var numslides = $('#slider').find('.item').length;
	var slidewidth = 462;
	var width = (numslides * slidewidth) ;
	var autoslide = setInterval('autoSlide(' + numslides + ',' + slidewidth + ',' + width + ')', 5000);
	
	$('#slider_content > div').css('width', width + 'px');

	$('#slideright').click(function () {
		clearInterval(autoslide);
		autoslide = setInterval('autoSlide(' + numslides + ',' + slidewidth + ',' + width + ')', 5000);
		
		if ($('#slider_content').scrollLeft() >= (width-(slidewidth * 2)))
		{
			$('#slider_content').animate({
				scrollLeft: 0
			});
		}
		else
		{
			$('#slider_content').animate({
				scrollLeft: ($('#slider_content').scrollLeft() + slidewidth) + 'px'
			});
		}
		
		return false;
	});
	
	$('#slideleft').click(function () {
		clearInterval(autoslide);
		autoslide = setInterval('autoSlide(' + numslides + ',' + slidewidth + ',' + width + ')', 5000);
		
		if ($('#slider_content').scrollLeft() == 0)
		{
			$('#slider_content').animate({
				scrollLeft: (width-slidewidth)
			});
		}
		else
		{
			$('#slider_content').animate({
				scrollLeft: ($('#slider_content').scrollLeft() - slidewidth) + 'px'
			});
		}
		
		return false;
	});
	
	// ## CART
	var cartpos = $('#cart').offset().top;
	$(window).scroll(function () {
		var scrollpos = $(window).scrollTop();
		// var scrollpos = 0;
		if (scrollpos > cartpos)
		{
			$('#cart').css({
				'position': 'fixed',
				'left': $('#cart').offset().left + 'px',
				'top': '-75px',
				'z-index': 999999999999999999999,
				'box-shadow' : '0 0 10px #333',
				'box-shadow' : '0 0 10px rgba(0, 0, 0, .5)'
			});
		}
		else
		{
			$('#cart').css({
				'position': 'relative',
				'left': 0,
				'top': 0,
				'z-index': 9999,
				'box-shadow' : 'none'
			});
		}
	});
	
	// ## SEGMENT
	$('ul.segment').children('li').click(function () {
		$('ul.segment').children('li').removeClass('selected');
		$(this).addClass('selected');
		$(this).children('input').attr('checked', true);
	});

	// ## SLIDER
	$('.slider').slider({
		value:0,
		min: 0,
		max: 3350,
		step: 1,
		slide: function( event, ui ) {
			$(this).parent().children('.slidervalue').children('span').html(ui.value);
			$(this).parent().children('input').val(ui.value);
		}
	});
	
	$('.slider_height').slider({
		min: 0,
		max: 3300,
		stepValues: [0, 1100, 1600, 2500, 3350],
		slide: function(event, ui) {
			var stepValues = $(this).slider("option", "stepValues"),
			distance = [],
			minDistance = $(this).slider("option", "max"),
			minI;
			$.each(stepValues, function(i, val) {
				distance[i] = Math.abs( ui.value - val );
				if ( distance[i] < minDistance ) {
					minDistance = distance[i];
					minI = i;
				}
			});
			if ( minDistance ) {
				$(this).slider("value", stepValues[ minI ]);
				$(this).parent().children('.slidervalue').children('span').html(stepValues[ minI ]);
				$(this).parent().children('input').val(stepValues[ minI ]);
				return false;
			}
		}
	});
	
	// Sets slider onload values
	
	$('.slider').slider("value", $('input#liftweight').val());
	$('.slider_height').slider("value", $('input#liftheight').val());
	
	
	// ## PRODUCTS IMAGES
	if ($('header#product_head').length > 0)
	{
		var startheight = $('header#product_head').children('ul').children('li:first').children().height();
		$('header#product_head').css('height', startheight + 'px');
		
		
		$.each($('header#product_head').children('ul').children('li'), function (index, value) {
			if ($(this).children('img').length > 0)
			{
				var source = $(this).children('img').attr('src');
				var alt = $(this).children('img').attr('alt');
				var datanumber = $(this).attr('data-number');
				var html = '<img src="' + source + '" alt="' + alt + '" title="' + alt + '" data-number="' + datanumber + '" height="55" />';
			}
			else
			{
				var source = $(this).attr('thumbsrc');
				var alt = $(this).attr('thumbalt');
				var datanumber = $(this).attr('data-number');
				var html = '<img src="'+source+'" alt="' + alt + '" title="' + alt + '" data-number="' + datanumber + '" height="66" />';
			}
			$('div#product_thumbs').append(html);
		});
		
		$('div#product_thumbs').children('img').click(function () {
			var datanumber = $(this).attr('data-number');
			$('header#product_head').children('ul').children('li').hide();
			$('header#product_head').children('ul').children('li[data-number=' + datanumber + ']').show();
			var newheight = $('header#product_head').children('ul').children('li[data-number=' + datanumber + ']').children().height();
			$('header#product_head').animate({
				height: newheight + 'px'
			});
		});
	}
});


function autoSlide(numslides, slidewidth, width) {
	if ($('#slider_content').scrollLeft() >= (width-(slidewidth * 2)))
	{
		$('#slider_content').animate({
			scrollLeft: 0
		});
	}
	else
	{
		$('#slider_content').animate({
			scrollLeft: ($('#slider_content').scrollLeft() + slidewidth) + 'px'
		});
	}
}

$.fn.clearOnFocus = function(){
    return this.focus(function(){
        var v = $(this).val();
        $(this).val( v === this.defaultValue ? '' : v );
    }).blur(function(){
        var v = $(this).val();
        $(this).val( v.match(/^\s+$|^$/) ? this.defaultValue : v );
    });
};

$.fn.vAlign = function() {
	return this.each(function(i){
		var ah = $(this).height();
		var ph = $(this).parent().height();
		var mh = Math.ceil((ph-ah) / 2);
		
		/*
		if ($(this).is('img'))
		{
			if (!$.browser.safari) {
				$(this).css('margin-top', mh);
			}
			else {
				$(this).parent().css('position', 'relative');
				
				var thisheight = $(this).height;
				var parentheight = $(this).parent().height();
				var mymargin = (parentheight-thisheight)/2;
				console.log(parentheight + ' - ' + thisheight);
				
				$(this).css({
					'top': mh,
					'position': 'relative'
					
				});
			}
		}
		else {
		*/
			$(this).css('margin-top', mh);
		/*
		}
		*/
	});
};

function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function CurrencyFormatted(amount)
{
	var delimiter = "."; // replace comma if desired
	amount += '.00';
	amount = new String(amount);
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount.substr(0,amount.length-3);
}
