$(document).ready(function(){
	var isiPad  = navigator.userAgent.match(/iPad/i) != null;
	var isiPhone  = navigator.userAgent.match(/iPhone/i) != null;
	var baseUrl = 'http://georgiew.de/';
	
	$('.fancybox').fancybox({
		'overlayOpacity': 0.5,
		'overlayColor'  : '#000000'
	});
	
	$('#slideNaviWrap').hide();
	if((checkSpecialLink(location.href, 'photography') || checkSpecialLink(location.href, 'fotographie') || checkSpecialLink(location.href, 'fotografie')) || location.href == baseUrl || location.href == 'http://www.georgiew.de/') {
		$('#slideNaviWrap').show();
		$('#footNews').hide();
	}

/* Accordion *************************************/

	$('.accordion h3').each(function(){
		$(this).siblings().wrapAll('<div></div>');
	});
	$('.accordion:first').before('<div class="theAccordion"></div>');
	$('.accordion').appendTo('.theAccordion');
	$('.theAccordion h3').unwrap();
	$(".theAccordion").accordion({header: 'h3', autoHeight: false, active: 'h3:first', collapsible: true});
	
	
/* Navi ******************************************/
	
	$('#mainNavi').find('li').find('.dropdown').hide();
	$('#mainNavi').find('li').hover(function(){
		$(this).find('.dropdown').css('opacity','0').show().animate({
			opacity: 1
		},200);
	},function() {
		$(this).find('.dropdown').animate({
			opacity: 0
		},0, function(){
			$(this).hide()
		});
	});
	

/* Navi bei toiSlider ausblenden *****************/	
	
	if($('#toiSlider').length > 0) {
		$('#naviWrap').hover(function() {
			$(this).animate({'opacity': 1}, 300);
		}, function() {
			$(this).animate({'opacity': 0.3}, 300);
		});
		
		$('#naviWrap').css('opacity', 0.3);
	}
	
	
/* Fancy MetaNavi **********************************/
	$('#metaNaviWrap').find('div').each(function() {
		$(this).hover(function() {
			$(this).css('opacity', 1);
			$(this).animate({'left': (-$(this).width() -5)}, 500);
		}, function() {
			$(this).stop(true, false);
			$(this).css('opacity', 0.5);
			$(this).animate({'left': 0}, 500);
		});
	});


/* MailForm ***************************************/
	$('#mailForm').find('input, textarea, select').focus(function() {
		$(this).attr('style', '');
	});
	
	p1Height = $('.formPart1').height();
	p2Height = $('.formPart2').height();
	$('.formPart1').height(p1Height);
	$('.formPart2').height(p2Height);


/* Layout related *********************************/
	$('#background').toiFullBG();
	$('body').append('<div id="topRight" />');
	$('body').append('<div id="naviHovers" />');
	
	
	
/* TEAM Seite **************************************/
	$('#fixedWidth .csc-default').each(function() {
		headline = $(this).find('h2').html();
		if(headline && headline != 'NULL' && headline != '') {
			$(this).before('<h2>'+headline+'</h2>');
			$(this).find('h2').remove();
		}
		$(this).prepend('<div class="toggler" />');
	});
	
	$('.textboxAboveCenter:first').find('.csc-textpic-text').show();
	$('.textboxAboveCenter:first').find('a').addClass('act');
	$('.textboxAboveCenter:first').parent().find('.toggler').addClass('act');
	
	$('.toggler').click(function() {
		if($(this).hasClass('act')) {
			$(this).parent().find('.textboxAboveCenter .csc-textpic-text').slideUp();
			$(this).parent().find('a').removeClass('act');
			$(this).removeClass('act');
		} else {			
			$(this).parent().find('.textboxAboveCenter .csc-textpic-text').slideDown();
			$(this).parent().find('a').addClass('act');
			$(this).addClass('act');
		}
	});
	
	function resetTextpicTexts() {
		$('.textboxAboveCenter').each(function() {
		$(this).find('.csc-textpic-text').hide();
		});
	}
	
	
	checkDimensions();
	reFormatContent(location.href);
	reInitContent(location.href);	
	
	
	
	function checkDimensions() {
		sW = $(window).width();
		sH = $(window).height();
		
		nW = $('#naviWrap').width();		
		fW = sW - nW - 130;
		
		$('#contentWrap').css({
			width: fW,
			left: nW + 40
		});
	}
	
	
	
	
/* Video Boxen erstellen und flowplayer init ******/

	/* * *
	* einzelne Content-Elemente nach ajaxRequest formatieren
	* (hier vorallem die Videoboxen für die Film-Seite
	*/
	function reFormatContent(url) {
		vidCount = 0;
		$('.csc-downloadBox').each(function() {
			$(this).data('num', vidCount);
			$(this).find('a').unbind('click').die('click');
			//$(this).find('a').unwrap();
			//$(this).find('.fileDesc:not(:first)').remove();
			
			imgFile = $(this).find('a:first').attr('href');
			vidFile = $(this).find('a:not(:first)').attr('href');
			vidDesc = $(this).find('.fileDesc').html();
			
			$(this).find('a:first').remove();
			$(this).find('br').remove();
			$(this).prepend('<a class="vidOverlay fancybox" href="#vidWrap'+vidCount+'"><img src="'+imgFile+'" /></a>');
			$(this).find('a:not(:first)').empty().attr('id', 'vidItem'+vidCount).attr('href', baseUrl + vidFile).attr('class', 'vidBox fancybox');
			$(this).find('a:not(:first)').parent().attr('id', 'vidWrap'+vidCount);
			$(this).find('a:not(:first)').parent().attr('class', 'vidWrapper');
			$(this).find('#vidWrap'+vidCount).wrap('<div style="display: none" />');
			
			$(this).append('<div id="vidControl'+vidCount+'"></div>');
			
			$(this).unwrap().wrap('<div class="videoBox" />');
			
			$(this).after('<div class="fileDesc"><span>'+vidDesc+'</span></div>');
			
			vidCount = vidCount + 1;
		});
		
		$('.csc-textpic-imagewrap').each(function() {
			imgWidth = $(this).find('img').width();
			$(this).css('width', imgWidth);
		});
		
		var vidMaxHeight = 0;
		$('.videoBox').each(function() {
			vidMaxHeight = ($(this).height() > vidMaxHeight) ? $(this).height() : vidMaxHeight;
		});
		
		$('.videoBox').css('height', vidMaxHeight);
	}	
	

	var vidRelation = false;
	
	function reInitContent(url) {
		isLoggedIn = true;
		vidCount = 0;
		$('.csc-downloadBox').each(function() {
			
			if(!isiPad && !isiPhone) {
				flowplayer("vidItem"+vidCount, "pub/js/flowplayer-3.2.7.swf", {
				    clip:  {
        		    	autoPlay: false,
        		    	autoBuffering: true
    			    },
    			    
    			    plugins: {
   						controls: {
   							all: true,
   							play: true,
   							fullscreen: true,
   							stop: true,
   							scrubber: true,
   							mute: isLoggedIn,
   							
   							progressColor: '#d9ca54',
   						}
					}
				});
			} else{
				flowplayer("vidItem"+vidCount, {src: "pub/js/flowplayer-3.2.7.swf", width: 600, height: 400}, {
				    clip:  {
        		    	autoPlay: false,
        		    	autoBuffering: true
    			    },
    			    
    			    plugins: {
   						controls: {
   							all: true,
   							play: true,
   							fullscreen: true,
   							stop: true,
   							scrubber: true,
   							mute: isLoggedIn,
   							
   							progressColor: '#d9ca54',
   						}
					}
				}).ipad();
			}
			
			$(this).find('a').unbind('click').die('click');
			
			$(this).find('.vidOverlay').click(function() {			
				vidRelation = $(this).parent();
			});
			
			$('a.vidOverlay').fancybox({
				'overlayOpacity': 0.6,
				'overlayColor'  : '#000000',
				'onComplete'    : function() { $f(vidRelation.data('num')).play(); }
			});

			vidCount = vidCount + 1;
		});
		
		checkScrollableContent($('#contentWrap'));
	}
	
	
	/* * *
	* #contentWrap Höhe gegen Fensterhöhe prüfen
	* -> bei Bedarf scrollbar machen
	*
	* @obj -> Object, welches mit Fensterhöhe verglichen werden soll
	*/
	function checkScrollableContent(obj) {
		if(($(window).height() - 300) < obj.height()) {
			obj.css({
				'overflow': 'auto',
				'height'  : $(window).height() - 300
			});
		} else {
			obj.css({
				'overflow': 'hidden',
				'height'  : 'auto'
			});
		}
	  }
	  
	  
	  
	/* * *
	* Link auf bestimmten String prüfen
	* 
	* @currentLink -> zu prüfender Link
	* @part -> String
	*
	* Ist @part in @currentLink enthalten -> return true
	*/
	function checkSpecialLink(currentLink, part) {
		splits = currentLink.split(part);
		if(splits.length > 1) {
		    return true;
		} else {
		    return false;
		}
	}
	
	
	
	
/* Animierte Navi *********************************/		
	/*
$('#mainNavi').find('li').each(function() {
		relation = $(this).find('a').attr('id');
		$('#naviHovers').append('<div class="'+relation+'">X</div>');
	});
	
	var hoverIndex = 0;
	var naviHeight = $('#mainNavi li').height();
	
	if(!isiPad) {
		$('#naviHovers').find('div').each(function() {
			offset = 20;
			$(this).css({
				'height': naviHeight,
				'top'   : (hoverIndex * naviHeight)  + (hoverIndex * 6)
			});
			hoverIndex++;
		});
	} else {
		$('#naviHovers').remove();
	}	
	
	$('#mainNavi li a').hover(function () {
		theWidth = $(this).width() + 40;
		$('.'+$(this).attr('id')).animate({'width': theWidth}, 200);
	}, function() {
		$('.'+$(this).attr('id')).animate({'width': 0}, 300);
	});	
*/
	
	
});
