var MV = {
	pppage: 3						// Number of paragraphs to be displayed per page
};

/***************************************************** */
// callUInteraction
// call UInteraction from SLIMBOX (mootools.js)
/***************************************************** */
function callUInteraction(){
	UInteraction(1,1,1);
}

/***************************************************** */
// articleGalleryFlow
// regelt Flow der Minigallery	
/***************************************************** */
function articleGalleryFlow(i){
	var j=1;
	$$('#minigallery_id img.i').each(function(el){
		if(j == i)	{el.fade('in')}
		else		{el.fade('out')}	
		j++;
	});
	i++;
	if (i > $$('#minigallery_id img.i').length){i = 1}

	setTimeout("articleGalleryFlow("+i+")", 5000);
} 


/***************************************************** */
// VideoRating
// sendet User rating fuer ein Video	
/***************************************************** */
function VideoRating(rating, video){
	UInteraction(1,1,1, '/V/' + video + '/Rating//Rate'); 
	var req = new Request({
		method: 'post',
		url: '/rpc/videoRatingSave',
		data: { 'rating': rating, 'video_id': video},
		onRequest: function() {
		},
		onComplete: function(response) {
				if (WSID == 4){
					alert('Bewertung wurde erfolgreich gespeichert!');	
				}
				else if (WSID == 34){
					alert('Thank you for your rate!');	
				}
		}
	}).send();
}

/***************************************************** */
// MakeRating
// sendet User rating fuer Marken, Baureihen	
/***************************************************** */
function MakeRating(rating, id, type){
	//UInteraction(1,1,1, '/V/' + video + '/Rating//Rate'); 
	var req = new Request({
		method: 'post',
		url: '/rpc/makeRatingSave',
		data: { 'rating': rating, 'id': id, 'type': type},
		onRequest: function() {
		},
		onComplete: function(response) {
				var json = JSON.decode(response,true);
				$('ratingValue').innerHTML = json.VALUE;
				$('ratingCount').innerHTML = json.COUNT;
				if (WSID == 4){
					alert('Bewertung wurde erfolgreich gespeichert!');	
				}
				else if (WSID == 34){
					alert('Thank you for your rate!');	
				}
		}
	}).send();
}

/***************************************************** */
// toggleMenuHomeClass
// selects home by mv-icon mouseover	
/***************************************************** */
function toggleMenuHomeClass(){
	if ($('menu_home').get('class') != 'selected'){
		$('menu_home').toggleClass('mouseover');   
	}
} 

/***************************************************** */
// handleModelTabs
// handels visibility between engine and weights table
/***************************************************** */
function handleModelTabs(tracking_src){
	UInteraction(1,1,1, tracking_src); 
	if($('model_engine').getStyle('display') == 'none'){
		$('model_engine').setStyle('display','table');
		$('model_weights').setStyle('display','none');
		$('engine_tab').toggleClass('selected');
		$('weights_tab').toggleClass('selected');
	}else if($('model_weights').getStyle('display') == 'none'){
		$('model_engine').setStyle('display','none');
		$('model_weights').setStyle('display','table');
		$('weights_tab').toggleClass('selected');
		$('engine_tab').toggleClass('selected');	
	}
}

/***************************************************** */
// handleRelatedTabs
// handels visibility between related Videos, related articles and related questions
/***************************************************** */
function handleRelatedTabs(ID, tracking_src){ 
	UInteraction(1,1,1, tracking_src);
	$$('#taps_top_main_related_tabs li a').each(function(a){
		if(a.id == ID+'_tab'){
			a.set('class', 'selected');
		}else{
			a.set('class', '');
		}
	});
	$$('#related div.group').each(function(div){
		if(div.id == ID){
			div.setStyle('display','block');
		}else{
			div.setStyle('display','none');
		}
	});
}

