$(document).ready(function(){
    $.url = {};
    $.extend($.url, {
        _params: {},
        init: function(){
            var paramsRaw = "";
            try {
                paramsRaw = (document.location.href.split("?", 2)[1] || "").split("#")[0].split("&") || [];
                for (var i = 0; i < paramsRaw.length; i++) {
                    var single = paramsRaw[i].split("=");
                    if (single[0]) 
                        this._params[single[0]] = unescape(single[1]);
                }
            } 
            catch (e) {
                alert(e);
            }
        },
        param: function(name){
            return this._params[name] || "";
        },
        paramAll: function(){
            return this._params;
        }
    });
    $.url.init();

    // more-Link
    $('p.more').each(function(index){
        var $moretext = $(this);
        var $morelink = $('<p><a href="#" class="more">MEHR INFORMATIONEN</a></p>');
        var $lesslink = $('<p><a href="#" class="less">WENIGER INFORMATIONEN</a></p>');
        $('a.more').attr('onfocus', 'if(this.blur)this.blur()');
        $('a.less').attr('onfocus', 'if(this.blur)this.blur()');
        $morelink.insertBefore($moretext);
        $lesslink.insertAfter($moretext);
        $moretext.hide();
        $lesslink.hide();
    });
    
    $('a.more').click(function(event){
        var $morelink = $(this);
        var $lesslink = $morelink.parent().parent().find('a.less');
        var $moretext = $morelink.parent().next();
        $moretext.animate({
            opacity: 'toggle',
            height: 'toggle'
        }, 'slow');
        $morelink.parent().hide('slow');
        $lesslink.parent().show('slow');
        return false;
    });
    
    $('a.less').click(function(event){
        var $lesslink = $(this);
        var $morelink = $lesslink.parent().prev().prev().find('a.more');
        var $moretext = $lesslink.parent().prev();
        $moretext.animate({
            opacity: 'toggle',
            height: 'toggle'
        }, 'slow');
        $morelink.parent().show('slow');
        $lesslink.parent().hide('slow');
        return false;
    });
    
    // ToTop-Link
    $('a.toplink').each(function(index){
        var $toplink = $(this);
        var $destination = $toplink.parent().prev().prev().find('.csc-textpic-text');
        $('a.toplink').attr('onfocus', 'if(this.blur)this.blur()');
        $toplink.parent().insertAfter($destination);
        /*
         $morelink.insertBefore($moretext);
         $lesslink.insertAfter($moretext);
         $moretext.hide();
         $lesslink.hide();
         */
    });
    
    // Tabelle-Styling
    $('table').each(function(){
        $(this).find('tr:last td').addClass('noline');
    });
    
    // Logo-Animationen
    //$('#logo').attr('onfocus','if(this.blur)this.blur()');
    var $logo = $('#logo');
    if ($.url.param("id") == 1 || $.url.param("id") == '') {
        $logo.hide().fadeTo(1000, 1).fadeIn('slow');
    }
    $logo.click(function(event){
        $(this).fadeOut('slow');
    });
    function logohoverover(){
        $logo.stop().animate({
            opacity: '0.5'
        }, 'fast');
    };
    function logohoverout(){
        $logo.stop().animate({
            opacity: '1.0'
        }, 'slow');
    };
    $logo.hoverIntent({
        sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
        interval: 100, // number = milliseconds for onMouseOver polling interval
        over: logohoverover, // function = onMouseOver callback (required)
        timeout: 0, // number = milliseconds delay before onMouseOut
        out: logohoverout // function = onMouseOut callback (required)
    });
    
    // ToTop-Animation
    var $totop = $('#totop > a > img');
    function totophoverover(){
        $totop.stop().animate({
            'opacity': '0.5',
            'height': '+=10px',
            'width': '+=10px',
            'left': '-=5px',
            'top': '-=5px'
        }, 'fast');
    };
    function totophoverout(){
        $totop.stop().animate({
            'opacity': '1.0',
            'height': '-=10px',
            'width': '-=10px',
            'left': '+=5px',
            'top': '+=5px'
        }, 'fast');
    };
    $totop.hoverIntent({
        sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
        interval: 100, // number = milliseconds for onMouseOver polling interval
        over: totophoverover, // function = onMouseOver callback (required)
        timeout: 0, // number = milliseconds delay before onMouseOut
        out: totophoverout // function = onMouseOut callback (required)
    });
    
    // Teaserelement 
    $('div.teasercontainer').each(function(index){
        var $header = $(this).prev('div');
        var $teasercontainer = $(this);
        var $destination = $teasercontainer.find('div.image');
        var $morelink = $teasercontainer.find('div.morelink a');
        $header.insertAfter($destination);
        $teasercontainer.css('opacity', 1.0).css('cursor', 'pointer');
        /*
        $teasercontainer.click(function(){
            $morelink.click();
        });
        */
        
        
        // Teasercontainer-Hover-Fading
        function teasercontainerhoverover(){
            $teasercontainer.stop().animate({
                opacity: '0.5'
            }, 'fast', function(){
            
            });
        };
        function teasercontainerhoverout(){
            $teasercontainer.stop().animate({
                opacity: '1.0'
            }, 'slow', function(){
            
            });
        };
        $teasercontainer.hoverIntent({
            sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
            interval: 100, // number = milliseconds for onMouseOver polling interval
            over: teasercontainerhoverover, // function = onMouseOver callback (required)
            timeout: 0, // number = milliseconds delay before onMouseOut
            out: teasercontainerhoverout // function = onMouseOut callback (required)
        });
    });
    
    // Teaserelement-Animationen
    $('.teaser_container').each(function(index){
        var $header = $(this).prev('div');
        var $teasercontainer = $(this);
        var $destination = $teasercontainer.find('img');
        var $hoverarea = $teasercontainer.find('dt');
        
        $teasercontainer.find('p.bodytext').hide();
        $teasercontainer.find('p.mehr').hide();
        var $header_new = $header.clone().insertAfter($destination).hide().fadeTo(1000, 1).fadeIn('slow', function(){
            $('p.bodytext, p.mehr').fadeIn('slow');
        });
        $header.hide();
        $teasercontainer.find('p.anleser').hide();
        
        // Header-Hover-Fading
        function headerhoverover(){
            $destination.stop().animate({
                opacity: '0.5'
            }, 'fast', function(){
                $header_new.animate({
                    opacity: '0.5'
                }, 'slow');
            });
        };
        function headerhoverout(){
            $header_new.stop().animate({
                opacity: '1.0'
            }, 'slow', function(){
                $destination.animate({
                    opacity: '1.0'
                }, 'slow');
            });
        };
        $hoverarea.hoverIntent({
            sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
            interval: 100, // number = milliseconds for onMouseOver polling interval
            over: headerhoverover, // function = onMouseOver callback (required)
            timeout: 0, // number = milliseconds delay before onMouseOut
            out: headerhoverout // function = onMouseOut callback (required)
        });
    });
	
	
	// MODALBOX!!! 
    //
	// Teaserelement-Animationen
    $('a.modalbox img').each(function(index){
        var $modalbox = $(this);
        
        // Modalbox-Hover-Fading
        function modalboxhoverover(){
            $modalbox.stop().animate({
                opacity: '0.5'
            }, 'fast');
        };
        function modalboxhoverout(){
            $modalbox.stop().animate({
                opacity: '1.0'
            }, 'slow');
        };
        $modalbox.hoverIntent({
            sensitivity: 7, // number = sensitivity threshold (must be 1 or higher)
            interval: 100, // number = milliseconds for onMouseOver polling interval
            over: modalboxhoverover, // function = onMouseOver callback (required)
            timeout: 0, // number = milliseconds delay before onMouseOut
            out: modalboxhoverout // function = onMouseOut callback (required)
        });
    });
	 
    $('a.modalbox').attr('rel','#overlay');
	$("a[rel]").overlay({ 
 
        expose: '#FFFFFF',
        effect: 'apple',
		progress: 'ajax-loader.gif',
 
        onBeforeLoad: function() { 
			var wrap = $('#overlay .wrap');
 			wrap.empty().hide();
			$('.progress').fadeIn('slow');
			url = this.getTrigger().attr("href") + "?type=1";
            //wrap.load(this.getTrigger().attr("href"));
			wrap.load(url + " #modalbox","",function(){
				$('.progress').fadeOut('slow');
				wrap.fadeIn('slow');
				if ($('#bestellung_form').length > 0) {
					bestellformprocessing();
				}
			}); 
        } 
    });
	
	function bestellformprocessing(){
		//submission scripts
	  	$('#bestellung_form').submit( function(){
			//statements to validate the form	
			var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			var email = document.getElementById('email');
			if (!filter.test(email.value)) {
				$('.email-missing').css('color','#93B614');
				$('input#email').css('background-color','#EBF8C0');	
			} else {
				$('.email-missing').css('color','#444444');
				$('input#email').css('background-color','#ffffff');
			}
			if ($('#firstname').val() == "") {
				$('.firstname-missing').css('color','#93B614');
				$('input#firstname').css('background-color','#EBF8C0');
			} else {
				$('.firstname-missing').css('color','#444444');
				$('input#firstname').css('background-color','#ffffff');
			}	
			if ($('#lastname').val() == "") {
				$('.lastname-missing').css('color','#93B614');
				$('input#lastname').css('background-color','#EBF8C0');
			} else {
				$('.lastname-missing').css('color','#444444');
				$('input#lastname').css('background-color','#ffffff');
			}
			if (!filter.test(email.value) || $('#firstname').val() == "" || $('#lastname').val() == "") {return false;}		
			});
			//$('#bestellung_form').click($('#bestellung_form').submit());	
	}  
	
	// jQuery SmoothScroll
    $('a[href*=#]').click(function(){
    
        // duration in ms
        var duration = 1500;
        
        // easing values: swing | linear
        var easing = 'swing';
        
        // make sure it's the same location      
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname && this.hash.replace(/#/, '')) {
            // get parameters
            var hash = this.hash;
            var target = $(hash).offset().top;
            
            // animate to target and set the hash to the window.location after the animation
            $("html:not(:animated),body:not(:animated)").animate({
                scrollTop: target
            }, duration, easing, function(){
                location.hash = hash;
            });
            
            // cancel default click action
            return false;
        }
    });
    
    
});
