$(function() {

	$('#frontpage-cycle p').before('<div id="cycle-pager">')
	.cycle({ 
	    fx:    'fade', 
	    speed:  2500,
	    pager:  '#cycle-pager' 
	 });
	
	var videolink = $('.video-box .video').text();
	$('.video-box .video-holder').html('<object width="315" height="243"><param name="movie" value="' + videolink + '?version=3&amp;hl=fi_FI&amp;rel=0"></param><param name="wmode" value="transparent"><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="' + videolink + '?version=3&amp;hl=fi_FI&amp;rel=0" wmode="transparent" type="application/x-shockwave-flash" width="315" height="243" allowscriptaccess="always" allowfullscreen="true"></embed></object>');

		
	var $defaultValue = $('span#default-value');
	var $searchInput = $('input#join');
	
	$searchInput.focus(function() {$defaultValue.hide();});
	$defaultValue.click(function() {$(this).hide();$searchInput.focus();});
	$searchInput.blur(function() {if($(this).val().length === 0) {$defaultValue.fadeIn(250);}});
	
	if($searchInput.val().length > 0 ) {$defaultValue.hide();}
	
	$('.selector').click(function() {
		$currentDropbox = $(this).children('.dropbox');
		$currentDropbox.prev().toggleClass('arrow-up');
		$('.selector').css('zIndex', '1');
		$('.dropbox').not($currentDropbox).fadeOut(250);
		$(this).css('zIndex', '10');
		$currentDropbox.toggle();
	});
        
        var total_price = parseFloat($('#total-price').text().replace(',', '.'));
        var payment_type = 0;
        var delivery_method = 0;
        
        $('ul.select-list a').click(function() {
			var $this = $(this);
	                var text = $this.text();
			var value = $this.closest('li').attr('class');
			var price = $this.closest('li').attr('price');
			var $target = $this.closest('.dropbox').parent().children().eq(0);
			$target.text(text);
			$target.attr('price', price);
			$target.attr('class', value);
			$('input[name="' + $target.attr('id') +'"]', $this.closest('form')).val(value).change();
			$('.dropbox').fadeOut(250);
			return false;
		});
        
		$('#cart3_payment_type a').click(function() {
            payment_type = parseFloat($(this).closest('li').attr('price').replace(',', '.'));
            updateOrderPrice();
        });
 
        $('#cart3_delivery_method a').click(function() {
            delivery_method = parseFloat($(this).closest('li').attr('price').replace(',', '.'));
            updateOrderPrice();
        });
        
        $('.klarna-payment input.klarna-payment-type').change(function () {
			updateOrderPrice();
		});

        function isPartPayment() {
			var id = $('#paymentType').val();
			var $klarna = $('input.klarna-payment-type:checked');
	
			switch (parseInt(id)) {
				case 27: // Klarna
					return true;
			}
	
			return false;
		}
        
        function updateOrderPrice()
        {
        	if (isPartPayment()) {
				$('.cart-table .klarna').show();
				
				var $klarna = $('input.klarna-payment-type:checked');

				if ($klarna.length <= 0) {
					$('#klarna-payment-sub-type').attr('disabled', 'disabled');
				}
				else if ($klarna.val() == '-1') {
					$('#klarna-payment-sub-type').attr('disabled', 'disabled');
				}
				else {
					$('#klarna-payment-sub-type').removeAttr('disabled');
				}
			}
			else {
				$('.cart-table .klarna').hide();
			}
        	
            var total = total_price + payment_type + delivery_method;
            $('#total-price').text(total.toFixed(2).replace('.', ',') + '€');
        } 

	$('#carousel').cycle({
		timeout: 10000, 
		speed: 3000}
	);
	
	$('#campaign-slideshow').cycle({
		timeout: 3000,
		prev:    '#camp-prev',
        	next:    '#camp-next',
		speed: 600}
	);
	
	$('#slideshow').cycle({
    		speed:  'fast',
    		prev:    '#prev',
        	next:    '#next',
    		timeout: 0,
                after: function() { 
			$('.cloud-zoom').CloudZoom();
		},
    		pager:  '#slideshow-thumbs', 
    		pagerAnchorBuilder: function(idx, slide) {
                  var image = $(slide).attr('data-image-url');
                  return '<a href="#" class="link'+ idx +'"><span class="image-border" style="background: url('+ image +') no-repeat scroll center center white;">&nbsp;</span></a>';
    		}
	});

        $('#magnifier').click(function() {
           $('#slideshow').find('a:visible').click();
           return false;
        });

	$('a.fancybox').fancybox();
$('a.fancytextbox').fancybox({'scrolling': 'no'});

$('#menu-top li').each( function () {
            var currentLocation = document.location.toString();
var currentLiUrl = $("a:first-child", this).attr('href');
if(currentLocation == currentLiUrl ) {
$(this).addClass('current');
}            
        });

	$('.product-list').each(function() {
		$(this).find('tr:first').addClass('first');
		$(this).find('tr:last').addClass('last');
	});

	$('.product-list tr').each(function() {
		$(this).find('td:first').addClass('first');
		$(this).find('td:last').addClass('last');
	});

});

