/* tickerTape */
var tickerData;
var tickerTimer;
var nextItem = 3500;
var animateItem = 1000;

var activeSlide = null;
var slideshowTimer = 10;
var slideshow;

var languageTimeout;

var backoffice = false;

//$(function() {
$(document).ready(function()
{
	if ($('div.controlBorder').length > 0)
	{
		backoffice = true;
	}
	
	var loc = window.location.toString();
	if (loc.indexOf('sunconnex-projects.com') >= 0)
	{
		$('div#Functions li.external a').attr('href', 'http://www.sunconnex.com');
		$('div#Functions li.external a').text('Sunconnex.com');
	}
	
	if ($("#Accordion"))
	{
		
		$("#Accordion").accordion({ autoHeight: false, active: false, event: 'mouseover' });
		
		var accordionH3 = $("#Accordion > h3");
	
		for (iH3 = 0; iH3 < accordionH3.length; iH3++)
		{
			var h3 = $(accordionH3[iH3]);
			h3.css('z-index', accordionH3.length - iH3);

			h3.click(function()
			{
				//Click to change slide
				//gotoSlide($(this).index('div#Accordion h3'));
				gotoSlide($(this).index('div#Accordion h3') + 1);
			});
		}
		
		$('div#Accordion h3 div.controlBorder div.popupMenu a').click(function()
		{
			window.location = $(this).attr('href');
			return false;
		});
		
		//Slideshow
		//$('div#Slideshow').css('position', 'relative');
		
		$('div#Slideshow').mouseenter(function()
		{
			clearTimeout(slideshow);
		});
		$('div#Slideshow').mouseleave(function()
		{
			slideshow = setTimeout(runSlideshow, slideshowTimer * 1000);
		});
		
		$('div#Slideshow p').find(':first-child:first').wrap('<div class="slide" />');
		$('div#Slideshow div.slide').each(function()
		{
			//Debug
			$(this).css('position', 'absolute');
			$(this).css('left', 0);
			$(this).css('top', 0);
			$(this).css('display', 'none');
			
			$(this).unwrap();
			
		});
		
		runSlideshow();
		
	}
	
	if ($("#ModelAccordion"))
	{
		$("#ModelAccordion").accordion({ autoHeight: false });
	}

	//Compare Gem code
	if ($("div.compareAccordion"))
	{
		$("div.compareAccordion").accordion({ autoHeight: false });
		
		$("div.compareAccordion input[type='checkbox']").change(function () {
	
			var parentAccordion = $(this).parents('div').get(1);
			
			if ($(parentAccordion).find('input[type="checkbox"]:checked').length > 3)
			{
				alert('Max 3 producten');
				$(this).attr('checked', '');
			}
			
			var clusterSize = $(this).parents('div').eq(0).find('input[type="checkbox"]:checked').length;
			var span = $(this).parents('div').eq(0).prev().find('span').eq(1);
			
			if (!$(span).attr('base'))
			{
				$(span).attr('base', $(span).text());
			}
			
			if (clusterSize > 0)
			{
				$(span).text($(span).attr('base') + ' (' + clusterSize + ')');
			}
			else
			{
				$(span).text($(span).attr('base'));
			}
			
		});
	}
	//End Compare Gem code
	
	if ($('.calendarSelect'))
	{
		$(".calendarSelect").selectbox();
	}
	
	var menuItems = $('#Items ul li');
	$(menuItems).mouseover(function(){
		$(this).addClass('active');
	});
	$(menuItems).mouseout(function(){
		$(this).removeClass('active');
	});


	if ($(".logo a"))
	{
		var logos = $(".logo a");
		$(logos).mouseover(function(){
			$(this).addClass('active');
		});
		$(logos).mouseout(function(){
			$(this).removeClass('active');
		});
		$(logos).click(function(){
			window.location = $(this).attr('href');
			//$(this).addClass('active');
		});
	}
	
	if ($('div.customGrid').length > 0 && backoffice == false)
	{
		$('div.customGrid').each(function()
		{
			var logos = $(this).find('td.logos a');
			
			var output = '';
			
			logos.each(function()
			{
				output += '<div class="logo">';
				output += '<a href="' + $(this).attr('href') + '">';
				output += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
				output += '<tr>';
				output += '<td class="logoContainer">';
				output += $(this).html();
				output += '</td>';
				output += '</tr>';
				output += '</table>';
				output += '</a>';
				output += '</div>';
			});
			
			$(this).find('td.logos').html(output);

		});
	}

	if (backoffice == false)
	{
		Cufon.replace('h2');
		Cufon.replace('h4');
	}
	
	//document.execCommand("BackgroundImageCache", false, true);
	
	//langSelect
	$('div#Flags').mouseenter(function() {
		clearTimeout(languageTimeout);
		$('div#LanguageSelect').show();
	});
	
	$('a.flag').click(function()
	{
		$('div#Flags span#LanguageText').text($(this).text());
		$('div#Flags span#LanguageText').parent().attr('class', $(this).attr('class'));
	});
	
	$('#Flags').mouseleave(function() {
	  	languageTimeout = setTimeout("$('#LanguageSelect').hide()",500);
	});
	
	var url = window.location.toString();
	var lang = '';

	if (url.indexOf('sunconnex.com/en') >= 0)
	{
		lang = 'uk';
	}
	else if (url.indexOf('sunconnex.com/fr') >= 0)
	{
		lang = 'fr';
	}
	else if (url.indexOf('sunconnex.com/de') >= 0)
	{
		lang = 'de';
	}
	else if (url.indexOf('sunconnex.com/nl') >= 0)
	{
		lang = 'nl';
	}
	else if (url.indexOf('sunconnex.com/it') >= 0)
	{
		lang = 'it';
	}
	else if (url.indexOf('sunconnex.com/ca') >= 0)
	{
		lang = 'ca';
	}
	else 
	{
		lang = 'int';
	}
	
	
	if (lang != '')
	{
		$('div#Flags div#Languages a.' + lang).click();
	}
	//end langSelect
	
	var logoUrl = '';	
	var career = '';	
	if (lang != 'int')
	{
		if (lang == 'uk')
		{
			logoUrl = '/en/';
			career = '/en/career';
		}
		else
		{
			logoUrl = '/' + lang + '/';
			career = '/' + lang + '/career';
		}
	}
	else 
	{
		logoUrl = '/';
		career = '/career';
	}
	
	$('div#Logo a').attr("href", logoUrl);
	$('div#Links a.career').attr("href", career);
	
	/* tickerTape */
	$.jGFeed('http://feeds.feedburner.com/solarplazadailynews.xml', function(feeds)
	{
		// Check for errors
		if(!feeds)
		{
			// there was an error
			return false;
		}
		
		var container = $('div#Ticker');
		container.html('')
		
		// do whatever you want with feeds here
		for(var i=0; i<feeds.entries.length; i++)
		{
			var entry = feeds.entries[i];
			var date = entry.publishedDate.substr(0, 16);
			// Entry title
			container.append('<div class="item"><a href="' + entry.link + '" target="_blank">' + date + ': ' + entry.title + '</a></div>');
		}
		
		nextTicker();

	}, 10);
	
	$('div#Ticker').mouseenter(function()
	{
		clearTimeout(tickerTimer);
	});
	$('div#Ticker').mouseleave(function()
	{
		tickerTimer = setTimeout(nextTicker, nextItem);
	});
});

