function ShowDimensionCubeElement(elId) {
    document.getElementById(elId).style.display = 'block';
}

function HideDimensionCubeElement(elId) {
    document.getElementById(elId).style.display = 'none';
}

function changeElemetCssClass(elId, newClassName) {
    document.getElementById(elId).className = newClassName;
}

function ShowHideDrillSectionBlock(elId, buttonId) {
    var elem = document.getElementById(elId);

    if (elem.style.display == 'block') {
        HideDimensionCubeElement(elId);
        changeElemetCssClass(buttonId, 'fexpand2');
    }
    else if (elem.style.display == 'none') {
        ShowDimensionCubeElement(elId);
        changeElemetCssClass(buttonId, 'fexpand');
    }
}

function openWin(url, winname, w, h, scroller, other) {
    centerX = (screen.availWidth - w) / 2;
    centerY = (screen.availHeight - h) / 2;
    more = 'Width=' + w + ',Height=' + h;
    more += ',left=' + centerX + ',top=' + centerY;
    more += ',screenX=' + centerX + ',screenY=' + centerY;
    more += ',menubar=no, toolbar=no, status=yes, resizeable=no';
    if (scroller) {
        more += ',scrollbars=yes';
    }
    else {
        more += ',scrollbars=no';
    }

    if (other) more += ',' + other;

    win = window.open(url, winname, more);
    win.focus()
}

function ChangePicture(onPlaceId, pictureUrl) {
    document.getElementById(onPlaceId).src = pictureUrl;
}

function openImage(imageId) {
    var title = document.getElementById(imageId).alt;
    var url = document.getElementById(imageId).src.replace(/-2./, "-0.");
    var imgwin = window.open('', 'img', 'top=100,left=100,width=820,height=610,scrollbars=yes,resizable=yes,movable=yes')
    imgwin.focus()
    var HTML = "<html>\r\n<head>\r\n<title>" + title + "</title>\r\n</head>\r\n<body leftmargin=\"0\" topmargin=\"0\">\r\n<img src=\"" + url + "\">\r\n</body>\r\n</html>"
    var doc = imgwin.document
    doc.open("text/html", "replace")
    doc.write(HTML)
    doc.close()
}

function openImageWithSrcAlt(imageSrc, imageAlt) {
    var title = imageAlt;
    var url = imageSrc.replace(/-3./, "-0.");
    var imgwin = window.open('', 'img', 'top=100,left=100,width=820,height=610,scrollbars=yes,resizable=yes,movable=yes')
    imgwin.focus()
    var HTML = "<html>\r\n<head>\r\n<title>" + title + "</title>\r\n</head>\r\n<body leftmargin=\"0\" topmargin=\"0\">\r\n<img src=\"" + url + "\">\r\n</body>\r\n</html>"
    var doc = imgwin.document
    doc.open("text/html", "replace")
    doc.write(HTML)
    doc.close()
}

function setImagesOptimalDimensions(id, maxWH, showNoImg, setMargins) {

    if (id == null || id == "") {
        return;
    }

    var i = 0;

    //var img = document.getElementById('img' + i);
    var img = document.getElementById(id);


    if (img.width > img.height) {
        if (img.width > maxWH) {
            var k = img.width / maxWH;
            if (k > img.height) {
                img.height = 1;
            }
            else {
                img.height /= k;
            }
            img.width = maxWH;
        }
    }
    else {
        if (img.height > maxWH) {
            var k = img.height / maxWH;
            if (k > img.width) {
                img.width = 1;
            }
            else {
                img.width /= k;
            }
            img.height = maxWH;
        }
    }
    if (showNoImg) {
        // check if image is usable, otherwise replace it with noimage
        if (maxW == 74) {
            if ((img.width < 5 || img.height < 5) && img.src != "images/img_s_00.jpg") {
                img.src = "images/img_s_00.jpg";
                img.width = 74;
                img.height = 54;
                return;
            }
        } else if (maxW == 95) {
            if ((img.width < 5 || img.height < 5) && img.src != "images/img_m_00.jpg") {
                img.src = "images/img_m_00.jpg";
                img.width = 95;
                img.height = 69;
                return;
            }
        }
    }

    if (setMargins) {
        var marginH = (maxWH - img.width) / 2;
        var marginV = (maxWH - img.height) / 2;

        img.style.marginLeft = img.style.marginRight = Math.floor(marginH) + 'px';
        img.style.marginTop = img.style.marginBottom = Math.floor(marginV) + 'px';
    }
}