/***************************************************** */
// MakeVideosPaging 
// Pages through Make Videos 
/***************************************************** */
function MakeVideosPaging(page,ga){
	UInteraction(1,1,1, ga);
	v_count = 6;
	$$('#make_video_paging a.selected')[0].id.match(/.*_([0-9]*)/);
	var current = parseInt(RegExp.$1);

	$$('#make_video_paging a')[$$('#make_video_paging a').length-1].id.match(/.*_([0-9]*)/);
	var max 	= parseInt(RegExp.$1);

	if(page == 'next')			{page = current+1}
	else if(page == 'prev')		{page = current-1}

	var start 	= (page-1)*v_count+1;
	var end 	= start+v_count-1;

	// Fade in the requested videos, fade out the not needed videos
	$$('#make_videos div').each(function(v){
		if(v.id.match(/video_([0-9]*)/)){
			var i = RegExp.$1;
			var style;
			if( i >= start && i <= end)	{style = 'block'}
			else						{style = 'none'}

			if($('video_'+i))			{$('video_'+i).setStyle('display', style)}
		}
	});

	// Select the current page number
	$$('#make_video_paging a').each(function(p){
		if(p.id.match(/make_video_paging_b_([0-9]*)/)){
			var i = RegExp.$1;
			p.removeClass('selected');
			if(i == page){p.addClass('selected')}
		}
	});

	// Handling PREV and NEXT button
	if (page == 1)		{
		$('make_video_prev_top').setStyle('visibility', 'hidden'); 
		$('make_video_prev_bottom').setStyle('visibility', 'hidden');
	}else{
		$('make_video_prev_top').setStyle('visibility', 'visible');
		$('make_video_prev_bottom').setStyle('visibility', 'visible');
	}
	
	if (page == max)	{
		$('make_video_next_top').setStyle('visibility', 'hidden');
		$('make_video_next_bottom').setStyle('visibility', 'hidden');
	}else{
		$('make_video_next_top').setStyle('visibility', 'visible');
		$('make_video_next_bottom').setStyle('visibility', 'visible');
	} 
}


/***************************************************** */
// MakeArticlesPaging 
// Pages through Make Articles 
/***************************************************** */
function MakeArticlesPaging(page, ga){
	UInteraction(1,1,1, ga);
	a_count = 5;
	$$('#make_article_paging a.selected')[0].id.match(/.*_([0-9]*)/);
	var current = parseInt(RegExp.$1);

	$$('#make_article_paging a')[$$('#make_article_paging a').length-1].id.match(/.*_([0-9]*)/);
	var max 	= parseInt(RegExp.$1);

	if(page == 'next')			{page = current+1}
	else if(page == 'prev')		{page = current-1}

	var start 	= (page-1)*a_count+1;
	var end 	= start+a_count-1;

	// Fade in the requested articles, fade out the not needed articles
	$$('#make_articles div').each(function(a){
		if(a.id.match(/article_([0-9]*)/)){
			var i = RegExp.$1;
			var style;
			if( i >= start && i <= end)	{style = 'block'}
			else						{style = 'none'}

			if($('article_'+i))			{$('article_'+i).setStyle('display', style)}
		}
	});

	// Select the current page number
	$$('#make_article_paging a').each(function(p){
		if(p.id.match(/make_article_paging_b_([0-9]*)/)){
			var i = RegExp.$1;
			p.removeClass('selected');
			if(i == page){p.addClass('selected')}
		}
	});

	// Handling PREV and NEXT button
	if (page == 1)		{
		$('make_article_prev_top').setStyle('visibility', 'hidden'); 
		$('make_article_prev_bottom').setStyle('visibility', 'hidden');
	}else{
		$('make_article_prev_top').setStyle('visibility', 'visible');
		$('make_article_prev_bottom').setStyle('visibility', 'visible');
	}
	
	if (page == max)	{
		$('make_article_next_top').setStyle('visibility', 'hidden');
		$('make_article_next_bottom').setStyle('visibility', 'hidden');
	}else{
		$('make_article_next_top').setStyle('visibility', 'visible');
		$('make_article_next_bottom').setStyle('visibility', 'visible');
	} 
}

