var scrollPosY = 0;
var debug = true;
var isAndroid = (/android/gi).test(navigator.appVersion);
var isIDevice = (/iphone|ipad/gi).test(navigator.appVersion);
var isPlaybook = (/playbook/gi).test(navigator.appVersion);
var isTouchPad = (/hp-tablet/gi).test(navigator.appVersion);
var isIOS5 = (/OS 5/gi).test(navigator.appVersion);
var hasTouch = (isTouchPad || isIDevice || isAndroid) && ('ontouchstart' in window);
var isAnimated = false;
var targetID = '';
var currentID = '';
var graphicid = '';
var videoIsPlaying = false;
var videoID = '';
var evnt = (hasTouch ? 'touchstart' : 'click');
var evntHover = (hasTouch ? 'touchstart' : 'mouseover');
var chapterPos = { 0: 0, 0: 0, 0: 0, 0: 0, 0: 0, 0: 0, 0: 0, 0: 0 };
var videoPlayers = null;
var flashAudio = !hasTouch;
var html5Audio = hasTouch;
var html5Video = hasTouch; // || true;
var globalVolume = 9;

$(document).ready(function () {
    if (!hasTouch) {
        $('.content > .intro').css({ 'height': '700px' }); // .css({ 'height': (window.screen.height * 0.6) + 'px' }); 
    }
    updateChapterPos();

    // main Navigation
    $("#btn-boxster").bind(evnt, function (e) {
        $("#boxster, #sidebar").removeClass("hidden");
        $("#navigation-meta, #navigation-meta-back").addClass("hidden");
        $("#btn-gallery").removeClass("active");
        if (!$("#gallery").hasClass("hidden")) {
            $("#gallery").addClass("hidden");
        }
        if (!$("#btn-boxster").hasClass("active")) {
            $("#btn-boxster").addClass("active");
            navigateTo('chapter-design');
        }
    });
    $("#btn-gallery").bind(evnt, function (e) {
        $("#gallery").removeClass("hidden");
        $("#btn-boxster").removeClass("active");
        if (!$("#boxster").hasClass("hidden")) {
            $("#boxster").addClass("hidden");
        }
        if (!$("#btn-gallery").hasClass("active")) {
            $("#btn-gallery").addClass("active");
            navigateTo('gallery');
        }
        if (!$("#sidebar").hasClass("hidden")) {
            $("#sidebar").addClass("hidden");
        }
    });
    $("#btn-chapter").bind(evntHover, function (e) {
        setActiveChapter(e);
        $("#sidebar, #btn-chapter").toggleClass('inactive');
    });
    $(".chapternavi a").bind(evnt, function (e) {
        //$(".chapternavi a").removeClass('active');
        //jQuery(this).addClass('active');
        navigateTo(jQuery(this).attr('rel').replace('#', ''));
    });


    // gallery
    $('#gallery .contentWrapper div.bgimage').each(function () {

        $(this).bind(evnt, function (e) {
            graphicid = $(this).attr('rel');
            $("#navigation-meta-wallpaper a").each(function () {
                $(this).attr("href", $(this).attr("rel") + "/graphic-" + graphicid + ".jpg");
            });
            $("#fbshare").attr("href", $("#fbshare").attr("rel") + $("#navigation-meta-share").attr("rel") + graphicid)

            $("#twshare").attr("href", $("#twshare").attr("rel") + $("#navigation-meta-share").attr("rel") + graphicid)
            $("#navigation-meta, #navigation-meta-back").removeClass("hidden");



        });
    });
    $("#navigation-meta-close a").bind(evnt, function (e) {
        $("#navigation-meta, #navigation-meta-back").addClass("hidden");
        //$("#navigation-meta-wallpaper a").attr("href", $("#navigation-meta-wallpaper a").attr("href").replace(graphicid, "#"));
    });

    $(".scrollable").scrollable({ 'touch': false, 'circular': true }); //.navigator({ 'navi': '.navi', 'naviClass': 'useraction' });
    $(".scrollableTextPages").scrollable({ 'touch': true, 'circular': false }).navigator({ 'navi': '.navi', 'naviClass': 'useraction' });

    /* meta navigation */
    $('#navigation-bottom-strap').bind(evnt, function () { jQuery('.navigation-center-bottom').toggleClass('inactive') });
    if (hasTouch) {
        $('#navigation-main, #navigation-bottom, #navigation-meta, .navigation-center, .navigation-center-bottom').bind('touchmove', function (e) { e.preventDefault() });
        $('video').bind('touchmove', function (e) { e.preventDefault() });
        $(document).bind('touchstart', function (e) {
            scrollPosY = document.body.scrollTop;
            // logEvent(e.target);
            if (e.target.className) {
                if (e.target.className.indexOf('useraction') > -1) {
                    return;
                }
            }
            hideWhileScrolling(e);
        }).bind('touchend', function (e) {
            if ((scrollPosY + 2) >= document.body.scrollTop) {
                scrollPosY = document.body.scrollTop;
                if (videoIsPlaying) {
                    return;
                }
                showAfterScrolling(e, 0);
            }
        }).bind('touchmove', function (e) {
            scrollPosY = document.body.scrollTop;
            if (videoIsPlaying) {
                e.preventDefault();
                return;
            }
            if (e.target.className) {
                if (e.target.className.indexOf('useraction') > -1) {
                    e.preventDefault();
                    return;
                }
            }
            hideWhileScrolling(e);
        });
        $(window).bind('scroll', function (e) {
            /*log('scroll');*/
            if (videoIsPlaying && videoID != '') {
                navigateTo(videoID, true);
            }
            else {
                checkNavigation(false);
            }
        });
        $(window).bind('orientationchange', function (e) {
            updateChapterPos();
            if (videoIsPlaying && videoID != '') {
                return;
            }
            hideWhileScrolling(e);
            showAfterScrolling(e, 0);
        });
    }
    else {
        $(window).bind('scroll', function (e) { setActiveChapter(e); });
        $('#boxster').bind('mouseover', function () {
            if (!$("#sidebar").hasClass("inactive")) {
                $("#sidebar, #btn-chapter").addClass("inactive");
            }
        });
    }
    setupColorator();
    setupSofttop();
    setupRearwing();

    setupAudio();
    setupVideo();

    $('#legalnotice a').attr('target','_blank');


    superTrack();

    if (GALLERY != '') {
        setTimeout("$('#btn-gallery').trigger('" + evnt + "')", 500);
    }
    else if (DEEPLINK == '') {
        setTimeout("window.scrollTo(0,0);", 50);
    }
    else {
        setTimeout('navigateTo("' + DEEPLINK + '", true , 30);', 50);
    }
});