function setImagesOptimalDimensionsWithBackground(id, noImageId, maxW, maxH, showNoImg, setMargins) {
    if (id == null || id == "") {
        return;
    }

    var i = 0;

    //var img = document.getElementById('img' + i);
    var img = document.getElementById(id);
    var imgHeight = getNaturalHeight(img);
    var imgWidth = getNaturalWidth(img);

    //show the insertion's image
    img.style.display = "";

    //hide the "no image" image
    var noImageImage = document.getElementById(noImageId);
    noImageImage.style.display = "none";

    var newImgWidth = imgWidth;
    var newImgHeight = imgHeight;

    if (newImgWidth > maxW) {
        var k = newImgWidth / maxW;
        if (k > newImgHeight) {
            newImgHeight = 1;
        }
        else {
            newImgHeight = newImgHeight / k;
        }
        newImgWidth = maxW;
    }
    if (newImgHeight > maxH) {
        var k = newImgHeight / maxH;
        if (k > newImgWidth) {
            newImgWidth = 1;
        }
        else {
            newImgWidth = newImgWidth / k;
        }
        newImgHeight = maxH;
    }

    // set correct image dimensions
    img.width = newImgWidth;
    img.height = newImgHeight;

    if (setMargins) {
        var marginH = (maxW - newImgWidth) / 2;
        var marginV = (maxH - newImgHeight) / 2;

        if (marginH > 0) { img.style.marginLeft = img.style.marginRight = Math.floor(marginH) + 'px'; }
        if (marginV > 0) { img.style.marginTop = img.style.marginBottom = Math.floor(marginV) + 'px'; }
    }
}

function setImagesOptimalDimensions(id, maxW, maxH, showNoImg, setMargins) {
    if (id == null || id == "") {
        return;
    }

    var i = 0;

    //var img = document.getElementById('img' + i);
    var img = document.getElementById(id);
    var imgHeight = getNaturalHeight(img);
    var imgWidth = getNaturalWidth(img);

    var newImgWidth = imgWidth;
    var newImgHeight = imgHeight;

    if (newImgWidth > maxW) {
        var k = newImgWidth / maxW;
        if (k > newImgHeight) {
            newImgHeight = 1;
        }
        else {
            newImgHeight = newImgHeight / k;
        }
        newImgWidth = maxW;
    }
    if (newImgHeight > maxH) {
        var k = newImgHeight / maxH;
        if (k > newImgWidth) {
            newImgWidth = 1;
        }
        else {
            newImgWidth = newImgWidth / k;
        }
        newImgHeight = maxH;
    }

    // set correct image dimensions
    img.width = newImgWidth;
    img.height = newImgHeight;

    if (showNoImg) {
        // check if image is usable, otherwise replace it with noimage
        if (maxW == 74) {
            if ((img.width < 5 || img.height < 5) && img.src != "images/img_s_00.jpg") {
                img.src = "images/img_s_00.jpg";
                img.width = 74;
                img.height = 54;
                return;
            }
        } else if (maxW == 95) {
            if ((img.width < 5 || img.height < 5) && img.src != "images/img_m_00.jpg") {
                img.src = "images/img_m_00.jpg";
                img.width = 95;
                img.height = 69;
                return;
            }
        }
    }

    if (setMargins) {
        var marginH = (maxW - img.width) / 2;
        var marginV = (maxH - img.height) / 2;

        if (marginH > 0) { img.style.marginLeft = img.style.marginRight = Math.floor(marginH) + 'px'; }
        if (marginV > 0) { img.style.marginTop = img.style.marginBottom = Math.floor(marginV) + 'px'; }
    }
}


// different img property (IE vs other browsers)
function getNaturalHeight(img) {
    if (img.naturalHeight) {
        return img.naturalHeight;
    } else {
        tmpImg = new Image();
        tmpImg.src = img.src;
        return tmpImg.height;
    }
}

