var Thermacare = {}
Thermacare.Startpage = {

    init:function(){
    	Thermacare.Startpage.isLoaded = false;
    	Thermacare.Startpage.contentItems = new Array();
        this.currentTeaser = 3;
        this.timerActive = 1;
        Thermacare.Startpage.startTimer();
        this._prepareTeaser();
        this._prepareMainNavigation();
        if($('div#startsite-teaser')){Thermacare.Startpage.isActive = false;}else{Thermacare.Startpage.isActive = true;}
    },
    
   	viewAnimation: function (){
        if(navigator.userAgent.indexOf('Windows NT 5.1') == -1){
        	return true;
        }else{
        	if(navigator.userAgent.indexOf('MSIE') == -1){return true;}else{return false;}
        }
    },
    
    startTimer: function(){Thermacare.Startpage.timer = setTimeout("Thermacare.Startpage.responseTimer()",5000);},
    
    responseTimer: function (){
        if(Thermacare.Startpage.timerActive == 1){
            Thermacare.Startpage.currentTeaser++;
            if(Thermacare.Startpage.currentTeaser ==  $('div#startsite-teaser ul li').size()){Thermacare.Startpage.currentTeaser = 0; }
            $('div#startsite-teaser ul li').each(function(){
                if($(this).index() == Thermacare.Startpage.currentTeaser){$(this).fadeIn(1000);}else{$(this).fadeOut(1000);}
            })
            
            $('div#main-navigation ul li').each(function(){
                if($(this).index() == Thermacare.Startpage.currentTeaser){
                    $(this).addClass('nav-mouseover nav-current-active');
                    if($(this).hasClass('first-item')){$(this).addClass('first-item-active');}
                    if($(this).hasClass('last-item')){$(this).addClass('nav-facebook-current');}
                }else{
                    $(this).removeClass('nav-mouseover nav-current-active');
                    if($(this).hasClass('first-item')){$(this).removeClass('first-item-active');}
                    if($(this).hasClass('last-item')){$(this).removeClass('nav-facebook-current');}
                }
            });        
        	Thermacare.Startpage.startTimer();
		}else{return false;}
    },
    
    _prepareMainNavigation: function(){
       $('div#main-navigation ul li:eq(2)').addClass('nav-mouseover nav-current-active');
        Thermacare.Startpage.navigationItems = new Array();
        $('div#main-navigation ul li').each(function(){
            var link = $(this).find('a');
            var index = $(this).index();
            if(Thermacare.Startpage.viewAnimation()){
             	/*if(!$(this).hasClass('last-item')){*/
            		Thermacare.Startpage.navigationItems[index] = link.attr('href');
            		link.attr('href', 'javascript:void(0)');
            		link.css('cursor', 'pointer');
            	/*}*/
            }
            link.click(function(){
            	if(Thermacare.Startpage.viewAnimation()){
                	/*if(!link.parent().parent().parent().hasClass('last-item')){*/
							Thermacare.Startpage.currentNavClickItem = this;
                			$(this).addClass('load');
							Thermacare.Startpage.viewContent(index);
                			$('div#main-navigation ul li').each(function(){
                    			$(this).removeClass('nav-mouseover nav-current-active');
                    			if($(this).hasClass('first-item')){$(this).removeClass('first-item-active');}
                    			if($(this).hasClass('last-item')){$(this).removeClass('nav-facebook-current');}
                			});
                			$(this).parent().parent().parent().addClass('nav-mouseover nav-current-active');
                			if($(this).parent().parent().parent().hasClass('first-item')){$(this).parent().parent().parent().addClass('first-item-active');}
                			if($(this).parent().parent().parent().hasClass('last-item')){$(this).parent().parent().parent().addClass('nav-facebook-current');}
                			$(this).unbind('mouseover');
                			location.hash = Thermacare.Startpage.navigationItems[index];

                			
                	/*}*/
               }
           }).mouseover(function(){
                if(Thermacare.Startpage.isActive == false){
                    Thermacare.Startpage.timerActive = 0;
                  	$('div#startsite-teaser ul li').each(function(){
                        if($(this).index() == index){$(this).fadeIn();}else{$(this).fadeOut();}
                        $('div#main-navigation ul li').each(function(){
                        	$(this).removeClass('nav-mouseover nav-current-active');
                        	if($(this).index() == 0){
                    			$(this).removeClass('first-item-active');
                    		}else if($(this).index() == 3){
                    			$(this).removeClass('nav-facebook-current last-current');
                    		}
                        });
                    })
                    $(this).parent().parent().parent().addClass('nav-mouseover nav-current-active');
                    if($(this).parent().parent().parent().index() == 0){
                    	$(this).parent().parent().parent().addClass('nav-mouseover nav-current-active first-item-active');
                    }else if($(this).parent().parent().parent().index() == 3){
                    	$(this).parent().parent().parent().removeClass('nav-mouseover nav-current-active');
                    	$(this).parent().parent().parent().addClass('nav-facebook-current last-current');
                    }

                }
            }).mouseout(function(){
                $(this).parent().removeClass('nav-mouseover');
            });
         });
     },
   
    _prepareTeaser: function(){
        $('div#startsite-teaser ul li').each(function(){
            if($(this).index() == 2){$(this).show();}
        });
    },
    
    viewContent:function(index){
        if(!Thermacare.Startpage.contentItems[index]){
        	 $.get(Thermacare.Startpage.navigationItems[index], function(data){
        	 	Thermacare.Startpage.contentItems[index] = data.replace('<script type = "text/javascript" src = "/fileadmin/resources/js/jquery.js"></script><script type = "text/javascript" src = "/fileadmin/resources/js/jqueryui.js"></script><script type = "text/javascript" src = "fileadmin/resources/js/base.js"></script>', '');
        	 	Thermacare.Startpage.viewContent(index);
        	 	
        	 });
        	 return false;
        }
        $('div.startpage div#startsite-2col-teaser').fadeOut(1000);
		$('div#main-content-container').animate({opacity:0}, 1000);
		$('div#main-teaser-image').animate({opacity:0}, 1000);
		$('div#footer-sitemap').animate({opacity:0}, 1000);
		$('div#footer-sitemap').css('display', 'none');
		$('div#footer-brands').animate({opacity:0}, 1000);
		Thermacare.Startpage.currentContent = $('div#ajaxContent');
      
        var content = '<div id="ajaxContent" style="display:none; position:absolute;left: 50%;margin-left: -495px;width: 980px;top:164px; opacity:0;filter: alpha(opacity=0);-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";">'+Thermacare.Startpage.contentItems[index]+'</div>';
        $(content).appendTo('#wrapper');
        $('div#ajaxContent head').remove();
        $('div#ajaxContent link').remove();
        $('div#ajaxContent meta').remove();
        $('div#ajaxContent script').remove();
        $('div#ajaxContent div#header').remove();
        $('div#ajaxContent div#main-navigation').remove();
        $('div#ajaxContent').show();
        if(Thermacare.Startpage.isActive == false){
            $('div#main-navigation').css('position', 'absolute');
            $('div#main-navigation').css('top',507);
            $('div#main-navigation').css('z-index',1000);
            $('div#startsite-teaser').css('z-index',1000);
            $('div#startsite-2col-teaser').css('margin-top', 78);
            $('div#ajaxContent div#main-content-container').css('display', 'none');
            $('div#ajaxContent div#footer-copyright').css('display', 'none');
            $('div#ajaxContent div#main').css('background', 'none');
           	$('div#ajaxContent').css('opacity', 1);
            $('div#startsite-teaser ul').animate({top:-410},1000);
            
            $('div#main-navigation').animate({top:97},1000, function(){
            	$('div#ajaxContent div#main-content-container').fadeIn(500,function(){
                     Thermacare.Startpage._transformContent();
                    $('div#main-navigation ul li').each(function(){
                        var link = $(this).find('a');
                        link.unbind('click');
                        var index = $(this).index();
                        link.attr('href', Thermacare.Startpage.navigationItems[index]);
                    });
                    $("div#wrapper").removeClass('startpage');
                    $(Thermacare.Startpage.currentNavClickItem).removeClass('load');
                });
            
            });
            Thermacare.Startpage.isActive = true;
        }else{
            Thermacare.Startpage.currentContent.fadeOut(500,function(){
            	Thermacare.Startpage._transformContent();$('div#ajaxContent').animate({ opacity:1},{duration: 500,specialEasing: {top: 'linear'},complete: function(){}});
            });
        }
     },
    
    _transformContent: function(){
        Thermacare.Startpage.currentContent.remove();
        $('div#startsite-teaser').css('visibility', 'hidden');
        Thermacare.Startpage.currentFooter = '<div id="footer-copyright">'+$('div#footer-copyright').html()+'</div>';
        $('div#footer-copyright').remove();
        $('div#ajaxContent div#main').css('background', 'none');
        $('div#ajaxContent div#main-content-container').css('visibility', '');
        $('div#ajaxContent div#footer-sitemap').css('visibility', '');
        $('div#ajaxContent div#footer-brands').css('visibility', '');
        $('div#ajaxContent div#main-teaser-image').css('visibility', '');
        $('div#ajaxContent div#footer-copyright').remove();
        $('body div#wrapper').css('height', $('div#ajaxContent').height() + 165);
        $('div#ajaxContent div#wrapper').css('height','');
        $('body').append(Thermacare.Startpage.currentFooter);
        $('div.footer-brands-images img').each(function(){
        $(this).mouseover(function(){$(this).attr('src', $(this).attr('src').replace('.png', '_mo.png'));}).mouseout(function(){$(this).attr('src', $(this).attr('src').replace('_mo.png', '.png'));});
    });
    }
    
    
    
};


