// PopUps aufrufen
function PopUp(src,w,h)
{
	if(w > screen.width-100)
	{
		h = Math.round((screen.width-100)/w*h);
		w = (screen.width-100);
	}
	if(h > screen.height-200)
	{
	    w = Math.round((screen.height-200)/h*w);
		h = (screen.height-200);
	}

	breite = Math.round((screen.width - w) / 2);
	hoehe = Math.round((screen.height - h) / 2);

	window.open(src,"popwin","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=no,width="+w+",height="+h+",top="+hoehe+",left="+breite);
}

// Druckansicht
function PrintView(src,w,h)
{
	var breite = (screen.width - w) / 2;
	var hoehe = (screen.height - h) / 2;
	window.open(src,"printview","toolbar=yes,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width="+w+",height="+h+",top="+hoehe+",left="+breite);
}

// Formulare leeren
function ClearForm(Formular)
{
	document.forms[Formular].reset(); // Zuerst reset!
	var x = 0;
	while(document.forms[Formular].elements[x])
	{
		if(document.forms[Formular].elements[x].type == "checkbox" || document.forms[Formular].elements[x].type == "radio")
		{
			// Checkboxen + Radiobuttons werden nicht angehakt
			document.forms[Formular].elements[x].checked = false;
		}
		else if(document.forms[Formular].elements[x].type != "hidden" && document.forms[Formular].elements[x].type != "submit" && document.forms[Formular].elements[x].type != "reset" && document.forms[Formular].elements[x].type != "button")
		{
			// Alle, ausser versteckte Felder, dürfen geleert werden
			document.forms[Formular].elements[x].value = "";
		}
		x++;
	}
}

// Zum Anfang der Seite scrollen
function ScrollToTop()
{
	var y = 0;

	if(window.pageYOffset) y = window.pageYOffset;
	else
	if(document.body && document.body.scrollTop) y=document.body.scrollTop;

	while(y > 0)
	{
		setTimeout("window.scrollBy(0,-20)",10);
		y = y-5;
	}
}

// Seite zu den Favoriten
function MakeFavorite(title,url)
{
	if((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4))
	{
		window.external.AddFavorite(url,title)
	}
	else if(navigator.appName == "Netscape")
	{
		alert("Unsere Homepage können Sie manuell oder\nmit den Tasten STRG+D zu den Favoriten hinzufügen.");
	}
}

// Bilder vorladen, mit PreloadImages('bild1.jpg','bild2.gif'); aufrufen
function PreloadImages()
{
	document.Vorladen = new Array();
	if(document.images)
	{
		for(var i=0; i<PreloadImages.arguments.length; i++)
		{
			document.Vorladen[i] = new Image();
			document.Vorladen[i].src = PreloadImages.arguments[i];
		}
	}
}

// Transparenz-Filter bei PNG-Bilder für IE 5.5 und IE 6 anwenden
// Aufruf bei $(document).ready() oder mit JS-Code vor </body>:
// <script type="text/javascript">
// fixPng();
// </script>
function fixPng()
{
    if($.browser.msie == true && $.browser.version > 5.5 && $.browser.version < 7.0 && document.body.filters)
    {
        $('img').each(function(){
			if(this.src.match(/\.png$/) != null){
				$(this)
					.css({
						filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+$(this).attr('src')+'\', sizingMethod=\'image\')',
						width: $(this).width()+'px',
						height: $(this).height()+'px'
					})
					.attr('src','images/t3_blank.gif');
			}
        });
	}
}

function gObj(id)
{
	if(typeof document.getElementById(id) == 'object' && document.getElementById(id) != null)
	{
		return document.getElementById(id);
	}
	else if(document.getElementsByName)
	{
		var namedObjects = document.getElementsByName(id);
		if(typeof namedObjects != 'undefined' && namedObjects.length)
		{
		    return namedObjects[0];
		}
	}
}

// Bei Enter soll Formular gesendet werden
function sendByEnter(e)
{
	if(!e)
	{
		e = window.event;
	}

	if(e.which)
	{
		key = e.which;
	}
	else if(e.keyCode)
	{
		key = e.keyCode;
	}

	if(key == 13){
		document.forms[0].submit();
		return true;
	}
}

function toolTip(content,autowidth)
{
	return Tip(content,
			BGCOLOR,'#FFFFFF',
			BORDERCOLOR,'#AAAAAA',
			DELAY,200,
			FONTFACE,'Arial,Tahoma',
			FONTSIZE,'12px',
			PADDING,8,
			SHADOW,true,
			SHADOWCOLOR,'#999999',
			SHADOWWIDTH,3,
			WIDTH,(autowidth ? 0 : 200)
			);
}