// different img property (IE vs other browsers)
function getNaturalWidth(img) {
    if (img.naturalWidth) {
        return img.naturalWidth;
    } else {
        tmpImg = new Image();
        tmpImg.src = img.src;
        return tmpImg.width;
    }
}

function setOptimalWHImageDimesions(id, w, h) {
    var img = document.getElementById(id);

    if (getNaturalHeight(img) > getNaturalWidth(img)) {
        setImagesOptimalDimensions(id, w, h, false, false);
    }
    else {
        setImagesOptimalDimensions(id, w, h, false, false);
    }
}

function setHiddenFieldValue(categoryId, destId, buttonId, whatId, whereId) {
    var what = document.getElementById(whatId);
    var where = document.getElementById(whereId);

    if ((what.value == '' || what.value == 'Was') && (where.value == '' || where.value == 'Wo')) {
        return true;
    }

    var dest = document.getElementById(destId);
    dest.value = categoryId;

    var button = document.getElementById(buttonId);
    button.click();
    return false;
}

function setHiddenFieldValueForSearchType(searchType, destId, buttonId, whatId, whereId, activeTabId, inactiveTabId, inactiveTab2Id) {
    var what = document.getElementById(whatId);
    var where = document.getElementById(whereId);

    var activeTab = document.getElementById(activeTabId);
    var inactiveTab = document.getElementById(inactiveTabId);

    var inactiveTab2 = document.getElementById(inactiveTab2Id);
    var dest = document.getElementById(destId);
    // if (what.value == '' && where.value == '')
    // {
    // //if the what and where are empty set the css classes only
    // activeTab.className = "kaz_on";
    // inactiveTab.className = "brb_off";
    // //inactiveTab2.className = "brb_off"; // Todo Enable Events
    //
    // dest.value = searchType;
    //
    //// if (searchType == 'ev') // Todo Enable Events
    //// {
    //// dest.value = searchType;
    ////
    //// var button = document.getElementById(buttonId);
    //// button.click();
    //// return false;
    //// }
    //
    // return true;
    // }

    dest.value = searchType;

    var button = document.getElementById(buttonId);
    button.click();
    return false;
}

function hideStatusLink() {
    var emptyStatusMsg = "";
    window.status = emptyStatusMsg;
    return true;
}

function scAdClick(icidValue, prop1Value) {
    // sitecatalyst trigger
    var s = s_gi(s_account);
    s.linkTrackVars = 'eVar45,prop1,events,eVar1';
    // ZN: event1 should not be tracked anymore
    //s.linkTrackEvents='event1';
    //s.events = 'event1';
    s.linkTrackEvents = '';
    s.events = '';
    if (icidValue != "") {
        s.eVar45 = icidValue;
    }
    s.prop1 = prop1Value;
    s.tl(this, 'o', 'Anzeige geklickt');
}

//function scAdClick(icidValue, prop1Value, evar12Value) {
//    // sitecatalyst trigger
//    dels();
//    var s = s_gi(s_account);

