
$(document).ready(
    function() 
    {
        
        $('a').click(
            function() 
            {
                var url = $.url(this.href);
                var host = url.attr("host");

                if (this.href.substr(0, 7) == 'mailto:')
                {
                    window.location = this.href;
                }    
                else if (
                    host != 'sante' && 
                    host != 'sante.de' && 
                    host != 'www.sante.de' && 
                    host != 'kirkstrobeck.local' && 
                    host != 'sante.de.s27151.gridserver.com' &&
                    host != 'sante.elektrobio.de'
                    )
                {
                    window.open(this.href);
                    return false;
                }
            }
        );
        
        
        // compatability
        document.documentElement.className = 'js'; // html5    
        
        
        
        // fix ie pngs
        /*
        var i;
        for (i in document.images) {
            if (document.images[i].src) {
                var imgSrc = document.images[i].src;
                if (imgSrc.substr(imgSrc.length-4) === '.png' || imgSrc.substr(imgSrc.length-4) === '.PNG') {
                    document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + imgSrc + "')";
                }
            }
        }
        */
        
        
        
        if( $('.page').exists() )
        {
            $('.page .entries .entry :first-child').css('margin-top', 0);
            $('.page .entries .entry :last-child').css('margin-bottom', 0);
            
            $('.page .entries .entry.first .image-wide').css('margin-top', '22px');
        }
        
        
        if( $('.page').exists() )
        {
        	$("[rel^='lightbox']").prettyPhoto(
        	    {
        	        default_width: 600,
        			show_title: true,
        			deeplinking: false,
        			social_tools: ''
        	    }
        	);
        	
            $('.page-list-article .images .image:first').css('margin-top', 0);
            $('.page-list-article .images .image:last').css('margin-bottom', 0);
            
            $('.page-list-article .content :first-child').css('margin-top', 0);
            $('.page-list-article .content :last-child').css('margin-bottom', 0);
            
            
        }
        
        if( $('.searchbar').exists() ) 
        {
        	$('.searchbar').find('.text').focus();
        }
        
        
        
        // products
        
        if( $('.product-detail').exists() )
        {
        	var tabbkd = "url('/workspace/images/tab-shadow.png')";
        	
        	//$('.product-detail .tabs .card .tab-2').css('opacity', .8);
        	$('.product-detail .tabs .headers .tab-2').css('background-image', tabbkd);
        	
            $('.product-detail .tabs .headers .tab-1').click(
            	function()
            	{
            		$(this).parents('.tabs').find('.card .tab-1').fadeIn();
            		$(this).parents('.tabs').find('.headers .tab-2').css('background-image', tabbkd);
            		$(this).parents('.tabs').find('.card .tab-2').fadeOut();
            		$(this).parents('.tabs').find('.headers .tab-1').css('background-image', 'none');
            	}
            );
            
            $('.product-detail .tabs .headers .tab-2').click(
            	function()
            	{ 
            		$(this).parents('.tabs').find('.card .tab-2').fadeIn();
            		$(this).parents('.tabs').find('.headers .tab-1').css('background-image', tabbkd);
            		$(this).parents('.tabs').find('.card .tab-1').fadeOut();
            		$(this).parents('.tabs').find('.headers .tab-2').css('background-image', 'none');
            	}
            );
        }
        //alert(3); 
        
//        
//        if ( 
//        	$('.browser').html() != 'Internet Explorer 7' && 
//        	$('.browser').html() != 'Internet Explorer 6' 
//        )
//        {
        
	        $('.nav-main .level-1').hover(
	        	function ()
	        	{
	        		$(this).addClass('active');
	        	
	        	},
	        	function ()
	        	{
	        		$(this).removeClass('active');
	        	
	        	}
	        );
		
	        
        
        
        
        // prettyPhoto
        
        if( $('.root-produkte').exists() )
        {
            $("[rel^='lightbox']").prettyPhoto(
                {
                    default_width: 600,
        			show_title: true,
        			deeplinking: false,
        			social_tools: ''
                }
            );
            
            $('.root-produkte .masthead .more').click(
            	function()
            	{
            		if( $('.root-produkte .masthead .description .more').attr('rel') == 'up' )
            		{
	            		$('.root-produkte .masthead .description .extra').slideDown();
	            		$('.root-produkte .masthead .description .more').attr('rel','down');
	            		$('.root-produkte .masthead .description .more').text('ausblenden ▲');
            		} 
            		else if( $('.root-produkte .masthead .description .more').attr('rel') == 'down' )
            		{
            			$('.root-produkte .masthead .description .extra').slideUp();
            			$('.root-produkte .masthead .description .more').attr('rel','up');
            			$('.root-produkte .masthead .description .more').text('weiterlesen ▼');
            		}
            	}
            );
            
            
            // no colors
            
            $('.root-produkte .variants .variant:first').addClass('active');
            $('.tabs .card .tab-2 .ingredients .unique').html( 
            	$(this).find('.root-produkte .variants .variant:first .unique-ingredients').html() );
            
            $('.root-produkte .variants .variant').click(
            	function()
            	{
            		$('.variants .variant').removeClass('active');
            		$(this).addClass('active');
            		
            		$('.product-full-size').fadeOut();
            		$('.product-full-size.position-' + $(this).attr('rel')).fadeIn();
            		
            		$('.tabs .card .tab-2 .ingredients .unique').html( $(this).find('.unique-ingredients').html() );
            		
            	}
            );
            
            
            
        }
        
        
        
        
        
        
        if( $('.page-home, .page-produkte').exists() )
        {
        	//$('.masthead-0').css('filter', 'alpha(opacity=40)');
            //$('.masthead-0').css('opacity', 50);
            //$('.masthead-0').css('filter', 'progid:DXImageTransform.Microsoft.Alpha(opacity=30)');
			
			var animationSpeed = 500;
			
            if( $('.browser').html() == 'Internet Explorer 8' || $('.browser').html() == 'Internet Explorer 7' || $('.browser').html() == 'Internet Explorer 6')
            {
            	animationSpeed = 0;
            }
            
            
            $('.nav-product a').hover(
            
                function () 
                {
                	
                    $('.' + $(this).attr('rel')).fadeIn(animationSpeed);
                },
                function () 
                {
                    $('.' + $(this).attr('rel')).fadeOut(animationSpeed);
                }
            );
            
            $('.nav-product').hover(
            
                function () 
                {
                    $('.masthead-0').fadeOut(animationSpeed);
                },
                function () 
                {
                    $('.masthead-0').fadeIn(animationSpeed);
                }
            );
        }
        


        // back to top
        $('.back_to_top').click( //↑
            function() 
            {
                $('html, body').animate(
                    {
                        scrollTop: 0
                    }, 
                    1000, 
                    'easeInOutCubic'
                );
                
                return false;
            }
        );
        
        
        
        $('.input-swap').focus( inputSwap );
        $('.input-swap').blur( inputSwap );
        
        
        
        // iphone
        if($('.ismobile').exists())
        {
            if (
                navigator.userAgent.indexOf('iPhone') != -1 ||
                navigator.userAgent.indexOf('iPad') != -1
            ) 
            {
                window.scrollTo(0, 1);
            }
        }
    }
);