Thermacare.FontResizer = {

    internalFontSize: 0,
    internalFontSizeMin: -3,
    internalFontSizeMax: 3,
    defaultPercentFontSize: 90,
	init: function (){
		$('#font-resizer ul li a').each(function(){
			$(this).attr('href', 'javascript:');
			$(this).click(function(){
				var type = "medium";
				switch($(this).index()){case 0:type = 'small';break;case 1:type = 'medium';break;case 2:type = 'big';break;}
				Thermacare.FontResizer.resize(type);
			});
		});
	},

	resize: function (type){
    	if(type != "null"){
    		var include = ('<link rel="stylesheet" type="text/css" href="/fileadmin/resources/css/'+type+'.css" media="screen" title="Standardansicht" />');
    		$('head').append(include);
    	 	document.cookie = "font="+type+'; path=/';
    	}
    	
    }
   
};

Thermacare.Social = {
    
    registerSocialLinks:function(){
        if($('div.socialmedia-links')){
            $('div.socialmedia-links li.facebook').click(function(){
            	window.open('http://www.facebook.com/sharer.php?u='+location.href+'&t='+ document.title);
            });
            $('div.socialmedia-links li.twitter').click(function(){
                window.open('http://twitter.com/home?status=Lese gerade '+ document.title + ' auf '+ location.href);
            });
            $('div.socialmedia-links li.print').click(function(){
                 window.open(location.href+'?type=98');
            });
        }
    }
};