//    //s.linkTrackVars = 'eVar45,prop1,events,eVar1';
//    // ZN: event1 should not be tracked anymore
//    //s.linkTrackEvents='event1';
//    //s.events = 'event1';
//    //s.linkTrackEvents = '';
//    s.events = '';
//    if (evar12Value != "") {
//        //s.linkTrackVars = 'eVar45,prop1,events,eVar1,eVar12,eVar26,prop14';
//        //s.linkTrackEvents = 'event17';
//        s.events = 'event17';
//        s.eVar12 = evar12Value;
//        s.eVar26 = evar12Value;
//        s.prop14 = s.pageName;
//    }
//    if (icidValue != "") {
//        s.eVar45 = icidValue;
//    }
//    s.prop1 = prop1Value;
//    var linkInternalFiltersTemp = s.linkInternalFilters;
//    s.linkInternalFilters = "javascript:,.";
//    //s.tl(this, 'o', 'Anzeige geklickt');
//    s.t();
//    s.linkInternalFilters = linkInternalFiltersTemp;
//    //s.linkTrackVars = '';
//    //s.linkTrackEvents = '';
//}
function scAdClick(icidValue, prop1Value, evar12Value) {
    // sitecatalyst trigger
    dels();
    var s = s_gi(s_account);
    var dom = document.domain.replace('www.', ''); ;
    //clickout f?r Partnerseiten
    var cm_got_cookie = Get_Cookie(encodeURIComponent(document.location.pathname));
    if (cm_got_cookie == null) {
        Set_Cookie(encodeURIComponent(document.location.pathname), s.pageName, '', '', dom, '');
        s.prop14 = s.pageName;
    } else {
        s.prop14 = cm_got_cookie;
    }
    s.pageName = "Exitpage_" + evar12Value;

    //s.linkTrackVars = 'eVar45,prop1,events,eVar1';
    // ZN: event1 should not be tracked anymore
    //s.linkTrackEvents='event1';
    //s.events = 'event1';
    //s.linkTrackEvents = '';
    s.events = '';
    if (evar12Value != "") {
        //s.linkTrackVars = 'eVar45,prop1,events,eVar1,eVar12,eVar26,prop14';
        //s.linkTrackEvents = 'event17';
        s.events = 'event17';
        s.eVar12 = evar12Value;
        s.eVar26 = evar12Value;
        //s.prop14 = s.pageName;
    }
    if (icidValue != "") {
        s.eVar45 = icidValue;
    }
    s.prop1 = prop1Value;
    var linkInternalFiltersTemp = s.linkInternalFilters;
    s.linkInternalFilters = "javascript:,.";
    //s.tl(this, 'o', 'Anzeige geklickt');
    s.t();
    s.linkInternalFilters = linkInternalFiltersTemp;
    //s.linkTrackVars = '';
}
function dels() {
    for (i = 1; i <= 50; i += 1) {
        s["prop" + i] = "";
        s["eVar" + i] = "";
    }
    s.campaign = "";
    s.events = "";
    s.products = "";
    s.channel = "";
    //s.pageName = "";
}

function scAdPartnerClick(evar12Value) {
    // sitecatalyst trigger
    var s = s_gi(s_account);
    s.linkTrackVars = 'eVar12,eVar26,eVar28,prop14,events,hier1';
    s.linkTrackEvents = 'event17';
    s.events = 'event17';
    s.eVar26 = evar12Value;
    s.prop14 = s.pageName;
    s.eVar12 = evar12Value;
    s.pageName = 'Exitpage_' + evar12Value;
    s.eVar28 = s.prop2 + '/' + s.prop3;
    s.tl(this, 'o', 'Anzeige geklickt');
}

function scInidClick(inidValue) {
    // sitecatalyst trigger
    var s = s_gi(s_account);
    //s.linkTrackVars='eVar12';
    //s.eVar12 = inidValue;
    s.linkTrackVars = '';
    //s.eVar12 = inidValue;
    s.tl(this, 'o', 'Seite element geklickt');
}

function scDrilldownClick(filterTypeValue) {
    // sitecatalyst trigger
    var s = s_gi(s_account);
    s.linkTrackVars = 'prop19';
    s.prop19 = filterTypeValue;
    s.tl(this, 'o', 'Ergebnisfilter Typ');
}

function scDrilldownClickFromButton(filterTypeValue, buttonId) {
    scDrilldownClick(filterTypeValue);

    var button = document.getElementById(buttonId)
    button.click();

    return false;
}

function scSendClick() {
    // sitecatalyst trigger
    var s = s_gi(s_account);

    s.linkTrackVars = 'events,channel';
    s.linkTrackEvents = 'event4';

    s.events = 'event4';
    //s.pageName = 'KA Details_Anbieter kontaktiert';
    s.channel = 'kleinanzeige';

    s.tl(this, 'o', 'Anbieter kontaktiert');
}

function scCL_ButtonCategorySuggest() {
    var s = s_gi(s_account);

    s.linkTrackVars = 'None';
    s.linkTrackEvents = 'None';

    s.tl(this, 'o', 'Button -Kategorie finden');
}

