$(document).ready( function() {

	if (document.getElementById('SlideshowElement1'))
		initSlideshow();

	$(".more_details_button").click(function() {
		
		if ($('.hide_more_details').css('display') == 'block')
		{
			$(".more_details_button img").attr('src', 'http://www.srsuntour-cycling.com/customGates/resources/binaries/universal/buttons/arrow-down-20x20.png');
			$(".more_details_link a").html('SHOW MORE DETAILS');
			
		}
		else
		{
			$(".more_details_button img").attr('src', 'http://www.srsuntour-cycling.com/customGates/resources/binaries/universal/buttons/arrow-up-20x20.png');
			$(".more_details_link a").html('SHOW LESS DETAILS');
		}

		$('.hide_more_details').slideToggle("normal");
	});
	
	$(".more_details_link").click(function() {
		
		if ($('.hide_more_details').css('display') == 'block')
		{
			$(".more_details_button img").attr('src', 'http://www.srsuntour-cycling.com/customGates/resources/binaries/universal/buttons/arrow-down-20x20.png');
			$(".more_details_link a").html('SHOW MORE DETAILS');
			
		}
		else
		{
			$(".more_details_button img").attr('src', 'http://www.srsuntour-cycling.com/customGates/resources/binaries/universal/buttons/arrow-up-20x20.png');
			$(".more_details_link a").html('SHOW LESS DETAILS');
		}
		
		$('.hide_more_details').slideToggle("normal");
	});
	
	$(".explodedviews_button").click(function() {
		$('#explodedviews_links').slideToggle("normal");
	});
	
	$(".ownersmanual_button").click(function() {
		$('#ownersmanual_links').slideToggle("normal");
	});
	
	$(".logo_indendeduse").click(function() {
		
		id = $(this).children().attr('id');
		
		if (id != "")
		{
			name = '#AjaxContainerIntendendUse_'+id;
		}
		else
		{
			name = '#AjaxContainerIntendendUse';
		}
		
		ModalDialog = $(name).modal({
			overlayCss: {
		    backgroundColor: '#000',
		    cursor: 'wait'
		  },
		  containerCss: {
		    height: 380,
		    width: 330,
		    border: '1px solid #3a3a3a',
		    backgroundColor: '#FFF'
		  },
		  onShow : function(dialog){
			  $('#recommend_submit').click(function (e) {
			  		e.preventDefault();
			  		sendRecommendData(ModalDialog);
			  	});
		  }
		});
	});
	
	$(".feature_details").click(function() {

		ModalDialog = $('#AjaxContainer'+$(this).attr('id')).modal({
			overlayCss: {
		    backgroundColor: '#000',
		    cursor: 'wait'
		  },
		  containerCss: {
		    height: 380,
		    width: 330,
		    border: '1px solid #3a3a3a',
		    backgroundColor: '#FFF'
		  },
		  onShow : function(dialog){
			  $('#recommend_submit').click(function (e) {
			  		e.preventDefault();
			  		sendRecommendData(ModalDialog);
			  	});
		  }
		});
	});

	//Sprachauswahl FAKE
	$('#LanguageSelection .selectbox select').change( function() {
		$('#LanguageSelection .selectbox form').submit();
	});
	
	//Hauptnavigation - Hover-Effekte
	$('.header .menu .left').hover( 
		function() {
			$(this).css('background-position','0px -36px');
			$(this).next().css('background-position','0px -36px');
		},
		function() {
			$(this).css('background-position','0px 0px');
			$(this).next().css('background-position','0px 0px');
		}
	);
	$('.header .menu .right').hover( 
		function() {
			$(this).css('background-position','0px -36px');
			$(this).prev().css('background-position','0px -36px');
		},
		function() {
			$(this).css('background-position','0px 0px');
			$(this).prev().css('background-position','0px 0px');
		}
	);
	
	
});

function activateRider(riderGroup, rider)
{
	var riders = $('a[rel^='+riderGroup+']');
	
	for(var i = 0; i < riders.length; i++)
	{
		if(rider == riders[i].id)
		{
			var imgSrc = 'http://www.srsuntour-cycling.com/customGates/resources/binaries/universal/buttons/'+ riders[i].id +'_active.gif';
			$('#'+riders[i].id+'Content').css("display", "block");
			$('#'+riders[i].id).find('img').attr({'src' : imgSrc});
		}
		else
		{
			var imgSrc = 'http://www.srsuntour-cycling.com/customGates/resources/binaries/universal/buttons/'+ riders[i].id +'_inactive.gif';
			$('#'+riders[i].id+'Content').css("display", "none");
			$('#'+riders[i].id).find('img').attr({'src' : imgSrc});
		}
	}
}