function navigateTo(elem, noNavi, offSet) {
    //log($('#' + elem));
    var z = document.getElementById(elem);
    if (z) {
        log(offSet);
        if (typeof (offSet) == 'undefined') {
            offSet = 0;
        }
        else {
            offSet = parseInt(offSet);
        }
        var showNavi = (typeof (noNavi) == 'undefined' ? true : (noNavi == true ? false : true));
        var top = jQuery('#' + elem).offset().top;
        if (videoIsPlaying) {
            offSet = 70;
        }
        top -= offSet;
        window.scrollTo(0, top);
        if (showNavi) {
            $('.related, #navigation-main, #navigation-bottom-wrapper, .navigation-center, .navigation-center-bottom').css({ 'display': 'none' });
            if (!$("#sidebar").hasClass("hidden")) {
                $('#btn-chapter, #sidebar').toggleClass('inactive');
            }
            //$('#' + elem).trigger(evnt);
            $('.related, #navigation-main, #navigation-bottom-wrapper, .navigation-center, .navigation-center-bottom').css({ 'display': 'block' });
        }
    }
    //log(z.offsetTop);
}

function setupSofttop() {
    $('#design06-roof-play').bind(evnt, function () {
        $(this).fadeOut(250, function () {
            jQuery('#design06-head1').fadeOut(1750, function () {
                jQuery('#design06-head2').fadeOut(1750, function () {
                    setTimeout("jQuery('#design06-head2').fadeIn(1750, function () {jQuery('#design06-head1').fadeIn(1750, function () {$('#design06-roof-play').fadeIn(250);});});", 1500);
                });
            });
        });
    });
}

function setupRearwing() {
    $('#design04-head2').css({ 'display': 'block', 'z-index': '1' }).addClass('active');
    $('#design04-head3').css({ 'display': 'block', 'z-index': '0' });
    setTimeout('fadeinOut("design04-head3")', 2000);
}