Thermacare.Lexikon = {

    init:function(){this.loadData();},
    
    loadData: function (){
    	 $.ajax({
            url: "/index.php?tx_thermacarelexikon_lexikon%5Bcontroller%5D=Main&%20tx_thermacarelexikon_lexikon%5Baction%5D=getJson&type=89657201",
            dataType: "json",
            success: function( jsonResponse ) {
                var data = new Array();
                jQuery.each(jsonResponse.entries, function(i,val) {
                	var dataArray = new Object();
                        dataArray['value'] = val.title;
                        dataArray['uid'] = val.uid;
                        dataArray['title'] = val.title;
                        dataArray['section'] = val.section;
                        dataArray['keywords'] = val.keywords;
                        data.push( dataArray );
                });     
                Thermacare.Lexikon.jsonData = data;
				Thermacare.Lexikon.initAccordion();
        		Thermacare.Lexikon.initSearch();	
            }
        });
    },

    initAccordion:function(){
    
        var hash = window.location.hash;
        if(hash != 0){
            var section =hash.replace('#','');
            $(".accordion").accordion( {collapsible: true } );
             $(".accordion").accordion('activate', false);
            var open = Thermacare.Lexikon.getIndexByKeyword(section);
            $(".accordion").accordion('activate', open);
            var replacer = new RegExp(" ","g");
            var keywordClass = Thermacare.Lexikon.currentKeyword.replace(replacer,'-');
            $("html").animate({scrollTop:$("a."+keywordClass).position().top}, 'slow'); 
       }else{
         $(".accordion").accordion({
            collapsible: true,
            active: false
        });
        }

    },

    initSearch: function(){
                $( "#keywords" ).autocomplete({
                    source: Thermacare.Lexikon.jsonData,
                    minLength: 0,
                    select: function( event, ui ) {
                    $(".accordion").accordion( {collapsible: true } );
             		$(".accordion").accordion('activate', false);
						Thermacare.Lexikon.openItem = false;
                    	Thermacare.Lexikon.getIndexByKeyword(ui.item.keywords);
                        $(".accordion").accordion('activate', false);
                        $(".accordion").accordion('activate', Thermacare.Lexikon.openItem);
                        var replacer = new RegExp(" ","g");
                        var keywordClass = ui.item.keywords.replace(replacer,'-');
                        $("html").animate({scrollTop:$("a."+keywordClass).position().top}, 'slow'); 
                    }
                });
    },
    
    getIndexByKeyword: function (keyword){
    	jQuery.each(Thermacare.Lexikon.jsonData, function (index, obj){
     		if(obj.keywords.indexOf(keyword) >= 0){
    			Thermacare.Lexikon.openItem = index;
    			Thermacare.Lexikon.currentKeyword = obj.keywords;
    		}
    	});
    	return Thermacare.Lexikon.openItem;
   	},

    jsonData: [],

    initTooltips:function(){
        $.ajax({
            url: "/index.php?tx_thermacarelexikon_lexikon%5Bcontroller%5D=Main&%20tx_thermacarelexikon_lexikon%5Baction%5D=getJson&type=89657201",
            dataType: "json",
            success: function( jsonResponse ) {

                var data = new Array();
                jQuery.each(jsonResponse.entries, function(i,val) {
					var dataArray = new Object();
                        dataArray['value'] = val.keywords;
                        dataArray['uid'] = val.uid;
                        dataArray['title'] = val.title;
                        dataArray['text'] = val.text;
                        dataArray['image'] = val.image;
                        dataArray['section'] = val.section;
                        data.push( dataArray ); });

                Thermacare.Lexikon.jsonData = data;
              $('a.lexikon').each(function(index) {
                   $(this).attr('href', null);
                   $(this).bind({
                        click: function( evt ){
                            if ($("#tooltip").css("opacity") != 0) {
                                //$("#tooltip").stop().remove();
                            }
                            Thermacare.Lexikon.initTooltipContent( evt );
                        }
                    });

                });
            }
        });
   },

    initTooltipContent:function( evt ){
        var tooltipHtml;
        $.each(Thermacare.Lexikon.jsonData, function(index, obj) {
            if(obj.value.indexOf($(evt.currentTarget).attr('rel')) >= 0 ){
                tooltipHtml =  "<div class='tooltip_container clearfix'><div class='tooltip_close'><img src=\"/fileadmin/resources/images/ui/X-CLOSE-Tooltip.jpg\" /></div>";
                var cropTextLength = 180;
                if(obj.image){
                    tooltipHtml += "<img src='/uploads/tx_thermacarelexikon/" + obj.image + "' />";
                    var cropTextLength = 120;
                }
              var croppedText = obj.text.substring(0, cropTextLength);
                tooltipHtml += "<p>" + croppedText + "...</p>";
                tooltipHtml += "<p><a href='/index.php?id=10#"+$(evt.currentTarget).attr('rel') +"' target='_blank'>weiterlesen</a></p>";
               tooltipHtml += "</div>";
            }

        });

        $("<div>", {
            id: "tooltip",
            css: {
                position: "absolute",
                left: evt.pageX + 20,
                top: evt.pageY + 20,
                background: "#FFF",
                display: "none",
                border: "1px solid #aaa"
            },
            html: tooltipHtml
        })
        .appendTo("body")
        .fadeIn(500, function(){
        	Thermacare.Lexikon.setToolTipDocumentCloseEvent(this);
        });
        
        $('#tooltip .tooltip_close').bind({
            click: function( evt ){
                $("#tooltip").fadeOut(
                    500,
                    function(){
                        $(this).remove();
                    }
                    );
            }
        });
       
    },
    
    setToolTipDocumentCloseEvent: function(tooltip){
    	$(document).click(function(){
        		$(tooltip).fadeOut(500, function(){
             		 	$(this).remove();
                    }
             	);
             	
       		 });
       		
       		 
    }

};


