// +------------------------------------------------------------+
// | Flash Movie Library										|
// +------------------------------------------------------------+
var movieLib = {
	requiredVersion : 8,
	MAINHOME: {path:"/vwbw/media/flash/vwmenubig.swf",name:"homemenu",width:"994",height:"380"},
	DEALERHOME: {path:"/vwbw/media/flash/dealermenubig.swf",name:"homemenudealer",width:"994",height:"380"},
	FLASHMENU: {path:"/vwbw/media/flash/vwmenusmall.swf",name:"vwmenu",width:"994",height:"380"},
	MODELDETAIL: {path:"/vwbw/media/flash/vwmenudetail.swf",name:"modeldetail",width:"994",height:"380"},
	NEWSACCORDION: {path:"/vwbw/media/flash/vwnewsflash.swf",name:"newsaccordion",width:"751",height:"151"},
	MEDIABROWSER: {path:"/vwbw/media/flash/mediabrowser.swf",name:"vwmediabrowser",width:"700",height:"467"},
	GWASEARCH: {path:"/vwbw/media/flash/VWB_GWA_expandable.swf",name:"vwgwasearch",width:"700",height:"100%"},
	VWPGWASEARCH: {path:"/vwpw/media/flash/VWP_GWA_expandable.swf",name:"vwpgwasearch",width:"700",height:"4000"},
	MODELSPECS: {path:"/vwbw/media/flash/vw_specs_module.swf",name:"vw_specs_module",width:"700",height:"621"},
	FLASHSUBMENU: {path:"/vwbw/media/flash/vwsubmenu.swf",name:"vwsubmenu",width:"994",height:"35"},
	FLASHHEADER: {path:"/vwbw/media/flash/flash_header.swf",name:"vwflashheader",width:"100%",height:"35"},
	FLASHLABEL: {path:"/vwbw/media/flash/flash_label.swf",name:"vwflashlabel",width:"100%",height:"20"},
	EZINE: {path:"/vwbw/media/flash/Magazine.swf",name:"ezine",width:"100%",height:"100%"}
};

// +------------------------------------------------------------+
// | Flash Headers												|
// +------------------------------------------------------------+
function execFlashHeaders(e)
{	
	if(!neteffect.flashInfo.check(movieLib.requiredVersion))
		return;
	
	var list = $$(".js-flash-flashheader, .js-flash-flashlabel, .bluemotion-theme .article h2, .home h2"),i=list.length;

	while(i--)
	{
		new neteffect.FlashHeader(list[i]);
	};
};
neteffect.onDomReady(execFlashHeaders);

// +------------------------------------------------------------+
// | Print Buttons												|
// +------------------------------------------------------------+
function execPrintButtons(e){
	$(document.body).observe("click",function(e){
		if($(e.target).hasClassName("js-print"))
			print();
		return;
	});
};
neteffect.onDomReady(execPrintButtons);

// +------------------------------------------------------------+
// | Flash Embed												|
// +------------------------------------------------------------+
function execFlashEmbed(e)
{
	$$(".js-flashembed").each(function(el,i){
		new neteffect.HtmlFlashEmbed(el);
	});
};
neteffect.onDomReady(execFlashEmbed);

// +------------------------------------------------------------+
// | Goto select.value (url) on Select.onchange					|
// +------------------------------------------------------------+
function execGotoSiteOnChange(e){
	
	function changeHandler(e)
	{
		var value = this.options[this.selectedIndex].value;
		if(value!="ignore" && value!="")
		{
			document.location.href = value;
		};
		return false;
	};
	
	var list=$$(".js-goto-site-onchange"),i=list.length;
	
	while(i--)
	{
		$(list[i]).observe("change",changeHandler);
	};	
};
neteffect.onDomReady(execGotoSiteOnChange);

// +------------------------------------------------------------+
// | Submit on change											|
// +------------------------------------------------------------+
function execSubmitFormOnChange(e)
{
	var list=$$("select.js-submit-onchange"),i=list.length;
	
	function changeHandler(e)
	{
		var value = this.options[this.selectedIndex].value;
		if (value !="ignore")
		{
			var form = this.up("form");
			if (form) {
				form.submit();
			};
		};
		return false;
	};
	
	while(i--)
	{
		$(list[i]).observe("change",changeHandler);
	};
	return;
};
neteffect.onDomReady(execSubmitFormOnChange);

// +------------------------------------------------------------+
// | submit (input@type='image') rollovers						|
// +------------------------------------------------------------+
function execInputRollOverImages()
{
	$$("input.ro").each(function(el){
		el.observe("mouseover",function(e){
			this.src = this.src.replace(".gif","_ro.gif");
		});
		el.observe("mouseout",function(e){
			this.src = this.src.replace("_ro.gif",".gif");
		});
	});
};
neteffect.onDomReady(execInputRollOverImages);