function fadeinOut(which) {
    //log(which);
    if (which == null) {
        return;
    }
    var activeID = jQuery('#design04-head .active').attr('id');
    var nextID = jQuery('#' + which).attr('id');
    jQuery('#' + activeID).fadeOut(2000, function () {
        jQuery('#' + nextID).css({ 'z-index': '1' }).addClass('active');
        jQuery('#' + activeID).css({ 'z-index': '0', 'display': 'block' }).removeClass('active');
        setTimeout('fadeinOut("' + activeID + '")', 3000);
    });
}

function setupColorator() {
    $('.colorator-navi > a').each(function (e) {
        jQuery(this).bind(evntHover, function (e) {
            if (isAnimated == true) {
                return;
            }
            isAnimated = true;
            targetID = jQuery(this).attr('id').replace('s-', '');
            currentID = jQuery('.colors .active').attr('id');
            if (targetID == currentID) {
                isAnimated = false;
                return;
            }
            jQuery('#s-' + currentID).removeClass('active');
            jQuery('#' + targetID).css({ 'z-index': '1', 'display': 'block' });
            jQuery('#s-' + targetID).addClass('active');
            jQuery('#' + currentID).fadeOut(250, function () {
                //log('current color = ' + currentID + ' | target color = ' + targetID);
                jQuery('#' + currentID).css({ 'z-index': '0' }).removeClass('active');
                jQuery('#' + targetID).css({ 'z-index': '2' }).addClass('active');
                isAnimated = false;
            });
        });
    });
}
function logScrollPos() {
    log('top : ' + document.body.scrollTop);
    log('left: ' + document.body.scrollLeft);
}
function logEvent(e) {
    for (i in e) {
        log(i + ":" + e[i]);
    }
}
function log(msg) {
    if (!debug) {
        return;
    }
    if (typeof (console) == 'undefined') {
        return;
    }
    console.log(msg);
}
var isFadingOut = false;
var isScrolling = false;

function checkNavigation(recalcOffset, offSet) {
    if (typeof (offSet) == 'undefined') {
        offSet = 0;
    }
    // log('offSet = ' + offSet);
    if (isScrolling == true) {
        isScrolling = false;
        if (videoIsPlaying) {
            e.preventDefault();
            return;
        }
        showAfterScrolling(null, offSet);
    }
}

function showAfterScrolling(e, offSet) {
    //log(document.body.clientHeight);
    $('#navigation-bottom-wrapper, .navigation-center').css({ 'top': (document.body.scrollTop + parseInt(offSet)) + 'px' });
    setActiveChapter(e);
    $('.related, #navigation-main, #navigation-bottom-wrapper, .navigation-center, .navigation-center-bottom').fadeIn(250); // .css({ 'display': 'block' }); //
}
function hideWhileScrolling(e) {
    isScrolling = true;
    if (isFadingOut == false) {
        isFadingOut = true;
        $('.related, #navigation-main, #navigation-bottom-wrapper, .navigation-center, .navigation-center-bottom').css({ 'display': 'none' }); //.fadeOut("slow"); //
        if (!$('.navigation-center-bottom').hasClass('inactive')) {
            $('.navigation-center-bottom').addClass('inactive');
        }
        if (!$('#sidebar').hasClass('inactive')) {
            $('#sidebar, #btn-chapter').addClass('inactive');
            //            $('#sidebar').addClass('inactive');
        }
        isFadingOut = false;
    }
}

function updateChapterPos() {
    $('#boxster > .content').each(function () {
        chapterPos[jQuery(this).index()] = jQuery(this).offset().top;
    });
}

 var activeChapter = 0;
var currentActiveChapter = 0;
function setActiveChapter(e) {
    var i = 8;
    var scrollTop = $(window).scrollTop();
    for (i; i >= 0; i--) {
        if (chapterPos[i] <= scrollTop) {
            activeChapter = i;
            break;
        }
    }
    if (currentActiveChapter != activeChapter) {
        currentActiveChapter = activeChapter;
        switch (activeChapter) {
            case 0: track('', 'explore|chaptername-intro'); break;
            case 1: track('', 'explore|chaptername-design'); break;
            case 2: track('', 'explore|chaptername-interior'); break;
            case 3: track('', 'explore|chaptername-drive'); break;
            case 4: track('', 'explore|chaptername-efficiency'); break;
            case 5: track('', 'explore|chaptername-chassis'); break;
            case 6: track('', 'explore|chaptername-comfort'); break;
            case 7: track('', 'explore|chaptername-moreporsche'); break;
            }
    }
    // log('activeChapter = ' + activeChapter + ' | ' + scrollTop);
    $('.chapternavi a').removeClass('active');
    $($('.chapternavi a')[activeChapter]).addClass('active');
}


