$(document).ready(function() {
	
	/**
	 * IE6 CSS Patches for Navigation
	 */
	if ($.browser.msie && $.browser.version <= 6 ) {
		$('#nav').attr('id', 'nav6');	
	}
	
	
	/** 
	 * Navigation: hover animation (thicker underline)
	 */
	/* .:TODO:. Minify if only class set:
	$("#nav a").hover(function(event) {
        $(this).toggleClass('active');
    });
	*/	
	$("#nav a").hover(
		function () {
			$(this).parent().addClass("hover");
		},
		function () {
			$(this).parent().removeClass("hover");
		}
	);
	
	/* Activate first link on pageload */
	//$("#nav a:first").parent().addClass("active");
	//$("#nav a:first").next().slideDown(1000); /* .:TODO:. Chain "#nav a:first" */
	
	/*
	$('#nav a.submenu').click(function(e) {		
		$(".nav_sub").slideUp(700, function() { // .nav_sub slideUp first
			e.next().slideDown("slow"); // ... wait for it to complete, then slideDown
		});		
		e.preventDefault();
	});
	*/
	
	
	/**
	 *	Table sorter: Kunden Liste
	 */
	$("#kundentabelle table .hide").hide(); // Hide sort column
	$("#kundentabelle table").tablesorter({
		sortList: [[3,0]],
        headers: { 
            // assign the secound column (we start counting zero) 
            2: { 
                // disable it by setting the property sorter to false 
                //sorter: false 
			}
		}
	});
	// Effect (CSS background image) on hover over row
	$("#kundentabelle tbody tr").hover(
  		function () {
    		$(this).toggleClass("active");
  		}
	);
	
	
	/* .:TODO:. */
	/*
		- Kompetenzen: IE Workaround für CSS #kompetenzen table td:last-child
		- Team: IE siehe oben (:last-child)
		- Aktuelles: siehe oben
	*/
	
	
	/* Referenzen Übersicht */
	$(".portfolio_item:nth-child(3n)").addClass("col_last");

	$(".portfolio_item").hover(function(event) {
        $(this).toggleClass('hover');
    });	
	
	
	/**
	 * LOGO SLIDESHOW
	**/
	$('#logo').cycle({
		fx: 		'fade',
		pause: 		true,
		sync:		false, // true if in/out transitions should occur simultaneously
		speed:      2000, // speed of the transition (any valid fx speed value) 
		timeout:    8000  // milliseconds between slide transitions (0 to disable auto advance) 
	});
	// Workaround: IE unexpected fade behavour 
	//$('#logo'). // Set anchor dimensions to img (child) dimensions
	
	
	/**
	 * KOMPETENZEN HOVER
	**/
	$("#kompetenzen td")
	$("#kompetenzen td").hover(
		function () {
			$(this).find(".main").hide();
			$(this).find(".meta").show();
		}, 
		function () {
			$(this).find(".meta").hide();
			$(this).find(".main").show();
		}
	);	


		
		
		
	/**
	 * REFERENCE SINGLE Slideshow
	**/
	/*
	$('.ref_slideshow').cycle({
		fx: 		'fade',
		pause: 		false,
		sync:		true,
		speed:      1000, // speed of the transition (any valid fx speed value) 
		timeout:    4000  // milliseconds between slide transitions (0 to disable auto advance) 
	});
	*/
	$('#ref_single tbody').cycle({
		timeout: 4000,
		fx: 'custom',
		after: function(curr,next,opts) {
			$(curr).css('zIndex',opts.slideCount + (opts.rev ? 1 : 0));
			$(next).css('zIndex',opts.slideCount + (opts.rev ? 0 : 1));
		},
		cssBefore:  { opacity: 1, display: 'block' },
		animOut: { opacity: 0 },
		animIn: { opacity: 1 }
	});
	
	
	$('.thumb_slideshow').cycle({
		fx: 		'fade',
		pause: 		true,
		sync:		true,
		speed:      2000, // speed of the transition (any valid fx speed value) 
		timeout:    4000  // milliseconds between slide transitions (0 to disable auto advance) 
	});	
	
	
	/**
	 * Show/Hide information text of references
	 * Save setting cross-site via cookies
	 * // $.cookie(mwd_text_status', null);
	 * .:TODO:. Clean-up
	 */
	if ($.cookie('mwd_text_status') == 'hidden') {
		$('.ref_info').hide();
		$('.toggle_info a').removeClass('hide_info').addClass('show_info');
	}		
	$('.toggle_info a').click(function(e) {
		e.preventDefault();
		$this = $(this);
		if ($(this).hasClass('hide_info')) {
			// 	Hide info text and toggle link class to "show_info"
			$('.ref_info').slideUp('slow', function() {
				$this.toggleClass('hide_info').toggleClass('show_info');	
			});
			// Set cookie value to hidden
			$.cookie('mwd_text_status', 'hidden');
		} else {
			// Show info text and toggle link class to "hide_info"
			$('.ref_info').slideDown('slow', function() {
				$this.toggleClass('hide_info').toggleClass('show_info');	
			});
			// Set cookie value to shown
			$.cookie('mwd_text_status', 'shown');				
		}
	});

	
	
	/* Logo Slideshow */
	$('.logo_slideshow tbody').cycle({
		fx: 		'fade',
		sync:		true,
		speed:      2000, // speed of the transition (any valid fx speed value) 
		timeout:    4000,  // milliseconds between slide transitions (0 to disable auto advance) 
		pager:  	'#slideshow_slides',
		next:   	'#next_slide', 
		prev:   	'#prev_slide' 
	});
	$('#slideshow_controls #pause').click(function(e) {
		e.preventDefault();
		$('.logo_slideshow tbody').cycle('pause'); 
	});		
	$('#slideshow_controls #play').click(function(e) { 
		e.preventDefault();
		$('.logo_slideshow tbody').cycle('resume'); 
	});
	
	var maxHeight = $(".logo_slideshow tbody img").height();
	
	$(".logo_slideshow").height(maxHeight);
		
		
	
	//$('#nav_categories').prependTo('.nav_sub');
	
	
	/**
	 * FLOWPLAYER SETUP: Flash Video Player
	 */
	flowplayer("a.player", "tl_files/mwd/flash/flowplayer-3.2.3.swf");


	/**
	 * SAFARI REF SLIDESHOW WORKAROUND
	 * Safari erkennt die durch das Cycle Plugin zugewiesene Höhe des tbody nicht.
	 * => Höhe von Kindelement auslesen, und dem übergeordneten Element (table) zuweisen
	 */
	var tbodyHeight = $('#ref_single .ce_gallery .image_container').height();
	$('#ref_single .ce_gallery table').height(tbodyHeight);
	
	
	/**
	 * Override prev/next with «/»
	 */
	$('#portfolio_overview .next, #ref_single .next').html('&raquo;');
	$('#portfolio_overview .previous, #ref_single .previous').html('&laquo;');
	/**
	 * Delete « Anfang / Ende » on Portfolio (Category) Overview
	 */	
	$('#portfolio_overview .pagination .last').parent().hide();
	$('#portfolio_overview .pagination .first').parent().hide();


	/**
	 * .:TODO:. Slideshow Collapse; find workaround
	 */	
	$('#ref_single .ce_gallery').each(function(index, element) {		
		var height = $(this).find('tr').height();
		$(this).height(height);
	});	
	
	/**
	 * Enhance: remove active "Alle" on "Logoübersicht"
	 */	
	$('.logos #nav_categories li').removeClass("active");

	/**
	 * CSS last-child Workaround
	 */	
	$('#aktuelles .ce_text:last-child').addClass('last-child');
	
	/**
	 * Take "Logoübersicht and move it to bottom of all reference categories
	 */
	$('#nav_main .nav_sub li.logos').appendTo('#nav_categories');
	$('#nav_categories').css('top', '16px');

});


