$(document).ready(function(){
	$('noscript').remove();
	$("ul#ticker01").css({'display':'block'}).liScroll();
	$(".externerlink").attr({target: "_blank", title: "Dieser Link öffnet sich in einem neuen Fenster."});
	$('form input:text:first').focus();
	$('#google').css({
		'display': 'block',
		'padding': '45px 433px',
		'height': '90px' 
	});
	
	// News
	$('#newsheadlines a').click(function(e){
		e.preventDefault();
		var nid = $(this).attr('rel');
		$('#newsentrys div[class*="zeigeeintrag"]').removeClass('zeigeeintrag');
		$('#newsentrys div.news_'+nid).addClass('zeigeeintrag');
	});
	
	// Multiple Selects
    $("select[multiple]:not('#newsselect')").asmSelect({
        animate: true,
        addItemTarget: 'bottom'
    });

	
	// Modeldetails ändern
	if($('#bildid_edit option').length > 0 || $('.asmSelect option').length > 0){
		var modelid = $('#bildid_edit, .asmSelect').parents('form').find('input:hidden')[0].value;
		$('#bildid_edit, .asmSelect').before('<div id="previewpic"><h6>Preview Bildauswahl</h6><img alt="preview" title="Vorschau" src="images/fotogalerie/'+modelid+'/thumb_'+$('option:eq(0)', $('#bildid_edit')).text()+'" /></div>');
		$('#previewpic').css({'position':'absolute','top':'90px','right':'40px','width':'200px'});

		$('#bildid_edit').change(function(){
			var bild = $('option:selected', $(this)).text();
			$('#previewpic img').attr('src', 'images/fotogalerie/'+modelid+'/thumb_'+bild);
		});
	}

	/* IE6 aussperren */

	if($.browser.msie && ($.browser.version <= '7.0')) {
		$.cookie('check_cookie', '1');
		if($.cookie('check_cookie') == 1){
			$.cookie('check_cookie', null);
			var cookie_setzen_erlaubt = true;
		}else{
			var cookie_setzen_erlaubt = false;
		}
		if($.cookie('iebox') == 'null' || $.cookie('iebox') == 'undefined' || $.cookie('iebox') != 'false'){
			var content = $('#wrapper');
			var info = $('<div />');
			info.attr('id', 'info');
			//info.css({'display':'none'});

			var title = 'Wichtiger Hinweis';
			var image = '<img src="layout/ie6-logo.jpg" width="175" height="175" alt="" title="" />';
			var message	= 'Du benutzt einen veralteten Internet Explorer von Microsoft.<br /><br />' + "\n";
				message	+= 'DuesterArt ist damit leider nicht vollständig benutzbar, aber mit einem neueren Browser schaut´s dann doch besser aus.' + "\n";
			var link = '<a id="ieboxclose" href="#nogo">Meldung ausblenden</a>';
			var auswahlcheckbox = '( <input type="checkbox" name="meldung" id="iebox" value="1" /> Hinweis immer ausblenden? )';

			var html  = image + '<h3>' + title + '</h3>' + "\n";
				html += '<p>' + message + '</p>';
				html += '<p>' + link + '</p>';
			if(cookie_setzen_erlaubt == true){
				html += '<p>' + auswahlcheckbox + '</p>';
			}
			info.html(html);
			content.prepend(info);
			//$('#info').fadeIn('slow');

			$('#ieboxclose').click(function(event){
				event.preventDefault();
				if($('input[id=iebox]:checked').val() == 1){
					$.cookie('iebox', 'false');
					//alert('Box checked');
				}
				$('#info').remove();
			});
		}
	}
	
});

function sorteditmodelpics(form){
	var mid = form.modelid.options[form.modelid.selectedIndex].value
	tb_show('Sortieren / Titel editieren', 'popup_models_sortieren_titeleditieren.php?modelid='+mid+'&TB_iframe=true&height=700&width=700');
	return false;
}

/**
 * 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;
    }
};

jQuery.fn.liScroll = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.07
		}, settings);		
		return this.each(function(){
				var $strip = jQuery(this);
				$strip.addClass("newsticker")
				var stripWidth = 0;
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
				var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
				$strip.find("li").each(function(i){
				stripWidth += jQuery(this, i).width();
				});
				$strip.width(stripWidth);			
				var defTiming = stripWidth/settings.travelocity;
				var totalTravel = stripWidth+containerWidth;								
				function scrollnews(spazio, tempo){
				$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
				}
				scrollnews(totalTravel, defTiming);				
				$strip.hover(function(){
				jQuery(this).stop();
				},
				function(){
				var offset = jQuery(this).offset();
				var residualSpace = offset.left + stripWidth;
				var residualTime = residualSpace/settings.travelocity;
				scrollnews(residualSpace, residualTime);
				});			
		});	
};
