
// Inspired by base2 and Prototype
(function(){
    var initializing = false, fnTest = /xyz/.test(function(){
	xyz;
    }) ? /\b_super\b/ : /.*/;

    // The base Class implementation (does nothing)
    this.Class = function(){};

    // Create a new Class that inherits from this class
    Class.extend = function(prop) {
	var _super = this.prototype;

	// Instantiate a base class (but only create the instance,
	// don't run the init constructor)
	initializing = true;
	var prototype = new this();
	initializing = false;

	// Copy the properties over onto the new prototype
	for (var name in prop) {
	    // Check if we're overwriting an existing function
	    prototype[name] = typeof prop[name] == "function" &&
	    typeof _super[name] == "function" && fnTest.test(prop[name]) ?
	    (function(name, fn){
		return function() {
		    var tmp = this._super;

		    // Add a new ._super() method that is the same method
		    // but on the super-class
		    this._super = _super[name];

		    // The method only need to be bound temporarily, so we
		    // remove it when we're done executing
		    var ret = fn.apply(this, arguments);
		    this._super = tmp;

		    return ret;
		};
	    })(name, prop[name]) :
	    prop[name];
	}

	// The dummy class constructor
	function Class() {
	    // All construction is actually done in the init method
	    if ( !initializing && this.init )
		this.init.apply(this, arguments);
	}

	// Populate our constructed prototype object
	Class.prototype = prototype;

	// Enforce the constructor to be what we expect
	Class.constructor = Class;

	// And make this class extendable
	Class.extend = arguments.callee;

	return Class;
    };
})();


var jQueryFAQ = {
    init: function(){
	this.faqbase = jQuery('#faq-base-box');

	if(this.faqbase.length > 0){

	    this.results = new FAQresults();

	    this.selector1 = new FAQselector(1);
	    this.selector2 = new FAQselector(2);

	    this.selector1.nextItem = this.selector2;
	    this.selector1.contentResults = this.results

	    //this.selector2.nextItem = this.selector3;
	    //this.selector2.contentResults = this.results
	    //this.selector2.specHandling = true;

	    this.selector2.nextItem = this.results;

	    this.faqmessages = jQuery('#faq-messages');
	    if(this.faqmessages.length > 0){
		this.faqmsgs = [
		'',
		this.faqmessages.find('.message1').text(),
		this.faqmessages.find('.message2').text(),
		this.faqmessages.find('.message3').text(),
		this.faqmessages.find('.message4').text()
		];
	    }

	    this.selector1.parentObj = this;
	    this.selector2.parentObj = this;
	    //this.selector3.parentObj = this;
	    this.results.parentObj = this;

	    var currentURL = location.href;
	    var relpart = currentURL.match(/\/faq\/(.*)\/faq\/(.*).html/);

	    if(relpart){
		this.selector1.updateMenuState(relpart[1]);
		this.selector2.updateMenuState(relpart[1]);
	    } else {
		this.selector2.removeContent(1);
	    }
	    this.initTabMenu();

	}

	new FAQtopten();
    },
    initTabMenu: function(){
	var localSelf = this;
	var elements = jQuery('#faq-base-box > .mootabs_title > li > a');
	var lielements = jQuery('#faq-base-box > .mootabs_title > li');

	elements.bind('click', function(e){
	    lielements.removeClass('active');
	    jQuery(this).parent().addClass('active');

	    localSelf.getTabEventContent(jQuery(this).attr('href'));

	    localSelf.selector2.removeContent(1);
	    return false;
	});
    },
    getTabEventContent: function (url) {
	var localSelf = this;
	localSelf.nextItem = this.selector1;

	localSelf.nextItem.showWait();

	var urlTyped = '';

	if(url.match(/\?/)){
	    urlTyped = url+'&type=65&kbshop_action=99';
	} else {
	    urlTyped = url+'?type=65&kbshop_action=99';
	}

	if(this.nextItem && this.nextItem.nextItem){
	    this.nextItem.nextItem.removeContent(this.itemNumber+1);
	}
	var nextContent = localSelf.nextItem.selector;

	jQuery.ajax({
	    url: urlTyped,
	    success: function(html){
		nextContent.html(html);
		if(localSelf.nextItem){
		    localSelf.nextItem.addEvents();
		    localSelf.nextItem.hideWait();
		    if(localSelf.nextItem.selector.find('ul > li > a').length < 1){
			localSelf.nextItem.removeContent(4);
		    }
		}
	    },
	    error: function(){
		localSelf.hideWait();
		alert('Request Error: Bitte laden Sie die Seite neu');
	    }

	});

    }
}


/**
 * FAQ Selector
 */
