var player, playerAPI;
//
function onYouTubePlayerReady(playerId){
	playerAPI = player.getApi();
	/*
	if (playerAPI && playerAPI.pauseVideo) {
		playerAPI.pauseVideo();
	};
	*/
}
//
$(function() {
	var config = {
		slideInterval : 38000,
		debug : false
	};

	var display = {

		cufonStore : [],

		init : function() {
			this.resizeContent();
			this.replaceFonts();
			this.initMainMenue();
			this.initSlides();
			this.initNews();

			// google tracking
			$.geekGaTrackPage('UA-1447777-1');
		},

		onComplete_replaceFonts : function(fn) {
			if (config.debug) {
				$.log('onComplete_replaceFonts | ' + this.cufonStore.length + ' fonts replaced and objects stored.');
				$.log(this.cufonStore);
			}
		},

		resizeContent : function() {
			var siteoffset = ($(window).width() > 1150) ? 110 : 40;
			if (config.debug) {
				$.log('resizeContent | site offset: ' + siteoffset + ', window width: ' + $(window).width());
			}
			if ($(window).width() < 1450) {
				$('.site-wrapper').animate( {
					left : siteoffset
				}, 600);
			}
			// this.refreshFonts();
		},

		refreshFonts : function(name) {
			$.each(this.cufonStore, function(index, item) {
				if (!name || name && item.name == name) {
					item.cufon.refresh();
					if (config.debug) {
						$.log('refreshFonts: | cufon: ' + item.name + ' refreshed!');
					}
				}
			});
		},

		replaceFonts : function() {
			this.cufonStore.push( {
				name : 'mainNav',
				cufon : Cufon.replace('#main-navigation li a.main', {
					fontFamily : 'VectoraLT',
					/* textShadow: 'rgba(0, 0, 0, 0.25) 1px 1px', */
					hover : true
				})
			});

			this.cufonStore.push( {
				name : 'mainNavMenueHl',
				cufon : Cufon.replace('#main-navigation li .menue h6', {
					fontFamily : 'VectoraLT',
					hover : true
				})
			});
			
			this.cufonStore.push( {
				name : 'siteContentH1',
				cufon : Cufon.replace('.site-contents h1', {
					fontFamily : 'VectoraLT',
					hover : true
				})
			});

			this.cufonStore.push( {
				name : 'siteContentH2',
				cufon : Cufon.replace('.site-contents h2', {
					fontFamily : 'VectoraLT',
					hover : true
				})
			});

			this.cufonStore.push( {
				name : 'siteContentH3',
				cufon : Cufon.replace('.site-contents h3', {
					fontFamily : 'VectoraLT',
					hover : true
				})
			});
			
			this.cufonStore.push( {
				name : 'siteContentH4',
				cufon : Cufon.replace('.site-contents h4', {
					fontFamily : 'VectoraLT',
					hover : true
				})
			});

			this.cufonStore.push( {
				name : 'smartlistsH3',
				cufon : Cufon.replace('.entry h3', {
					fontFamily : 'VectoraLT',
					hover : true
				})
			});

			$(this).trigger('onComplete_replaceFonts');
		},

		initMainMenue : function() {
			var menueLink = $('#main-navigation ul a.main');
			
			menueLink.click(function(e) {
				if (!$(e.currentTarget).attr('href')) {
					e.preventDefault();
				}
			});

			$('#main-navigation ul').lavaLamp( {
				fx : 'swing',
				speed : 333,
				returnHome : true,
				autoReturn : false,
				homeLeft : 44,
				homeTop : 0,
				homeWidth : 0,
				homeHeight : 48
			});

			$('#main-navigation li').bind('hover', {
				speed : 130,
				delay : 350
			}, function() {
				$(this).children('div').slideDown();
				$(this).children('a').addClass('mactive');
				Cufon.refresh('#main-navigation li a.main');
			}).bind('hoverend', function() {
				$(this).children('div').slideUp();
				$(this).children('a').removeClass('mactive');
				Cufon.refresh('#main-navigation li a.main');
			});
		},

		slidesStore : [],

		initSlides : function() {
			try {		
				var tabOptions = {
					effect : 'fade',
					fadeInSpeed : 'slow',
					fadeOutSpeed : 'slow',
					rotate : true,
					// history: true,
					onBeforeClick : function(event, i) {
						var pane, tabs, tab, url;
						// get the pane to be opened
					pane = this.getPanes().eq(i);
					tabs = this.getTabs();
					tab = tabs.eq(i);

					if (pane.is(':empty')) {

						var href = tab.attr('href');
						url = (href.substring(0, 1) == '#') ? '/api/shared/' + href.substring(1) + '_tab.html' : href;

						if (config.debug) {
							$.log('Lade Tab: "' + i + '" von URL: ' + url);
						}
						
						pane.load(url, {}, function() {
							if (!$('body').hasClass('subpage')) {
								$('#feature-box').load('/api/shared/' + href.substring(1) + '_features.html', {}, function(responseText) {
									display.slidesStore[i] = {
										name : href.substring(1),
										slide : pane,
										featureBox : responseText
									};
									display.initFeatureList();
								});
							}
						});
					} else {
						if (display.slidesStore[i]) {

							var currSlide = display.slidesStore[i];
							pane = currSlide.slide;
							$('.feature-contents').hide();
							$('#feature-content-' + i).show();

							display.initFeatureList(false);
						} else {
							$('.feature-contents').hide();
							$('#feature-content-' + i).show();
							display.initFeatureList();
						}
					}
				}
				};

				$('.tabs').tabs('.slides > li', tabOptions).slideshow( {
					interval : config.slideInterval,
					clickable : false,
					autoplay : true
				});
			} catch (e) {
				$.log(e);
			}
		},

		initFeatureList : function(noFonts) {
			try {
				if (!$('body').hasClass('subpage')) {
					$('.feature-contents:visible .feature-items li a').click(function(e) {
						e.preventDefault();
					});

					if (!noFonts || noFonts === true) {
						this.cufonStore.push( {
							name : 'featureListItems',
							cufon : Cufon.replace('.feature-items a', {
								fontFamily : 'VectoraLT'
							})
						});
						this.cufonStore.push( {
							name : 'featureListDetails',
							cufon : Cufon.replace('.feature-details h2', {
								fontFamily : 'VectoraLT'
							})
						});
					}
					$('.feature-contents:visible .feature-items li a').removeClass('current');
					var interval = config.slideInterval / $(".feature-contents:visible .feature-items li a").length;
					
					$.featureList($(".feature-contents:visible .feature-items li a"),
							$(".feature-contents:visible .feature-details li"), {
								start_item : 0,
								transition_interval : interval
							});
					// $(".feature-details li").show();
					// hy.run();
				}

			} catch (e) {
				$.log(e);
			}
		},

		initNews : function() {
			try {

				$('.sl-box').each(function() {
					var id = $(this).attr('id');

					var effect = ($.browser.msie) ? '' : 'Blind';

					$(this).smartlist( {
						baseName : id,
						itemClass : id + '-item',
						showFlagCount : true,
						itemFlagClass : id + '-flags',
						itemFlagSeparator : ' | ',
						itemChangeEffect : effect,
						pageChangeDuration : 800,
						pageChangeEffect : effect,
						numItemsPerPage : ($(this).attr('rev')) ? $(this).attr('rev') : 3,
						paginationLeft : '\u00ab',
						paginationRight : '\u00bb',
						maxPaginationLinks : 10,
						defaultDropdownOptText : 'Alle anzeigen'
					});
					display.initVideos();
					
				});
			} catch (e) {
				if (config.debug) {
					$.log('smartlists: ' + e);
				}
			}
		},
		
		initVideos : function() {
			try {
				if ($('#videos').length > 0) {
					var ytvideos = $('a[href*="www.youtube."]');
					//

					ytvideos.click(function(e){
						e.preventDefault();
						$(this).overlay().load();
					});
					//
					ytvideos.each(function(i){
						$(this).overlay({
							//
							oneInstance: true,
							expose: 'black',
							target: '#videos',
							effect: 'apple',
							onBeforeLoad: function(){
								// 
								var wrapper = this.getContent().find(".contentWrap");
								var videoID = this.getTrigger().attr("href").match(/watch\?v=(.+)+/)[1];
								//
								player = wrapper.flashembed({
									api: true,
									src: 'http://www.youtube.com/v/' + videoID + '&hl=de&fs=1&rel=0&hd=1&autoplay=1&enablejsapi=1&playerapiid=youtube-api-' + videoID,
									width: 853,
									height: 505,
									version: [10, 0],
									wmode: 'transparent',
									quality: 'high',
									allowfullscreen: true,
									autoplay: true,
									allowscriptaccess: 'always',
									expressInstall: '/resources/flash/playerProductInstall.swf',
									onFail: function(){
										$('.yt-subtitle').html("<br/><br/><h3>Sie benötigen den Flash Player Version 10.0 oder höher!</h3>");
									}
								});
								//
								// $('.yt-subtitle').html('test text');
							}
						});
					});
				}
			} catch (e) {
				if (config.debug) {
					$.log('smartlists: ' + e);
				}
			}
		}
	};

	// global events
	$(document).bind("ready", function() {
		display.init();
	});

	$(window).bind("resize", function() {
		display.resizeContent();
	});
});

var hy = Hyphenator;
hy.config( {
	selectorfunction : function() {
		var el = $('p');
		return jQuery.makeArray(el);
	},
	minwordlength : 9,
	remoteloading : false
});
hy.addExceptions('', 'Kontor.NET, .NET, .net');
hy.run();
