﻿/* #EASING
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend(jQuery.easing,
{
	def: 'easeOutBack',
	swing: function (x, t, b, c, d) {
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	}
});


/* #MODAL-WINDOW
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
(function($) {

    $.fn.modalwindow = function(options) {

        // set default options
        var defaults = {
            id: 'modal-window',
            html: '',
            theme: 'normal',
            openX: '0',
            openY: '0',
            onBeforeOpen: function() { },
            onOpen: function() { },
            onClose: function() { }
        },

		options = $.extend(defaults, options),

		$modal_window = ($('#' + options.id).length) ? $('#' + options.id) : $('<div />')
			.attr('id', options.id)
			.data('open', false)
			.appendTo('body'),

		$img = ($('#' + options.id + '-image').length) ? $('#' + options.id + '-image') : $('<img />')
			.attr('id', options.id + '-image')
			.attr('src', '/assets/gfx/pop_message.png')
			.appendTo('body');

        $content = ($('#' + options.id + '-content').length) ? $('#' + options.id + '-content') : $('<div />')
			.attr('id', options.id + '-content')
			.appendTo($modal_window);

        $close = ($('#' + options.id + '-close').length) ? $('#' + options.id + '-close') : $('<a />')
			.attr('id', options.id + '-close')
			.appendTo($modal_window);

        // get overlay dimensions
        var outer_width = $modal_window.outerWidth(),
			outer_height = $modal_window.outerHeight(),
			w = $(window);

        // get top-left corner position
        var top = Math.max((w.height() - outer_height) / 2, 0),
			left = Math.max((w.width() - outer_width) / 2, 0);


        //open instantly
        if ($modal_window.data('open') == false) {

            // update data
            $modal_window.addClass(options.theme).data('open', true);

            // position overlay
            $modal_window.css({ top: top + w.scrollTop(), left: left + w.scrollLeft(), position: 'absolute', display: 'none' });

            // add content
            $content.html(options.html);

            // call onBeforeOpen (callback function), $(this) refers to $modal_window)
            options.onBeforeOpen.call($modal_window[0]);

            // initialize background image and make it visible
            $img.css({
                top: options.openY,
                left: options.openX,
                display: 'block'
            });

            // begin growing
            $img.animate({
                top: $modal_window.css("top"),
                left: $modal_window.css("left"),
                height: outer_height,
                width: outer_width
            }, '500', function() {

                // set close button and content over the image
                $modal_window.css("zIndex", 11000).show();

                // call onOpen (callback function), $(this) refers to $modal_window)
                options.onOpen.call($modal_window[0]);
            });

        }

        // when close-button is clicked
        $close.unbind('click').bind('click', function(e) {

            if ($modal_window.data('open')) {

                // update data
                $modal_window.removeClass(options.theme).data('open', false);

                // hide content
                $modal_window.hide();

                $img.animate({
                    top: options.openY,
                    left: options.openX,
                    width: 0,
                    height: 0
                }, '500', function() {
                    // call onClose (callback function), $(this) refers to $modal_window)
                    options.onClose.call($modal_window[0]);
                    if (options.theme == "tinymce") {
                        $('textarea.contentArea').tinymce().remove();
                    }
                });
            }

            e.preventDefault();

        });

    } // End plugin. Go eat lunch.

})(jQuery);


/* #SLIDESHOW
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
(function($) {
	
	$.fn.slideshow = function(options) {

		// set default options
		var defaults = {
			speed : 1000,
			pause : 5000,
			transition : 'slide'
		},

		// Take the options that the user selects, and merge them with defaults.
		options = $.extend(defaults, options);
	
		// for each item in the wrapped set
		return this.each(function() {
		
			var $this = $(this);
			var slideInterval = '';
			var slider_controls = $('.slideshow-controls');
			
			// Wrap "this" in a div with a class of "slider-wrap."
			$this.wrap('<div class="slider-wrap" />');
			
			// Set the width to a really high number. Adjusting the "left" css values, so need to set positioning.
			$this.css({
				'width' : '99999px',
				'position' : 'relative',
				'padding' : 0
			});

			// If the user chose the "slide" transition...
			if(options.transition === 'slide') {
			
				// Add some slideshow-controls and attach events to them
				/*
				var show_image = $('<a />', {
						id: 	'show-image',
						title: 	'Visa större bilder',
						text: 	'Visa större bilder',
						rel:	'gallery',
						href: $this.parents('#product-teaser').find('.slideshow a:first').attr('href')
					})
					.appendTo(slider_controls);
				*/
				
				var step_forward = $('#step-forward')
					.removeAttr('href')
					.bind('click', function() {
						clearInterval(slideInterval);
						instantSlide();
					});
			
				$this.children().css({
					'float' : 'left',
					'list-style' : 'none'
				});
				
				$('.slider-wrap').css({
					'width' : $this.children().width(),
					'overflow' : 'hidden'
				});
				
				slide();
			}
			
			// If the user chose the "fade" transition, instead pile all of the images on top of each other.
			if(options.transition === 'fade') {
				$this.children().css({
					'width' : $this.children().width(),
					'position' : 'absolute',
					'left' : 0
				});
				
				// reorder elements to fix z-index issue.
				
				for(var i = $this.children().length, y = 0; i > 0; i--, y++) { 		
					$this.children().eq(y).css('zIndex', i + 99999);
				}	

				// Call the fade function. 
				fade();
			}
			
			function slide() {
				slideInterval = setInterval(function() {
					// Animate to the left the width of the image/div
					$this.animate({'left' : '-' + $this.parent().width()}, options.speed, 'jswing', function() {
						// Return the "left" CSS back to 0, and append the first child to the very end of the list.
						$this
						   .css('left', 0)
						   .children(':first')
						   .appendTo($this); // move it to the end of the line.
					});
				}, options.pause);
			} // end slide
			
			function instantSlide() {
				step_forward.unbind('click');
				$this.stop(true).animate({'left' : '-' + $this.parent().width()}, '500', 'easeOutBack', function() {
					// Return the "left" CSS back to 0, and append the first child to the very end of the list.
					$this
					   .css('left', 0)
					   .children(':first')
					   .appendTo($this); // move it to the end of the line.

					step_forward.click(function(){
						clearInterval(slideInterval);
						instantSlide();
					});
					
				});
				slide();
			} // end instant slide

			function fade() {
				setInterval(function() {
					$this.children(':first').animate({'opacity' : 0}, options.speed, function() {	
						$this
						   .children(':first')
						   .css('opacity', 1) // Return opacity back to 1 for next time.
						   .css('zIndex', $this.children(':last').css('zIndex') - 1) // Reduces zIndex by 1 so that it's no Int64er on top.					
						   .appendTo($this); // move it to the end of the line.
					});
				}, options.pause);
			} // end fade			

		}); // end each		
	
	} // End plugin. Go eat cake.
	
})(jQuery);