function scSendCategorySelectedEvent(eventName, productArea, categorySubcategoryString) {
    var s = s_gi(s_account);
    // ZN: eVar9, eVar10 should not be tracked anymore
    //s.linkTrackVars = 'products,events,prop9,eVar9,eVar10,pageName';
    s.linkTrackVars = 'products,events,prop9,pageName';
    // ZN: event18 should not be tracked anymore
    //s.linkTrackEvents = 'event18,event25';
    s.linkTrackEvents = 'event25';

    s.events = eventName;
    s.products = productArea + ";" + categorySubcategoryString + ";0;0.00";

    s_doPlugins(s);
    s.tl(this, 'o', 'Link -Kategorie selected');
}

function scINSPreview() {
    var s = s_gi(s_account);
    // ZN: eVar9, eVar10 should not be tracked anymore
    //s.linkTrackVars = 'products,events,prop9,eVar9,eVar10,pageName';
    s.linkTrackVars = 'products,events,prop9,pageName';
    s.linkTrackEvents = 'scView';

    s.events = 'scView';

    s_doPlugins(s);
    s.tl(this, 'o', 'Button -Ins Preview');
}

function scINSCheckoutNew(eVar29String) {
    var s = s_gi(s_account);
    // ZN: eVar9, eVar10 should not be tracked anymore
    //s.linkTrackVars = 'products,events,prop9,eVar9,eVar10,eVar11,eVar29,pageName';
    s.linkTrackVars = 'products,events,prop9,eVar11,eVar29,pageName';
    s.linkTrackEvents = 'scCheckout';

    s.events = 'scCheckout';

    //populate the following variables HERE
    s.eVar29 = eVar29String;

    s_doPlugins(s);
    s.tl(this, 'o', 'Button -Ins Checkout');
}

function scINSCheckoutRegistered(eVar41String, eVar42String, eVar43String, eVar44String) {
    var s = s_gi(s_account);

    // ZN: eVar9,eVar10 should not be tracked anymore
    //s.linkTrackVars = 'products,events,prop9,eVar9,eVar10,eVar11,eVar29,eVar41,eVar42,eVar43,eVar44,pageName';
    s.linkTrackVars = 'products,events,prop9,eVar11,eVar29,eVar41,eVar42,eVar43,eVar44,pageName';
    s.linkTrackEvents = 'scCheckout';

    s.events = 'scCheckout';

    //populate the following variables HERE
    s.eVar41 = eVar41String;
    s.eVar42 = eVar42String;
    s.eVar43 = eVar43String;
    s.eVar44 = eVar44String;

    s_doPlugins(s);
    s.tl(this, 'o', 'Button -Ins Checkout');
}

function scINSCheckoutLogged(eVar29String, eVar41String, eVar42String, eVar43String, eVar44String) {
    var s = s_gi(s_account);

    // ZN: eVar9,eVar10 should not be tracked anymore
    //s.linkTrackVars = 'products,events,prop9,eVar9,eVar10,eVar11,eVar29,eVar41,eVar42,eVar43,eVar44,pageName';
    s.linkTrackVars = 'products,events,prop9,eVar11,eVar29,eVar41,eVar42,eVar43,eVar44,pageName';
    s.linkTrackEvents = 'scCheckout';

    s.events = 'scCheckout';

    //populate the following variables HERE
    s.eVar29 = eVar29String;
    s.eVar41 = eVar41String;
    s.eVar42 = eVar42String;
    s.eVar43 = eVar43String;
    s.eVar44 = eVar44String;

    s_doPlugins(s);
    s.tl(this, 'o', 'Button -Ins Checkout');
}

function scForgotPasswordSend(linkName) {
    var s = s_gi(s_account);

    s.linkTrackVars = 'None';
    s.linkTrackEvents = 'None';

    s.tl(this, 'o', linkName);
}

//function scSendPagenameAndChannel(pageName, channel)
//{
// // sitecatalyst trigger
// var s = s_gi(s_account);
//
// s.linkTrackVars='pageName,channel';
//
// s.pageName = pageName;
// s.channel = channel;
//
// s.tl(this,'o',pageName);
//}

//function scSendPageNameAndChannelFromButton(pageName, channel, buttonId)
//{
// scSendPagenameAndChannel(pageName, channel);
//
// var button = document.getElementById(buttonId)
// button.click();
//
// return false;
//}


