(function($) {
		  
	// jQuery classHandler 1.0
	$.fn.classHandler=function(a,b){var c={'mouseover':'mouseout','mouseenter':'mouseleave','focus':'blur'};var d=$(this);d.bind(a,function(){$(this).addClass(b)});d.bind(c[a],function(){$(this).removeClass(b)});return this};
	
	// jQuery hMenu 1.1
	$.fn.hMenu=function(c){var c=$.extend({submenu:'ul',active:'active'},c);var d=this;var e=[];return this.each(function(){var a=$(this);var b=a.children(c.submenu);e.push(b);a.mouseover(function(){d.removeClass(c.active);$(this).addClass(c.active)})})};
	
	// Check the selected index of a single SELECT element
	$.fn.getSelectedIndex = function() {
		if(this.length == 1 && this[0].nodeName == 'SELECT')
			return this.children().index(this.children(':selected'));
	};

})(jQuery);

function _enhanceSearchPage() {
	
	var container = $('#search-page');
	
	if(container.length) {
		
		// Add the complete URL to each element
		container.find('.element').each(function() {
			
			var element = $(this);
			var link = element.find('h2 a');
			var target = $('base').attr('href') + link.attr('href').substring(1);
			
			if(target.length > 75)
				element.append('<p class="address"><a href="' + target + '" title="' + target + '">' + target.substring(0, 75) + '&#133' + '</a></p>');
			else
				element.append('<p class="address"><a href="' + target + '">' + target + '</a></p>');
				
		});
		
	};
	
};

function _enhanceBoxedCart(selector, total, plural, singular) {
	
	var string = total == 1 ? singular : plural;
	$(selector).append(' (' + total + ' ' + string + ')');
	
};

function _enhancePlaceList() {
	if($('#customdb-stores, #customdb-stores-item').length)
		$('#stores-places').hide();
};

$(window).load(function() {
	
	$('.movie').each(function(i) {
							  
		// Set up variables
		var fpContainer = $(this),
			fpMovie = fpContainer.children('.movie-player'),
			fpMovieId = fpMovie.attr('class') + '-' + i,
			fpControls = fpContainer.children('.movie-controls'),
			fpControlsId = fpControls.attr('class') + '-' + i,
			fpAutoplay = fpControls.hasClass('autoplay_true') ? true : false;
			
		// Assign attributes
		fpMovie.attr('id', fpMovieId);
		fpControls.attr('id', fpControlsId);
		
		// Set up flowplayer
		flowplayer(fpMovieId, {
			src : 'style/orthopharma/flowplayer/flash/flowplayer.swf',
			wmode : 'opaque'
		}, {
			canvas : {
				backgroundColor : '#333333'
			},
			clip: {
				autoPlay : fpAutoplay,
				autoBuffering : true
			},
			plugins: {
				controls : null
			}
		}).controls(fpControlsId);

		// Set up initial state
		var oBottom = fpControls.css('bottom'),
			initStyle = {
			opacity : 0,
			bottom : oBottom.substring(0, oBottom.length - 2) / 2 + 'px'
		};
		fpControls.css(initStyle);
			
		// Set up events
		fpContainer.hover(function() {
			fpControls.stop().animate({
				opacity : 1,
				bottom: oBottom
			}, 250);
		}, function() {
			fpControls.stop().animate(initStyle, 500);
		});

	});
		
});