function nextTicker()
{
	if (tickerData >= 0)
	{
		$('div#Ticker div.item').eq(tickerData).animate(
		{
			top: 20
		}, animateItem, function() { $(this).css('top', -20)});
		
		tickerData++;
	}
	else
	{
		tickerData = 0;
	}
	
	if (tickerData == $('div#Ticker div.item').length)
	{
		tickerData = 0;
	}
	
	$('div#Ticker div.item').eq(tickerData).animate(
	{
		top: 0
	}, animateItem);
	
	tickerTimer = setTimeout(nextTicker, nextItem);
}

function gotoSlide(index)
{
	if (activeSlide == index)
	{
		return false;
	}
	
	clearTimeout(slideshow);
	
	var active = $('div#Slideshow div.slide').eq(activeSlide);
	active.stop();
	//active.fadeOut('slow');
	active.fadeTo('slow', 0);
	
	var slide = $('div#Slideshow div.slide').eq(index);
	slide.stop();
	//slide.fadeIn('slow');
	slide.fadeTo('slow', 1);
	
	activeSlide = index;
	
	slideshow = setTimeout(runSlideshow, slideshowTimer * 1000);
}

function runSlideshow()
{
	var length = $('div#Slideshow div.slide').length;
	
	if (activeSlide == null || activeSlide == (length - 1))
	{
		goto = 0;
	}
	else
	{
		goto = activeSlide + 1;
	}
	
	gotoSlide(goto);
}