function setupAudio() {
    if (flashAudio) {
        var params = {
            loop: "false",
            menu: "false",
            quality: "high",
            allowScriptAccess: "always",
            allowfullscreen: "false"
        };
        var flashvars = {
            playlist: 'track1.mp3,track2.mp3,track3.mp3',
            basePath: './flash/tracks/',
            initVolume: (globalVolume / 100.0).toString(),
            playerid: 'flash_jukebox',
            cdnhost: 'http://special.porsche.com/'
        };
        var attributes = {
            id: 'flash_jukebox',
            name: 'flash_jukebox'
        };

        swfobject.embedSWF("./flash/audio.swf", "flash_jukebox", "1", "1", "10.0.0", "", flashvars, params, attributes);
    }
    else if (html5Audio) {
        //$('#flash_jukebox').remove();
        //$('#audio-placeholder').append('<audio id="ws-audio0" loop="true" autoplay="false" controls="true"><source  src="{$webspecialPath}/flash/tracks/track1.mp3" type="audio/mp3" /></audio><audio id="ws-audio1" loop="true" autoplay="false" controls="true"><source  src="{$webspecialPath}/flash/tracks/track2.mp3" type="audio/mp3" /></audio><audio id="ws-audio2" loop="true" autoplay="false" controls="true"><source  src="{$webspecialPath}/flash/tracks/track3.mp3" type="audio/mp3" /></audio><audio id="ws-audio3" loop="true" autoplay="false" controls="true"><source  src="{$webspecialPath}/flash/tracks/track4.mp3" type="audio/mp3" /></audio><audio id="ws-audio4" loop="true" autoplay="false" controls="true"><source  src="{$webspecialPath}/flash/tracks/track5.mp3" type="audio/mp3" /></audio>');
    }
    else {
        // no audio
        $('#flash_jukebox').remove();
        return;
    }

    if (flashAudio) {
        $('.volume > div').each(function () {
            $(this).bind(evnt, function () {
                var idx = $(this).index('.volume > div');
                setVolume(idx + 1);
            });
        });
        $('.audiotrack a').addClass('checked').removeClass('unchecked');
    }
    else if(html5Audio) {
        $('#volumecontrol').remove();
        pauseAllAudio();
    }

    $('.audiotrack a').each(function () {
        $(this).bind(evnt, function () {
            if ($(this).hasClass('isPlaying')) {
                pauseAudio(jQuery(this).index('.audiotrack a'));
                if (html5Audio) {
                    $(this).removeClass('checked');
                }
            }
            else {
                if($(this).hasClass('isPaused')){
                    $('.audiotrack a').removeClass('isPlaying').removeClass('isPaused');
                    continueAudio(jQuery(this).index('.audiotrack a'));
                    $(this).addClass('isPlaying');
                }
                else
                {
                    $('.audiotrack a').removeClass('isPlaying').removeClass('isPaused');
                    $(this).addClass('isPlaying');
                    $(this).removeClass('isPaused');
                    playAudio(jQuery(this).index('.audiotrack a'));
                }
                if (html5Audio) {
                    /* $('.audiotrack a').removeClass('isPlaying checked'); */
                    $(this).addClass('checked');
                }
            }
        });
    });
    $('.more').bind(evnt, function () {
        log($('.volume > .isset').length);
        if ($('.volume > .isset').length < 15) {
            var lastIdx = $('.volume > .isset').length -1;
            setVolume(lastIdx+2)
        }
    });
    $('.less').bind(evnt, function () {
        log($('.volume > .isset').length);
        if ($('.volume > .isset').length > 0) {
            var lastIdx = $('.volume > .isset').length - 1;
            setVolume(lastIdx)
        }
    });

}
function setVolume(idx) {
    $('.volume > div').each(function () {
        if ($(this).index('.volume > div') < idx) {
            if (!$(this).hasClass('isset')) {
                $(this).addClass('isset');
            }
        }
        else {
            if ($(this).hasClass('isset')) {
                $(this).removeClass('isset');
            }
        }
    });
    var diff = (15 - (idx)) * 7;
    if (diff > 100) {
        diff = 100;
    }
    if (diff < 0) {
        diff = 0;
    }
    globalVolume = 100 - diff;
    setVolumeTo(globalVolume);
}