Thermacare.Contactform = {
    init:function(){
        Thermacare.Contactform.defaultValues = {};
        $('form#contactform input').each(function(){
            Thermacare.Contactform.defaultValues[$(this).attr('id')] = $(this).val();
            $(this).focus(function(){
                if($(this).val() == Thermacare.Contactform.defaultValues[$(this).attr('id')]){$(this).val(''); }
            }).blur(function(){
                if($(this).val() == ''){
                    $(this).val(Thermacare.Contactform.defaultValues[$(this).attr('id')]);

                }
            });

        })
    }
    
}

Thermacare.Search = {
    init: function(){
        $('input#global-search-query').focus(function(){
            if($(this).val() == "Suche"){
                $(this).val('');
            }
        }).blur(function(){
            if($(this).val() == ''){
                $(this).val('Suche');
            }
        }).keypress(function(e){
            var code = (e.keyCode ? e.keyCode : e.which);
            if(code == 13) { //Enter keycode
                if($(this).val() != "Suche"){
                    location.href = "/service/suche.html?no_cache=1&tx_indexedsearch[sword]="+$('input#global-search-query').val();
                }
            }
       });
        
        $('div#global-search-submit').click(function(){
            if($(this).val() != "Suche"){
                location.href = "/service/suche.html?no_cache=1&tx_indexedsearch[sword]="+$('input#global-search-query').val();
            }
        });
    }
}