/***************************************************** */
// ArticlePaging 
// Pages through an article
/***************************************************** */
function ArticlePaging(page, tracking_src){
	UInteraction(1,1,1, tracking_src);

	$$('#article_paging a.selected')[0].id.match(/.*_([0-9]*)/);
	var current = parseInt(RegExp.$1);

	$$('#article_paging a')[$$('#article_paging a').length-1].id.match(/.*_([0-9]*)/);
	var max 	= parseInt(RegExp.$1);

	if(page == 'next')			{page = current+1}
	else if(page == 'prev')		{page = current-1}

	window.scrollTo(0,0);
	var start 	= (page-1)*MV.pppage+1;
	var end 	= start+MV.pppage-1;

	// Fade in the requested paragraphs, fade out the not needed paragraphs
	$$('#news_art_art_co p').each(function(p){
		if(p.id.match(/para_([0-9]*)/)){
			var i = RegExp.$1;
			var style;
			if( i >= start && i <= end)	{style = 'block'}
			else						{style = 'none'}

			if($('para_'+i))			{$('para_'+i).setStyle('display', style)}
			if($('para_ph_'+i))			{$('para_ph_'+i).setStyle('display', style)}
			if($('para_img_'+i))		{$('para_img_'+i).setStyle('display', style)}
		}
	});

	$$('#news_art_art_co div').each(function(div){
		if(div.id.match(/para_([0-9]*)/)){
			var i = RegExp.$1;
			var style;
			if( i >= start && i <= end) {style = 'block'}
			else                        {style = 'none'}
			if($('para_'+i))            {$('para_'+i).setStyle('display', style)}
			if($('para_ph_'+i))         {$('para_ph_'+i).setStyle('display', style)}
		}
	});

	$$('#news_art_art_co table').each(function(table){
		if(table.id.match(/para_([0-9]*)/)){
			var i = RegExp.$1;
			var style;
			if( i >= start && i <= end)	{style = 'table'}
			else						{style = 'none'}

			if($('para_'+i))			{$('para_'+i).setStyle('display', style)}
		}
	});


	// Teaser shall be displayed only on the first page
	if(page == 1)	{$('art_teaser').setStyle('display','block')}
	else			{$('art_teaser').setStyle('display','none')}

	// Select the current page number
	$$('#article_paging a').each(function(p){
		if(p.id.match(/article_paging_b_([0-9]*)/)){
			var i = RegExp.$1;
			p.removeClass('selected');
			if(i == page){p.addClass('selected')}
		}
	});

	// Handling PREV and NEXT button
	if (page == 1)		{
		$('article_gallery_paging_prev_top').setStyle('visibility', 'hidden'); 
		$('article_gallery_paging_prev_bottom').setStyle('visibility', 'hidden');
		MoveGallery('top');
	}else{
		$('article_gallery_paging_prev_top').setStyle('visibility', 'visible');
		$('article_gallery_paging_prev_bottom').setStyle('visibility', 'visible');
		 MoveGallery('bottom');
	}
	
	if (page == max)	{
		$('article_gallery_paging_next_top').setStyle('visibility', 'hidden');
		$('article_gallery_paging_next_bottom').setStyle('visibility', 'hidden');
	}else{
		$('article_gallery_paging_next_top').setStyle('visibility', 'visible');
		$('article_gallery_paging_next_bottom').setStyle('visibility', 'visible');
	} 
}


/***************************************************** */
// SMZBOX 
// Creates the SZM pixel if not existing, otherwise it changes the SZM pixel's source 
/***************************************************** */
var noszm = 0;
function szmbox(){

	var ref					= 'noref';
	var random 				= Math.round(Math.random() * 10000000);
	if (document.referrer) 	{ref = escape(document.referrer);}
	var szmurl 				= 'http://motorvis.ivwbox.de/cgi-bin/ivw/CP/'+szm+';?r='+ref+'&d='+random;

/*
	if(GetQuery('utm_campaign') == 'videopu' && noszm == 0){
		noszm = 1;
		document.write('<img src="#" id="ivwpix" width="1" height="1" style="position: absolute; top: -100px; left: -100px">');
		return;
	}
*/

	if(!$('ivwpix'))		{document.write('<img src="'+szmurl+'" id="ivwpix" width="1" height="1" style="position: absolute; top: -100px; left: -100px">');}
	else					{$('ivwpix').src = szmurl;}

}


/***************************************************** */
// gaReload 
// Recalls the Google Analytics
/***************************************************** */
function gaReload(param){
  	_gaq.push(['_trackPageview', param]);
}