function playAudio(playerID) {
    log('play: #ws-audio' + playerID);
    if (flashAudio) {
        jscall_playTrack((playerID + 1).toString());
    }
    else if (html5Audio) {
        if ($('#ws-audio' + playerID).length > 0) {
            $('#ws-audio' + playerID)[0].play();
        }
    }
}
function continueAudio() {
     if (flashAudio) {
        jsCall_tooglePlayPause();
    }   
}
function pauseAudio(playerID) {
    log('pause: #ws-audio' + playerID);
    //$('audiotrack a.isPlaying')
    $('.audiotrack a.isPlaying').removeClass('isPlaying').addClass('isPaused');
    if (flashAudio) {
        jsCall_tooglePlayPause();
    }
    else if (html5Audio) {
        if ($('#ws-audio' + playerID).length > 0) {
            $('#ws-audio' + playerID)[0].pause();
        }
    }
}
function pauseAllAudio() {
    log('pauseAllAudio');
    var i = 0;
    $('.audiotrack a').each(function () {
        if ($(this).hasClass('isPlaying')) {
            $(this).removeClass('isPlaying').addClass('isPaused');
            pauseAudio($(this).index('.audiotrack a'));
        }
    });
//    for (i; i < 5; i++) {
//        pauseAudio(i);
//    }
}
function setVolumeTo(newVolume) {
    log('setVolume: ' + newVolume);
    if (flashAudio) {
        jsCall_updateVolume((newVolume / 100).toString());
    }
    else if (html5Audio) {
        $('.audiotrack a').each(
            $(this).index('.audiotrack a').volume = newVolume
        );
    }
    if (!html5Video) {
        var vids = {0: "design04-videoplayer_flash",
                    1: "design06-videoplayer_flash",
                    2: "drive03-videoplayer_flash", 
                    3: "drive04-videoplayer_flash",
                    4: "efficiency03-videoplayer_flash",
                    5: "interior02-videoplayer_flash" };
        for (x in vids) {
            flashVideo_updateVolume(vids[x], (globalVolume/100.0));
        }
    }
}
function setupVideo() {
    if(!html5Video){
        $('video').remove();
    }
    $('.playVideo').each(function () {
        $(this).bind(evnt, function () {
            var ID = $(this).attr('id');
            playVideo(ID, $(this).index('.playVideo'));
        });
    });
}

function playVideo(ID, videoIdx) {
    log('PLAY: ' + ID + ' || ' + videoIdx);
    if ((ID == null) || (videoIdx == null)) {
        log('parameter is null');
        return;
    }
    var created = false;
    if (html5Video) {
        if (videoPlayers == null) {
            $('.video-js-box').show();
            videoPlayers = VideoJS.setup("All");
            var i = 0;
            for (i; i < videoPlayers.length; i++) {
                videoPlayers[i].onEnded(function () {
                    var ID = this.element.id.replace('-videoplayer', '-video');
                    log('onEnded.ID = ' + ID);
                    $('#' + ID + ' .video-js-box').fadeOut(500); //.hide();
                    $('#' + ID + '-play').fadeIn(1500); //.show();
                    if ($('.audiotrack a.isPaused').length > 0) {
                        $('.audiotrack a.isPaused').trigger(evnt);
                    }
                    if (hasTouch) {
                        videoID = '';
                        videoIsPlaying = false;
                    //    $('#introvideo-video-skip').trigger('click'); kein intro auf pad
                        showAfterScrolling(null, 0);
                    }
                });
                videoPlayers[i].onPause(function () {
                    videoIsPlaying = false;
                    videoID = '';
                    showAfterScrolling(null, 0);
                });
                videoPlayers[i].onPlay(function () {
                    videoIsPlaying = true;
                    var ID = this.element.id.replace('-videoplayer', '-video');
                    videoID = ID.replace('#', '');
                    //pauseAllAudio();
                    if (hasTouch) {
                        hideWhileScrolling(null);
                        navigateTo(videoID, true);
                    }
                });
            }
            $('.video-js-box').hide();
        }
    }
    else {
        //createVideo(ID.replace('-video-play', '-videoplayer_flash'), $('#' + ID).attr('rel'));
        if (videoPlayers == null) {
            videoPlayers = { 0: false, 1: false, 2: false, 3: false, 4: false, 5: false };
        }
        if (videoPlayers[videoIdx] == false) {
            log('create');
            createVideo(ID.replace('-video-play', '-videoplayer_flash'), $('#' + ID).attr('rel'));
            videoPlayers[videoIdx] = true;
            created = true;
        }
    }

    $('#' + ID.replace('-play', '') + ' .video-js-box').show();

    if (html5Video) {
        if (videoPlayers[videoIdx] == null) {
            log('Video[' + videoIdx + '] is null.');
            return;
        }
        else {
            setTimeout('videoPlayers[' + videoIdx + '].play();', 500);
        }
    }
    else {
        if ($('.audiotrack a.isPlaying').length > 0) {
            $('.audiotrack a.isPlaying').trigger(evnt);
        }
        var flashID = ID.replace('-video-play', '-videoplayer_flash');
        log(flashID);
        $('#' + ID).fadeOut(250);
        flashVideo_play(flashID, ID);
    }
}

