/**
 **************************************************
 * B&W Microsite / microsite.gute-anlage.de
 * userfunctions.microsite.js
 *
 * @author:HDNET GmbH & Co. KG
 * @version:1.0
 * @updated:2011-07-14 (AH)
 **************************************************
**/




/**
 **************************************************
 HELPER FUNCTIONS
 *
 * @description: needful functions
 **************************************************
**/
/* *** Initalize the gallery on product page - with scrolling *** */
$.fn.gallery = function(options) {
	return this.each(function(){
		var options = options;
		var settings = $.extend({
			size:5,
			fadeTime:300,
			speed:900
		}, options);
		
		var wrapper = $(this);
	
		wrapper.ready(function() {
			if(wrapper.find('.scrollable').length > 0) {
				wrapper.find('.scrollable .nav').scrollable({
					vertical:false,
					size:settings.size,
					speed:settings.speed
				});
			}
				
			var item = wrapper.find('.nav ul li');
			
			item.each(function(i) {
				$(this).click(function() {
					wrapper.find('div.detail div').fadeOut(settings.fadeTime/2);
					wrapper.find('div.detail div:eq('+i+')').fadeIn(settings.fadeTime);
				});
			}).filter(':first').click();
			
		});
	});
};




/**
 **************************************************
 GLOBAL DEFINITIONS
 *
 * @description: global domready function for all functions
 * run jQuery in noConflict mode
 **************************************************
**/
/* no conflict mode - it works besides prototype */
//var $ = jQuery.noConflict();

	
/* *** GLOBAL domReady begin *** */
$(document).ready(function() {
	
	// ga:map View Helper
	if(typeof initMap == 'function') {
		initMap();
	}
	
	/* Accordion based on jquery.tabs - hopefully it works within a nested tab
	 */
	//Slide up and down on click
	$('div.head .icons .more').click(function(){
		$('.accordion div.head').removeClass('current');
		$('.accordion div.pane').hide().removeClass('current');
		$(this).parent().parent().addClass('current');
		$(this).parent().parent().next('.accordion div.pane').slideDown().addClass('current');
		$(this).parent().find('span.less').show();
	});
	
	$('div.head .icons .less').click(function(){
		$('.accordion div.pane').slideUp().removeClass('current');
		$(this).parent().parent().removeClass('current');
		$(this).parent().find('span.more').show();
		$(this).hide();
	});
	
	
	/* More Link in the dealer search box
	 * shows more dealers and removes the more link
	 */
	$('ul.dealers li:gt(2)').addClass('none');
	
	var noneChildren = $('ul.dealers').children('.none');
    
    if (noneChildren.size() >= 2) {
    	$('.dealer a.more').live('click', function(e){
    		var noneChildren = $('ul.dealers').children('.none');
    		if (noneChildren.size() <= 2) {
    			$('ul.dealers li.none').removeClass('none');
    			$(this).remove();
    		} else {
    			$('ul.dealers li.none:lt(2)').removeClass('none');
    		}
    	
    		return false;
    	});
    } else {
		$('.dealer a.more').remove();
    }
	
	
	/* TABS for the product detail page
	 * used jquery tool tabs..
	 */
	$('ul.tabs').tabs('div.panes > div');
	
	
	/* Scroll to the changing content below the info/gallery box
	 * on the product detail page - jquery.scrollTo needed
	 */
	$('ul.tabs>li').each(function(i){
		$(this).click(function() {
			var $target = $('div.panes').find('div.tabContent:eq('+i+')');
			$('body').stop().scrollTo($target,800,{offset:-300});
		});
	});
	
	
	/* Creates the product gallery on the detail page
	 * includes jquery.scrollable and lightbox - see function above
	 */
	$('.product .images').gallery();
	
	
	/* Slider for suitable products based on jquery.scrollable
	 */
	$('.product .suitable .nav').scrollable({
		size:4,
		speed:900
	});
	
	if($('.product .suitable .nav li').size() <= 4) {
		$('.product .suitable a.browse').addClass('disabled');
	}
	
	
	/* DealerList in boxes
	 * Slide up and down on click
	 */
	$('div.teaser .dealersList h3').click(function(){
		$('div.teaser .dealersList h3').removeClass('current');
		if($(this).next('div.county').hasClass('current')) {
			$(this).removeClass('current');
			$(this).next('div.county').slideUp().removeClass('current');
		} else {
			$(this).parent().find('div.county').slideUp().removeClass('current');
			$(this).next('div.county').slideDown().addClass('current');
			$(this).addClass('current');
		}
	});
	

/* *** GLOBAL domready end *** */
});
