
// need this for some ie6 specials
//
var is_ie6  =  !!(document.all && navigator.appVersion.indexOf("MSIE 6.")!=-1);

function transmitOfferVars(link, obj, modify_existing_res) {

    if(JQ('#teaserlist').length != 0){
        var id = link.id;

        var position = 0;
        if(id == "teaser-button-left"){
            position = 1;
        }else if(id == "teaser-button-center"){
            position = 2;
        }else if(id == "teaser-button-right"){
            position = 3;
        }else if(id == "teaser-button-hidden"){
            position = 4;
        }
   
        var type = "";
        var type_input = JQ('[name=ctyp]').val();
        if(type_input == "P"){
            type = "PKW";
        }else if(type_input == "L"){
            type = "LKW";
        }

        firstTracker._trackEvent(
            "Benutzerverhalten",
            "Startseite-Topangebote",
            "teaser" + position + "-" + type
            );
    }
    
    var param = "";
    if (obj)
        for (var name in obj)
            if (name != "spci")
                param += (param ? "&" : "") + escape(name) + "=" + escape(obj[name]);
    sx_open_reservation_overlay(param, "topoffer");
    return false;
}

function sx_open_reservation_overlay(param, page) {
    var url = location.protocol + "/\/" + location.host + "/php/res/" + (page || "start_overlay") + "?";
    if (param)
        url += param + "&";
        
    // modified, set modal again back to true, because we need the cancellation event
    //
    tb_show("", url + "TB_iframe&modal=true&width=750&height=370", null);
    
    // we can not use the thickbox build-in-remove because we need
    // to tell the application about the cancellation
    //
    var time        =  new Date().getTime();
    var reset_url   =  "/php/res/resettopoffer?" +  time;
    JQ("#TB_overlay").click(function(){
        JQ.get(reset_url);
        tb_remove();
    });
}

JQ(document).ready(function() {
    if (JQ.browser.opera && parseFloat(JQ.browser.version) < 9.5) {
        JQ('#mainnav > li').each(function(){
            JQ(this).css('width',this.offsetWidth);
        });
    }

    JQ('#sitescontainer').appendTo(document.body).jqm({
        trigger: '#sitelang'
    });

    // add pics for maincontainer to domtree
    JQ("#rinav_a").append("<img src='/common/img/base/rac/re-nav-bot.gif' alt='' id='rinav_ende'/>")
    .prepend("<img src='/common/img/base/rac/re-nav-top.gif' alt='' id='rinav_anfang'/>");

    if (JQ.browser.msie && JQ.browser.version <= 6) {
        var img = document.images;
        for (var i = 0; i < img.length; i++) {
            if (/\.pngJQ/i.test(img[i].src)) {
                img[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img[i].src + "')";
                img[i].src = "/common/img/base/rac/nopx.gif";
            }
        }
    }

    //hack for select inputs in ie
    if (jQuery.browser.msie) {
        if(typeof(JQ('#webform').val()) != 'undefined'){
            select_init();
        }
    }

});



function select_init () {
    // get all selects
    // loop and initialize
    //
    var selects = JQ('#webform select');
    if(typeof selects != 'undefeined'){
        for (var i = 0; i < selects.length; i++) {
            _resizeselect.init(selects[i]);
        }
    }
}


var _resizeselect = {
    obj : new Array(),
    init : function (el) {
        this.obj[el] = new resizeselect (el);
    }
}

function resizeselect (el) {

    this.el = el;
    this.p = el.parentNode;
    this.ht = el.parentNode.offsetHeight;
    var obj = this;
    this.set = false;

    el.onmousedown = function () {
        obj.set_select("mousedown");
    }
    el.onblur = function () {
        obj.reset_select("blur");
    }
    el.onchange = function () {
        obj.reset_select("change");
    }

}

resizeselect.prototype.set_select = function (str) {

    if (this.set) {
        this.set = false;
        return;
    }

    this.el.style.width = "auto";
    this.el.style.position = "absolute";
    this.p.style.height = this.ht + "px";
    this.p.style.zIndex = 100;
    this.set = true;
    this.el.focus();
}

resizeselect.prototype.reset_select = function (str) {
    this.el.style.width = "";
    this.el.style.position = "";
    this.p.style.height = "";
    this.p.style.zIndex = 1;
    this.set = false;
    window.focus();
}




function jump_to_anchor(){
    if(typeof(JQ('#toiContent').val()) != 'undefined'){
        var url=window.location;
        var anchor = url.hash;
        if(anchor != null && anchor != ''  && typeof anchor != 'undefined'){
            document.location=document.location;
        }
    }
}

// Fenster zentriert oeffnen
// Parameter: "url",breite (number),hoehe (number),[optional scrollbalken, default = "yes"]
var neufenster = false;
function neuWin(page, br, ho) {
    var scr = typeof arguments[3] != "undefined" ? arguments[3] : "yes";
    var breit = screen.availWidth;
    var hoch = screen.availHeight;
    var winPosX = breit/2 - br/2;
    var winPosY = hoch/2 - ho/2;
    neufenster = open(page,"","resizable=yes,scrollbars="+scr+",width="+br+",height="+ho+",left="+winPosX+",top="+winPosY);
    neufenster.focus();
}

function toggleElement(o) {
    o.style.display = o.style.display != "block" ? "block" : "none";
}

/*
 * Use this function for toggling elements using jquery
 * elem = element(id) to be toggled
 */
function toggleElementById(elem){
    JQ('#' + elem).toggle();
}

// in case client is ie6
// navi divs are unable to lay over selects
// therefore hide all selects owning class "sx-cards-hide-in-ie6"
//
function openSubNav(o) {
    if (is_ie6) JQ("select.sx-cards-hide-in-ie6").css("visibility","hidden");
    if (is_ie6) o.getElementsByTagName('ul')[0].style.display = "block";
}
function closeSubNav(o) {
    if (is_ie6) JQ("select.sx-cards-hide-in-ie6").css("visibility","visible");
    if (is_ie6) o.getElementsByTagName('ul')[0].style.display = "none";
    
}


// decrypt helper function
function decryptCharcode(n,start,end,offset)	{
    n = n + offset;
    if (offset > 0 && n > end)	{
        n = start + (n - end - 1);
    } else if (offset < 0 && n < start)	{
        n = end - (start - n - 1);
    }
    return String.fromCharCode(n);
}
// decrypt string
function decryptString(enc,offset)	{
    var dec = "";
    var len = enc.length;
    for(var i=0; i < len; i++)	{
        var n = enc.charCodeAt(i);
        if (n >= 0x2B && n <= 0x3A)	{
            dec += decryptCharcode(n,0x2B,0x3A,offset);	// 0-9 . , - + / :
        } else if (n >= 0x40 && n <= 0x5A)	{
            dec += decryptCharcode(n,0x40,0x5A,offset);	// A-Z @
        } else if (n >= 0x61 && n <= 0x7A)	{
            dec += decryptCharcode(n,0x61,0x7A,offset);	// a-z
        } else {
            dec += enc.charAt(i);
        }
    }
    return dec;
}
// decrypt spam-protected emails
function linkTo_UnCryptMailto(s)	{
    location.href = decryptString(s,3);
}