function sharethis(href, encoding) {
   // window.open('http://google.de/', 'share');
   // return;
    var link;
    if (encoding == null)
        link = href.replace('URL', encodeURIComponent(document.location.toString()));
    else
        link = href.replace('URL', document.location.toString());

    link = link.replace('TITEL', document.title);

    window.open(link, 'share');
}

function createVideo(playerID, movie) {
    var videoContainer = $(playerID);
    log(videoContainer);
    var params = {
        loop: "false",
        menu: "false",
        quality: "high",
        allowScriptAccess: "always",
        allowfullscreen: "true",
        wmode: "transparent"
    };
    var flashvars = {
        initVolume: (globalVolume / 100.0).toString(),
        playerid: playerID,
        videoPath: movie,
        controlBar:  playerID.indexOf('introvideo') > -1 ? false : true,
        cdnhost: 'http://special.porsche.com/'
    };
    var attributes = {
        id: playerID,
        name: playerID
    };
    log(flashvars);
    var width = playerID.indexOf('interior') > -1 ? 1024 : 924;
    var height = playerID.indexOf('interior') > -1 ? 576 : 513;
    swfobject.embedSWF("./flash/video.swf", playerID, width.toString(), height.toString(), "10.0.0", "", flashvars, params, attributes);
}

function getFlashObj(objID) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[objID];
    }
    else {
        return document[objID];
    }
}

function swfReady(playerid) {
    log("SWF #" + playerid + " is ready.");
    //log(playerid == 'flash_jukebox');
    if (playerid == 'flash_jukebox') {
        $('.audiotrack a:first').trigger('click');
    }
    else {
        $('#' + playerid.replace('videoplayer_flash', 'video-play')).addClass('isReady');
    }
}

/* flash - audio */
function jsCall_pauseTrack() {
    getFlashObj('flash_jukebox').jsCall_pauseTrack();
    return;
}
function jsCall_nextTrack() {
    getFlashObj('flash_jukebox').jsCall_nextTrack();
    return;
}
function jsCall_prevTrack() {
    getFlashObj('flash_jukebox').jsCall_prevTrack();
    return;
}
function jscall_playTrack(trackID) {
    getFlashObj('flash_jukebox').jscall_play_track('track' + trackID + '.mp3');
    return;
}
function jsCall_updateVolume(vol) {
    getFlashObj('flash_jukebox').jsCall_updateVolume(vol);
    return;
}
function jsCall_tooglePlayPause() {
    getFlashObj('flash_jukebox').jsCall_tooglePlayPause();
    return;
}
function jsCall_updateTracklist(csvTracklist) {
    getFlashObj('flash_jukebox').jsCall_updateTracklist(csvTracklist);
    return;
}
function jsCall_playingComplete(nextTrackNumber) {
    log('jsCall_playingComplete, next is ' + nextTrackNumber + toString());
    //$('.audiotrack a').removeClass('isPlaying');
    $('.audiotrack a.isPlaying').removeClass('isPlaying');
    if ($('.audiotrack a').length > nextTrackNumber) {
        $($('.audiotrack a')[nextTrackNumber]).addClass('isPlaying');
    }
    return;
}