var FAQselector = Class.extend({

    options: {
    },

    init: function(itemNumber,options){
	//this.setOptions(options);
	this.itemNumber = itemNumber;

	// identify select criteria boxes
	this.selector = jQuery('#criteria-selector-' + this.itemNumber);

	// identify spiner
	this.wait = jQuery('.criteria-selector-title-' + this.itemNumber + ' .wait');

	this.hideWait();
	this.addEvents();

    },

    updateMenuState: function(relpart){
	var elements  = this.selector.getElements('li a');
	var actel;
	elements.each(function(element){
	    var currenthrefmatch = element.attr('href').match(/\/faq\/(.*).html/);
	    if(relpart.test('^'+currenthrefmatch[1]+'($|\/)')){
		actel = element;
	    }

	});
//console.log(typeof(actel));
	if(typeof(actel)=='element'){
	    actel.addClass('active');
	    ulparentelement = actel.getParent().getParent();
	    if(ulparentelement.getStyle('display')=='none'){
		ulparentelement.getParent().addClass('active');
		ulparentelement.css('display','block');
	    }
	}

    },

    addEvents: function(){
	var elements  = this.selector.find('li a');
	var localSelf = this;
	var firstitem = false;

	elements.bind('click', function(e){
	    elements.removeClass('active');
	    jQuery(this).addClass('active');


	    if(jQuery(this).hasClass('firstItem')){
		localSelf.getContent(jQuery(this).attr('href'),true);
	    } else {
		localSelf.getContent(jQuery(this).attr('href'),false);
	    }
	    return false;
	});


	var firstlevelMenuItems = this.selector.find('ul li.topitem');
	var subMenuBoxes = this.selector.find('ul ul');

	if(firstlevelMenuItems.length > 0){
	    subMenuBoxes.each(function(el){
		jQuery(this).css('display','none');
	    });
	    firstlevelMenuItems.each(function(element){
		jQuery(this).bind('click',function(e){

		    if(jQuery(this).hasClass('active')){
			jQuery(this).removeClass('active');
			jQuery(this).find('ul').css('display','none');
		    } else {

			subMenuBoxes.each(function(el){
			    jQuery(this).css('display','none');
			});
			firstlevelMenuItems.each(function(el){
			    jQuery(this).removeClass('active');
			});

			jQuery(this).addClass('active');
			jQuery(this).find('ul').css('display','block');
		    }
		});
	    });
	}

    },


    hideWait: function(){
	this.wait.css('display','none');
    },

    showWait: function(){
	this.wait.css('display','block');
    },

    removeContent: function(item){
	//this.selector.setHTML('');
	this.selector.html('<div class="empty-message">' + this.parentObj.faqmsgs[item] + '</div>');

	if(this.nextItem){
	    this.nextItem.removeContent(item);
	}
    },

    selectFirst: function(){
	var firstitem = this.selector.find('li:first a');
	if(firstitem.length > 0){
	    firstitem.addClass('active');
	}
    },

    getContent: function (url,firstitem) {
	var localSelf = this;

	if(!localSelf.nextItem){
	    return false;
	}

	localSelf.nextItem.showWait();

	var urlTyped = '';
	if(url.match(/\?/)){
	    urlTyped = url+'&type=65&kbshop_action=' + this.itemNumber;
	} else {
	    urlTyped = url+'?type=65&kbshop_action=' + this.itemNumber;
	}

	var nextContent = localSelf.nextItem.selector;

	jQuery.ajax({
	    url: urlTyped,
	    success: function(html){
		nextContent.html(html);
		if(localSelf.nextItem){
		    localSelf.nextItem.addEvents();
		    localSelf.nextItem.hideWait();
		    localSelf.nextItem.selectFirst();
		}
	    },
	    error: function(){
		localSelf.hideWait();
		alert('Request Error: Bitte laden Sie die Seite neu');
	    }

	});

	if(typeof(this.contentResults) != 'object'){
	    if(this.nextItem && this.nextItem.nextItem){
		this.nextItem.nextItem.removeContent(this.itemNumber+1);
	    }
	}

	//if((typeof(this.contentResults) == 'object') && localSelf.specHandling && firstitem){
	if(typeof(this.contentResults) == 'object'){
	    // action 3 special
	    localSelf.contentResults.showWait();

	    if(url.match(/\?/)){
		urlTyped = url+'&type=65&kbshop_action=3';
	    } else {
		urlTyped = url+'?type=65&kbshop_action=3';
	    }

	    jQuery.ajax({
		url: urlTyped,
		success: function(html){
		    localSelf.contentResults.selector.html(html);
		    if(localSelf.contentResults){
			localSelf.contentResults.addEvents();
			localSelf.contentResults.hideWait();
		    }
		},
		error: function(){
		    localSelf.hideWait();
		    alert('Request Error: Bitte laden Sie die Seite neu');
		}

	    });


	}

	return false;
    }

});