$(function() {

	// Set up large banner
	var bannerContainer = $('#cycle-large'),
		banner = bannerContainer.find('.element');

	banner.each(function() {
			
		// The SPAN element (metaElement) is used as placeholder for meta data
		var element = $(this),
			target = element.find('strong'),
			metaElement = element.find('span');
		
		// Retrieve and set the STRONG element (target) color
		if(target.length) {
			target.css('color', metaElement.css('color'));
		};
		
		// If the metaElement has the class "content_disabled_0" then the content will be hidden and the whole element will be a link
		if(metaElement.attr('class') == 'content_disabled_0') {
			element.css('cursor', 'pointer')
			element.click(function() {
				window.location.href = element.find('a').attr('href');
			});
			element.children().hide();
		};
	
		var nav = $('<div />', {
			className : 'nav'
		});
		nav.appendTo(element);
		
		var prevBtn = $('<a />', {
			className : 'button prev',
			click : function(event) {
				banner.showPrevious();
				event.stopPropagation();
				event.preventDefault();
			},
			href : '#',
			text : 'Vorige'
		});
		prevBtn.appendTo(nav);
		
		banner.each(function(index) {
			var btn = $('<a />', {
				className : 'button',
				click : function(event) {
					banner.showIndex(index);
					event.stopPropagation();
					event.preventDefault();
				},
				href : '#',
				text : index
			});
			btn.appendTo(nav);
		});
		
		var nextBtn = $('<a />', {
			className : 'button next',
			click : function(event) {
				banner.showNext();
				event.stopPropagation();
				event.preventDefault();
			},
			href : '#',
			text : 'Volgende'
		});
		nextBtn.appendTo(nav);
		
	});
	
	banner.cycle({
		interval : bannerContainer.attr('rel') * 1000 || 1000,
		keep : true,
		onCycle : function() {
			var index = this.getCurrentIndex(),
				btns = $(this).eq(index).find('.nav a:not(.prev, .next)');
			btns.each(function(i) {
				var btn = $(this);
				if(i == index) {
					btn.addClass('active');
				} else {
					btn.removeClass('active');
				};
			});
		}
	});
	
	
	// Set up small banner
	$('#cycle-small img').cycle();
	
	// Set up hMenu for the main navigation
	$('#navigation > ul > li').hMenu();
	
	// Assign classHandler to form fields
	$('.form input[type=text], .form textarea, .form input[type=password]').classHandler('focus', 'active');
	
	// Enhance search inputs
	$('#search form, #search-page form').each(function() {
		
		var form = $(this);
		
		form.children('.text')
			.focus(function() {
				form.addClass('active');
			})
			.blur(function() {
				form.removeClass('active');
			});
		
	});
	
	// Enhance buttons
	$('button, a.button:not(#cycle-large .nav a)').each(function() {
		
		var oButton = $(this),
			oButtonClass = oButton.attr('class'),
			oButtonType = this.nodeName,
			title = oButton.attr('title') ? oButton.attr('title') : oButton.text(),
			nButton = $(
				'<div class="button" title="' + title + '">'+
					'<div class="left">&nbsp;</div>'+
					'<div class="middle">' + oButton.html() + '</div>'+
					'<div class="right">&nbsp;</div>'+
					'<div class="clear"></div>'+
				'</div>'
			);

		if(oButtonClass)
			nButton.addClass(oButtonClass);
			
		oButton.hide();
		nButton
			.insertAfter(oButton)
			.click(function() {
				if(oButtonType == 'A')
					window.location.href = oButton.attr('href');
				else
					oButton.trigger('click');
			})
		
	});
	
	// Fix PNG images if the user client is IE 6 or lower
	if($.browser.msie) {
		if($.browser.version <= 6) {
			$('head').append('<script type="text/javascript" src="style/orthopharma/js/jquery.pngfix.js" />');
			$('#navigation .corner, div.button .left, div.button .right').pngfix();
		};
	};
	
	// Hide navigation "Direct naar" in right column when they're empty	
	$('.shortcuts ul').hideIfEmpty({
		target : '.shortcuts',
		ignoreDummy : true
	});
	
	// Set up momentum download form, store email address in cookie on submit
	var form = $('#momentum-download');
	if(form.length) {
		if(getCookie('momentum-download')) {
			setCookie('momentum-download', '', -1);
		};
		form.bind('submit', function() {
			setCookie('momentum-download', form.find('input[name=email_address]').val());
		});
	};

	var formFinish = $('#momentum-download-finish');
	if(formFinish.length) {
		var cookie = getCookie('momentum-download');
		if(cookie) {
			formFinish.append('<img src="http://dy.testnet.nl/lead/leadTracker.cfm?cID=14779&OrderNr=myOrderNr&CustNr=' + cookie + '" width="1" height="1" alt="" />');
			setCookie('momentum-download', '', -1);
		};
	};

});