/* flash - video */
function flashVideo_play(playerID, buttonID) {
    var obj = getFlashObj(playerID);
    if ($('#' + playerID.replace('videoplayer_flash', 'video-play')).hasClass('isReady')) {
        $('#' + playerID).fadeIn(250);
        log('playVideo')
        obj.playVideo();
        flashVideo_updateVolume(playerID, (globalVolume / 100.0));
        $('#' + playerID.replace('videoplayer_flash', 'video-play')).removeClass('isReady');
    }
    else {
        setTimeout('flashVideo_play("' + playerID + '","' + buttonID + '")', 500);
    }
}
function flashVideo_pause() {
    getFlashObj(playerID).pause();
}

function flashVideo_destroy() {
    getFlashObj(playerID).destroy();
}

function flashVideo_updateVolume(playerID, volume) {
    obj = getFlashObj(playerID);
    if (obj != null) {
        if (typeof(obj.updateVolume) == 'function') {
            obj.updateVolume(volume);
        }
    }
}

function playingComplete(playerID) {
    log('playingComplete ' + playerID);
    $('#' + playerID).parent().fadeOut(250); // .addClass('hideFlash'); //


    if (playerID == 'introvideo-videoplayer_flash') {
        track('', 'intro|complete');
        $('#introvideo-video-skip')[0].click();
    }
    else {
        $('#' + playerID.replace('videoplayer_flash', 'video-play')).fadeIn(250);
    }

    if ($('.audiotrack a.isPaused').length > 0) {
        $('.audiotrack a.isPaused').trigger(evnt);
    }
}


