
function set_lb_close_position(){
      $('a.lb_hidden').each(function(){         
        $(this).css('left',Math.ceil($('.lb_main').offset().left + $('.lb_main').width() - 30) + 'px');
        $(this).removeClass('lb_hidden');
    });
}

function embed_guided_tour(group){
    if(!check_flash()) return;
    var groups = {
        'parents' : 'Eltern',
        'teachers' : 'Lehrer',
        'students' : 'Schueler'
    };
    
    if(typeof(groups[group]) == "undefined"){
        alert('Es ist ein Fehler aufgetreten');
        return false;
    }
    
    var flashVars = { 
            movieURL: bm_options.path_to_theme + "/_swf/tour" + groups[group] + ".swf",
            autoplay : 'true'
        };
    
    var params = {
            quality: "high",
            allowscriptaccess: "domain",
            menu: "false",
            wmode: "none"
        };
    var attributes = {
            id: "usability_flash_help",
            name: "GlobalHelp"
        };

                                         
     swfobject.embedSWF(bm_options.path_to_theme + "/_swf/bm_swfplayer.swf",
                   "guided_tour", "640px", "360px",
                   "10.0",
                   false, flashVars, params, attributes);                   
}

function embed_intro_movie(){
    if(!check_flash()) return;
     var params = {
         quality: "high",
         allowscriptaccess: "domain",
         menu: "false",
         wmode: "none"
     }

     var flashVars = {
         movieURL : bm_options.path_to_theme + "/_swf/intro_movie_de.swf",
         autoplay : 'true'
     }
     swfobject.embedSWF(bm_options.path_to_theme + "/_swf/bm_swfplayer.swf", "intro_movie", "638px", "359px", "9.0.0", false, flashVars, params );
}

function embed_iphone_movie(){
    if(!check_flash()) return;
     var params = {
            base : bm_options.domain + bm_options.path_to_theme + "/_swf/",
            salign : "tl",
            quality: "high"
     }

     var flashVars = {
         movieURL : bm_options.path_to_theme + "/_swf/rechentrainer.flv",
         autoplay : 'true'
     }

     $('#lightbox #lb_main').html('').css({'width':'900px', 'background-image':'none'});
     swfobject.embedSWF(bm_options.path_to_theme + "/_swf/i-phone.swf", "iPhone_vid_box", "900px", "560px", "9.0.0", "", flashVars, params );
}

/* flex schnupperaufgaben */
var flexapp;
var jsReady = false;

function closeWindow(){
      window.close();
      return false;
}

function isReady() {
    return jsReady;
}

function open_register(role) {
    if($.browser.version == '8.0') {
	window.open(bm_options.domain + '/bm/register/' + role)
	window.close();
        return false
    }else{
        window.opener.location.href = '/bm/register/' + role;
	window.close();
        return false;
    }
}

function open_register_students() {
    open_register('student');
}

function open_register_parents() {
    open_register('parents');
}

function open_register_teacher() {
    open_register('teacher');
}



function openKemPopup(speaking_id){
    if (navigator.appName.indexOf ("Microsoft") !=-1) {
         flexapp = window["bm_exerciseseries"];
    } else {
         flexapp = document["bm_exerciseseries"];
    }

    var href = bm_options.explain_url;
    if(speaking_id){
        href = href + speaking_id + '?nohead=ws';
    }

    $('#lightbox').html('<div class="bm-explain-title"><h1>bettermarks Erklärung</h1><p><a href="#" rel="close-lightbox" class="lb_close bm-explain-close" onclick="closeLightbox();">Abbrechen</a></p></div>' +
                            '<iframe src="' + href + '" class="bm-explain" ></iframe>').fadeIn($.animationSpeed);

}

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    
    return vars;
}

function check_flash(){
    try{
        if($.cookie('has_flash') != 1){
            $.get('/bm/open/flash-warning', function(data){
                $('#lightbox').html($(data).html()).enhance();
           });
           return false;
        }else{
            return true;
        }
    }catch(e){
        /* better return true than show flash warning by mistake */
        return true;
    }
}

