/* 
    Document   : frontend.js
    Created on : 25.08.2011, 15:15:13
    Author     : Mike vom Scheidt || CTS Media Gmbh 2011
    Description: Default javascript for Jäckering
 */

$.noConflict();

jQuery(function(){
    Shadowbox.init();
    easyHover(jQuery('#navi_main .level_2 a').not('.active, .trail'), '#f0b338', 1000);
    openNews('#footer .mod_newslist a', '#main .mod_newslist .layout_full');
    teamViewer('.click-details', '.person_details', 500);
});

//onload
jQuery(window).load(function () {   
    fadeImages(jQuery('#header .ce_gallery .image_container'), 500, 3500);
    videoControl();
});

var videoControl = function() {
    //don't show button in ios (iphone, ipad ....)
    if(!jQuery('body').hasClass('ios')) {
        jQuery('.ce_html5').prepend('<img class="playbutton" src="tl_files/page/images/play.png" alt="Play" />');
        
        //mouseover image change
        jQuery('.playbutton').mouseover(function() {
            jQuery(this).attr('src', 'tl_files/page/images/play-active.png')
        }).mouseout(function() {
            jQuery(this).attr('src', 'tl_files/page/images/play.png')
        });
    }

    
    //if flowplayer exists
    if(typeof $f != 'undefined' && (typeof $f(0) != 'undefined' || $f(0) != null)) {
        
        $f(0).onPause(function() {
            jQuery('.playbutton').fadeIn(500);
        }).onStop(function() {
            jQuery('.playbutton').fadeIn(500);
        }).onResume(function() {
            jQuery('.playbutton').fadeOut(500);
        }).onStart(function() {
            jQuery('.playbutton').fadeOut(500);
        });
        
        jQuery('a.video, .playbutton').click(function(e) {
            e.preventDefault();

            if($f(0).toggle()) {
                jQuery('.playbutton').fadeOut(500);
            } else {
                jQuery('.playbutton').fadeIn(500);
            }            
        });
    //html 5 native player
    } else {
        jQuery('a.video, .playbutton').click(function(e) {
            e.preventDefault(); 
            if (jQuery('video').get(0).paused == true) {
                jQuery('video').get(0).play();
                jQuery('.playbutton').fadeOut(500);
            } else {
                jQuery('video').get(0).pause();
                jQuery('.playbutton').fadeIn(500);
            }
        }); 
    }
};

var easyHover = function(elements, textColor, animationTime) {
    var jQEls = jQuery(elements);
    
    if(jQEls.length > 0) {
        var defaultColor = elements.css('color');
        elements.parents('.level_1').addClass('noHover');
                
        if(typeof animationTime == "undefined") {
            animationTime = 1000;
        }
        
        elements.each(function() {
            var element = jQuery(this);
            
            element.mouseover(function() {
                element.stop().animate({
                    color: textColor
                }, 250);
            }).mouseout(function() {
                element.stop().animate({
                    color: defaultColor
                }, 500);
            });  
        });
    }
};

/**
 * Fades elements into each other (all img elements you specified)<br />
 * Does not fade if only one image is given<br />
 * Adds a class "add_z_index" to the more important element<br /><br />
 * <strong>elements (jQuery Obj|string)</strong> - the elements to be used in the fading animation<br />
 * <strong>fadeTime (int)</strong> - the time it takes to fade over to the next element<br />
 * <strong>[waitTime (int)]</strong> - the time the current element is shown before fading restarts. Defaults to 2000ms<br />
 */
var fadeImages = function(elements, fadeTime, waitTime) {
    var jQEls = jQuery(elements);
    
    if(jQEls.length > 1) {
        var timeoutID = null;
        
        if(typeof fadeTime == "undefined") {
            fadeTime = 1000;
        }
        
        if(typeof waitTime == "undefined") {
            waitTime = 2000;
        }
        
        var run = function(index) {
            clearTimeout(timeoutID);
            if(!jQEls.hasClass('mouseover')) {
                var nextElement = jQuery(jQEls[index]);
            
                nextElement.addClass("z_index");
                nextElement.fadeIn(fadeTime, function(){
                    jQEls.not(nextElement).hide();
                    nextElement.removeClass("z_index");
                
                    timeoutID = setTimeout(function() {
                        if(jQEls.length > index+1) {
                            run(index+1);
                        } else {
                            run(0);
                        }
                    }, waitTime);
                });
            }
        }

        //automate image change
        var startDelayed = function(index) {
            clearTimeout(timeoutID);
            
            timeoutID = setTimeout(function() {
                run(index);
            }, waitTime);
        }

        //stops automated image switch when thumbs are hovered
        jQEls.mouseover(function() {
            clearTimeout(timeoutID);
            jQuery(this).addClass("mouseover");
        });       

        //restarts automated image switch
        jQEls.mouseout(function() {
            startDelayed(0);
            jQuery(this).removeClass("mouseover");
        });        

        //start automation once
        startDelayed(0);        
    }
};

var openNews = function(eventElements, targets) {
    eventElements = jQuery(eventElements);
    targets = jQuery(targets);
    var parent = targets.parents('.mod_newslist');
    var location = window.location.hash;
    location = location.substring(location.lastIndexOf('#') + 1);

    if(location.length > 0) {
        var element = jQuery('a.' + location);
        var target = targets.filter('#' + location);
        
        elementOpener(parent, targets, eventElements, element, target);
    } else {
        //hide currently shown element
        var defaultAlias = targets.filter(':visible').attr('id');
        eventElements.filter('.' + defaultAlias).parents('.layout_latest').find('> div').fadeTo(1000, 0.5);
    }
    
    eventElements.click(function(e) {
        e.preventDefault();
        
        if(!parent.hasClass('animation')) {
            parent.addClass('animation');
            var element = jQuery(this);
            var alias = element.attr('href').substr(element.attr('href').lastIndexOf('#') + 1);
            var target = targets.filter('#' + alias);
            
            elementOpener(parent, targets, eventElements, element, target);
        }
    });
}

var elementOpener = function(parent, targets, eventElements, element, target) {         
    eventElements.not(element).parents('.layout_latest').find('> div').fadeTo(1000, 1);
    element.parents('.layout_latest').find('> div').fadeTo(1000, 0.5);

    targets.not(target).filter(':visible').animate({
        opacity: 0,
        height: 'toggle'
    }, 1000, function() {
        target.css('opacity', 0);
        target.animate({
            opacity: 1,
            height: 'toggle'
        }, 1000, function() {
            parent.removeClass('animation');
        }); 
    });
    
    //exception for when user clicks the same project
    if(target.filter(':visible').length > 0) {
        parent.removeClass('animation');
    }
}

var teamViewer = function(container, containerDetails, fadeSpeed) {
    container = jQuery(container);
    containerDetails = jQuery(containerDetails);
    var links = container.find('.ce_text a');
    var containers = container.find('.ce_text');
    var details = containerDetails.find('.ce_text');
    var lang = '';
    var translation = 'Fenster schließen';
    
    if(jQuery('html').attr('lang') == 'en') {
    	translation = 'Close window'	
    }
    
    links.click(function(e){
        e.preventDefault();
        var currLink = jQuery(this);
        var index = containers.index(currLink.parents('.ce_text')); //links.index(currLink);
        
        details.not(details.eq(index)).fadeOut(100);
        details.eq(index).fadeIn(500);
        
        details.eq(index).append('<a href="#close" class="close">' + translation + '</a>');
        details.eq(index).find('.close').click(function(e) {
            e.preventDefault();
            details.eq(index).fadeOut(500);
        });
    });
}