/**
 * FAQ Results
 */
var FAQresults = Class.extend({

    options: {
    },

    init: function(itemNumber,options){
	//this.setOptions(options);
	this.itemNumber = itemNumber;
	// identify select criteria boxes
	this.selector = jQuery('#faq-results');

	// identify spiner
	this.wait = jQuery('.faq-results-box > .wait');

	this.hideWait();
	this.addEvents();

    },

    selectFirst: function(){

    },

    addEvents: function(){
	localSelf = this;
	var elements  = this.selector.find('a');
	elements.bind('click', function(e){
	    var url = jQuery(this).attr('href');
	    if(!url.match(/(^http[s]?:\/\/)|(^\/)/)){
		url = '/' + url;
	    }

	    localSelf.openPopup(url,'smafaq',800,600,'auto','center');
	    return false;
	});

    },


    openPopup: function(mypage,myname,w,h,scroll,pos){
	if(pos=="random"){
	    LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
	    TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	}
	if(pos=="center"){
	    LeftPosition=(screen.width)?(screen.width-w)/2:100;
	    TopPosition=(screen.height)?(screen.height-h)/2:100;
	}
	else if((pos!="center" && pos!="random") || pos==null){
	    LeftPosition=0;
	    TopPosition=20
	}
	if(scroll=='no' || scroll=='yes'){
	    scroll = '=' + scroll;
	}else{
	    scroll='';
	}

	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars'+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
	win=window.open(mypage,myname,settings);
	win.focus();
    },

    hideWait: function(){
	this.wait.css('display','none');
    },

    showWait: function(){
	this.wait.css('display','block');
    },

    removeContent: function(item){
	this.selector.html('<div class="empty-message">' + this.parentObj.faqmsgs[item] + '</div>');
	//this.selector.setHTML('');
    }

});





var FAQtopten = Class.extend({

    options: {
    },

    init: function(itemNumber,options){
	// identify select criteria boxes
	this.selector = jQuery('#topten-faq, #faq-preview, .tx-indexedsearch');

	if(this.selector.length > 0){
	    this.addEvents();
	}




    },

    addEvents: function(){
	var localSelf = this;

	this.selector.each(function(selector){
	    var elements  = jQuery(this).find('a');

	    elements.each(function(element){

		var url = jQuery(this).attr('href');
		// DE faq links
		if(url.match(/\/artikel\/faq\//)){
		    jQuery(this).bind('click', function(e){
			if(!url.match(/(^http[s]?:\/\/)|(^\/)/)){
			    url = '/' + url;
			}
			localSelf.openPopup(url,'smafaq',800,600,'auto','center');
			return false;
		    });
		// EN faq links
		}else if(url.match(/\/item\/faq\//)){

		    jQuery(this).bind('click', function(e){
			if(!url.match(/(^http[s]?:\/\/)|(^\/)/)){
			    url = '/' + url;
			}
			localSelf.openPopup(url,'smafaq',800,600,'auto','center');
			return false;
		    });
		// IT faq links
		}else if(url.match(/\/articolo\/faq\//)){

		    jQuery(this).bind('click', function(e){
			if(!url.match(/(^http[s]?:\/\/)|(^\/)/)){
			    url = '/' + url;
			}

			localSelf.openPopup(url,'smafaq',800,600,'auto','center');
		    });
		// FR faq links
		}else if(url.match(/\/articles\/faq\//)){

		    jQuery(this).bind('click', function(e){
			if(!url.match(/(^http[s]?:\/\/)|(^\/)/)){
			    url = '/' + url;
			}

			localSelf.openPopup(url,'smafaq',800,600,'auto','center');
			return false;
		    });
		}

	    });


	});

    },


    openPopup: function(mypage,myname,w,h,scroll,pos){
	if(pos=="random"){
	    LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
	    TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	}
	if(pos=="center"){
	    LeftPosition=(screen.width)?(screen.width-w)/2:100;
	    TopPosition=(screen.height)?(screen.height-h)/2:100;
	}
	else if((pos!="center" && pos!="random") || pos==null){
	    LeftPosition=0;
	    TopPosition=20
	}
	if(scroll=='no' || scroll=='yes'){
	    scroll = '=' + scroll;
	}else{
	    scroll='';
	}

	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars'+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
	win=window.open(mypage,myname,settings);
	win.focus();
    }

});