/***************************************************** */
// adReload 
// Reloads the Ads
/***************************************************** */
function adReload(){


	if(document.getElementById('adsense')){return};

	// WALLPAPER
	if(document.getElementById('mv_wallpaper')){
		document.getElementById('iqd_mainAd').removeChild(document.getElementById('mv_wallpaper'));
	}

	while(document.getElementById('iqd_mainAd').hasChildNodes()){
		document.getElementById('iqd_mainAd').removeChild(document.getElementById('iqd_mainAd').firstChild);
	}

	//<script type='text/javascript' src='http://ox-d.mvsuite.de/spc.php?zones=172|173|174|175|176'></script>

	var srcs = document.getElementsByTagName('script');
	var zones 	= Array();
	for(var i = 0; i < srcs.length; i++){
		if(srcs[i].src && srcs[i].src.match(/http:\/\/ox-d.mvsuite.de\/spc.php\?zones=(.*?)$/)){
			zones = RegExp.$1.split(/\|/);
		}
	}


	document.getElementById('iqd_mainAd').setAttribute('style', 'height: 100px');
/*
	while(document.getElementById('iqadtile1').hasChildNodes()){
		document.getElementById('iqadtile1').removeChild(document.getElementById('iqadtile1').firstChild);
	}
	while(document.getElementById('iqadtile2').hasChildNodes()){
		document.getElementById('iqadtile2').removeChild(document.getElementById('iqadtile2').firstChild);
	}
*/


	var height = CheckHeight();
	var rnd = Math.random()*10000000000000000;
	var wallpaper = document.createElement('iframe');
	wallpaper.setAttribute('src', '/js/switch_ads_wallpaper.html?zones='+zones+'&r='+rnd);
	wallpaper.setAttribute('id', 'mv_wallpaper');
	wallpaper.setAttribute('allowtransparency', true);
	wallpaper.setAttribute('frameborder', 0);
	wallpaper.setAttribute('border', 0);
	wallpaper.setAttribute('style', 'position: absolute; height: '+height+'px; width: 100%; top: 0px; left: 0px; z-index: 0');
	document.getElementById('iqd_mainAd').appendChild(wallpaper);


	while(document.getElementById('iqadtile8').hasChildNodes()){
		document.getElementById('iqadtile8').removeChild(document.getElementById('iqadtile8').firstChild);
	}

	var rect_1 = document.createElement('iframe');
	rect_1.setAttribute('src', '/js/switch_ads_rect_1.html?zones='+zones+'&r='+rnd);
	rect_1.setAttribute('id', 'mv_rect_1');
	rect_1.setAttribute('allowtransparency', true);
	rect_1.setAttribute('frameborder', 0);
	rect_1.setAttribute('border', 0);
	rect_1.setAttribute('scrolling', 'no');
	rect_1.setAttribute('style', 'height: 250px; width: 300px;');
	document.getElementById('iqadtile8').appendChild(rect_1);


	while(document.getElementById('iqadtile10').hasChildNodes()){
		document.getElementById('iqadtile10').removeChild(document.getElementById('iqadtile10').firstChild);
	}

	var rect_2 = document.createElement('iframe');
	rect_2.setAttribute('src', '/js/switch_ads_rect_2.html?zones='+zones+'&r='+rnd);
	rect_2.setAttribute('id', 'mv_rect_2');
	rect_2.setAttribute('allowtransparency', true);
	rect_2.setAttribute('frameborder', 0);
	rect_2.setAttribute('border', 0);
	rect_2.setAttribute('scrolling', 'no');
	rect_2.setAttribute('style', 'height: 250px; width: 300px;');
	document.getElementById('iqadtile10').appendChild(rect_2);

	while(document.getElementById('iqadtile3').hasChildNodes()){
		document.getElementById('iqadtile3').removeChild(document.getElementById('iqadtile3').firstChild);
	}

	var flashbuehne = document.createElement('iframe');
	flashbuehne.setAttribute('src', '/js/switch_ads_flashbuehne.html?zones='+zones+'&r='+rnd);
	flashbuehne.setAttribute('id', 'mv_flashbuehne');
	flashbuehne.setAttribute('allowtransparency', true);
	flashbuehne.setAttribute('frameborder', 0);
	flashbuehne.setAttribute('border', 0);
	flashbuehne.setAttribute('scrolling', 'no');
	flashbuehne.setAttribute('style', 'height: 0px; width: 970px;');
	document.getElementById('iqadtile3').appendChild(flashbuehne);


}


