/**
 * @package Main
 */

/**
 * @param  Boolean  
 * @return Debug
 * @contructor
 */

function Debug(active)
{
	this.active = active;
	
    /**
     * @param Object msg
     * @return void 
     */
     
    this.log = function(msg) 
    {
        if(!this.active) 
        {
        	return;
        }
        
        if(jQuery.browser.mozilla && window.console)
        {
            console.debug(msg);
        }
        else
        {
        	jQuery("#debug").append(msg + "<br />");
        }
    };

};

/**
 * @return Ajax
 * @constructor
 */
 
function Ajax()
{
	/**
	 * @param  String
     */
     
	this.ajax = function(address, callbackOnSuccess){
	    
		//address = address.replace('0.html', '100.html');
		//Debug.log(address);
		
		jQuery.ajax({
         
	        // Add TYPO3 page type
	        
	        url     : address.replace('0.html', '100.html'), //url+'&type=100',
	        dataType: 'html',
	        
	        success : callbackOnSuccess
	        
	    });
	}
} 

/**
 * @param  Ajax
 * @return News
 * @contructor
 */

function News(Ajax)
{
    this.Ajax      = Ajax;
    this.container = null;
}

/**
 * @param  jQuery container   
 * @return Boolean
 */
  
News.prototype.setContainer = function(container)
{
    if(container instanceof jQuery)
    {
        this.container = container;
        return true;
    }
    else
    {
        Debug.log('Object of instance jQuery required')
        return false;
    }
}

/**
 * @return void
 */

News.prototype.bindEvents = function()
{
	// Store scope
	
	var this_  = this;
	
	// Bind list view
	
	jQuery('.news-list-item a').bind('click', function(eventObj) 
    {
    	var entry = jQuery(this).parents('.news-list-item');
    	var child = entry.children('.news-single-item');
    	
    	// If we already have a loaded news item in place, just fade it in
    	
    	if(child.length > 0)
    	{
    		entry.children(':not(.news-single-item)').hide();
    		child.fadeIn();
    		return false;
    	}
    	
    	// Otherwise, load & replace
    	
    	if(this_.setContainer(entry))
    	{
    	    entry.children().fadeTo('fast', 0.33);
    		this_.Ajax.ajax(jQuery(this).attr('href'), this_.replaceListItem);
    		return false;
    	}
    });
    
    // Bind single view click event. 
    //
    // Since the single view is loaded later on and doesn't exist when this method is called,
    // we use jQuery's 'live' method here to bind events, though it has some disadvantages over 'bind', cf. 
    // jQuery's documentation on events. 
    
    jQuery('.news-single-item h3 a, .news-single-backlink a').live('click', function(eventObj) 
    {
    	jQuery(this).parents('.news-single-item').hide();
        jQuery(this).parents('.news-list-item').children(':not(.news-single-item)').css('opacity', '100').fadeIn();
        return false;
    });
}

/**
 * @param String
 */

News.prototype.replaceListItem = function(data, entry)
{
	var content = jQuery(data).hide();
	
	if(window.News.container)
	{
		window.News.container.children().hide();
		window.News.container.append(content.fadeIn());
	}
}

/**
 * @return PositionElement
 */

function PositionElement(){}

PositionElement.prototype.setPosition = function()
{
	var the_tower  = jQuery(".backpic img[src$='turm.gif']");
	
	if(the_tower.length == 0){
	    return;
	}

	var offset_top = the_tower.offset().top;
	 
	if(offset_top < -2)
	{
		the_tower     = the_tower.parent();
		the_tower.css('position', 'absolute');
        the_tower.css('bottom', '');
        the_tower.css('top', 20);
	}
}

/**
 * Globals
 */
 
var Debug    = new Debug(false);
var Ajax     = new Ajax();
var News     = new News(Ajax);
var Position = new PositionElement();  
    
jQuery(document).ready(function(){
    //News.bindEvents();
    Position.setPosition();
});

jQuery(document).scroll(function(){
    Position.setPosition();
});

/**
 * Overwrite jQuery's fade methods to overcome IE's font rendering bugs.
 *
 * @see http://www.malsup.com/jquery/fadetest.html
 */

jQuery.fn.fadeIn = function(speed, callback) 
{ 
    return this.animate({opacity: 'show'}, speed, function(){ 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) 
{ 
    return this.animate({opacity: 'hide'}, speed, function(){ 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeTo = function(speed,to,callback) 
{ 
    return this.animate({opacity: to}, speed, function(){ 
        if (to == 1 && jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