// Kalender-Monat wechseln bei Veranstaltungen
function changeCalenderMonth(page_id,month)
{
	$.ajax({
		url: "ajax/ajax.calendar.php?id="+PUid,
		type: 'get',
		data: { page_id: page_id, change_month: month },
		success: function(html){
		    $('#ajax_calendar').html(html);
		}
	});
}

// Sternenbewertung abgeben
function saveItemRating(item,objecttype,stars)
{
	$.ajax({
		url: "ajax/ajax.ratingsystem.php?id="+PUid,
		type: 'post',
		data: { item: item, objecttype: objecttype, stars: stars },
		success: function(html){
		    $('#itemrating'+item).html(html);
		}
	});
}

// Sterne auswählen
function changeStars(item,numActive,numRated)
{
	for(i=1; i<=5; i++)
	{
		if(i <= numActive) $('#star'+item+'s'+i).attr('src','images/star_active.gif');
		else if(i <= numRated) $('#star'+item+'s'+i).attr('src','images/star_on.gif');
		else $('#star'+item+'s'+i).attr('src','images/star_off.gif');
	}
}

function ratingTip(content,autowidth)
{
	return Tip(content,
			BGCOLOR,'#FFFFFF',
			BORDERCOLOR,'#AAAAAA',
			DELAY,100,
			FONTFACE,'Arial,Tahoma',
			FONTSIZE,'11px',
			PADDING,3,
			SHADOW,false
			);
}

// Ändert die DropDown-Liste und behält das selektierte Element, falls vorhanden
function changeSelectValues(boxes, selectEntries, exec_onchange)
{
	$(boxes).each(function(){
	    var select = $(this);
		var options = select.attr('options');

		var collect_selected = {};
	    for(var s=0; s<options.length; s++)
	    {
			// bisher Ausgewähltes merken
	        if(options[s].selected) collect_selected[options[s].value] = true;
	    }
	    
		$('option', select).remove();
		for(var i = 0; i < selectEntries.length; i++){
		    options[options.length] = new Option(selectEntries[i][1], selectEntries[i][0]);
		    if(collect_selected[options[i].value] === true) options[i].selected = true; // bisher Ausgewählte Elemente wieder selektieren
		}

		// Ggf. OnChange-Funktionen ausführen
		if(exec_onchange === true && typeof this.onchange == 'function'){
		    this.onchange();
		}
	});
}

// Mail Adressen verschleiern
function UnHide(s)
{
    var n = 0;
    var r = "";
    for( var i = 0; i < s.length; i++)
    {
        n = s.charCodeAt(i);
        if(n >= 8364) n = 128;
        r += String.fromCharCode(n-1);
    }
    return r;
}
function linkTo_UnHide(s)
{
    location.href = UnHide(s);
}

// Aktuelle Uhrzeit anzeigen
function UR_Start()
{
	if(document.getElementById("ur_time"))
	{
		Zeit = new Date();
		var Std = Zeit.getHours();
		var Min = Zeit.getMinutes();
		var Tag = Zeit.getDay();
		var isOpened = false;

		if(Tag > 0 && Tag < 6) // Mo-Fr
		{
			if(Std >= 8 && Std < 19) isOpened = true;
		}
		if(Tag == 6) // Sa
		{
			if(Std >= 9 && Std < 14) isOpened = true;
		}

		var Std_Min = showFilled(Std) + ":" + showFilled(Min);
		document.getElementById("ur_time").innerHTML = Std_Min;
		document.getElementById("ur_image_closed").style.display = (isOpened == true ? 'none' : 'block');
		document.getElementById("ur_image_opened").style.display = (isOpened == true ? 'block' : 'none');

		setTimeout("UR_Start()",10000);
	}
}
function showFilled(Value)
{
	return (Value > 9) ? "" + Value : "0" + Value;
}


function picHover(){
	var maxWidth = 150;
	var maxHeight = 100;
	$('#product_list img.preview:hidden').each(function(){
	    var img = $(this);
	    var tout;
	    $(this).parent().mouseenter(function(e){
	        if($(img).height() >= $(img).width() && $(img).height() > maxHeight) $(img).height(maxHeight);
			if($(img).width() > $(img).height() && $(img).width() > maxWidth) $(img).width(maxWidth);
	        $(img).css({
				'position': 'absolute',
				'left': '-'+($(img).width()+15)+'px',
				'top': '0px'
			});
	        tout = window.setTimeout(function(){
				$(img).show();//fadeIn(200);
			}, 400);
	    });
	    $(this).parent().mouseleave(function(){
	        window.clearTimeout(tout);
	        $(img).hide();
	        //$('#shop_results img.preview:visible').hide();
	    });
	});
}

