(function($) {
	var closeSearchContainer = false;
	
	/* Tabellen */
	var current = 'products';
	var currentSwitch = window.location.hash;
	currentSwitch = currentSwitch.substr(1);
	if (currentSwitch == 'accessories' || currentSwitch == 'software') {
		$('table').hide();
		$('#'+currentSwitch+'_table').show();
		current = currentSwitch;
	}
	
	$('#'+current+'_table tr').each(function() {
		var height = 0;
		$(this).children('td').children('div').each(function() {
			if ($(this).height() > height) {
				height = $(this).height();
			}
		});
		$(this).children('td').children('div').height(height);
	});
		$('#login_form').dialog({
			autoOpen: false,
			modal: true,
			width: 350,
			height: 390
		});
	$('a:contains("log in"), , a:contains("Log in"), , a:contains("Log In")').click(function(event) {
		$('#login_form').dialog('open');
		event.preventDefault();
	});
	$('#modal_login button').click(function(event) {
		$('#login_form').dialog('close');
	});
	
	/* Sökfältet */
	var defaultValue = $('#search_field').val();  
	$('#search').show();
    jQuery('#search_field').click(function() {
        if( jQuery(this).val() == defaultValue ) {
            jQuery(this).val("");
        }
    }); 	
	$('#search_btn').click(function(event) {
		$('#search').ajaxSubmit({
			success: function(data) {
				$('.search_suggestions').remove();
				$('.searchwrapper').append(data);
				$('.search_suggestions').click(function() {
					closeSearchContainer = false;	
				});
			}
		});
		event.preventDefault();	
	});
	
	$('body').click(function() {
		if (closeSearchContainer)
			$('.search_suggestions').remove();
		closeSearchContainer = true;	
	});
	
	/* Newsletter Signup */
	var newsletterDefaultValue = $('#newsletter_signup_field').val();  
	$('#newsletter_signup_field').focus(function() {
        if( $(this).val() == newsletterDefaultValue ) {
            $(this).val("");
        }
    });
	$('#newsletter_signup_field').blur(function() {
        if( $(this).val() == "" ) {
            $(this).val(newsletterDefaultValue);
        }
    });
		
	$('#newsletter_submit').click(function(event) {
		var emailAddress = $('#newsletter_signup_field').val();
		if (!emailAddress.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i)) {
			alert('Please enter a valid email address');
			return false;
		}
		if ($('#Form_NewsletterForm :radio:checked').length == 0) {
			alert('Please choose one of the Newsletter Types');
			return false;
		}
	});	
	
	
	/* Nyhetssidan */
	var newsItems = $('.news_container').length;
	$('#news_container div:not(:first, .news_excerpt, .news_excerpt div)').hide();
	if (newsItems > 1) {
		var items = '';
		var currentPage = 1;
		var nextPage;
		
		for (i = 1; i <= newsItems; i++) {
			if (i == 1)
				items += '<li><a id="newspage_'+i+'" class="selected" href="#">'+i+'</a></li>';
			else
				items += '<li><a id="newspage_'+i+'" href="#">'+i+'</a></li>';
		}

		$('#news_container').append('<ul class="pagination"><li><a id="goback" href="#">&lt;</a></li>'
			+items+'<li><a id="goforward" href="#">&gt;</a></li></ul>');

		$('ul.pagination li a:not(#goforward, #goback)').click(function(event) {
			if (!$(this).hasClass('selected')) {
				nextPage = this.id.split('_');
				nextPage = parseInt(nextPage[1], 10);
				doFade();
			}
			event.preventDefault();
		});
		
		$('#goback').click(function(event) {
			if (currentPage > 1) {
				nextPage = currentPage-1;
				doFade();
			}
			event.preventDefault();
		});
		$('#goforward').click(function(event) {
			if (currentPage < newsItems) {
				nextPage = currentPage+1;
				doFade();
			}
			event.preventDefault();
		});
	}
	function doFade() {
		$('ul.pagination li a').removeClass('selected');
		$('#newspage_'+nextPage).addClass('selected');
		$('#container_'+currentPage).fadeOut('normal', function() {
			$('#container_'+nextPage).fadeIn('normal');	
		});
		currentPage = nextPage;		
	}
	/* Förstasidan */	
	if ($('.home_slideshow').length > 0) {
		$('.home_slideshow').cycle({fx:'fade', pager: '#pager_nav', pause: 1, pauseOnPagerHover: 1});
		$('#pager_nav a').text('');
		var navLinks = new Array('mammography/', 'radiation-protection/', 'radiography-and-flouroscopy/', 'computed-tomography/', 'dental/');
		$('#pager_nav a').each(function(i) {
			$(this).wrap('<div id="slidenav_'+(i+1)+'"></div>');
			$(this).attr('href', navLinks[i]);
			if (i == 4) {
				$(this).addClass('last');
			}
		});
	}

	if ($('#SliderArea').length > 0) {
		$('#SliderArea').cycle({fx:'scrollLeft', pager: '#splash_pager_nav', pause: 1, pauseOnPagerHover: 1});
		$('#splash_pager_nav a').each(function(i) {
			var realLink = $('#SplashPageContainer a:eq('+i+')');
			realLink.replaceWith($(this).clone(true).addClass('download').html(realLink.html()));
		});
		$('#splash_pager_nav').remove();
		$('#SplashPageContainer ul:first').attr('id', 'splash_pager_nav');
	}
//	$('.related_products').height($('.bo_main').height());

	$('.answers').hide();
	$('.questions').toggle(
	function(event) {
		$(this).addClass('q_open').next('.answers').slideDown('normal');
		event.preventDefault();
	},
	function(event) {
		$(this).removeClass('q_open').next('.answers').slideUp('normal');
		event.preventDefault();
	});

	$('#LoginForm input:text, #LoginForm input:password, #modal_login input:text, #modal_login input:password').focus(function() {
		var value = $(this).val();
		if (value == 'User Name' || value == 'Password') {
			$(this).val('');
		}
	});
	$('#LoginForm input:text, #modal_login input:text').blur(function() {
		var formField = $(this);
		if (formField.val() == '') {
			formField.val('User Name');
		}
	});
//	$('#LoginForm input:password').blur(function() {
//		var formField = $(this);
//		if (formField.val() == '') {
//			formField.val('Password');
//		}
//	});
	$('#lost_password_form').hide();
	$('#lost_password').click(function(event) {
		$('#lost_password_form').toggle();
		event.preventDefault();
	});

	$('#products_header a').click(function(event) {
		var clickedLink = this.id;
		if (clickedLink != current) {
			$('#'+current+'_table').fadeOut('slow', function() {
				$('#'+clickedLink+'_table').fadeIn('slow');
				$('#'+clickedLink+'_table tr').each(function() {
					var height = 0;
					$(this).children('td').children('div').each(function() {
						if ($(this).height() > height) {
							height = $(this).height();
						}
					});
					$(this).children('td').children('div').height(height);
				});
				current = clickedLink;
			});
		}
		event.preventDefault();
	});
	
	/* Application Notes */
	$('#application_notes_filter').change(function() {
		if ($(this).val() != '') {
			$('.application_note').hide();
			$('.'+$(this).val()).show();
		}
		else {
			$('.application_note').show();
		}	
	});
})(jQuery);