/*
 * Filters4Spas.se
 * Javascript functions
 */

$(document).ready(function() {

	$('.javascript_only').show();

	/* Product Picture */
	if( $( 'div.productimage a' ).length > 0 ) {
		$( 'div.productimage a' ).fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	true,
			'overlayColor'	:	'#000',
			'overlayOpacity':	0.8
		});
	}
	/* Header Search Field */
	if( $( '#Search_Text' ).length > 0 ) {
		search_val = $( '#Search_Text' ).val();
		$('#Search_Text').blur(function() {
			$( this ).addClass( 'inactive' );
			$( this ).val( search_val );
		});
		$('#Search_Text').focus(function() {
			$( this ).removeClass( 'inactive' );
			$( this ).val( '' );
		});
	}
	if( $( '.click-show' ).length > 0 ) {
		$( '.click-show' ).click(function () {
			$( this ).toggleClass( 'open' );
		});
	}
	
	/* Reference Table: Show picture on MouseOver */
	if( $( 'table.product.reference' ).length > 0 ) {
		$( 'table.product.reference tr td:first-child' ).hover(
			function () {
				$( $( '.pop-up > div', this ) ).load( $( 'a', this ).attr( 'href' ) + ' #content .produkt ' );
				$( '.pop-up', this ).fadeIn( 350 );
			},
			function () {
				$( '.pop-up', this ).hide( 350 );
			}
		);
	}

});

$(window).load(function() {
						   
	/* Fixed items: Cart & Table */
	if( $( 'table.scrollable' ).length > 0 ) {
		
		// Create table header and fill with a copy of the actual header
		$( 'table.scrollable' ).before( '<table class="product scrollee"><thead><tr></tr></thead></table>' );
		$( 'table.scrollable thead tr th' ).each(function(index) {
			$( 'table.scrollee thead tr' ).append( '<th width="'+$( this ).attr( 'width' )+'">'+$( this ).text()+'</th>' );
		});
		
		// Create event to activate scroll function
		x = $( 'table.scrollable' ).offset().top;
		
		
		$( window ).scroll(function() {							
			updateScrollee( x );
		});
		
		
	} else if( $( '.scrollable' ).length > 0 ) {
		
		x = $( '.scrollable' ).offset().top;
		updateScrollable( x );
		
		$( window ).scroll(function() {									
			updateScrollable( x );
		});
		
	}
	
});

function updateScrollee( ScrollableOffset ) {
	if( $( window ).scrollTop() > ScrollableOffset ) {
		$('table.scrollee' ).addClass( 'active' );
	} else {
		$('table.scrollee' ).removeClass( 'active' );
	}	
}
function updateScrollable( ScrollableOffset ) {
	if( $( window ).scrollTop() > ScrollableOffset - 10 ) {
		$('.scrollable' ).addClass( 'active' );
	} else {
		$('.scrollable' ).removeClass( 'active' );
	}	
}
function filterSpas() {
$msgError = "Ett fel har uppstått - vänligen kontakta kundtjänst";
$msgNumeric = "Både längd och diameter ska anges i numeriska heltal";


if( $("form#filter-spafilter").length ) {
		
		$iLength	= $("form#filter-spafilter #length").attr("value");
		$iDiameter	= $("form#filter-spafilter #diameter").attr("value");
		
		// Check if input is numeric
		if( ! isNaN( $iLength ) && ! isNaN( $iDiameter ) ) {
			
			// Send to new URL with correct querystrings
			$newUrl = $("form#filter-spafilter #url").attr("value")
				+ "l=" + $iLength
				+ "&d=" + $iDiameter;

			window.location = $newUrl;
			
		} else {
			alert( $msgNumeric );
		}
					  
	} else {
		alert( $msgError );	
	}
}
