var 
	windowLoadComplete = false,
	firstScaling = false,
	interfaceInitialized = false,
	oldDestH = 0,
	newContainerWidth = 0,
	newWidth = 0,
	slideWidth = 0,
	slideInnerSlideWidth = 0,
	destinationX = 0,
	slideWidthMin = 0,
	slideWidthMax = -10000,
	currPos = 0,
	slides = [],
	slidePointsArray = [],
	navNeeded = true,
	leftNavNeeded = false,
	rightNavNeeded = true,
	prevDelta = 0,
	nextDelta = 0,
	mouseX = 0,
	mouseY = 0,
	xoffset = 0,
	yoffset = 5,
	captionText = '',
	content = '',
	tooltip,
	ie= $.browser.msie,
	agent= navigator.userAgent,
	mobile = (/iphone|ipod|ipad|android/i).test(agent),
	//cleanup = function(pass){ ie || delete this; return pass },
	os = '',
	stageWidth = 0,
	stageHeight = 0,
	scaleIt = 'null',
	imageStage = null,
	slidesContainer = null,
	toolTip = null,
	slidesFullWidth = false,
	navOffset = 160,
	oldTarget = null
	
if(slidesMarginRight == undefined) var slidesMarginRight = 20

var slideShow = {
	
	init: function(){
		
		imageStage = $('#imageStage');
		slidesContainer = $('#slidesContainer');
	
		os = navigator.platform;
		os = os.substring(0, 3);
	
		//$('#imageStage').css('visibility','hidden');
		
		//this.showLoading();
		
		slides = $('.csc-frame');
		
		for(var i = 0; i < slides.length; i++){
				if(i == 0){
					$(slides[i]).css('visibility','visible')
				}else{
					$(slides[i]).css('visibility','hidden')
				}
			}
		
		this.columnSlides = $('.csc-frame-columnize');
		this.changeCSS();
		stageWidth = imageStage.width();
		stageHeight = imageStage.height();
		this.loadingControl();
	},
	changeCSS: function(){
		
		if(mobile == true){
			$('body').css('overflow','visible');
			//$('#ctr-head').css('font-size','130%');
			//$('#ctr-head').css('width','auto');
			//$('#ctr-head').css('white-space','nowrap');
			
			$('img').css('-webkit-user-select','none');
			$('.csc-frame-scaleImg img').css('position','relative');
			//$('.captionText').css('font-size','130%');
			
		}else{
			$('body').css('overflow','hidden');
			
			imageStage.css('overflow','hidden')
						.css('overflow-x','hidden');
						
			slidesContainer.css('overflow','hidden')
						.css('overflow-x','hidden');
						
			$('.captionText').css('display','none')
			
		}
		
		slidesContainer.css('width','10000px');
		
		for(var i = 0; i < slides.length; i++){
			$(slides[i]).css('float','left');
			$(slides[i]).css('cursor','default');
		}
		
		$('.csc-frame-columnize')
								.css('overflow','hidden')
								.css('visibility','hidden');
		
		$('.csc-frame-columnize-output').css('display','block');
		
		$('.csc-frame-scaleImg img')
									.css('width','auto')
									.css('height','100%');
	
	},
	showLoading: function(){
		$('body').append('<div id="loader"><img src="fileadmin/hge-styles/loader.gif" alt=""/></div>');
		
	},
	loadingControl: function(){
		this.checkTimer = setInterval( this.checkLoading, 100);
	},
	checkLoading: function(){
		if(windowLoadComplete === true && firstScaling === false){
			slideShow.scaleElements(null);
		}
		
		if(windowLoadComplete == true && firstScaling == true && interfaceInitialized == true){
			clearInterval(slideShow.checkTimer);
			//slideShow.removeLoading();
			//$('#imageStage').css('visibility','visible');
			for(var i = 0; i < slides.length; i++){
				$(slides[i]).css('visibility','visible');
			}
		}
	},
	removeLoading: function(){
		$("div#loader").remove();
		
	},
	scaleElements: function(e){
		
		if(slidesContainer.height() < 410 || imageStage.width() < 300 && mobile == false) return;
		
		stageWidth = imageStage.width();
		
		stageHeight = imageStage.height();
	
		slidePointsArray = [];
		newContainerWidth = 0;
		
		slidesContainer.width('100000px');
		
		var currXoffset = imageStage.scrollLeft();
		
		if(slidesContainer.hasClass('fullWidthSlides')) slidesFullWidth = true;
		
		navOffset = Math.round(imageStage.width()/8);
		
		for(var i = 0; i < slides.length; i++){
									
			var	slide = $(slides[i]);
			
			newWidth = 0;
			
			if(slide.find('img')[0] != undefined){
				var currImg = slide.find('img');
				var currWidth = slide.width();
				var currHeight = slide.height();
				var fac = currWidth/currHeight;
				var captionHeight = slide.find('.captionText').height();
				
				if(currHeight != stageHeight){
					if(mobile){	
						currImg.height(stageHeight - captionHeight);
						currImg.width((stageHeight - captionHeight)*fac);
					}
					
					currHeight = stageHeight;
					currWidth = stageHeight*fac;
					
					if(mobile){
						currWidth = (stageHeight - captionHeight)*fac	
					}
					
					slide.width(currWidth);
					slide.height(currHeight);
				}
				newWidth = currWidth;
				
			}else if(slide.find('.csc-frame-columnize')[0] != undefined){
				slideShow.columnizeSlide(slide.find('.csc-frame-columnize'));
				newWidth = slide.find('.csc-frame-columnize-output').width();
			}else if(slide.hasClass('csc-fixwidth')){
				newWidth = Math.floor(slide.width());
			}else if(slide.find('video')[0] != undefined){
				newWidth = Math.floor(slide.find('video').width());
				slide.width(newWidth+'px');
			}	
			
			var newMarginRight =  slidesMarginRight;
			
			if(slidesFullWidth == true && mobile == false){
				newMarginRight = Math.round(imageStage.width() - newWidth) + slidesMarginRight;
				var extraOffset = slide.width() - imageStage.width() + 120;
				if(extraOffset > 0) newMarginRight += extraOffset;
			}else if(slidesFullWidth == true && mobile == true){
				newMarginRight = 100;
			}
			
			if(i < slides.length-1){
				slide.css('margin-right', newMarginRight+'px');
				newContainerWidth += newWidth + newMarginRight;
			}else if(i == slides.length-1){
				var lastSlideMarginRight = imageStage.width() - slide.width();
				if(lastSlideMarginRight < 0){
					lastSlideMarginRight = 20;
					//if(mobile == true)lastSlideMarginRight = 100;
					newWidth += 1;
				}
				slide.css('margin-right', lastSlideMarginRight+'px');
				newContainerWidth += newWidth + lastSlideMarginRight + 1;
			}
			
			destinationX = 0;
			destinationX = Math.floor(slide.position().left + currXoffset)*-1;
			slidePointsArray.push(destinationX);
			
			if(slides.length == 1){
				if(newContainerWidth + 40 > $('body').width()){
					destinationX = imageStage.width() - slide.width();
					slidePointsArray.push(destinationX);
				}
			}
		}
		
		slidesContainer.width(newContainerWidth+'px');
		
		slideWidthMax = Math.floor(newContainerWidth*-1) + imageStage.width();
		
		if(newContainerWidth + 40 < $('body').width()){
			navNeeded = false;
		}
		
		imageStage.scrollLeft(slidePointsArray[currPos]*-1);
		
		if(firstScaling == false){
			firstScaling = true;
			this.initInterface();
		}
		
		
	},
	columnizeSlide: function(columnSlide){
		
		var destH = imageStage.height();
			
		columnSlide.css('display', 'block');
		
		var outputId = '#'+columnSlide.parent().find('.csc-frame-columnize-output').attr('id');
		
		$(outputId).width('10000px');
		
		var overflowId = '#'+columnSlide.parent().find('.csc-columnize-overflow').attr('id');
		
		columnSlide.columnize({	width : 250,
								//height : destH,
								buildOnce : true,
								target : outputId,
								overflow:{
											height: destH,
											id: overflowId,
											doneFunc: function(){
												columnizingDone(outputId, overflowId);
											}
										}
								});
		
		function columnizingDone(outputid, overflowid){
			
			var columnWidth = $('.first').outerWidth(true);
															
			var widthFactor = 0;
			var childArray = $(outputid).contents();
			var removeArray = [];
			
			for(var i=0; i < childArray.length; i++){
				if($(childArray[i]).html() != '') {
					widthFactor += 1;
				}else{
					removeArray.push(childArray[i]);	
				}
			}
			
			if($(overflowid).html() != ''){
				widthFactor += 1;
				$(overflowid).css('display','block');
			}else{
				$(overflowid).css('display','none');
			}
			
			var destW = (columnWidth * widthFactor)+'px';
			
			$(outputid).width(destW);
			
			$(outputid).parent().width(destW);
			
			if(newContainerWidth != undefined){
				var newWidth = newContainerWidth + destW + 10;
				slidesContainer.width(newWidth);
			}
			
			for(var i=0; i < removeArray.length; i++){
				$(removeArray[i]).remove();
			}
			
			columnSlide.css('display', 'none');
		}
	},
	initInterface: function(){
		var scope = this;
		if(navNeeded == false){
			interfaceInitialized = true;
			return;
		}
		if(mobile == false){
			for(var i = 0; i < slides.length; i++){
				$(slides[i]).mouseenter(function() {
					captionText = $(this).find('.captionText').html();					 
				});
			}
			
			this.initTooltip();
			
			imageStage.mouseover(function(e){
								
				toolTip.css('display','block');				
				
				scope.setTooltipText(e);
				scope.setTooltipPosition(e);
				return true;
			});
			imageStage.mousemove(function(e){
								
				scope.setTooltipText(e);
				scope.setTooltipPosition(e);
				return true;
			});
			imageStage.mouseout(function(e){
				
				toolTip.html('');
				toolTip.css('display','none');
				return true;
			});
			this.keyController();
			this.extraBtn();
		}else{
			
			$(window).scroll(function(e){
				
				var xpos = $(window).scrollLeft();
				$('#ctr-head').css('left', xpos+'px');
				return false;
			});
		}
		this.clickController();
		this.mouseWheelController();
		interfaceInitialized = true;
	},
	clickController: function(){
		var scope = this;
		
		imageStage.click(function(e) {
			if(mouseX < navOffset && navNeeded == true && leftNavNeeded == true && mobile == false){
				scope.clickSlide('prev');
				return false;
			}else if(mouseX < navOffset && navNeeded == true && leftNavNeeded == false && mobile == false){
				return false;
			}else if(mouseX > ($('body').width() - navOffset) && navNeeded == true && rightNavNeeded == true && mobile == false){
				scope.clickSlide('next');
				return false;
			}else if(mouseX > ($('body').width() - navOffset) && navNeeded == true && rightNavNeeded == false && mobile == false){
				return false;
			}else{
				return true;
			}
		});
	},
	keyController: function(){
		var scope = this;
		$(document).keydown(function(e){
			if (e.keyCode == 37) {
			   scope.clickSlide('prev');
			}
			if (e.keyCode == 39) {
			   scope.clickSlide('next');
			}
			return true;
		});
	},
	extraBtn: function(){
		var scope = this;
		$("body").append("<div id='extraBtn'><div id='prev'>&laquo;</div><div id='next'>&raquo;</div></div>");
		
		$('#extraBtn')
						.css("position","fixed")
						.css("top","180px")
						.css("left","50px")
						.css("font-size","18px")
						.css("width","30px");
		$('#prev')
						.css("cursor","pointer")
						.css("float","left");
		$('#next')
						.css("cursor","pointer")
						.css("float","right");
		
		$('#prev').click(function(e) {
			scope.clickSlide('prev');
			return false;
		});
		
		$('#next').click(function(e) {
			scope.clickSlide('next');
			return false;
		});
	},
	initTooltip: function(){
		$("body").append("<div id='toolTip'></div>");
		toolTip = $('#toolTip');
	},
	setTooltipPosition: function(e){
		
		mouseX = e.pageX;
		mouseY = e.pageY;
		
		toolTip
				.css("top",(mouseY - yoffset) + "px")
				.css("left",(mouseX + xoffset) + "px");
	},
	setTooltipText: function(e){
		
		if(mobile == false){
			
			if(mouseX < navOffset && navNeeded == true && leftNavNeeded == true ){
				$(e.target).css('cursor','pointer');
				if(xoffset != 20) xoffset = 20;
				if($(e.target).parent().parent().hasClass('csc-frame-scaleImg') || $(e.target).parent().parent().hasClass('csc-default') || $(e.target).parent().hasClass('csc-frame-scaleImg') || $(e.target).parent().hasClass('csc-default')){
					toolTip.css('color','white');
				}else{
					toolTip.css('color','black');
				}
				if(toolTip.html() != 'PREV')toolTip.html('PREV');
			}else if(mouseX < navOffset && navNeeded == true && leftNavNeeded == false){
				$(e.target).css('cursor','default');
				if(toolTip.html() != '')toolTip.html('');
			}else if(mouseX > $('body').width() - navOffset && navNeeded == true && rightNavNeeded == true){
				$(e.target).css('cursor','pointer');
				if($(e.target).parent().parent().hasClass('csc-frame-scaleImg') || $(e.target).parent().parent().hasClass('csc-default') || $(e.target).parent().hasClass('csc-frame-scaleImg') || $(e.target).parent().hasClass('csc-default')){
					toolTip.css('color','white');
				}else{
					toolTip.css('color','black');
				}
				if(toolTip.html() != 'NEXT')toolTip.html('NEXT');	
				if(xoffset != toolTip.width()*-1.5) xoffset = toolTip.width()*-1.5;
			}else if(mouseX > $('body').width() - navOffset && navNeeded == true && rightNavNeeded == false){
				$(e.target).css('cursor','default');
				if(toolTip.html() != '')toolTip.html('');
			}else{
				if(xoffset != 20) xoffset = 20;
				
				$(e.target).css('cursor','default');
				
				if($(e.target).attr('id') != 'slidesContainer'){
					
					if(toolTip.html() != captionText)toolTip.html(captionText);
					
					var parentTag = $(e.target).parent().get(0).tagName;
					if(parentTag == 'A') $(e.target).css('cursor','pointer');
					
					if($(e.target).parent().parent().hasClass('csc-frame-scaleImg') || $(e.target).parent().parent().hasClass('csc-default') || $(e.target).parent().hasClass('csc-frame-scaleImg') || $(e.target).parent().hasClass('csc-default')){
						toolTip.css('color','white');
					}else{
						toolTip.css('color','black');
					}
					
				}else{
					$(e.target).css('cursor','default');
					if(toolTip.html() != '')toolTip.html('');
					toolTip.css('color','black');
				}
				oldTarget = $(e.target);
			}
		}
	},
	mouseWheelController: function(){
		
		imageStage.mousewheel(function (e, delta) {
			this.scrollLeft -= (delta * 30);
			
			var xpos = this.scrollLeft;
			
			for(var i = 0; i < slidePointsArray.length; i++){
				if(slidePointsArray.length > 2){
					if(delta < 0){
						if( xpos > Math.abs(slidePointsArray[i]) - imageStage.width()/3 ){
							currPos = i;
						}
					}else{
						if( xpos > Math.abs(slidePointsArray[i]) - (imageStage.width()/3)*2 ){
							currPos = i;
						}
					}
				}else{
					if( xpos > Math.abs(slidePointsArray[i])){
						currPos = i;
					}
				}
			}
			
			if(currPos != 0 ){
				leftNavNeeded = true;
			}else if(currPos == 0 && xpos != 0){
				leftNavNeeded = true;
			}else{
				leftNavNeeded = false;
			}
			
			if(currPos != slidePointsArray.length-1){
				rightNavNeeded = true;
			}else if(currPos == slidePointsArray.length-1 && xpos != Math.abs(slideWidthMax)){
				rightNavNeeded = true;
			}else{
				rightNavNeeded = false;
			}
			
			e.preventDefault();
			return false;
        });
	},
	clickSlide: function(dir){
		
		var scope = this;
		
		if(slides.length == 0) return;
		
		if(dir == 'prev'){
			if(currPos == 0 && imageStage.scrollLeft() == 0){
				leftNavNeeded = false;
				return;
			}
			currPos -= 1;
			
		}else if(dir == 'next'){
			if(currPos == slidePointsArray.length-1 && imageStage.scrollLeft() >= Math.abs(slideWidthMax+22)){
				rightNavNeeded = false;
				return;
			}
			currPos += 1;
		}
		
		if(currPos < 0) currPos = 0;
		if(currPos > slidePointsArray.length-1) currPos = slidePointsArray.length-1;
		
		if(currPos == 0){
			leftNavNeeded = false;
		}else{
			leftNavNeeded = true;
		}
		
		if(currPos == slidePointsArray.length-1){
			rightNavNeeded = false;
		}else{
			rightNavNeeded = true;
		}
		
		slideWidth = slidePointsArray[currPos];
		
		var dest = imageStage.css('margin-left').split('px')[0] - slideWidth;
		
		imageStage.stop().animate({scrollLeft: dest}, 'slow');
	}
};

function updateOrientation(){
	slideShow.scaleElements();
}

$(document).ready(function()
			{
				slideShow.init();
			});

$(window).load(function()
			{
				windowLoadComplete = true;
			});

$(window).resize(function(e)
			{
				if(mobile == false)slideShow.scaleElements(e);
			});