function openRecommendDialog(TreeNodeID)
{
	if( TreeNodeID > 0 ) {
		var postData = "screen=sh.recommend&TreeNodeID=" + TreeNodeID; 
	}
	else {
		var postData = "screen=sh.recommend";
	}
	
	$.ajax({
		   type: "POST",
		   url: DSTORE_BASE_URL + "/index.php",
		   data: postData,
		   dataType: 'json',
		   success: function(msg)
		   {
				$('#AjaxContainer').html(unescape(msg.Content));
				
				ModalDialog = $('#AjaxContainer').modal({
					overlayCss: {
				    backgroundColor: '#000',
				    cursor: 'wait'
				  },
				  containerCss: {
				    height: 380,
				    width: 330,
				    border: '1px solid #3a3a3a',
				    backgroundColor: '#FFF'
				  },
				  onShow : function(dialog){
					  $('#recommend_submit').click(function (e) {
					  		e.preventDefault();
					  		sendRecommendData(ModalDialog);
					  	});
				  }
				});
		   },
		   error: function(xhr)
		   {
			   alert(xhr.responseText);
		   }
		});
}

function sendRecommendData(ModalDialog)
{
	$.ajax({
	   type: "POST",
	   url: DSTORE_BASE_URL + "/index.php",
	   data: $('#recommendForm').serialize(),
	   dataType: 'json',
	   success: function(msg)
	   {
			ModalDialog.close();
			
		   	$('#AjaxContainer').html(unescape(msg.Content));
				
		   	ModalDialog = $('#AjaxContainer').modal({
					overlayCss: {
				    backgroundColor: '#000',
				    cursor: 'wait'
				  },
				  containerCss: {
				    height: 380,
				    width: 330,
				    backgroundColor: '#FFF',
				    border: '1px solid #3a3a3a'
				  },
				  onShow : function(dialog){
					  $('#recommend_submit').click(function (e) {
					  		e.preventDefault();
					  		sendRecommendData(ModalDialog);
					  	});
					  
					  $('#close_button').click(function (e) {
						  ModalDialog.close();
					  	});
				  }
				});
	   },
	   error: function(xhr)
	   {
		   alert(xhr.responseText);
	   }

   });
}

var i=1;
var elements=0;
var slidesArray = new Array();
var slidesIconOnArray = new Array();
var slidesIconOffArray = new Array();

function initSlideshow() {
	
	elements=1;
 	while(document.getElementById('SlideshowElement'+elements)){
 		elements++;
 	}

 	elements--;
 	startSlideshow();
}

function startSlideshow(){
	slideshow = setInterval('nextSlideshowElement()', 10000);
}

function restartSlideshow(){
	startSlideshow();
}
function stopSlideshow(){
	window.clearInterval(slideshow);
}

function nextSlideshowElement() {
	$('#SlideshowElement'+i).fadeOut("normal");
	
	$('#SlideshowIconOn'+i).fadeOut("normal");
	$('#SlideshowIconOff'+i).fadeIn("normal");

	if ((i + 1) > elements)
		i = 1;
	else
		i++;

	$('#SlideshowElement'+i).fadeIn("normal");
	
	$('#SlideshowIconOff'+i).fadeOut("normal");
	$('#SlideshowIconOn'+i).fadeIn("normal");
}

function showSlideshowElement(j) {
	$('#SlideshowElement'+i).fadeOut("normal");
	$('#SlideshowElement'+j).fadeIn("normal");

	$('#SlideshowIconOn'+i).fadeOut("normal");
	$('#SlideshowIconOff'+i).fadeIn("normal");

	$('#SlideshowIconOff'+j).fadeOut("normal");
	$('#SlideshowIconOn'+j).fadeIn("normal");
	
	i=j;

}

function showNewsEntry(id, folder)
{
	$.ajax({
		   type: "POST",
		   url: 'http://www.srsuntour-cycling.com/index.php',
		   data: 'screen='+folder+'.article&pid='+id,
		   dataType: 'json',
		   success: function(response)
		   {
				$('#article').html(unescape(response.Content));
				if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
					jQuery(function($) {
						$("a[rel^='lightbox']").slimbox({/* Put custom options here */}, null, function(el) {
							return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
						});
					});
				}
		   }
	});
}