/***************************************************** */
// CheckHeight 
/***************************************************** */
function CheckHeight(){
	var isOffsetHeight = parseInt(document.getElementsByTagName('body')[0].offsetHeight);
	if (isOffsetHeight < 800){
		var sumHeight = 0;
		var childs = document.body.childNodes;
		for (var i = 0; i < childs.length; i++){
			if(childs[i].nodeType == 1){
				sumHeight += childs[i].offsetHeight;
			}
		}
		isOffsetHeight = sumHeight;
	}
	return isOffsetHeight; 
}


/***************************************************** */
// UInteraction 
// Dependion on passed parameters, it re-calls several tracking functions
/***************************************************** */
function UInteraction(szm,ga,ad, param){
	if(szm)	{szmbox()}
	if(ga)	{gaReload(param)}
	if(ad)	{adReload()}
}



/***************************************************** */
// FBComment 
// Fired if a user sends  a comment
/***************************************************** */
function FBComment(href, text_id, video_id){
	var tracking_src;
	if (text_id != 'NULL')	{ tracking_src = '/A/' + text_id; }
	else					{ tracking_src = '/V/' + video_id; }
	tracking_src += '/Comment/Bottom/Submit';
	UInteraction(1,1,1, tracking_src);
	_gaq.push(['_trackSocial', 'facebook', 'comment'])

	var req = new Request({
		method: 'post',
		url: '/rpc/fbComment',
		data: { 'link': href, 'text_id': text_id, 'video_id': video_id },
		onRequest: function() {
		},
		onComplete: function(response) {
		}
	}).send();
}


/***************************************************** */
// FBCommentRemove
// Fired if a user deletes a comment
/***************************************************** */
function FBCommentRemove(href, text_id, video_id){
	var tracking_src;
	if (text_id != 'NULL')	{ tracking_src = '/A/' + text_id; }
	else					{ tracking_src = '/V/' + video_id; }
	tracking_src += '/Comment/Bottom/Remove';
	UInteraction(1,1,1, tracking_src);
	_gaq.push(['_trackSocial', 'facebook', 'uncomment'])

	var req = new Request({
		method: 'post',
		url: '/rpc/fbCommentRemove',
		data: { 'link': href, 'text_id': text_id, 'video_id': video_id },
		onRequest: function() {
		},
		onComplete: function(response) {
		}
	}).send();
}


/***************************************************** */
// FBLike 
// Fired if a user clicks "I Like" or "I Dislike"
/***************************************************** */
function FBLike(r){
	UInteraction(1,0,1);
}


/***************************************************** */
// MoveGallery
// Brings gallery to the top / bottom of the article
/***************************************************** */
function MoveGallery(dir){

	if(dir == 'top')			{$('art_teaser').grab($('te_gallery'), 'after')}
	else						{$('news_art_art_co').grab($('te_gallery'),'bottom');$('news_art_art_co').grab($('social_banner_bottom'),'bottom');}
	/*
	$('ga_lb_1').setStyle('display', 'inline');
	$('ga_ar_r').setStyle('visibility', 'visible');
	*/
}

/***************************************************** */
// MoveSocialBanner
// Brings social-banner to the top of the article
/***************************************************** */
function MoveSocialBanner(){
	$('news_art_art_co').grab($('social_banner_top'),'top');
}

/***************************************************** */
// Voting
// sendet Voting zu einem Bild und blendet naechstes Bild ein 
/***************************************************** */
function Voting(rating, tracking_src){
	dir = 1;
	$$('#ga_img_con div').each(function(e){
		if(e.style.display == 'inline'){
			e.id.match(/.*_([0-9]*)/);
			current 	= parseInt(RegExp.$1);
			voting_item = $$('#'+e.id+' img'); 
			voting_item[0].id.match(/.*_([0-9]*)/);
			voting_item = RegExp.$1;
			current_obj = e;
		}
	});
	UInteraction(1,1,1, tracking_src + '/Voting/' + voting_item);

	var req = new Request({
		method: 'post',
		url: '/rpc/votingRatingSave',
		data: { 'rating': rating, 'id': voting_item},
		onRequest: function() {
		},
		onComplete: function(response) {
			var requested			= parseInt(current+dir);
			var max = $$('#ga_img_con div').length;
			if(requested == max+1)		{location.href = window.location.pathname.replace('artikel', 'ergebnis')}
			else{
				current_obj.setStyle('display', 'none');
				$('ga_lb_'+requested).setStyle('display', 'inline');
				$('ga_t').set('text',$$('#ga_lb_'+requested+' img')[0].title);
				$('gal_r_c').set('text',requested);
			}
		}
	}).send();

}