Thermacare.Gallery = {
    init:function(){
        this.galleryWidth = 690;
        
        this._prepareGalleryPages();
        this._prepareMainNavigation();
        
    },
    
    _prepareGalleryPages: function(){
        $('ul.se-gallery-content-container').css('width', $('.se-gallery-content-container li').length * this.galleryWidth);
        $('ul.se-gallery-content-container li').each(function(){
            Thermacare.Gallery._prepareGalleryPage(this);
        });
    },
    
    _prepareGalleryPage: function(item){
    //var $(item).find(''));
    },
    
    _prepareMainNavigation: function(){
        var width = 0;
        
        $('ul.se-gallery-navigation li').each(function(){
            width = width+($(this).width()+50);
            $(this).click(function(){
                var index = $(this).index();
                $('ul.se-gallery-content-container').css('margin-left', -(index*Thermacare.Gallery.galleryWidth));
                $('div.se-gallery-content-breadcrumb ul.se-gallery-content-breadcrumb-list li').each(function(){
                    if($(this).index() > 0){
                        $(this).remove();
                    }
                }
                );
                
                $('div.se-gallery-content-breadcrumb ul.se-gallery-content-breadcrumb-list').append('<li>'+$(this).html()+' ></li>');
                
            });
        });
        $('ul.se-gallery-navigation').css('width', width);
    }
    
}