// Video Overlay
function openoverlay (url, subject, bildurl) {
	var breite = screen.width;
	var hoehe = screen.height;
	breite = (breite - 600) / 2;
	hoehe = (hoehe - 600) / 2;
	var player = $f("player", "http://www.srsuntour-cycling.com/customGates/resources/binaries/universal/flash/flowplayer-3.2.5.swf", {
		clip:[url],
		scaling: "fit",
		controls: {
			backgroundColor: '#000',
			height: 28,
			backgroundGradient: 'low'
		}
	});
	$(".simplemodal-container").modal(
	{
		onOpen: function (dialog) 
		{
			dialog.overlay.fadeIn('slow', function () 
			{
				dialog.data.hide();
				dialog.container.fadeIn('slow', function () {
					dialog.data.slideDown('slow');
					$('#player').attr('href', url);
					$('#player').html('<img src="'+url+'"/>');
					$('#video_name').html(subject);
					player.load();
				});
			});
		},
		onClose: function (dialog) {
			player.unload();
			$.modal.close();
		},
		position: [hoehe+'px',breite+'px']
	});

}
// Slideing configuration
var currentPage = 1;
var MaxPages = 0;

$(document).ready(function() {
	 MaxPages = $('#Slideshow > div').size();
	 $('#Slideshow').cycle({
		 fx: 'scrollHorz',
		 timeout: 0,
		 cleartype: true,
		 cleartypeNoBg: true
	 });
});

// SeitenNavigation für die Video Seiten
function jumpto (jumpPage) {
	// Eine Seite nach vorne Blättern
	if (jumpPage == '+' && currentPage != MaxPages) {
		// Navigation Seiten nummer Hintergrund ändern
		$("#"+currentPage).removeClass('current_page');
		$("#"+currentPage).addClass('page_number');
		currentPage = currentPage + 1;
		if (currentPage  > MaxPages) {
			currentPage = currentPage = 1;
		}
		
		imageLoad();
		// Seite Springen
		$('#Slideshow').cycle('next');
		
		
		// Neue Seitennummer in der Navigation Hintergrund ändern.
		$("#"+currentPage).removeClass('page_number');
		$("#"+currentPage).addClass('current_page');
	// Seite zurück Blättern.
	} else if (jumpPage == '-' && currentPage != 1) {
		$("#"+currentPage).removeClass('current_page');
		$("#"+currentPage).addClass('page_number');
		currentPage = currentPage - 1;
		if (currentPage == 0) {
			currentPage = currentPage = MaxPages;
		}
		
		imageLoad();
		
		$('#Slideshow').cycle('prev');

		$("#"+currentPage).removeClass('page_number');
		$("#"+currentPage).addClass('current_page');
	// Mehrere Seiten Vor Blättern
	} else if (jumpPage >= currentPage) {
		$("#"+currentPage).removeClass('current_page');
		$("#"+currentPage).addClass('page_number');
		currentPage = jumpPage;
		if (currentPage > MaxPages) {
			currentPage = currentPage = 1;
		}
		imageLoad();
		// auf gewünschte Seite springen 0 Index.
		$('#Slideshow').cycle(jumpPage-1);  
		$("#"+currentPage).removeClass('page_number');
		$("#"+currentPage).addClass('current_page');
		
	// Mehrere Seiten zurück Blättern
	} else {
		$("#"+currentPage).removeClass('current_page');
		$("#"+currentPage).addClass('page_number');
		currentPage = jumpPage;
		if (currentPage == 0) {
			currentPage = currentPage = MaxPages;
		}
		imageLoad();
		$('#Slideshow').cycle(jumpPage-1);  
		$("#"+currentPage).removeClass('page_number');
		$("#"+currentPage).addClass('current_page');
	}
	handleMenu();
	return;
}