// +------------------------------------------------------------+
// | Link Popup Window CLICK DELEGATE							|
// +------------------------------------------------------------+
function delegateLinkPopWindow(e)
{
	var el= e.findElement("a.js-link-pop-window");
	/* 
	 * checking on className again since prototype 1.6 has issues in IE9 
	 *
	 **/
	if(el && el.hasClassName("js-link-pop-window"))
	{
		e.stop();
		neteffect.linkPopWindow( el );		// was: this, should be el as this was 'undefined'!! (20080818, JV)
	};
	return;
};
$(document).observe("click",delegateLinkPopWindow);

//+-------------------------------------------------------------+
//| Link Popup Window width dimensions CLICK DELEGATE			|
//+-------------------------------------------------------------+
function delegateLinkDimPopWindow(e)
{
	var el = e.findElement("a[class*=js-popup-]");
	/* 
	 * checking on className again since prototype 1.6 has issues in IE9 
	 *
	 **/
	if(el && /(?:^|\s+)js-popup-/.test(el.className))
	{
		e.stop();
		var url = el.href;
		
		if(!url)
			return;
		
		var cache = neteffect.Cache.getElementCache(el);
		
		if(!cache.PopUpObject)
		{
			var re = /js-popup-(\w+)?-?(\w+)?-?(\w+)?/g;
			re.test(el.className);
			
			var params = {
				width : RegExp.$1,
				height : RegExp.$2,
				scrollbars : RegExp.$3 ? 1 : 0,
				title : el.innerHTML,
				url : url
			};
			cache.PopUpObject = neteffect.PopUp(params);
		}
		else
		{
			cache.PopUpObject.open();
		};
	};
	return;
};
$(document).observe("click",delegateLinkDimPopWindow);

// +------------------------------------------------------------+
// | Link Popin Box	CLICK DELEGATE								|
// +------------------------------------------------------------+
function delegateLinkPopInBoxClick(e)
{
	var el = e.findElement("a.js-link-popin-box");
	/* 
	 * checking on className again since prototype 1.6 has issues in IE9 
	 *
	 **/
	if(el && el.hasClassName("js-link-popin-box"))
	{
		new neteffect.gui.LinkPopInBox(el);
		e.stop();
	};
};
$(document).observe("click",delegateLinkPopInBoxClick);

// +------------------------------------------------------------+
// | Previous page link											|
// +------------------------------------------------------------+
function delegatePreviousPageLink(e)
{
	var el = e.findElement("a.js-previous-page");
	
	/* 
	 * checking on className again since prototype 1.6 has issues in IE9 
	 *
	 **/
	if(el && el.hasClassName("js-previous-page"))
	{
		e.stop();
		history.go(-1);
	};
	return;
};
$(document).observe("click",delegatePreviousPageLink);

// +------------------------------------------------------------+
// | Car Texture Configurators									|
// +------------------------------------------------------------+
function execCarTextureConfigurators(e)
{
	
	var TextureConfiguratorsCache = [];
	var TextureConfiguratorsElementsCache = [];

	$$("div.js-texture-configurator").each(function(element){
		if(!TextureConfiguratorsElementsCache.member(element))
			TextureConfiguratorsCache.push(new neteffect.CarTextureConfigurator(element));
		TextureConfiguratorsElementsCache.push(element);
	});
	
};
neteffect.onDomReady(execCarTextureConfigurators);

// +------------------------------------------------------------+
// | Simple SlideShow											|
// +------------------------------------------------------------+
function execSimpleSlideshow(e)
{
	
	neteffect._SimpleSlideShowCache = [];
	
	$$("ul.js-slideshow").each(function(element){
		
		neteffect._SimpleSlideShowCache.push(new neteffect.SimpleSlideShow(element));
		
	});
	
};
neteffect.onDomReady(execSimpleSlideshow);

// +------------------------------------------------------------+
// | Visual Size Refreshing for IE								|
// +------------------------------------------------------------+
if (Prototype.Browser.IE) {
	
	neteffect.onDomReady(function(e){

		$$("div#visual img").each(function(element){
			
			element.setStyle({
				width: "auto",
				height: "auto"
			});
			
		});
		
	});
	
};

// +------------------------------------------------------------+
// | Tabbed Views												|
// +------------------------------------------------------------+
function execTabs()
{
	$$("div.tab-module").each(function(element){
		new neteffect.Tabs(element);
		new neteffect.MoreInfo(element);
	});
};
neteffect.onDomReady(execTabs);

function execMainBannersTabs()
{
	$$("div.tab-banner-module").each(function(e){
		new neteffect.MainBannersTabs(e);
	});
};
neteffect.onDomReady(execMainBannersTabs);

