(function($) {

jQuery(document).ready(function() {

	/* NOTE. use jQuery syntax instead of $ to avoid conflicts (w.prototype) in SilverStripe */
	
	//jQuery('#Form_NewsletterForm_action_SignupAction').after('<label for="Form_NewsletterForm_action_SignupAction">Signup</label>');
	
	// pick-lang
	
	jQuery('#lang').mouseover(function(){
		var pos = jQuery('#lang').position();
		var wid = jQuery('#lang').width();
		jQuery('#lang-pick').fadeIn().css({ 'left' : pos.left, 'top' : 0, 'width' : wid });
		jQuery('#lang-pick ul').css({ 'width' : wid-11 });
	});
	jQuery('#lang-pick ul').mouseover(function(){
		jQuery('#lang-pick').show();
	});	
	jQuery('#lang-pick').mouseout(function(){
		jQuery('#lang-pick').hide();
	});	
	
	// slideshow-home

	if ($('#slide-images').length > 0){ 
		$('#slide-images').cycle({ 
		    fx:     	'scrollVert',
			easing:  	'easeOutQuad',
			speed:		400,
			after:  	onAfterSlide,
		    timeout: 	0
		});	

		var init = false;
		var curSlide = '<img id="curSlide" src="mysite/images/btn_slideshow.gif" alt="" />';

		$('.slide').bind("mouseenter",function(){
			var slideID = $(this).find('a').attr('href');
			var tmp = slideID.split('#slide');
		    $('#slide-images').cycle(Number(tmp[1]-1));
		    return false;  
	    });
		
	}
	
	// cases-home
	
	if (jQuery('#cases-outline').length > 0){ 
		jQuery('.case-content not(:first)').hide();
		jQuery('#cases-outline').cycle({ 
		    fx:     		'scrollHorz',
			easing:  		'easeOutQuad',
			speed:			400,
			next:   		'#case-next', 
			prev:   		'#case-prev',
			after:			onAfterCase,
			cleartype:  	true,
			cleartypeNoBg:  true,		
		    timeout: 		15000
		});
	}

	// products
	
	if (jQuery('#product-slide').length > 0){ 
		jQuery('.slides-product not(:first)').hide();
		jQuery('#product-slide').cycle({ 
		    fx:     		'scrollHorz',
			easing:  		'easeOutQuad',
			speed:			400,
			slideExpr: 		'.slides-product',
			next:   		'#product-next', 
			prev:   		'#product-prev',
			cleartype:  	true,
			cleartypeNoBg:  true,		
		    timeout: 		7500
		});	
	}
	
	// tabs
	
	if (jQuery('#product-content').length > 0){
		jQuery('#product-content > ul').tabs();
	}	
	
	// faq
	
	if (jQuery('#faq-items').length > 0){
	
		jQuery("#faq-items").accordion({
		    active: 	false, 
			autoHeight: false,
		    navigation: true
		});
		
	}
	
	// alert newsletter
	
	if(jQuery('#alert').is(':visible')){
		jQuery('#alert').animate({opacity: 1.0}, 3000).fadeOut();
	}
	
	// functions
	function onAfterSlide(curr,next,opts){
		if (init == false) jQuery('#curSlide').remove();
		jQuery('#slide-tabs').children().removeClass('slide-on slide-last-on');
		var cl = (opts.currSlide == opts.slideCount - 1) ? 'slide-last-on' : 'slide-on';
		jQuery('#slide-tabs').children().eq(opts.currSlide).addClass(cl).find('h3').append(curSlide);
	}	
	function onAfterCase(curr,next,opts){
		if(opts.currSlide == 0){
			jQuery("#cases-prev").hide();
			jQuery("#cases-next").fadeIn();
		} else if(opts.currSlide >= 1){
			jQuery("#cases-prev,#cases-next").fadeIn();
		}
		if(opts.currSlide == opts.slideCount-1){
			jQuery("#cases-next").hide();			
		}
		jQuery("#cases-head span").html((opts.currSlide+1)+'/'+opts.slideCount);
	}
	jQuery(function() { 
		jQuery.fn.toggleFocus = function() { 
			return this.focus(function() { 
				if( this.value == this.defaultValue ) { 
					this.value = ""; 
				} 
			}).blur(function() { 
				if( !this.value.length ) { 
					this.value = this.defaultValue; 
				} 
			}); 
		}; 
		jQuery("#SearchForm_SearchForm_Search,#Form_NewsletterForm_Email").toggleFocus();
	});
	
});

})(jQuery);	