/***************************************************** */
// GalleryPaging
// Pages through the gallery
/***************************************************** */
function GalleryPaging(dir, voting, tracking_src){
	UInteraction(1,1,1, tracking_src);
	var max = $$('#ga_img_con div').length;
	$$('#ga_img_con div').each(function(e){
		if(e.style.display == 'inline'){
			e.id.match(/.*_([0-9]*)/);
			current = parseInt(RegExp.$1);
		}
		if(parseInt(current+dir) <= max && parseInt(current+dir) >= 1){e.setStyle('display', 'none');}
	});
	var requested = parseInt(current+dir);
	if(requested == max+1 && voting){location.href = window.location.pathname.replace('artikel', 'ergebnis')}
	else if(requested >= 1 && requested <= max){
		$('ga_lb_'+requested).setStyle('display', 'inline');
		$('ga_t').set('text',$$('#ga_lb_'+requested+' img')[0].title);
		$('gal_r_c').set('text',requested);
	}

//	$$('#ga_lb_'+requested+' img')[0].id.match(/.*_([0-9]*)/);
//	alert(RegExp.$1);
}

/***************************************************** */
// PreviewGalleryPaging
// Pages through the preview gallery 
/***************************************************** */
function PreviewGalleryPaging(dir,max, tracking_src){
	UInteraction(1,1,1, tracking_src);
	$$('#mga_img_co a').some(function(e){
		if(e.style.display == 'inline'){
			e.id.match(/.*_([0-9]*)/);
			current = parseInt(RegExp.$1);
			return true;
		}
	});
	var requested = parseInt(current+dir);
	if(parseInt(current+dir) >= 1 && requested+5 <= max){
		$$('#mga_img_co a').each(function(e){
			if(e.id.match(/.*_([0-9]*)/)){
				if(parseInt(RegExp.$1) >= requested && requested+5 >= parseInt(RegExp.$1)){
					e.setStyle('display', 'inline');
				}else{
					e.setStyle('display', 'none');
				}
			}
		});
	}
}

/***************************************************** */
// LoadPreview2Gallery
// loads thumb pic into main gallery
/***************************************************** */
function LoadPreview2Gallery(num, tracking_src){
	UInteraction(1,1,1, tracking_src);
	$$('#ga_img_con div').each(function(e){
		if($('ga_lb_'+num).match(e))	{e.setStyle('display', 'inline');}
		else							{e.setStyle('display', 'none');}
	});
	$('ga_t').set('text',$$('#ga_lb_'+num+' img')[0].title);
	$('gal_r_c').set('text',num);
/*
	var max = $$('#ga_img_con a').length;

	if(num == max)	{$('ga_ar_r').setStyle('visibility', 'hidden')}
	else			{$('ga_ar_r').setStyle('visibility', 'visible')}

	if(num >1 )		{$('ga_ar_l').setStyle('visibility', 'visible')}
	else			{$('ga_ar_l').setStyle('visibility', 'hidden')}*/
}

/***************************************************** */
// PollingCheck
// Checks / Unchecks the Polling checkboxes
/***************************************************** */
function PollingCheck(e, tracking_src){
	UInteraction(1,1,1, tracking_src);

	$$('#form_polling  a').each(function(a){a.removeClass('selected')});
	var event = e ? e : window.event;
	var target = event.target || event.srcElement;
	target.addClass('selected');
	target.checked = true;
	target.id.match(/.*_([0-9]*)/);
	var answer = parseInt(RegExp.$1);
	$('radio_'+answer).checked = true;
}

/***************************************************** */
// AjaxFormError
// ???????????????????????????????
/***************************************************** */
function AjaxFormError(e, form, formid){
	var json = JSON.decode(e,true);
	if (json.success == false){
		for(var i = 0; i < json.errors.length; i++){
			var obj = $(formid).getElements('input[name='+json.errors[i].field+']')[0];
			obj.errors = Array(json.errors[i].msg);
			form.addError(obj);
		}
	}
	else if (json.success == true){
		document.location.href = json.url;
	}
}

