// reference to the popup window
var winId;

function openWindow(inURL, inWindowName, inAttributes) {
    var theWindowName = inWindowName ? inWindowName : 'Popup';
    var theAttributes = inAttributes;

    if(!theAttributes) {
        theAttributes =
        'left=50,top=50,width=800,height=600,menubar=yes,status=yes,scrollbars=yes,resizable=yes';
    }
    theWindow = open(inURL, theWindowName, theAttributes);
    theWindow.focus();
}


function showPopupOrder(form, target) {
	if (winId){
		winId.close();
	}
	features="height=750,width=650,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes, resizable=yes";
	winId=window.open('','popup',features); // open an empty window
    winId.focus();

	//This is an emulation of the action link on the hidden form being clicked and submitted.
	document.getElementById(form+':'+target).onclick(null);
}

function showCatalogPopup1(url) {
	if (winId){
		winId.close();
	}
	features="height=805,width=1024,status=no,toolbar=no,menubar=no,location=no,scrollbars=no, resizable=no";
	winId=window.open(url,'popupCat',features); // open an empty window
    winId.focus();
}

function getCatalogURL(catid,pn) {
	var pn1;
	if (!catid){
		alert("katid darf nicht Null sein!");
	}
	if (!pn){
		pn1 = 1;
	}
	else{
		pn1 = pn;
	}
	var url = "/medias/onlinekataloge/" + catid + "/index.html?onlinekatpage=" + pn1;
	return url;
}

function openCalendarPopup(pageid) {
    var WIDTH  = 984;
    var HEIGHT = 704;
    var LEFT   = -170;
    var TOP    = -100;
    var url    = 'pages/adventCalendar.jsf?pageid=' + pageid;
    openLayer(url, WIDTH, HEIGHT, LEFT, TOP);
}

function showCatalogPopup(id,catid,pn){
	var url = getCatalogURL(catid,pn);
	showCatalogPopup1(url);
}

function openColorImageLayer(link, width, height, left, top){
    var colorNameSelector = 'product';
    var form = jQuery(link).parents().map(function () {if (jQuery(this).hasClass(colorNameSelector)) return this;});
    var aColor = form.find("select.wbVariantAttribute1").val();
    var aURL = link.href;
    if (aColor != 0 && aColor != undefined){
        aURL += "&color=" + encodeURI(aColor);
    }
    openLayerLink(link, width, height, left, top, aURL);
}

function openReplacementView(link, width, height, left, top){
    var aPK = jQuery(".productDetailsForm .pk").val();
    var aQuantity = jQuery(".productDetailsForm .select_quantity").val();
    var aURL = "pages/replacementProduct.jsf?productid=" + aPK + "&quantity=" + aQuantity;
    openLayerLink(link, width, height, left, top, aURL);
}

function openPrintView(element, selector, inWidth, inHeight, top){
    
    var size         = jQuery(selector + " .wbVariantAttribute2").val();
    var color        = jQuery(selector + " .wbVariantAttribute1").val();
    var sleeveLength = jQuery(selector + " .wbVariantAttribute3").val();
    var cup          = jQuery(selector + " .wbVariantAttribute4").val();
    var quantity     = jQuery(selector + " .select_quantity").val();
    var article      = jQuery(selector + " .article").text();

    var url = element.href;

    if (size != ''){
        url += '&size=' + size;
    }
    if(cup != null){
        url += '&cup=' + cup;    
    }

    if (color != ''){
        url += '&color=' + color;
        }
     
    if (sleeveLength != null){
        url += '&sleeveLength=' + sleeveLength;
        }

    url += '&quantity=' + quantity;

    if(article != ''){
        url += "&article=" + article;
    }

    openLayerLink(element, inWidth, inHeight, 'null', top, url);
}

function openLayerLink(link, width, height, left, top, url){

    var aLayerWidth = (typeof width != 'undefined' && width != 'null') ? width : 'null';
    var aLayerHeight = (typeof height != 'undefined' && height != 'null') ? height : 'null';
    var aLayerLeft = (typeof left != 'undefined' && left != 'null') ? left : 'null';
    var aLayerTop = (typeof top != 'undefined' && top != 'null') ? top : 'null';
    var aContentOffsetTop = getCoveredContents().offset().top;

    if (aLayerTop == 'auto'){
        var anOffset = aLayerHeight != 'null' ? parseInt(aLayerHeight) / 2 : 250;
        aLayerTop =  jQuery(link).offset().top - aContentOffsetTop - anOffset;// use half layer height as offset instead of fixed offset of 250;

        // move layer up, if parts of layer are not visible
        var aScrollTop = jQuery(window).scrollTop();
        var aWindowHeight = jQuery(window).height();
        if (aLayerTop != 'null' && aLayerTop > 0 && aLayerHeight != 'null' && (parseInt(aLayerTop) + parseInt(aLayerHeight) + aContentOffsetTop) > (aScrollTop + aWindowHeight)){
            aLayerTop =  aScrollTop + aWindowHeight  - parseInt(aLayerHeight) - aContentOffsetTop - 10;
        }
        if (aLayerTop < 0){
            aLayerTop = 0;
        }

    } else if(aLayerTop == 'shirtAutoPos') {
        aLayerTop =  jQuery(link).offset().top - aContentOffsetTop + 40;
        if (aLayerTop < 0){
            aLayerTop = 0;   
        }
    }
    var aURL = (typeof url != 'undefined') ? url : link.href;

    openLayer(aURL, aLayerWidth, aLayerHeight, aLayerLeft, aLayerTop);
}

