var rSlideShowInterval = null;
var bSlideBack = false;
var iCurrSlidePos = 0;
var g_iItemSize = 0;

$(function() 
{
	if( $(".scrollable") == null )
	{
		$(".scrollable").scrollable();
		$(".scrollable").data("scrollable").getConf().keyboard = false;
	}
	
	var strActItem = 0;
	var arrItems = Array();
	var iItemSize = 0;
	var iScroll = 0;
	
	objScrollItems = document.getElementById("scroll_items");
	if( objScrollItems != null )
	{	
		iChildLen = objScrollItems.childNodes.length;
		for( iIndex = 0; iIndex < iChildLen; iIndex++ )
		{
			if( objScrollItems.childNodes[iIndex] == null )
			{
				continue;
			}
			
			if( objScrollItems.childNodes[iIndex].id != "" )
			{
				if( objScrollItems.childNodes[iIndex].id.search(/slideshow_pre_/) == -1 )
				{
					
				} else {
					if( strActItem == 0 )
					{
						strActItem = objScrollItems.childNodes[iIndex].id;
					}
					
					if( objScrollItems.childNodes[iIndex].id != "slideshow_pre_" )
					{
						arrItems.push(objScrollItems.childNodes[iIndex].id);
					}					
					
					iItemSize++;
				}
			}
		}
	}

	$("#gallery_prev").click(function() {
		iActIndex = 0;
		iArrItemLen = arrItems.length;

		for( iItem = 0; iItem < iArrItemLen; iItem++ )
		{
			if( strActItem == arrItems[iItem] )
			{
				iActIndex = iItem;
				break;
			}
		}
		
		if( arrItems[iActIndex-1] != null )
		{
			strActItem = arrItems[iActIndex-1];
			objNewActItem = document.getElementById(strActItem);
			if( objNewActItem != null )
			{
				$("#"+strActItem).click();
				// objNewActItem.click();
			}
		}
		
		if( iItemSize > 6 )
		{	
			if( iScroll > 0 )
			{		
				var api = $(".scrollable").data("scrollable");
				api.prev(82);
				iScroll--;
			}
		}		
	});
	
	$("#gallery_next").click(function() {
		iActIndex = 0;
		iArrItemLen = arrItems.length;
		
		for( iItem = 0; iItem < iArrItemLen; iItem++ )
		{
			if( strActItem == arrItems[iItem] )
			{
				iActIndex = iItem;
				break;
			}
		}
		
		if( arrItems[iActIndex+1] != null )
		{
			strActItem = arrItems[iActIndex+1];
			objNewActItem = document.getElementById(strActItem);
			if( objNewActItem != null )
			{
				$("#"+strActItem).click();
			}
			
			if( iItemSize > 6 )
			{					
				if( iScroll < (iArrItemLen-6) )
				{
					var api = $(".scrollable").data("scrollable");
					api.next(82);
					iScroll++;
				}		
			}			
		}		
	});
	
	$(".items img").click(function() {
	
		if( $(this).attr("src") == "fileadmin/rr_template/images/img-contentboxes/no-image-thumb.jpg" ) 
		{
			return; 
		}
	
		// see if same thumb is being clicked
		if ($(this).hasClass("active")) { return; }
		
		// calclulate large image's URL based on the thumbnail URL (flickr specific)
		//var url = $(this).attr("src").replace("_t", "");
		var url = $(this).attr("src");
		strActItem = $(this).attr("id");
		// get handle to element that wraps the image and make it semi-transparent
		var wrap = $("#image_wrap").fadeTo("medium", 0.5);
		
		// the large image from www.flickr.com
		var img = new Image();
		
		// call this function after it's loaded
		img.onload = function() {
	
			// make wrapper fully visible
			wrap.fadeTo("fast", 1);
			
			// change the image
			wrap.find("img").attr("src", url);
		};
		
		var strIsMovie = $(this).attr("movie");
		if( strIsMovie == "true" )
		{
			objImgDescript = document.getElementById("Img-Descript");
			if( objImgDescript != null )
			{
				objImgDescript.style.display = "none";
			}
			
			$(".items img").removeClass("active");
			$(this).addClass("active");
			
			$("#image_wrap").html( "<iframe type=\"text/html\" width=\"587\" height=\"347\" src=\"" + $(this).attr("val") + "?autoplay=1\" frameborder=\"0\"></iframe>" );
		} else {	
			// begin loading the image from www.flickr.com
			if( $(this).attr("val") != "" )
			{					
				if( $(this).attr("link") == undefined || 
					$(this).attr("link") == "" )
				{
					$("#image_wrap").html("<div id=\"Img-Descript\"></div><img src=\"\" />");					
				} else {
					$("#image_wrap").html("<a href=\"" + $(this).attr("link") + "\"><div id=\"Img-Descript\"></div><img src=\"\" border=\"0\" /></a>");
				}
				img.src = url;
			
				// activate item
				$(".items img").removeClass("active");
				$(this).addClass("active");
				
				objImgDescript = document.getElementById("Img-Descript");
				if( objImgDescript != null )
				{
					objImgDescript.style.display = "block";
				}	
				
				$("#Img-Descript").html( $(this).attr("val") );
			} else {
				$("#image_wrap").html("<img src=\"\" />");
				img.src = url;				
			}
		}
	
	// when page loads simulate a "click" on the first image
	}).filter(":first").click();
	
	/*
	g_iItemSize = iItemSize;
	if( iItemSize > 1 )
	{
		rSlideShowInterval = window.setInterval( "slideShow()", 10000 );			
	}
	*/
	
});