/***************************************************** */
// AjaxFormReset
// ???????????????????????????????
/***************************************************** */
function AjaxFormReset(){
	$$('div.fc-tbx').each(function(obj){obj.setStyle('opacity',0)})
}

/***************************************************** */
// toggleTeaserPicture
// switches pictures in main teaser
/***************************************************** */
function toggleTeaserPicture(count,auto, tracking_src){
	var el = $('teaser_item_'+count);
	$$('li.bg_carbon_teaser').each(function(e){
		if(e.match(el)){
			el.addClass('red_bg');
			var b=$('teaser_pics');
			var a=new Fx.Tween(b,{property:"marginTop",link:"cancel",duration:500});
			a.start(-288*(count-1));
		}else{
			e.removeClass('red_bg');
		}
	});
	if(auto){
		if(count == 3){
			count = 1;
		}else{
			count++;
		}	
		teaserFlowTimer = setTimeout(function(){toggleTeaserPicture(count,true);}, 5000);
	}else{
		clearTimeout(teaserFlowTimer);
		UInteraction(1,1,1, tracking_src);
	}
}

/***************************************************** */                                                                                                                                                                                
// toggleVideoTeaserPicture
// switches pictures in video teaser
/***************************************************** */
function toggleVideoTeaserPicture(count,auto, tracking_src){
	var el = $('teaser_item_'+count);
	$$('li.bg_video_teaser').each(function(e){                                                                                                                                                                                           
		if(e.match(el)){
			el.addClass('selected_bg');
			var b=$('teaser_pics');                                                                                                                                                                                                      
			var a=new Fx.Tween(b,{property:"marginTop",link:"cancel",duration:500});
			a.start(-356*(count-1));
		}else{                                                                                                                                                                                                                           
			e.removeClass('selected_bg');
		}
	});                                                                                                                                                                                                                                  
	if(auto){
		if(count == 4){
			count = 1;                                                                                                                                                                                                                   
		}else{
			count++;
		}                                                                                                                                                                                                                                
		videoTeaserFlowTimer = setTimeout(function(){toggleVideoTeaserPicture(count,true);}, 5000);
	}else{
		clearTimeout(videoTeaserFlowTimer);
		UInteraction(1,1,1, tracking_src);
	}
}

/***************************************************** */
// 
//
/***************************************************** */
function galleryPaging(dir,max,offset,visibleItems,id, tracking_src){
	UInteraction(1,1,1, tracking_src);
	var end = (max-visibleItems)*offset;
	var b = $(id);
	var a=new Fx.Tween(b,{property:"marginLeft",link:"cancel",duration:500});
	if(dir == 'next'){
		if(b.getStyle('marginLeft').toInt() <= (end*-1)){
			a.start(0);
		}else{
			a.start(b.getStyle('marginLeft').toInt()-parseInt(offset));
		}
	}else if(dir == 'prev'){
		if(b.getStyle('marginLeft').toInt() == 0){
			a.start(-end);
		}else{
			a.start(b.getStyle('marginLeft').toInt()+parseInt(offset));
		}
	}
}

/***************************************************** */
//GetQuery
//Returns the value of a query param
/***************************************************** */
function GetQuery(name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )    return "";  else    return results[1];
}


function GetCaptcha(){
	var req = new Request({
		method: 'post',
		url: '/rpc/getCaptcha',
		onRequest: function() {
		},
		onComplete: function(response) {
			var json = JSON.decode(response,true);
			$('captcha_img').src = '/img/captcha/'+json.captcha+'.png';
			$('captcha_md5').value = json.captcha;
		}
	}).send();
}

/***************************************************** */
//GetSeriesByMake
//Returns make series 
/***************************************************** */
function GetSeriesByMake(make){
	var req = new Request({
		method: 'post',
		url: '/rpc/getSeriesByMake',
		data: { 'make': make },
		onRequest: function() {
		},
		onComplete: function(response) {
			var json = JSON.decode(response,true);
			document.getElementById('search_series').options.length = 0;
			document.getElementById('search_structure').options.length = 0;
			//document.getElementById('fuel').options.length = 0;
			//document.getElementById('ps').options.length = 0;
			var i = 0;
			for (i = 0; i < json.data.length; i++){
				if ( i == 0 ){
					$('search_series').grab(new Element('option', {'value' : 0}));
				}
				$('search_series').grab(new Element('option', {'value' : json.data[i].ID}).appendText(json.data[i].SERIES));
			}
		}
	}).send();
}

