var isInIFrame = (window.location != window.parent.location) ? true : false;

if(isInIFrame)
{ 
	if (document.createStyleSheet)
	{              
		document.createStyleSheet('fileadmin/template/fb-style.css');           
	} else {           
		$('head').append($("<link rel='stylesheet' href='fileadmin/template/fb-style.css' type='text/css' media='screen' />"));   
	}        
}

var lang = $('#language-selector .active a').text();
var pageId = $('body').attr('id');

/*********** temporary back link in cloud price list subpage ***********/
	if(pageId == 'uid17')
	{		
		$('#prev-slide')
			.click(function() {
				var url = 'http://www.zweipunkt.com/en/cloud.html';
					if(lang == 'DE')
					{		
						url = url.replace('/en/', '/de/');
					}
				window.location.href = url;
			})		
			.show();
	}

/*********** correct home link on logo if language is DE ***********/
	if(lang == 'DE')
	{		
		$('#logo').attr('href', 'http://www.zweipunkt.com/de');
	}


/*********** hover around like button: show mailto link ***********/

$('#like-button-iframe')
	.hover(
	function(){	
	
	if(lang == 'DE')
	{		
		$('#mail-de').show();
	}
	if(lang == 'EN')
	{		
		$('#mail-en').show();
	}
	}, function () { 
		$('#mail-de').delay(5000).fadeOut();
		$('#mail-en').delay(5000).fadeOut();
	})

/*********** display title below menu links ***********/

$('#navigation a').each(function(i){
	var menuLink = $(this);
	var title = menuLink.attr('title');
	
  $('<p>' + title + '</p>').insertAfter(menuLink);
})

/*********** fade in content area on page load ***********/
$('#content').hide().fadeIn(800);


/*********** tooltips ***********/

// provide case-insensitive selection of elements
$.extend($.expr[":"], {
 "containsNC": function(elem, i, match, array) {
 return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
 }
});
 
// apply tooltip icons to each link in main content area that has a corresponding tooltip
$('.csc-default a').each(function(i) {
																	
	var currentLink = $(this);

	// if link links on same page remove link formatting and return false
	if('/' + currentLink.attr('href') == window.location.pathname)
	{
		currentLink.addClass('same-page');
		currentLink.click(function(){
			return false;													 
		})
		
	} else {	
		
		var clickLink = $('<div>').addClass('click-link').text('Click on the link below to see more.');
		
		if(lang == 'DE')
		{
			clickLink.text('Klicken Sie auf den untenstehenden Link, um mehr zu erfahren.')
		}
	}


var title = currentLink.attr('title');
var posLeft = currentLink.position().left;
var posTop = currentLink.position().top;
var correspondingTooltips = $('#tooltips h1:containsNC(' + title + ')');


	var titlesReallyMatch = false;
	var matchingTooltip = false;
	correspondingTooltips.each(function(i){	
		var toolTipTitle = $(this).text();
		//alert('toolTipTitle ' + toolTipTitle);
		//alert('title ' + title);
		
		if(toolTipTitle === title)
		{
			titlesReallyMatch = true;
			matchingTooltip = $(this);
		}
		
		//alert('titlesReallyMatch ' + titlesReallyMatch);
	});
	
	
	if(titlesReallyMatch)
	{				
	var tooltipArrow = $('<div>').addClass('tooltip-arrow');
	
	var wholeTooltipDiv =  matchingTooltip
														.parent()
														.parent()
														.clone()
														.hide()
														.addClass('temporaryTooltip')
														.append(tooltipArrow)
														.append(clickLink);


		

		
		var settingOffsetY = -60;
		var settingOffsetX = 180;
		var settingPosition = 'top center';
		
		if(isInIFrame)
		{
			//var toolTipWidth = tooltipArrow.parent().width();
			tooltipArrow.css({
			"left": (posLeft - 20) + "px"
			});
			settingOffsetY = -60;
			settingOffsetX =  240 - posLeft;
			settingPosition = 'top center';
		}
		
		currentLink.attr('title', '')
		.addClass('tooltipIcon')
		.after(wholeTooltipDiv)		
		.tooltip({
			position: settingPosition,
			delay: 250,
			relative: true,	
			offset: [settingOffsetY,settingOffsetX],
			effect: 'fade',				
			onShow: function() {
				var start = new Date();		
				this.startK = start.getHours() * 60 * 60 * 1000 + start.getMinutes() * 60 * 1000 + start.getSeconds() * 1000 + start.getMilliseconds();					
			},
			onHide: function() {
				var end = new Date();	
				var endeK = end.getHours() * 60 * 60 * 1000 + end.getMinutes() * 60 * 1000 + end.getSeconds() * 1000 + end.getMilliseconds();						
				var dif = (endeK-this.startK) /1000;
				
				// only track if tooltip content shown long enough
				if(dif > 1.5)
				{
					dcsMultiTrack('DCSext.vhit', '1', 'DCSext.tooltip', title);
				}
			}
		})			
	}
})

/*********** team site  ***********/


var vCards = $('#v-cards');
if(vCards.length > 0)
{
	var settingsShape = 'tearDrop';
	
	if(isInIFrame)
	{
		settingsShape = 'waterWheel';
		$('#v-cards').css({
		"height": "700px",
		"width": "300px",
		"left": "50px",
		"top": "-210px"
	})
		
		
	} else {
		initNextAndPrevButtons();
	}
		
	vCards.roundabout({
		reflect: true,
		shape: settingsShape,
		easing: 'easeOutQuint',
		btnNext: '#next-slide',
		btnPrev: '#prev-slide'
	});
}
			
