var contMinWidth = 975;

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
	if (typeof value != 'undefined') { // name and value given, set cookie
		options = options || {};
		if (value === null) {
			value = '';
			options.expires = -1;
		}
		var expires = '';
		if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
			var date;
			if (typeof options.expires == 'number') {
				date = new Date();
				date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
			} else {
				date = options.expires;
			}
			expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
		}
		// CAUTION: Needed to parenthesize options.path and options.domain
		// in the following expressions, otherwise they evaluate to undefined
		// in the packed version for some reason...
		var path = options.path ? '; path=' + (options.path) : '';
		var domain = options.domain ? '; domain=' + (options.domain) : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	} else { // only name given, get cookie
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};

function statistik( sPath )
{
	var sIdentifier = $.cookie( 'spIdent' );
	var sSession = $.cookie( 'spSession' );

	if( 'undefined' !== typeof( console ) )
	{
		console.log( sIdentifier, '-|-', sSession );
	}
	
	if( !sIdentifier )
	{
		sIdentifier = new Date().getTime();
		$.cookie( 'spIdent', sIdentifier, { expires: 365, path: '/' } )
		
		if( 'undefined' !== typeof( console ) )
		{
			console.log( 'spIdent: ', sIdentifier );
		}
	}

	if( !sSession )
	{
		sSession = new Date().getTime();
		$.cookie( 'spSession', sSession, { path: '/' } )

		if( 'undefined' !== typeof( console ) )
		{
			console.log( 'spSession: ', sSession );
		}
	}
	
	if( 'undefined' !== typeof( console ) )
	{
		console.log( sIdentifier, '-|-', sSession );
	}

	if( 'nc/' === sPath.substr( 0, 3 ) )
	{
		sPath = sPath.substr( 3 );
	}

	sPath = '/www/' + sPath;
	
	var oImg = new Image();
	oImg.src = '/tracking.php?debug=1&path=' + encodeURI( sPath ) + '&sessionId=' + sIdentifier + '-' + sSession;
	
	if( 'undefined' !== typeof( console ) )
	{
		console.log( oImg.src );
	}
}

function resizeLayout()
{
    var oBody = $( 'body' );
        
    var oMainLeft   = $( '#main_left' );
    var oMainMiddle = $( '#main_middle' );
    var oMainRight  = $( '#main_right' );

    var iBodyWidth = ( parseInt( oBody.width()) > contMinWidth ) ? parseInt( oBody.width()) : contMinWidth ;
            
    var iContainerWidth = iBodyWidth-(iBodyWidth-contMinWidth)/2;
    
    var bgBodyPos = Math.ceil(iBodyWidth/2) -252;
    
    var bgHtmlPos = Math.ceil(iBodyWidth/2)+(Math.ceil(iContainerWidth/2)-200) ;

    var iMainLeftWidth = oMainLeft.width();
    
    var iMainMiddleLeft = ( ( iContainerWidth - iMainLeftWidth - oMainRight.width() ) - oMainMiddle.width() ) / 2;
    
    var iMainRightPos = -1*iMainMiddleLeft-180;
    
    //mittlerer Bereich um 40 pixel nach links 
    if(iBodyWidth > (contMinWidth+40))
    {
        bgBodyPos = Math.ceil(iBodyWidth/2)-292;
        iMainMiddleLeft = iMainMiddleLeft-40;
        iMainRightPos = iMainRightPos-40;
    }
    
    
    if( 'undefined' !== typeof( console ) )
    {
        console.log( iBodyWidth );
    }
    
    
    $( '#container' ).css
    (
        {
            'width' : iContainerWidth + 'px'
        }
    );
    
    $( '#header' ).css
    (
        {
            'width' : iContainerWidth + 'px'
        }
    );
    
    oMainMiddle.css
    (
        {
            'left' : ( iMainLeftWidth + iMainMiddleLeft ) + 'px'
        }
    );

    $( '#header_middle' ).css
    (
        {
            'left' : ( iMainMiddleLeft ) + 'px'
        }
    );
    
    $( 'body' ).css
    (
        {
            'background-position' : bgBodyPos + 'px 0px'
        }
    );
    
    $( 'html' ).css
    (
        {
            'background-position' : bgHtmlPos + 'px 0px'
        }
    );
    
        //3 layoutTypen
    if($('#player_holder').width())
    {//mediagallery detail
        oMainRight.css
        (
            {
                'right' : ( iMainRightPos ) + 'px'
            }
        );
    }
    else if($('#flvplayerlist').width())
    {//mediagallery liste
        oMainMiddle.css
        (
            {
                'left' : ( iMainLeftWidth + iMainMiddleLeft -90 ) + 'px'
            }
        );
        $( '#header_middle' ).css
        (
            {
                'left' : ( iMainMiddleLeft-90 ) + 'px'
            }
        );
    }
}

function initLayout()
{
    $( 'body' ).css
    (
        {
            'background-image' : 'url(./fileadmin/templates/images/bg/bg_line_1x1.gif)',
            'background-color' : 'transparent'
        }
    );
    
    $( 'html' ).css
    (
        {
            'background-image' : 'url(./fileadmin/templates/images/bg/bg_line_1x1.gif)',
            'background-color' : '#f0f0f0',
            'background-repeat' : 'repeat-y'
            
        }
    );
    resizeLayout();
}


$(document).ready
(
	function()
	{
        //init
        initLayout();
        $(window).resize( resizeLayout );
        
    
        // png-fix-init
        $('#main_middle').pngFix( { blankgif:'ext.gif' } );
        $('#main_right').pngFix( { blankgif:'ext.gif' } );
      
      
        $( 'img.more' ).parent("a").hover (
            function()
            {
                $(this).children("img.more").attr("src","./fileadmin/templates/images/buttons/bu_more_hl.gif");
            },
            function()
            {
                $(this).children("img.more").attr("src","./fileadmin/templates/images/buttons/bu_more.gif");
            }
        );
      
        //presse
        $("#cat_header").click(function () {
            $("#cat_select_options").slideToggle("fast");
        });
      
        $( 'ul#arbeiten_catmenu ul.arbeiten_level2' ).css("display","none");
        $( 'ul#arbeiten_catmenu li.level1:has(ul)' ).hover
        (
            function()
            {
                $(this).addClass("hover");
            },
            function()
            {
                $(this).removeClass("hover");
            }
        );
        
        
	}
);