/***************************************************** */
//GetStructureBySeries
//Returns series structures 
/***************************************************** */
function GetStructuresBySeries(series){
	var req = new Request({
		method: 'post',
		url: '/rpc/getStructuresBySeries',
		data: { 'series': series },
		onRequest: function() {
		},
		onComplete: function(response) {
			var json = JSON.decode(response,true);
			document.getElementById('search_structure').options.length = 0;
			//document.getElementById('fuel').options.length = 0;
			//document.getElementById('ps').options.length = 0;
			var i = 0;
			for (i = 0; i < json.data.length; i++){
				if ( i == 0 ){
					$('search_structure').grab(new Element('option', {'value' : 0}));
				}
				$('search_structure').grab(new Element('option', {'value' : json.data[i].ID}).appendText(json.data[i].NAME));
			}
		}
	}).send();
}


/***************************************************** */
//GetFuelBySeriesStructure
//Returns series structure  fuel
/***************************************************** */
function GetFuelBySeriesStructure(structure){
	var req = new Request({
		method: 'post',
		url: '/rpc/getFuelBySeriesStructure',
		data: { 'structure': structure },
		onRequest: function() {
		},
		onComplete: function(response) {
			var json = JSON.decode(response,true);
			//document.getElementById('fuel').options.length = 0;
			//document.getElementById('ps').options.length = 0;
			var i = 0;
			for (i = 0; i < json.data.length; i++){
				if ( i == 0 ){
					$('search_fuel').grab(new Element('option', {'value' : 0}));
				}
				$('search_fuel').grab(new Element('option', {'value' : json.data[i].NAME}).appendText(json.data[i].NAME));
			}
		}
	}).send();
}

/***************************************************** */
//GetPowerByStructuresFuel
//Returns series structure  fuel
/***************************************************** */
function GetPowerByStructuresFuel(structure, fuel){
	var req = new Request({
		method: 'post',
		url: '/rpc/getPowerByStructuresFuel',
		data: { 'structure': structure, 'fuel': fuel },
		onRequest: function() {
		},
		onComplete: function(response) {
			var json = JSON.decode(response,true);
			document.getElementById('search_ps').options.length = 0;
			var i = 0;
			for (i = 0; i < json.data.length; i++){
				if ( i == 0 ){
					$('search_ps').grab(new Element('option', {'value' : 0}));
				}
				json.data[i].NAME.match(/\((.*)\)/);
				var ps = RegExp.$1;
				$('search_ps').grab(new Element('option', {'value' : ps}).appendText(ps));
			}
		}
	}).send();
}


/***************************************************** */
//ShowModelSearchResult
// generate and display model result table
/***************************************************** */
function ShowModelSearchResult(models){
	document.getElementById('model_search').innerHTML = '';
	var table = new Element('table')
	var bg_class = '';
	for (var i = 0; i < models.length; i++){
		if (i % 2)	{ bg_class = 'bg_bright'; }
		else		{ bg_class = 'bg_dark'; }
		if ( i == 0 ){
			// Table Header
			table.grab(new Element('tr', {'class': 'bg_bright'}).grab(new Element('th').appendText('Modell')).grab(new Element('th').appendText('Leistung')).grab(new Element('th').appendText('V-Max')).grab(
				new Element('th').appendText('Preis')));
		}
		models[i].MAXPOWER.match(/(.*\))/);
		var power = RegExp.$1;
		var price = models[i].PRICE.split(' ');
		table.grab(new Element('tr', {'class': bg_class}).grab(new Element('td').grab(new Element('a', {'href': models[i].LINK}).appendText(models[i].MODEL))).grab(new Element('td').appendText(power)).grab(
			new Element('td').appendText(models[i].VMAX)).grab(new Element('td').appendText(price[0] + ' EUR')));

	}

	$('model_search').set('class', 'grid_group bg_dark');
	$('model_search').grab(table);
}