function showAllCities(obj) {
    // var linkPosition = $('#' + obj.id).offset({ scroll: false });
    // var linkWidth = $('#' + obj.id).width();

    if ($('.FirstTimeRegions').val() != '') {
        $('#citiesDialog').dialog('open');
        // $('.allcities').css('left', (linkPosition.left + linkWidth) + 'px');
        // $('.allcities').css('top', linkPosition.top + 'px');
    }
    else {
        $('.FirstTimeRegions').val('requested');
        $(".loading").show();
        $('#citiesBody').html("");
        $('#citiesDialog').dialog('open');
        // $('.allcities').css('left', (linkPosition.left + linkWidth) + 'px');
        // $('.allcities').css('top', linkPosition.top + 'px');

        $.ajax({
            type: "POST",
            url: "services/drilldowncities.asmx/GetAllCities",
            dataType: "xml",
            data: "currentUrl=" + window.location,
            processData: false,
            error: function(XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest, textStatus, errorThrown); },
            success: function(xml) {
                var theList = '<ul id="citiesList">';

                $("CityLink", xml).each(function() {
                    url = $("Url", this).text();
                    hits = $("Hits", this).text();
                    name = $("Name", this).text();
                    // Add the title with a named anchor
                    //$("#citiesList").append('<li><a href="' + url + '" onclick="javascript:scDrilldownClick("Regionen");">' + name + '</a><span class="count"> ('+ hits + ')</span></li>');
                    theList += '<li><a href="' + url + '" onclick="javascript:scDrilldownClick(Regionen);">' + name + '</a><span class="count"> (' + hits + ')</span></li>';
                });

                theList += "</ul>";
                $("#citiesBody").append(theList);
                $(".loading").hide();
            }
        });
    }
}

////////////////// Bookmarks //////////////////////////////
function sbOver(text) {
    if (text == '') {
        text = '...';
    } else {
        text = '&nbsp;<strong>' + text + '</strong>';
    }
    document.getElementById('sbText').innerHTML = text;
}

function sbDo(was) {
    sburl = encodeURIComponent(location.href);
    sbtitle = encodeURIComponent(document.title);
    switch (was) {
        case 'delicious':
            window.open('http://del.icio.us/post?url=' + sburl + '&title=' + sbtitle);
            break;
        case 'wong':
            window.open('http://www.mister-wong.de/index.php?action=addurl&bm_url=' + sburl + '&bm_description=' + sbtitle);
            break;
        case 'blinkList':
            window.open('http://www.blinklist.com/index.php?Action=Blink/addblink.php&Description=&Url=' + sburl + '&Title=' + sbtitle);
            break;
        case 'yahoo':
            window.open('http://myweb2.search.yahoo.com/myresults/bookmarklet?u=' + sburl + '&t=' + sbtitle);
            break;
        case 'yigg':
            window.open('http://yigg.de/neu?exturl=' + sburl + '&exttitle=' + sbtitle);
            break;
        case 'furl':
            window.open('http://www.furl.net/storeIt.jsp?u=' + sburl + '&t=' + sbtitle);
            break;
        case 'oneview':
            window.open('http://beta.oneview.de:80/quickadd/neu/addBookmark.jsf?URL=' + sburl + '&title=' + sbtitle);
            break;
        case 'folkd':
            window.open('http://www.folkd.com/submit/page/' + sburl);
            break;
        case 'linkarena':
            window.open('http://linkarena.com/bookmarks/addlink/?url=' + sburl + '&title=' + sbtitle + '&desc=&tags=');
            break;
        case 'google':
            window.open('http://www.google.com/bookmarks/mark?op=add&hl=de&bkmk=' + sburl + '&title=' + sbtitle);
            break;
        case 'webnews':
            window.open('http://www.webnews.de/einstellen?url=' + sburl + '&title=' + sbtitle);
            break;
        case 'tausendreporter':
            window.open('http://tausendreporter.stern.de/submit.php?url=' + sburl);
            break;
    }
}

function ChangeCheckbox() {
    if ($(".sendmeacopylabel").val() == 'true') {
        $(".sendmeacopylabel").val('false');
        $(".checkboxReplacement").addClass('uncheck');
    }
    else {
        $(".sendmeacopylabel").val('true');
        $(".checkboxReplacement").removeClass('uncheck');
    }
}