function superTrack() {
    $("#btn-boxster").bind(evnt, function (e) { track('', 'explore|click'); });

    $("#btn-gallery").bind(evnt, function (e) { track('', 'gallery|click'); });

    $("#btn-dialog").bind(evnt, function (e) { track('', 'dialog|click'); });
    $("#btn-chapter").bind(evnt, function (e) { track('', 'chapter|open'); });
    
    $("#navigation-bottom-strap").bind(evnt, function (e) { track('', 'meta_navigation|open'); });


    $("#design06-roof-play").bind(evnt, function (e) { track('', 'explore|sildeshow|open_roof'); });

    $("#design04-video-play").bind(evnt, function (e) { track('', 'explore|video|spoiler'); });
    $("#design06-video-play").bind(evnt, function (e) { track('', 'explore|video|open_roof'); });
    $("#interior02-video-play").bind(evnt, function (e) { track('', 'explore|video|interieur'); });
    $("#drive03-video-play").bind(evnt, function (e) { track('', 'explore|video|engine'); });
    $("#drive04-video-play").bind(evnt, function (e) { track('', 'explore|video|transmission'); });
    $("#efficiency03-video-play").bind(evnt, function (e) { track('', 'explore|video|sailing'); });

    $("#interior04 .browse").bind(evnt, function (e) { track('', 'explore|slideshow|seats'); });
    $("#design01-360 .browse").bind(evnt, function (e) { track('', 'explore|360degree'); });

    $("#gal01").bind(evnt, function (e) { track('', 'wallpaper_panel|open|01'); });
    $("#gal02").bind(evnt, function (e) { track('', 'wallpaper_panel|open|02'); });
    $("#gal03").bind(evnt, function (e) { track('', 'wallpaper_panel|open|03'); });
    $("#gal07").bind(evnt, function (e) { track('', 'wallpaper_panel|open|07'); });
    $("#gal04").bind(evnt, function (e) { track('', 'wallpaper_panel|open|04'); });
    $("#gal05").bind(evnt, function (e) { track('', 'wallpaper_panel|open|05'); });
    $("#gal06").bind(evnt, function (e) { track('', 'wallpaper_panel|open|06'); });
    $("#gal09").bind(evnt, function (e) { track('', 'wallpaper_panel|open|09'); });
    $("#gal10").bind(evnt, function (e) { track('', 'wallpaper_panel|open|10'); });
    $("#gal13").bind(evnt, function (e) { track('', 'wallpaper_panel|open|13'); });
    $("#gal14").bind(evnt, function (e) { track('', 'wallpaper_panel|open|14'); });
    $("#gal15").bind(evnt, function (e) { track('', 'wallpaper_panel|open|15'); });
    $("#gal16").bind(evnt, function (e) { track('', 'wallpaper_panel|open|16'); });
    $("#gal17").bind(evnt, function (e) { track('', 'wallpaper_panel|open|17'); });
    $("#gal18").bind(evnt, function (e) { track('', 'wallpaper_panel|open|18'); });
    $("#gal19").bind(evnt, function (e) { track('', 'wallpaper_panel|open|19'); });
    $("#gal20").bind(evnt, function (e) { track('', 'wallpaper_panel|open|20'); });
    $("#gal21").bind(evnt, function (e) { track('', 'wallpaper_panel|open|21'); });
    $("#gal22").bind(evnt, function (e) { track('', 'wallpaper_panel|open|22'); });
    $("#gal23").bind(evnt, function (e) { track('', 'wallpaper_panel|open|23'); });
    $("#gal24").bind(evnt, function (e) { track('', 'wallpaper_panel|open|24'); });
    $("#gal25").bind(evnt, function (e) { track('', 'wallpaper_panel|open|25'); });
    $("#gal26").bind(evnt, function (e) { track('', 'wallpaper_panel|open|26'); });
    $("#gal27").bind(evnt, function (e) { track('', 'wallpaper_panel|open|27'); });
    $("#gal28").bind(evnt, function (e) { track('', 'wallpaper_panel|open|28'); });
    $("#gal29").bind(evnt, function (e) { track('', 'wallpaper_panel|open|29'); });
    $("#gal30").bind(evnt, function (e) { track('', 'wallpaper_panel|open|30'); });
    $("#gal31").bind(evnt, function (e) { track('', 'wallpaper_panel|open|31'); });
    $("#gal32").bind(evnt, function (e) { track('', 'wallpaper_panel|open|32'); });
    $("#gal33").bind(evnt, function (e) { track('', 'wallpaper_panel|open|33'); });
    $("#gal34").bind(evnt, function (e) { track('', 'wallpaper_panel|open|34'); });

    $("#link-porsche02-head").bind(evnt, function (e) { track('', 'explore|more_porsche|carconfigurator'); });
    $("#porsche02-01").bind(evnt, function (e) { track('', 'explore|more_porsche|exclusive'); });
    $("#porsche02-04").bind(evnt, function (e) { track('', 'explore|more_porsche|ptc'); });


    $("a.followfacebook").bind(evnt, function (e) { track('', 'meta_navigation|facebookfanpage'); });
    $("a.followyoutube").bind(evnt, function (e) { track('', 'meta_navigation|youtubefanpage'); });

    $("#fbshare").bind(evnt, function (e) { track('', 'wallpapershare|facebook'); });
    $("#twshare").bind(evnt, function (e) { track('', 'wallpapershare|twitter'); });

    $("#legalnotice a").bind(evnt, function (e) { track('', 'meta_navigation|imprint-legal_notice'); });

    $(".sharemail").bind(evnt, function (e) { track('', 'meta_navigation|tellafriend'); });
    $(".sharefacebook").bind(evnt, function (e) { track('', 'meta_navigation|facebook'); });
    $(".sharetwitter").bind(evnt, function (e) { track('', 'meta_navigation|twitter'); });
    $(".sharedigg").bind(evnt, function (e) { track('', 'meta_navigation|sharedigg'); });
    $(".sharegmail").bind(evnt, function (e) { track('', 'meta_navigation|sharegmail'); });
    $(".sharegoogle").bind(evnt, function (e) { track('', 'meta_navigation|google'); });
    $(".shareyahoo ").bind(evnt, function (e) { track('', 'meta_navigation|yahoo'); });
    $(".sharedelicious").bind(evnt, function (e) { track('', 'meta_navigation|delicious'); });

    $("#introvideo-video-skip").bind(evnt, function (e) { track('', 'intro|skip'); });

    $("#navigation-bottom .col1 a:first").bind(evnt, function (e) { track('', 'meta_navigation|boxster modelsite'); });
    $("#navigation-bottom .col1 a:last").bind(evnt, function (e) { track('', 'meta_navigation|boxster s modelsite'); });

    $('.audiotrack a').each(function () {
        $(this).bind(evnt, function (e) {
            track('', 'meta_navigation|tracks_play|' + $(this).attr('rel'));
        });
    });

    $('.colorator-navi a').each(function () {
        $(this).bind(evntHover, function (e) {
            track('', 'explore|colorator');
        });
    });

}

function gotoUrlNewWinSizeScrollable(s, sWidth, sHeight) {
    sWidth += 20;
    sHeight += 25;
    newWin = window.open(s, "carConfigurator", "width=" + sWidth + ",height=" + sHeight + ",left=40,top=40,dependent=yes,location=no,resizable=no,scrollbars=yes,status=no");
}