// Effekte der navigations Leiste.
// Flippen der Page Nummern und markieren der aktuellen Seite.
function handleMenu() {
	
	var menuJump = 0;
	var menuentrys = $('.pagenumbers > div').size();
	var last = $('.pagenumbers div:nth-child('+menuentrys+')');
	// Ids der ersten und letzen Pagenummer holen
	var idDesletzens = new Number($(last).attr('id'));
	var idDesErstens = new Number($('.pagenumbers div:first').attr('id'));
	
	// Wenn die letze Navigationsnummer makiert ist und noch weitere seiten folgen.
	if ($('.pagenumbers :last-child').hasClass("current_page") && (idDesletzens +1) != MaxPages ) {
		menuJump = currentPage - Math.floor(menuentrys/2);
		if (currentPage + menuentrys >= MaxPages) {
			while (menuJump + menuentrys > MaxPages) {
				menuJump--;
			}
		}
		for (var i = 1; i <= menuentrys; i++ ) {
			var item =  $('.pagenumbers :nth-child('+i+')');
			item.html('');
			if ($(item).hasClass("current_page")) {
				item.removeClass('current_page');
				item.addClass('page_number');
			}

			item.html('<a href="#" onclick="javascript:jumpto('+menuJump+')">'+menuJump+'</a>');
			item.attr('id', '');
			item.attr('id', menuJump);
			menuJump++;
		}
		if (MaxPages == menuJump) {
			$('.page_points').css('display', 'none');
		} else {
			$('.page_points').css('display', 'block');
		}
	} else if ($('.pagenumbers :first-child').hasClass('current_page') && (idDesErstens) != 1) {
		menuJump = currentPage - Math.floor(menuentrys/2);
		if (menuJump < 1) {
			while (menuJump < 1) {
				menuJump++;
			}
		}
		for (var i = 1; i <= menuentrys; i++ ) {
			var item =  $('.pagenumbers :nth-child('+i+')');
			item.html('');
			if ($(item).hasClass("current_page")) {
				item.removeClass('current_page');
				item.addClass('page_number');
			}
			item.html('<a href="#" onclick="javascript:jumpto('+menuJump+')">'+menuJump+'</a>');
			item.attr('id', '');
			item.attr('id', menuJump);
			menuJump++;
		}
		if (MaxPages == menuJump) {
			$('.page_points').css('display', 'none');
		} else {
			$('.page_points').css('display', 'block');
		}
		
	} else if (currentPage == MaxPages) {
		menuJump = currentPage -menuentrys;
		for (var i = 1; i <= menuentrys; i++ ) {
			var item =  $('.pagenumbers :nth-child('+i+')');
			item.html('');
			if ($(item).hasClass("current_page")) {
				item.removeClass('current_page');
				item.addClass('page_number');
			}
			item.html('<a href="#" onclick="javascript:jumpto('+menuJump+')">'+menuJump+'</a>');
			item.attr('id', '');
			item.attr('id', menuJump);
			menuJump++;
		}
		if (MaxPages == menuJump) {
			$('.page_points').css('display', 'none');
		} else {
			$('.page_points').css('display', 'block');
		}

	} else if (currentPage == 1) {
		menuJump = currentPage;
		for (var i = 1; i <= menuentrys; i++ ) {
			var item =  $('.pagenumbers :nth-child('+i+')');
			item.html('');
			if ($(item).hasClass("current_page")) {
				item.removeClass('current_page');
				item.addClass('page_number');
			}
			item.html('<a href="#" onclick="javascript:jumpto('+menuJump+')">'+menuJump+'</a>');
			item.attr('id', '');
			item.attr('id', menuJump);
			menuJump++;
		}
		if (MaxPages == menuJump) {
			$('.page_points').css('display', 'none');
		} else {
			$('.page_points').css('display', 'block');
		}

	}
	// CurrentPage makieren.
	$('#'+currentPage).addClass('current_page');
}

function imageLoad () {
	var innerhtml = $('#page_'+currentPage+' .video_preview a').html();
	if (!innerhtml.match(/<img/)) {
		rel = $('#page_'+currentPage+ ' .video_preview a').each(
			function(){
				var rel = $(this).attr('rel');
				var binary = rel.split('_')[1];
				var title = rel.split('_')[2];
				$(this).html('<img border="0" src="http://www.srsuntour-cycling.com/gateTools/scripts/postingbinary.php?PostingID='+binary+'&amp;SortNo=1&amp;convert[-resize]=!182x102" alt="'+title+'" title="'+title+'"/>');

			});
	}
	
}

//<img border="0" src="http://www.srsuntour-cycling.com/gateTools/scripts/postingbinary.php?PostingID={$video.PostingID}&amp;SortNo=1&amp;convert[-resize]=!182x102" alt="{$video.Subject}" title="{$video.Subject}"/>