/*	
function slideShow()
{
	if( iCurrSlidePos == g_iItemSize )
	{
		bSlideBack = true;
	} else {
		if( iCurrSlidePos == 0 )
		{
			bSlideBack = false;
			iCurrSlidePos = 0;
		}
	}
	
	if( bSlideBack == true )
	{
		$('#gallery_prev').click();			
		iCurrSlidePos--;
	} else {
		$('#gallery_next').click();
		iCurrSlidePos++;
	}
}

function stopSlide()
{
	if( rSlideShowInterval != null )
	{
		window.clearInterval(rSlideShowInterval);
	}
}

function iframeclick( strId ) 
{
	if( strId == "" )
	{
		alert('strId is empty');
		return;
	}
	
	document.getElementById( strId ).onclick = function() 
	{
		alert("click");
		window.clearInterval(rSlideShowInterval);
	}
}
*/
$(document).ready(function()
{	
	var currentPosition = 0;
	var slideWidth = 873;
	var slides = $('.slide');
	var numberOfSlides = slides.length;
	// Remove scrollbar in JS
	$('#slidesContainer').css('overflow', 'hidden');
	// Wrap all .slides with #slideInner div
	slides
	.wrapAll('<div id="slideInner"></div>')
	// Float left to display horizontally, readjust .slides width
	.css({
		'float' : 'left',
		'width' : slideWidth
	});
	// Set #slideInner width equal to total width of all slides
	$('#slideInner').css('width', slideWidth * numberOfSlides);
	// Insert controls in the DOM
	$('#slideshow')
	.prepend('<span class="control" id="leftControl">Clicking moves left</span>')
	.append('<span class="control" id="rightControl">Clicking moves right</span>');
	// Hide left arrow control on first load
	manageControls(currentPosition);
	// Create event listeners for .controls clicks
	$('.control')
	.bind('click', function(){
		// Determine new position
		currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
		// Hide / show controls
		manageControls(currentPosition);
		// Move slideInner using margin-left
		$('#slideInner').animate({
			'marginLeft' : slideWidth*(-currentPosition)
		});
	});				
	// manageControls: Hides and Shows controls depending on currentPosition
	function manageControls(position){
		// Hide left arrow if position is first slide
		if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
		// Hide right arrow if position is last slide
		if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
	}	
});

