/**
 * Produktfinder Layer Prodtotype
 * @version 1.0.2
 */

(function($){
    /**
     * basic function for opening layer
     */
    $.fn.pEWCLayer = function (options) 
    {
        options = $.extend({
        eventAction : 'click',
        onopen : function(){},
        onclose : function(){}
    }, options);


    var $link_element = this.each( function(){ return $(this); } );
    var opener = '';
    var elAttr = new Object();
    var relAttr = new Object();

    /**
     * extract rel value and insert into object
     */
    var getLinkAttr = function (e) {
        var r = e.attr('rel');
        r = r.split(';');
        relAttr['position'] = 'absolute';
        relAttr['z-index'] 	= '30';
        if(r.length>0) {
            for (var i = 0; i < r.length; i++) {
                var op = r[i].split('=');
                relAttr[op[0]] = op[1];
            }
        }
    };

    /**
        * get size of window and position of opener elements and insert into object
        */
    var setAttribute = function(e) {
        var main    	= $('#main');
        var pos 	= e.position();
        var offset 	= e.offset();
        elAttr.eTOP 	= pos.top;
        elAttr.eLEFT 	= pos.top;
        elAttr.oTOP 	= offset.top;
        elAttr.oLEFT 	= offset.left;
        elAttr.eWIDTH 	= e.width();
        elAttr.eHEIGHT 	= e.height();
        elAttr.wWIDTH	= $(window).width();
        elAttr.wHEIGHT	= $(window).height();
        elAttr.mWIDTH   = main.width();
        elAttr.mHEIGHT  = main.height();
        getLinkAttr(e);
    };


    var init = function(e) {

        var layer_ID = getLayerID(e);
        e.bind(options.eventAction, function (){
            closeAllLayer(layer_ID);
            opener = $(this);
            if($(layer_ID).css('display') == 'block'){
                hideLayer($(layer_ID));
            } else {
                setAttribute($(this));
                showLayer($(layer_ID));
            }

            $('.c_close').click(function(){hideLayer($(layer_ID));});
            $('#header, #main, #extended #footer').click(function(){hideLayer($(layer_ID));});
            return false;
        });

        /**
         * set new object value, if browser size is changed
         */	
        $(window).resize(function(){
            if($(layer_ID).css('display')=='block') {
                setAttribute(opener);
                setLayerPosition($(layer_ID));
            }
        });
    };

    /**
     * get ID of layer
     */
    var getLayerID = function (e) {
        return e.attr('href');
    };

    /**
     * open layer and create a new DIV element
     */
    var showLayer = function(e) {       
        $('body').append('<div id="c_layer_h"></div>');
        var container_width = (relAttr.width) ? relAttr.width : 'auto';
        e.addClass('c_overlayer');
        e.css({'width':container_width,'float':'left'});
        // Layer position
        setLayerPosition(e);
        e.show();
        // custom function
        options.onopen(e);
    };

    /**
     * layer position
     */
    var setLayerPosition = function (e) {
        var bt_left = 0;
        var elLeft  = elAttr.oLEFT;
        var elTop   = (elAttr.oTOP+(elAttr.eHEIGHT-7));
        var custom_width  = (relAttr.width) ? relAttr.width : 'auto'; 
        // Button overlayer position
        $('#c_layer_h').css({'left': Math.round(elLeft+4)+'px','top':(elTop-5)+'px','width':(elAttr.eWIDTH-8)+'px'});
        // Layer position
        if(elAttr.eWIDTH>e.width()) {
            bt_left = ($.browser.msie == true ) ? ( elAttr.oLEFT  ) : ( elAttr.oLEFT +4 );
            custom_width  = (elAttr.eWIDTH-8)+'px';
        }
        else if(opener.parents('#col2').length>0){
            bt_left = ($.browser.msie == true ) ? Math.round( (elAttr.oLEFT+elAttr.eWIDTH)-e.width() -1 ) : Math.round( (elAttr.oLEFT+elAttr.eWIDTH)-e.width() -4 );
        }
        else {
            //original line
            bt_left = Math.round( ( elAttr.wWIDTH - e.width() ) /2 );
            //Bugfix by Benni
            bt_left = ($.browser.msie == true ) ? ( elAttr.oLEFT  ) : ( elAttr.oLEFT +4 );
            custom_width  = (elAttr.eWIDTH-8)+'px';
        }
        bt_left -= 1; // Bugfix
        e.css({'left':bt_left+'px','top':elTop+'px','width':custom_width});
    };		

    /**
     * closes layer and remove generated DIV
     */
    var hideLayer = function(e) {
        e.hide();
        resetAttr();
        $('#c_layer_h').remove();
        // custom function
        options.onclose(e);
        return false;
    };

    /**
     * reset object value
     */
    var resetAttr = function() {
        elAttr = new Object();
        relAttr = new Object();
    };

    /**
     * close all opened layer before opening new layer
     */
    var closeAllLayer = function (layer_ID) {
        $link_element.each(function(){
            var this_ID = getLayerID($(this));
            if(layer_ID != this_ID) {
                hideLayer($(this_ID));
            }
        });
    };

    return $link_element.each(function(){
        var $e = $(this);
        init($e);
    });

    }
})(jQuery);


    
/**
    * Examble
    * jQuery(function(){
    *		jQuery('a.showlayer').pEWCLayer({
    *			onopen: function (e) {
    *				e.load('produkt_finder.html');// load external file into container
    *			}
    *		});
    * });
    *
    */