/*********** show facebook like button only on startpage ***********/



if(pageId == 'uid1'){
	$('#fb-like-button').show();
	}
//$('#fb-like-button').hide();
//$('#uid1 #fb-like-button').show()

/*********** slideshow ***********/

// create next and previous buttons
function initNextAndPrevButtons(){
	$('#next-slide')
		.hover(
		function(){	
			$(this).addClass('hover');
		}, function () { 
			$(this).removeClass('hover');
		})
		.show();
	$('#prev-slide')
		.hover(
		function(){	
			$(this).addClass('hover');
		}, function () { 
			$(this).removeClass('hover');
		})
		.show();
}

// settings
var marginTop = 100;
var animationDurationInMs = 800;

// initialisation
var currentBlock = 0;
	
var contentBlocks = $('#content > .csc-default');
var numberOfBlocks = contentBlocks.size();

// show a slide show if more than one content block in main content area
if(numberOfBlocks > 1)
{
	initNextAndPrevButtons();
	
	$('#next-slide:not(.disabled)').live('click', function() {
		move(1);
	}); 
	
	$('#prev-slide:not(.disabled)').live('click', function() {
		move(-1);
	}); 
	
	// for faster access
	var nextSlide = $('#next-slide');
	var prevSlide = $('#prev-slide');
	
	
	// add container for dots to be able to center them horizontally
	$(document.createElement('div'))
		.attr('id', 'dots')
		.appendTo('#topbox')
		.append($('<div id="dotsdiv">'));
		
	var dotsDiv   = $('#dotsdiv')
	
	// set width of dotsdiv to center it horizontally
	dotsDiv.width(numberOfBlocks * 30);
	
	// wrap content blocks in a viewport (that has overflow: hidden)
	var viewPort = $('<div id="viewport" />');
	contentBlocks.wrapAll(viewPort);	
	//var viewWidth = viewPort.width();
	var viewWidth = 850;
	
	
	// place all right of viewport and add the dots
	contentBlocks.each(function(i){
		$(this).css({
			"position": "absolute",
			"left": (viewWidth) + "px", 	
			"top": marginTop + "px" 
		})	
		$('<span>')
			.click(function() {
				moveMore(i);
			})
			.appendTo('#dotsdiv');
	})

	// init: place first in viewport
	contentBlocks.eq(0).css({
		"left": "0px", 	
	})
	
	// init: set first dot active
	$('#dots span').eq(0).addClass('active');
	
	// init: hide previous at
	prevSlide.hide();

	// this function is called after the sliding is done, it repositions the previous and next content block
	function prepareSiblingContentBlocks()
	{	
		var nextBlock = (currentBlock + 1)%numberOfBlocks;
		var prevBlock = (numberOfBlocks + currentBlock - 1)%numberOfBlocks;
		
		contentBlocks.eq(nextBlock).css({
			"left": (viewWidth) + "px", 	
		})
		// place last left of viewport
		contentBlocks.eq(prevBlock).css({
			"left": (-viewWidth) + "px", 	
		})	
	}
		
	function move(dir)
	{
		nextSlide.addClass('disabled');
		prevSlide.addClass('disabled');
		dotsDiv.addClass('disabled');
	
		var currentBlockObject = contentBlocks.eq(currentBlock);
		var otherBlock = (currentBlock + dir + numberOfBlocks) % numberOfBlocks;
		var otherBlockObjectToMove = contentBlocks.eq(otherBlock);
			
		// the one to slide in
		otherBlockObjectToMove.animate({
			left: "0px",
		}, {
				duration: animationDurationInMs,
				specialEasing: {
				left: 'easeOutQuint',
				}
	 	});
	
		// the one to slide out	
		var hidePos = contentBlocks.eq(currentBlock).position().left + (-dir * viewWidth);
			currentBlockObject.animate({
				left: hidePos + "px",
			}, {
					duration: animationDurationInMs,
					specialEasing: {
						left: 'easeOutQuint',
					},
				complete: function() { 
					// enable controlling elements
					nextSlide.removeClass('disabled');
					prevSlide.removeClass('disabled');
					dotsDiv.removeClass('disabled');
					}
	 	});
		
		// set new current
		currentBlock = (currentBlock += dir) % numberOfBlocks;
		
		if(currentBlock == numberOfBlocks - 1)
		{
			nextSlide.hide();
		} else {
			nextSlide.show();
		}
		
		if(currentBlock == 0)
		{
			prevSlide.hide();
		} else {
			prevSlide.show();
		}
		
		// highlight dot
		var dotsSpan = $('#dots span');
		dotsSpan.removeClass('active');
		dotsSpan.eq(currentBlock).addClass('active');
	
		prepareSiblingContentBlocks();
	} // end move
	
	function moveMore(index)
	{
		var steps = 0;
		steps = index - currentBlock;
		move(steps);	
	}

	// key events
	$( document ).keydown(function ( e ) {
			var disabled = false;
			disabled = nextSlide.hasClass('disabled');		
			if(disabled === false)
			{
				var key = e.which === null ? e.keyCode : e.which;
				if (key === 39) {				
						move(1);
				} else if ( key === 37) {
						move(-1);
				}
			}
	});
	
	// to be ready for the first sliding
	prepareSiblingContentBlocks();

} // end if more than 1 content block