// +------------------------------------------------------------+
// | Wallpaper Links											|
// +------------------------------------------------------------+
function execWallPaperLinks()
{
	$$("a.wallpaper").each(function(el,i){
		el.observe("click",function(e){
			e.stop();
			var dim = this.innerHTML.split("x");
			var w = dim[0];
			var h = dim[1];
			
			var win = new neteffect.PopUp({
				url : this.href,
				width : w,
				height : h
			});
		});
	});
};
neteffect.onDomReady(execWallPaperLinks);

// +------------------------------------------------------------+
// | News accordion												|
// +------------------------------------------------------------+
function execNewsAccordion(e){

	$$("div.js-accordion").each(function(element){

		new neteffect.Accordion(element);
		
	});
	
};
neteffect.onDomReady(execNewsAccordion);

// +------------------------------------------------------------+
// | Table accordion											|
// +------------------------------------------------------------+
function execTableAccordion(e)
{
	$$("table.table-accordion").each(function(el,i){
		new neteffect.TableAccordion(el);
	});
};
neteffect.onDomReady(execTableAccordion);

// +------------------------------------------------------------+
// | Table row hover + click									|
// +------------------------------------------------------------+
function trHover(e){
	$$("table.js-hover-table tr").each(function(el){
		el.observe("mouseover",function(e){
			this.className = "hover"; 
		});
		el.observe("mouseout",function(e){
			this.className = ""; 
		});
		el.observe("click",function(e){
			var link = this.down("a.more");
			if (link) {
				var href = (link && link.href) ? link.href : "";
				window.location.href = href;
			}
			return;
		});
	});
};
neteffect.onDomReady(trHover);

// +------------------------------------------------------------+
// | Display alternate text when checkbox is activated			|
// +------------------------------------------------------------+
function checkBoxText(e){
	$$("span.js-active-text").invoke("hide");
	$$("input.js-active-checkbox").each(function(el){
		el.observe("click",function(e){
		var neighbours = this.nextSiblings();
		for(var n=0; n<neighbours.length; n++){
			if(neighbours[n].className.indexOf("js-active-label") != -1) {
				var checkLabel = neighbours[n];
			}
			if(neighbours[n].className.indexOf("js-active-text") != -1) {
				var checkText = neighbours[n];				
			}
		}
		checkLabel.toggle();
		checkText.toggle();
		return;
		});
	});
};
neteffect.onDomReady(checkBoxText);

// +------------------------------------------------------------+
// | Alternate Main Nav (IE6 and lower only)					|
// +------------------------------------------------------------+
(function(){
	if(!Prototype.Browser.IE)
		return;
	var version = parseInt(navigator.appVersion.split("MSIE").last());
	if(version >= 7)
		return;
		
	function altNav(e)
	{
		$$("ul.alternate-main-nav > li").each(function(element){
			
			element.observe("mouseover",function(e){
				this.addClassName("hover");
			});
			
			element.observe("mouseout",function(e){
				if(!$(e.target).ancestors().member(this))
					this.removeClassName("hover");
			});
			
		});
	};
	
	neteffect.onDomReady(altNav);
	
})();

// +------------------------------------------------------------+
// | Home Page Banners											|
// +------------------------------------------------------------+
function execHomePageBanners(e){
	
	$$("ul.alternate-banner-application > li").each(function(element){
		
		var preview = element.down("div");
		var oldZindex = preview.getStyle("zIndex");
		
		element.observe("mouseover",function(){
			preview.setStyle({zIndex:100});
		});
		
		element.observe("mouseout",function(){
			preview.setStyle({zIndex:oldZindex});
		});
		
	});
		
};
neteffect.onDomReady(execHomePageBanners);