function bm_kem_print(speaking_id) {
    window.open(bm_options.kempdf + '/6/' + speaking_id + '.pdf', 'kempdf');
}

$.Enhancer.add(function(){
    $('div.bm-teasers h3').filter(':not(.h3-parsed)').each(function(){        
        if($(this).height() > 26){
            $(this).addClass('bm-title-two-row');
            $('p',$(this).parent()).addClass('bm-teaser-two-row');
        }
    }).addClass('h3-parsed');

    $('div.bm-teasers div.bm-teaser.bm-box').filter(':not(.bmclck)').each(function(){        
        $(this).click(function(){
            window.location.href= $('a.bm-teaser-link',$(this)).attr('href');
        });
    }).addClass('bmclck');

      $('a.bm-teaser-exs:not(.jteaser)', this).bind('click',function() {
        if(swfobject.hasFlashPlayerVersion(bm_min_flex_version)){
          h = parseInt(screen.availHeight);
          w = parseInt(screen.availWidth);
          var url = bm_options.sneak_preview_url + '/' + $(this).attr('id');
          var parameters = "width= "+ w + ",height=" + h + ",scrollbars=no,resizable=no,toolbar=no";
          window.open(url, 'sneakpreview', parameters);
        } else {
          $('#lightbox').gcsNavigate({
            url: '/bm/open/flash-warning',
            history: false
          });
        }
        return false;
    }).addClass('jteaser');
    
    
    $('#add-guest-user, div.bm-teaser_test_for_free a, div.bm-teaser_1c_test_it_for_free a').click(function(){
        if(typeof(bm_options.portal_url) != 'undefined' && bm_options.portal_url != '') {
            window.location.href = bm_options.portal_url + '/bm/add-guest-user/student' +
                                        ($.cookie('campaign') != null
                                                                 ? '?campaign=' + $.cookie('campaign')
                                                                 : '');
            return false;
        }
    });

    window.setTimeout(set_lb_close_position, 1000);

});

$(function() {
	var url_get_params = getUrlVars();
	
	if(typeof(url_get_params['wtkw']) != 'undefined') {
		$.cookie("campaign", url_get_params['wtkw'], {expires: 30});
	} else if(typeof(url_get_params['wt_mc']) != 'undefined') {
		$.cookie("campaign", url_get_params['wt_mc'], {expires: 30});
	}
    
    /*listen for flowplayermovies*/

    $('a.bm-movie-lightbox').each(function(){
        var extend_url = bm_options.flex_static + $(this).attr('href');
        $(this).attr('href', extend_url);
    });

    $('a.bm-movie-lightbox').click(function(){
        var movie_url = $(this).attr('href');
        var movie_class = '';

        if ($(this).hasClass('ration4x3')) {
            movie_class = 'ration4x3';
        }

        $.get('/bm/open/movie-player', function(data){
             $('#lightbox').html($(data).html()).enhance();
             $('#lightbox').fadeIn($.animationSpeed);
             $('#bm_movie_player').addClass(movie_class);

             // Fallback if html is not available
             if (!$.cookie('has_flash')) {
                    var movieWidth = $('#bm_movie_player').css('width');
                    var movieHeight = $('#bm_movie_player').css('height');
                    $('#bm_movie_player').html(
                        '<video  width="' + movieWidth + '" height="' + movieHeight + '" controls="controls" autoplay="autoplay">'
                            +'<source src="'+ movie_url +'">'
                       +'</video>');
                    return;
             }

             flowplayer("bm_movie_player", bm_options.path_to_theme + "/_swf/flowplayer.commercial-3.2.7.swf", {
                  key: bm_options.flowplayer_key,
                  buffering : true,
                  clip: {
                      url:  movie_url,
                      scaling: 'orig',
                      onFinish: function() {
                         closeLightbox();
                      }
                  }
             });
        });


        return false;
    });

    if($('input#password').length > 0) {
        window.setInterval(checkPwField, 1000);
    }

});

function checkPwField() {
    if($('input#password').val() != '') {
       $('input#password').css({backgroundPosition: '100% 100%'});
    }
}