function layer(pageID, width, height, left, top){
    openLayer('pages/layerPage.jsf?pageid=' + pageID, width, height, left, top);
}

function prefixURL(inURL){
    //make url absolute if it is not
    if (inURL.substr(0,1) != "/" && inURL.substr(0,4) != 'http'){
        return getAppRoot() + inURL;
    }
    inURL = window.location.protocol + inURL.split(':')[1];  
    return inURL;
}

function openLayer(url, width, height, left, top){

    url = prefixURL(url);

    var aContent = getCoveredContents();

    var aLayerWidth  = (typeof width != 'undefined' && width != 'null') ? parseInt(width) : aContent.width() - 150;
    var aLayerHeight = (typeof height != 'undefined' && height != 'null') ? parseInt(height) : 525;
    var aLayerTop    = aContent.offset().top + ((typeof top != 'undefined' && top != 'null') ? parseInt(top) : 0);
    var aLayerLeft   = aContent.offset().left + ((typeof left != 'undefined' && left != 'null') ? parseInt(left) : 15);

    //adjust layer properties
    getLayer(true).css('width',   aLayerWidth)
                  .css('height',  aLayerHeight)
                  .css('left',    aLayerLeft)
                  .css('top',     aLayerTop)
                  .attr('width',  aLayerWidth)
                  .attr('height', aLayerHeight)
                  .attr("src",    url)
                  .fadeIn(600);

    //adjust cover properties
    getOverlay(true).css('left',    aContent.offset().left)
                    .css('top',     aContent.offset().top)
                    .css('width',   aContent.width())
                    .css('height',  aContent.height())
                    .css("opacity", 0)
                    .show()
                    .fadeTo(400, .75);

    scrollIntoView(getLayer(), 10);
}

function getCoveredContents(){
    return jQuery('.maincontentFrameRight');
}
function getLayer(createIfNotExists){
    //create layer, if not created yet
    var doCreate = (typeof createIfNotExists != 'undefined') ? createIfNotExists : false;
    if (jQuery('#layerPopup').length == 0 && doCreate){
        jQuery('body').append('<iframe id="layerPopup" src="." name="layerPopup" style="z-index:100000000; position:absolute; display:none;" scrolling="auto" marginheight="0" marginwidth="0" frameborder="0" border="0" />');
    }
    return jQuery('#layerPopup');
}
function getOverlay(createIfNotExists){
    //create overlay, if not created yet
    var doCreate = (typeof createIfNotExists != 'undefined') ? createIfNotExists : false;
    if (jQuery('#overlay').length == 0 && doCreate){
        jQuery('body').append('<div id="overlay"  style="z-index:99999999; position:absolute;background-color:#FFFFFF;">&#160;</div>');
        getOverlay().bind('click', function(){
            closeLayer();
        });
    }
    return jQuery('#overlay');
}

function closeLayerImmediately(url){
    closeLayer(url, 0);
}

function closeLayer(url, duration){
    if(url)
       window.location.href = prefixURL(url);
    var aDuration = (typeof duration != 'undefined') ? duration : 0;//'fast';
    getLayer().fadeOut(aDuration, function(){
        getLayer().remove();
    });
    getOverlay().fadeOut(aDuration).hide();
}

function scrollIntoView(obj, minTopSpace){
    var offsetTop = obj.offset().top;

    if (jQuery(document).scrollTop() > offsetTop){
        window.scrollTo(jQuery(document).scrollLeft(), offsetTop - minTopSpace);
    }
}

function adjustLayerHeight(inShrinkIfNecessary){
    // only access if we have no SSL page or we get a security
    try  {
        jQuery(document).ready(function() {
            if (getLayer() != null){
                var aRealReight = jQuery(document).height();
                var aLayerHeight = getLayer().height();
                if (aLayerHeight < aRealReight){
                    getLayer().height(aRealReight);
                }
                var aBodyHeight = jQuery("body").height();
                var shrinkIfNecessary = typeof inShrinkIfNecessary != 'undefined' && inShrinkIfNecessary == 'true';
                if (shrinkIfNecessary && aLayerHeight > aBodyHeight){
                    getLayer().height(aBodyHeight + 10);
                }
            }
        });
    }catch(e){

    }
}