/* #DOM-READY
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
jQuery(function () {
    /* Set login text on and off
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    $('fieldset .un').bind('focus', function () {
        if ($(this).val() == 'e-postadress' || $(this).val() == '') {
            $(this).val('');
        }
    });

    $('fieldset .un').bind('blur', function () {
        if ($(this).val() == '') {
            $(this).val('e-postadress');
        }
    });


    // Hide login-form
    document.getElementById('login-area').style.display="none";

    // #TODO - This class should be applied with PHP instead...
    $('.product-list li:nth-child(4n+1)').addClass('first-on-row');
	
	
    /* Set height of sidebar cart
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	//$('#cart').css({minHeight: $('.content-head').outerHeight() || $('.product-head').outerHeight()});


	/* SHOW/HIDE LOGIN AREA
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/	
	$('#search-area').find('.btn-login').bind('click', function(e) {
		$('#search-area, .company-navigation').hide();
		$('#login-area').show();
		e.preventDefault();
	});
	$('#login-area').find('.btn-search').bind('click', function(e) {
		$('#login-area').hide();
		$('#search-area, .company-navigation').show();
		e.preventDefault();
	});
	
	
	/* Style selectbox
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/	
	$('.custom-select-wrapper select').each(function(){
		var $this = $(this),
			$mask = $('<div class="overlay" />')
						.text($this.find(':selected').text())
						.prependTo($this.parent());
						
		$this.bind('change',function(){
			$mask.text($this.find(':selected').text());
		});
		
		$this.css('opacity','0').addClass('custom-js-styled-select');
    });

	/* Toggle creditcard details
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/	    
	/*
	$('.card-details').hide();
	$('input[name=payment]').bind('click',function() {
        if( $('#creditcard').is(':checked') ) {
            $('.card-details').show();
        }
        else {
            $('.card-details').hide();
        }
	});
	*/
	
	$('#cvcHelp').bind('click',function(e){
		$.fn.modalwindow({
			openX: e.pageX,
			openY: e.pageY,
			html: '<h2>cvc förklaring</h2><p>lorem ipsum...</p>'
		});
		return false;
	});

	$('#deliveryTerms').bind('click',function(e){
        
		    $.fn.modalwindow({
			    openX: e.pageX,
			    openY: e.pageY,
				theme: 'scroll-content',
			    html: '<div id="loaded-terms"></div>',
                onOpen: function() {
                $("#loaded-terms").load("/pages/checkout/termsAndShipping.aspx #terms-n-shipping");
                }
		    });
		return false;
	});

	/* Modal window examples
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/	
    /*
	$('.content .button').bind('click',function(e){
		$.fn.modalwindow({
			openX: e.pageX,
			openY: e.pageY,
			html: '<h2>Button</h2><h3>open coords</h3><p>x:' + e.pageX + ', y: ' + e.pageY + '</p>'
		});
		return false;
	});
	
	$('#search').bind('click',function(e){
		$.fn.modalwindow({
			openX: e.pageX,
			openY: e.pageY,
			html: '<h2>Search</h2><h3>open coords</h3><p>x:' + e.pageX + ', y: ' + e.pageY + '</p>'
		});
		return false;
	});
	*/

});

// To make sure only numbers are allowed in the input field
function OnlyNumber($iInput) {
    var myRegExp = new RegExp("[^0-9]", "gi");
    var replacedVal = $iInput.val().replace(myRegExp, "");
    $iInput.val(replacedVal);
}