Thermacare.Access = {
    init: function(){
        $('div#footer-copyright-content a').each(function(index){
            switch(index){
                case 7:
                    $(this).attr('accesskey', 2);
                    break;
                case 5:
                    $(this).attr('accesskey', 6);
                    break;
                case 6:
                    $(this).attr('accesskey', 3);
                    break;
                case 3:
                    $(this).attr('accesskey', 7);
                    break;
                case 4:
                    $(this).attr('accesskey', 8);
                    break;
                case 8:
                    $(this).attr('accesskey', 0);
                    break;
            }
        
        });
    
        $('div#header div#meta a:eq(2)').attr('accesskey', 9);
        $('div#logo a').attr('accesskey', 1);
        $('div#main-content-container').prepend('<a id="maincontent" accesskey="s" name="maincontent" href="'+location.href+'#maincontent"></a>');
    }
}


$(document).ready(function(){

	if(location.href.indexOf('.php') == -1){
		$(window).bind('hashchange', function() {
   			if(location.hash == ""){
   				location.href = '/';
   			}
   		});

		if(location.hash){
       		var hash = location.hash.replace('#', '');
       		location.href = hash;
       }
   }
   $('a').each(function(){this.blur();});
    
	Thermacare.Access.init();
    if($('div.startpage').length > 0){
        Thermacare.Startpage.init();
    }
    
   if(document.cookie){var font = getCookie("font");Thermacare.FontResizer.resize(font);}
    
   Thermacare.Social.registerSocialLinks();

	Thermacare.Search.init();
    Thermacare.FontResizer.init();
    
    if($('se-gallery')){Thermacare.Gallery.init();}

    if($('a.lexikon').length > 0){
        Thermacare.Lexikon.initTooltips();
    }
    if($('div.accordion').length > 0){
        Thermacare.Lexikon.init();
    }
    
    $('div.footer-brands-images img').each(function(){
        $(this).mouseover(function(){
            $(this).attr('src', $(this).attr('src').replace('.png', '_mo.png')); 
        }).mouseout(function(){
            $(this).attr('src', $(this).attr('src').replace('_mo.png', '.png'));
        });
    });
    

});

function getCookie(nam) {
    
        var dc   =  document.cookie;
        if(dc.indexOf(nam + "=") != -1) {
            var anf  =  dc.indexOf(nam) + nam.length + 1;
            var ende =  (dc.indexOf(";", anf) != -1)? dc.indexOf(";", anf) : dc.length;
            return unescape(dc.substring(anf,ende)); 
        }
        else return null;
    }
    
    
/* IE PNG fix multiple filters */
(function ($) {
    if (!$) return;
    $.fn.extend({
        fixPNG: function(sizingMethod, forceBG) {
                if (!($.browser.msie)) return this;
                var emptyimg = "empty.gif"; //Path to empty 1x1px GIF goes here
                sizingMethod = sizingMethod || "scale"; //sizingMethod, defaults to scale (matches image dimensions)
                this.each(function() {
                        var isImg = (forceBG) ? false : jQuery.nodeName(this, "img"),
                                imgname = (isImg) ? this.src : this.currentStyle.backgroundImage,
                                src = (isImg) ? imgname : imgname.substring(5,imgname.length-2);
                        this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + sizingMethod + "')";
                        if (isImg) this.src = emptyimg;
                        else this.style.backgroundImage = "url(" + emptyimg + ")";
                });
                return this;
        }
    });
})(jQuery);


