/* -------------------------------------------------------------- 
   
   JavaScript build on jQuery
  
-------------------------------------------------------------- */ 

//ie6 check
var ie6 = false;
if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
  	ie6 = true;
};


// some defaults
	$.easing.easeOutExpo = function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	};

	$.easing.def = "easeOutExpo";

	// check if a variable is defined or not
	function defined( constant_name )  {
	    return (typeof window[constant_name] !== 'undefined');
	}
	
	

// E-mail defuscate
jQuery.fn.defuscate = function(settings) {
	settings = jQuery.extend({link: true}, settings);
	regex = /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi;
	classes = $(this).attr("class");
	mailto = '<a href="mailto:$1@$2" class="'+classes+'">$1@$2</a>';
	plain = "$1@$2";
	return this.each(function() {
	defuscated = $(this).html().replace(regex, settings.link ? mailto : plain)
		$(this).after(defuscated).remove();
	});
};




// init rotating image-Gallery
function initGallery() {
	if ($.fn.cycle) {
		var $gallery = $('ul#imagerotate');
		
		//progressive enhancement 
		if (defined('galleryimages')) {
			for (var i = 0; i < galleryimages.length; ++i){
				$gallery.append('<li><img src="'+galleryimages[i]+'" alt=""></li>');
			};
		};
		
		if ($('#nuflogo').length > 0) {
			$gallery.after('<div id="nufnav">');
		}
		
		$gallery.cycle({
			fx: 'fade',
			speed: 750,
			timeout: 10000,
			pager: '#nufnav'
		});
	}
}


// init Moebel-Detail-Gallery
function initDetailGallery() {
	var $gallery = $('#detailgallery');

	$('ul.thumblist li a', $gallery).bind('click', function() {
		var imgsrc = $(this).attr('href');
		var imgalt = $('img', this).attr('alt');
		$('#detailimage').attr('src',imgsrc).attr('alt',imgalt);
		return false;
	});	
}


// init Switcher
function initSwitcher(content, nav, heightcorrection) {
	if ($.fn.cycle) {
		var $rotator = $(content);
		
		
		$rotator.cycle({
			fx: 'fade',
			speed: 250,
			timeout: 0,
			cleartype: true,
			pager: nav,
			pagerAnchorBuilder: function(idx, slide) { 
	        // return selector string for existing anchor 
	        return nav + ' li:eq(' + idx + ') a'; 
	    },
	    before: function() { 
				var thisheight = $(this).height();
				
				if (!heightcorrection) {
					if (!$.browser.msie) {
				  	 thisheight -= 20;
					}
				}
      	$rotator.animate({ height: thisheight}, 250, "easeOutExpo" ); 
      }
		});
	}
}



// init Quicksearch
function initQuicksearch() {

	if ($.fn.quicksearch) {
	
		// wrap heading in custom code
		$('#dealerlist h2').addClass('el_1').wrap('<div class="two_elements"></div>').after('<div class="el_2"></div>');
		

		if ($('body').hasClass('lang_de')) {
			var quicksearchlabel = "Händler-Schnellsuche";
			var quicksearchtitle = "Suchen Sie hier nach Name, Ort, Telefonnummer usw. um die Liste einzugrenzen";
		} else {
			var quicksearchlabel = "Dealer-Quicksearch";
			var quicksearchtitle = "Search here for name, city, fonnumber eg.";
		}
		
		//de
		if ($('#dealer_de ul li').length > 1) {
			$('#dealer_de ul li').quicksearch({
			  position: 'append',
			  attached: '#dealer_de .el_2',
			  loaderText: '',
			  formId: 'quicksearch_de',
			  labelText: quicksearchlabel,
			  delay: 100
			});
		};
		
		//at
		if ($('#dealer_at ul li').length > 1) {
			$('#dealer_at ul li').quicksearch({
			  position: 'append',
			  attached: '#dealer_at .el_2',
			  loaderText: '',
			  formId: 'quicksearch_at',
			  labelText: quicksearchlabel,
			  delay: 100
			});
		}
		
		//ch
		if ($('#dealer_ch ul li').length > 1) {
			$('#dealer_ch ul li').quicksearch({
			  position: 'append',
			  attached: '#dealer_ch .el_2',
			  loaderText: '',
			  formId: 'quicksearch_ch',
			  labelText: quicksearchlabel,
			  delay: 100
			});
		}
		
		//nl
		if ($('#dealer_nl ul li').length > 1) {
			$('#dealer_nl ul li').quicksearch({
			  position: 'append',
			  attached: '#dealer_nl .el_2',
			  loaderText: '',
			  formId: 'quicksearch_nl',
			  labelText: quicksearchlabel,
			  delay: 100
			});
		}
		
		//be
		if ($('#dealer_be ul li').length > 1) {
			$('#dealer_be ul li').quicksearch({
			  position: 'append',
			  attached: '#dealer_be .el_2',
			  loaderText: '',
			  formId: 'quicksearch_be',
			  labelText: quicksearchlabel,
			  delay: 100
			});
		}
		
		$('#dealerlist input[type="text"]').addClass('text').attr('title', quicksearchtitle);
	};
}


// Validate Contact-Form
function initFormValidator(where){
	if ($.fn.validate) {
		$form_holder = $(where);
		var v = $($form_holder).validate();
	};
}



jQuery(function( $ ){
	// indicator for css if js is available or not
	$('body').removeClass('nojs');
	
	//decode e-mail-addresses
	$("span.email, p.email, strong.email").defuscate();
	
	//functions for ie6
	if(ie6) {
		//add hover for color-swatch-magnification
		$('ul#colorswatches ul li').hover(
		  function () {
		    $(this).addClass("hover");
		  },
		  function () {
		    $(this).removeClass("hover");
		  }
		);
	};
	
	
	// redefine Cycle-active-class
	$.fn.cycle.updateActivePagerLink = function(pager, currSlide) {
		$(pager).find('a').removeClass('active').filter('a:eq('+currSlide+')').addClass('active');
	};
	
	
	
	// init functions
	initGallery();
	initDetailGallery();
	initSwitcher('ul#colorswatches', '#colorswatchnav');
	initSwitcher('ul#dealerlist','#dealernav');
	initSwitcher('ul#agencies','#dealernav', true);
	initQuicksearch();
	
	// performaNuf Microsite
	initSwitcher('ul#nufcontent','ul#nufcontentnav', true);
	
	initFormValidator('#feedback');
	
	
	// make whole list-item clickable
	$("ul.textlist a").bigTarget();
	
});

