/* User Agent (Browserkennung) auf einen bestimmten Browsertyp prüfen */  
function checkBrowserName(name){  
	var agent = navigator.userAgent.toLowerCase();  
	if (agent.indexOf(name.toLowerCase())>-1) {  
		return true;  
	}  
	return false;  
}  

/* Raphael-Icons */
function sjpArrow(position, colorFG, colorBG) {

	var p = Raphael(position, 25, 25);

	var c = p.rect(0, 0, 25, 25);
	c.attr("stroke", false);
	c.attr("fill", colorFG);

	var t = p.path("M0 0L0 100 87 50z");
	t.scale(.07,.07, 15, 13.5);
	t.attr("stroke", false);
	t.attr("fill", colorBG);

	//p.scale(.5,.5);

	return false;
}

/* Search */
function clearQuery(){
    if (document.simpleSearch.queryStr.value == 'Suche') {
        document.simpleSearch.queryStr.value = '';
    }
}
function resetQuery(){
    if (document.simpleSearch.queryStr.value == '') {
        document.simpleSearch.queryStr.value = 'Suche';
    }
}

function onAfter(curr, next, opts) {
    var index = opts.currSlide;
    $('.prev')[index == 0 ? 'hide' : 'show']();
    $('.next')[index == opts.slideCount - 1 ? 'hide' : 'show']();
}

$(document).ready(function(){

	// This is a dev helper, it's not in use live	
	// hide #col3, set #col1 100% width, remove padding
	$('a[id*=toggle]').click(function(){
		if ($(this).hasClass('active') === true) {
			$('a').removeClass('active');
			$('body').removeAttr('class');
		} else {
			$('a').removeClass('active');
			$('body').removeAttr('class').addClass($(this).text());
			$(this).addClass('active');
		}     
	});
	$('a#reset').click(function(){
		$('a').removeClass('active');
		$('body').removeAttr('class');
		$(this).addClass('active');
	});


	/* Form validation */
	$("#registration").formvalidate();


	/* Carousel */
	

	$("#carousel").carousel({ 
		autoSlide : true,
		autoSlideInterval: carouselInterval,
		loop : true
	});
	

	$(".cycle").cycle({ 
		fx:     'scrollHorz', 
		prev:   '.prev', 
		next:   '.next',
		speed:	'500', 
		backwards: true,
		after:   onAfter,
		startingSlide: parseInt($(".cycle").children().length)-1,
		timeout: 0
	});

	/* Shops */
	/*
	// vertival positioning of the logos in all ie
	if (checkBrowserName('MSIE')) {
		$('#shop-wrapper .item-wrapper').each(function(i) {
			var height = $(this).parent().height();
			var itemHeight = $(this).height();
			var margin = (height - itemHeight) / 2;
			$(this).css('margin-top', margin);
		});
	}


	// Display shop details on mouseover
	$('.shop-logo').mouseover(function(){

		$(this).blur();

		$('.shop-details').each(function(i){
			$(this).hide().html('');
		});
	
		var row = $(this).parent().parent().attr("class").split(' ');
		var container = $(".shop-details[id=" + row[0] + "]");
		var img = $(this).find("div:nth-child(2) img").clone();
		var title = '<h2>' + $(this).parent().find('h3').html() + '</h2>';
		var html = $(this).parent().find('.shop-desc').html();
	
		img.appendTo(container).show();	
		container.append('<div>' + title + html + '</div>');
		container.show();

	});
	*/

	/* Shops new version */
	$("#shop-wrapper .item").each(function() {
		var height = $(this).height();
		var itemHeight = $(this).find(".item-wrapper").height();
		itemHeight = itemHeight + (itemHeight*0.5);
		var margin = (height - itemHeight) / 2;
		$(this).find(".item-wrapper").css('margin-top', margin);
	});

	
	$('.bubble').each(function () {
		// options
		var distance = 5;
		var time = 100;
		var hideDelay = 100;

		var hideDelayTimer = null;

		// tracker
		var beingShown = false;
		var shown = false;

		var trigger = $('.trigger', this);
		var popup = $('.popup', this).css('opacity', 0);

		// set the mouseover and mouseout on both element
		$([trigger.get(0), popup.get(0)]).mouseover(function () {
			// stops the hide event if we move from the trigger to the popup element
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// don't trigger the animation again if we're being shown, or already visible
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;
				// reset position of popup box
				var leftPosition = "-18px";
				if (popup.find(".popup-bottom-last").length > 0) {
						var leftPosition = "-90px";
				}
				popup.css({
					top: "-"+(popup.height()-20)+"px",
					left: leftPosition,
					zIndex: 10000,
					display: 'block' // brings the popup back in to view
				})
				// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					// once the animation is complete, set the tracker variables
					beingShown = false;
					shown = true;
				});
			}
		}).mouseout(function () {
			// reset the timer if we get fired again - avoids double animations
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
		  
			// store the timer so that it can be cleared in the mouseover if required
				hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					// once the animate is complete, set the tracker variables
					shown = false;
					// hide the popup entirely after the effect (opacity alone doesn't do the job)
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	});


	$('#registration .terms a').click(function() {
		$('#terms').slideToggle();
	});




	/* IE z-index fix */
    if ($.browser.msie && (jQuery.browser.version < 8)) {
		$('#teaser .float_left:nth-child(2)').css('margin-left', '9px');
		$('#teaser-top .float_left:nth-child(2)').css('margin-left', '9px');
        var zIndexNumber = 1000;
        $('div').each(function() {
                $(this).css('zIndex', zIndexNumber);
                zIndexNumber -= 10;
        });
    }

});


/**
 * Formvalidation
 */
(function($) {
	$.fn.formvalidate = function() {
		
		return this.each(function() {
			
			var $this = $(this);

			$this.submit(function() {
				
				var valid = true;
				
				$this.find('.notempty').each(function() {
					if ($.trim($(this).val()) == '') {
						$(this).addClass('error');
						valid = false;
					}
					else {
						$(this).removeClass('error');
					}
				});
				
				$this.find('.email').each(function() {
					if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test($(this).val())) {
						$(this).removeClass('error');
					}
					else {
						$(this).addClass('error');
						valid = false;
					}
				});

				$this.find("select").each(function() {
					if ($(this).val() == "empty") {
						$(this).addClass('error');
						valid = false;
					}
					else {
						$(this).removeClass("error");
					}
				});
				/*

				var hasChecked = false;
				$this.find(".checkbox-field input").each(function() {
					if ($(this).is(":checked")) {
						hasChecked = true;
					}
				});

				$this.find(".checked").each(function() {
					if ($(this).is(":checked")) {
						hasChecked = true;
					}				
				});

				if (!hasChecked) {
					$this.find(".checkbox-label").parent().addClass("checkbox-error");
					valid = false;	
				}
				else {
					$this.find(".checkbox-label").parent().removeClass("checkbox-error");
				}
				*/
				if (!valid) {
					return false;
				}
			})
		});
	}
}) (jQuery);