$(window).load(
    function() 
    {
        
        var distanceFromWall = 0;
        
        activeIndex = $('.nav-page .active').parents('td').index();
        
        for( i = 0; i < activeIndex; i++ )
        {
            distanceFromWall += $('.nav-page td:eq(' + i + ')').width();
            
            
        }
        
        $('.nav-page .highlight').css(
            {
                'left': distanceFromWall,
                'width': $('.nav-page td:eq(' + activeIndex + ')').width(),
                'height': '1px',
                'top': '32px'
            }
        ).animate(
            { 
                'height': '15px'
            }, 
            500, 
            'easeOutQuint'
        ); 
        
        var tallGrass = 0;
        
        if ( $('body.lang-fr').exists() || $('body.lang-es').exists() )
        {
        	tallGrass = 10
        }
        
        activeIndex = $('.nav-product .active').index();
        
        var marginOffset = ( $('.nav-product .insulation').width() - $('.nav-product .insulation table').width() ) / 2;
        
        for( i = 0; i < activeIndex; i++ )
        {
            distanceFromWall += $('.nav-product td:eq(' + i + ')').width() + 16;
        }
        
        if( activeIndex == -1 ) { $('.nav-product .highlight').css('display', 'none'); }
        
        $('.nav-product .highlight').css(
            {
                'left': distanceFromWall + 27 + marginOffset,
                'width': $('.nav-product td:eq(' + activeIndex + ')').width(),
                'height': '1px',
                'top': 82
            }
        ).animate(
            {
                'height': 15 + tallGrass
            }, 
            500, 
            'easeOutQuint'
        ); 
    }
);



function inputSwap()
{
    var defaultVal = $(this).parents('form').find('.default').html();
    
    if( $(this).val() == defaultVal )
    {
        $(this).val('');
    }
    else if ( !$(this).val() )
    {
        $(this).val( defaultVal );
    }
}



// scroll to this
function scrollToThis(obj)
{
    $('html, body').animate(
        {
            scrollTop: $(obj).offset().top
        }, 
        1000, 
        'easeInOutCubic'
    );
    
    return false;
}  



jQuery.fn.exists = function() // test to see if an object exists
{
    return jQuery(this).length > 0;
}





// sante analytics
/*
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-26581600-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
*/