/**
    * Updates the number of products of the current selection
    */
(function ($) {
    var unifier = function(l1, l2){
        var newl = []
        $.each(l1, function(i1, v1){
            if ($.inArray(v1, l2) >= 0){
                newl.push(v1);
            }
        });
        return newl;
    };

    PF_updateNoProducts = function() {
        /**
        * Takes to lists as arguments and returns the elements from l1 
        * which are present in l2
        */
        var l;
        var checkedRadioButtons = $('#finderLayer div.logical-and input:checked');
        if (checkedRadioButtons.length === 0){
            l = []
        } else {
            checkedRadioButtons.each(function(i){
                // the "class" attribute ist used to store a comma separated list of products
                var p = $(this).attr('class').split(';'); 
                if (!l){
                    l = p;
                } else {
                    l = unifier(l, p);
                }
            });
        }

    var checkedCheckboxes = $('#finderLayer div.logical-or input:checked');
    if (checkedCheckboxes.length > 0){
        var products = {};
        checkedCheckboxes.each(function(i){
            var p = $(this).attr('class').split(';');
            $.each(p, function(index, value){
                products[value] = true;
            });
        });

    var newl = []
    if (checkedRadioButtons.length > 0){
        $.each(l, function(index, value){
            if (products[value] === true){
                newl.push(value);
            }
        });
    } else {
        $.each(products, function(name, value){
            newl.push(name);
        });
    }
    l = newl;
}

    var noProducts;
    if (checkedRadioButtons.length === 0 && checkedCheckboxes.length === 0){
        noProducts = $('#totalprod').attr('class');
    } else {
        noProducts = l.length;	
    }
    $('#sum-products').html('<strong>Filter: ' + noProducts + ' Produkte</strong>');
};   
})(jQuery);



jQuery(function(){
    jQuery('a.showlayer').pEWCLayer();
    
    var productfinder_button = $('#productfinderbutton');
    
    productfinder_button.click(PF_updateNoProducts);

    /** Register event handler for the inputs of the product finder */
    $('#finderLayer div.type-check input').click(PF_updateNoProducts);


    /**
    * Reset button
    */
    $('#produkt-finder-reset').click( function(e){
        $('#finderLayer div.type-check input:checked').removeAttr('checked');
        PF_updateNoProducts();
    });
    

    /**
     * Text links which should open the product finder
     */
    $('a.productfinder_link').click(
        
        function(e){
            e.stopPropagation();
            productfinder_button.click();
        }
    );



    $("label.overlabel").overlabel();
});