var check_add_timeout;
function addToBasket(pro_id, update_item, num, exec_after_success){
    if(check_add_timeout) window.clearTimeout(check_add_timeout);
    var pro_num = (typeof num == 'undefined' ? 0 : num);
	$.ajax({
		url: "ajax/ajax.basket_action.php?id="+PUid,
		type: "post",
		data: {
            pro_id: pro_id,
            num: pro_num
		},
		success: function(html){
			if(typeof update_item == 'object'){
				$(update_item).html('<span class="count">' + html + '</span>');
			}
			check_add_timeout = window.setTimeout(refreshMiniBasket, 1000);
			$('#product_in_basket:hidden').show('slide',{direction:'up'});
			$('#product_in_basket').find('.count').html(html);
			if(typeof exec_after_success == 'function'){
				exec_after_success();
			}
		}
	});
	if(typeof update_item == 'object') $(update_item).blur();
}

function addToBookmarklist(artikel_id, unterartikel_id){

	$.ajax({
		url: "ajax/ajax.bookmark_action.php",
		type: "post",
		data: {
			artikel_id: artikel_id,
			unterartikel_id: unterartikel_id
		},
		success: function(html){
			$('#to_bookmark').show();
			$('#add_bookmark').hide();
		}
	});
}

function addToWatchlist(artikel_id, unterartikel_id){

	$.ajax({
		url: "ajax/ajax.watchlist_action.php",
		type: "post",
		data: {
			artikel_id: artikel_id,
			unterartikel_id: unterartikel_id
		},
		success: function(html){
			$('#to_watchlist').show();
			$('#add_watchlist').hide();
		}
	});
}

function refreshMiniBasket(){
	if($('#mini_basket').length > 0){
		$.ajax({
			url: "ajax/ajax.basket_action.php?id="+PUid,
			type: "post",
			data: {
	            refresh_mini_basket: 1
			},
			success: function(html){
				$('#mini_basket').html(html);
			}
		});
	}
}

function refreshBasket(){
	if($('#basket').length > 0){
	
	    var loader = '<div class="overlay_loader" style="width:'+$('#basket').width()+'px; height:'+$('#basket').height()+'px;"><div class="overlay_loader_inner"></div></div>';
		$('#basket').prepend(loader);

		var data = {refresh_basket: 1};
		$('#basket input').each(function(){
		    data[this.name] = this.value;
		});

		$.ajax({
			url: "ajax/ajax.basket_action.php?id="+PUid,
			type: "post",
			data: data,
			success: function(html){
			    //alert(html);
				$('#basket').replaceWith(html);
				makeCountable();
			}
		});
	}
}

function makeCountable()
{
	$('.countable').each(function(){
		$(this).unbind('keydown').bind('keydown',function(e){
			if(e.keyCode == 38){ // rauf zählen
				e.preventDefault();
				var num = (parseInt(this.value)>1 ? parseInt(this.value) : 1);
				if(num + 1 < 100) this.value = num + 1;
			}
			if(e.keyCode == 40){ // runter zählen
				e.preventDefault();
				var num = (parseInt(this.value)>1 ? parseInt(this.value) : 1);
				if(num - 1 > 0) this.value = num - 1;
			}
		});
	    $(this).click(function(){
	        //this.select();
	    });
	    $(this).blur(function(){
	        this.value = (parseInt(this.value)>1 ? parseInt(this.value) : 1);
	    });
		$(this).removeClass('countable');
	});
}

// Ausklappbarer Bereich mit Checkboxen (Alternative zu multiple Select-Boxen)
function multicheck(a){
	if($(a).is(':checked') == true){
	    $(a).parent().css({'background':'#CED6E1'});
	} else {
	    $(a).parent().css({'background':'#ffffff'});
	}
	$(a).closest('.multicheck').find('.multicheck-toggle span').html('('+$(a).closest('.multicheck').find('input[type=checkbox]:checked').length+')');
}
function multicheck_init(){
	$('.multicheck input[type=checkbox]').click(function(){
		multicheck(this);
	});
	  $('.multicheck input[type=checkbox]:checked').each(function(){
		multicheck(this);
	});
	  $('.multicheck .multicheck-toggle').click(function(){
		$(this).next('.multicheck-list').slideToggle(100);
	});
}


function getCarConstruction(fgnr,into){
	$.ajax({
		url: "ajax/ajax.car_construction.php?id="+PUid,
		type: "post",
		data: {fgnr: fgnr},
		success: function(html){
			$(into).html(html);
		}
	});
}



$(document).ready(function(){
    picHover();
    $('.zoombox').zoombox();
    makeCountable();
    multicheck_init();
    $('.ttip[title]').tipsy({gravity: 's', html: true});
});

$(window).load(function(){
	UR_Start();
	//if($('body').html().match('Warning') || $('body').html().match(' on line ')) alert('PHP-Fehler auf dieser Seite gefunden!');
});