// +------------------------------------------------------------+
// | Location Suggest											|
// +------------------------------------------------------------+
function execLocationSuggest(e)
{
	var initialized = [];
	$$("form.search-dealer").each(function(form){
		var field = form.down(".location-suggest-field");
		var target = form.down(".location-suggest-target");
		var zip = form.down("#zipcode");
		var errBox = form.next(".form-error-box");
		
		if(initialized.indexOf(field) > 0)
			return;
		else
			initialized.push(field);
		
		target.value = "";

		var ls = new neteffect.LocationSuggest(field,target,{
			url : "/vwbw/media/js/suggestions.txt",
			rows: 6,
			selectFirst : true,
			limitSize : true,
			limitMessage : "Er zijn helaas geen opties beschikbaar",
			boxClass : "autosuggest-box"
		});
		
		function validateForm(e)
		{
			if(!field.value && !zip.value)
			{
				errBox.innerHTML = "<p>Een plaatsnaam of postcode is vereist om dealers bij u in de buurt te vinden.</p>";
				$(errBox).show();
				return false;
			}
			else if(!field.value && !/^\d{4}$/.test(zip.value)) {
				errBox.innerHTML = "<p>De postcode mag alleen cijfers bevatten.</p><p>Probeer een andere plaats of postcode.</p>";
				$(errBox).show();
				return false;
			}
			$(errBox).hide();
			return form.submit();
		}
		
		ls.observe("match",function(e) {
			if(e.keyCode != Event.KEY_TAB) {
				validateForm();
			}
		});
		
		form.observe("submit", validateForm);
		
		/*
		var validator = new neteffect.FormValidator(form,{
			submitOnValid : false,
			compare : {
				ZIPNUMBERS : function(field){ return /^\d{4}$/.test($F(field)); }
			}
		});
		
		validator.observe("done",function(e){
			if(e.errors == 2){
			}else{
				form.submit();
			};
		});
		*/
	});
};
neteffect.onDomReady(execLocationSuggest);


// +------------------------------------------------------------+
// | Lokatie Net Adapter										|
// +------------------------------------------------------------+
function execLokatieNetAdapter()
{
	$$(".lokatie-adapter").each(function(el){
		
		new neteffect.LokatieNetAdapter(el);
		
	});
};
neteffect.onDomReady(execLokatieNetAdapter);

// +------------------------------------------------------------+
// | Compare Module												|
// +------------------------------------------------------------+
function execCompareModule()
{
	$$("div.compare-module").each(function(el){
		var src = $F(el.down("*[name='dataSrc']")) || null;
		new neteffect.CompareModule(el,src);
	});
};
neteffect.onDomReady(execCompareModule);

//+-------------------------------------------------------------+
//| Compare Module 2											|
//+-------------------------------------------------------------+
function execCompareModule2()
{
	var list = $$("div.compare-module2"),i=list.length;
	while(i--)
	{
		new neteffect.CompareModule2(list[i]);
	};
	return;
};
neteffect.onDomReady(execCompareModule2);

// +------------------------------------------------------------+
// | Image Swapper												|
// +------------------------------------------------------------+
function execImageSwapper()
{
	var list = $$(".js-picture-swapper"),l=list.length;i=0;
	for(i;i<l;i++)
		new neteffect.ImageSwapper(list[i],"longdesc");
};
neteffect.onDomReady(execImageSwapper);

//+-------------------------------------------------------------+
//| Google Map Implementation									|
//+-------------------------------------------------------------+
function execGoogleMaps()
{
	var list = $$("div.google-maps");
	for(var i=0,n;n=list[i++];)
	{
		new neteffect.GoogleMapAdapter(n);
	};
};
neteffect.onDomReady(execGoogleMaps);


// +------------------------------------------------------------+
// | Temporary VGN/XOPUS fix									|
// +------------------------------------------------------------+
$(document).observe("dom:loaded",function(e){
	/* Fixes vgn created zIndex bugs with template lay-outs.
	 */	
	$$("div[id^='top_']").each(function(element){
		var menu = element.down("div[id^='topMenu_']");
		var button = element.down("a[id^='regionBug_']");
		
		function fixPos(e)
		{
			var coords = this.positionedOffset();
			menu.setStyle({
				top: (coords.top + 10) + "px",
				left: (coords.left + 10) + "px"
			});
			this.stopObserving("mouseover",fixPos);
		};
		
		if (button) 
		{
			button.observe("mouseover", fixPos);
			
			menu.setStyle({
				zIndex : 1000
			});
		};
		element.setStyle({
			position: "static"
		});
		
	});
		
});

// +------------------------------------------------------------+
// | Flex, line 56? ERROR fix									|
// +------------------------------------------------------------+
if (Prototype.IE)
{
	$(document).observe("beforeunload",function(){
		var list = ["gwasearch","vwgwasearch"],i=list.length,obj,key;
		while(i--)
		{
			if(obj = eval(list[i]))
			{
				for(key in obj)
				{
					if(typeof key === "function")
					{
						try {obj[key]=null;}catch(e){};
					};
				};
			};
		};
		return;
	});
};

//+------------------------------------------------------------+
//| Shadowbox impl		      	                               |
//+------------------------------------------------------------+
$(document).observe("dom:loaded" , neteffect.implementShadowbox );

//+------------------------------------------------------------+
//| Social links		      	                               |
//+------------------------------------------------------------+
$( document ).observe("click",function( e ){
	
	if ( e.target === document || e.target.parentNode === document ||  !e.target.parentNode.hasClassName("social-share") || e.target.hasClassName("email")  ) {
		return;
	}

	window.open( e.target.href , "volkswagen", "width=750,height=600");
	e.stop();
	
});





