//$(function(){
$(document).ready(function() {

    //cookie handling
    if (!$.cookie('hauff_solution_map')) {
        var date = new Date();
        date.setTime(date.getTime() + (720 * 60 * 1000));
        $.cookie('hauff_solution_map', 'opened', {path: '/', expires: date});
    } else if ($.cookie('hauff_solution_map') == 'closed') {
        /*
        $(".flash-part .flash-bt").parent().stop().animate({height:"140px"},1,"",function(){
            $(".flash-bt").addClass("aa");
        });
        */
        $(".flash-part").css({height:"140px"});
        $(".flash-bt").addClass("aa");
    }


    
    //main-nav
    $(".dl-part > dl").each(function(){
        if ($(this).children('dd').size() == 2) {
            var tDdFirst = $(this).children('dd:first');
            $(this).children('dd:first').replaceWith($(this).children('dd:last'));
            $(this).append(tDdFirst);
        }
    });
    $(".sub-nav").hide();
    $(".main-nav>li").each(function(){
        $(this).hover(function(){
            $(this).children(".sub-nav").show();
            $(">a",$(this)).addClass("selected");
            if ($.cookie('hauff_solution_map') == 'opened') {
                $(".flash-part").stop().animate({height:"140px"},1000,"",function(){
                    updateCookie();
                });
            }
            $(".flash-part .flash-bt").addClass("aa");
            if ($(this).children(".sub-nav").length > 0) {
                $(".flash-part .flash").css('visibility', 'hidden');
            }
        },function(){
            $(this).children(".sub-nav").hide();
            $(">a",$(this)).removeClass("selected");
            $(".flash-part .flash").css('visibility', 'visible');
        })
    });

    //flash-part slide        
    $(".flash-part .flash-bt").hide();
    $(".flash-part").hover(function(){
        $(".flash-part .flash-bt").show();
    },function(){
        $(".flash-part .flash-bt").hide();
    });

    $(".flash-part .flash-bt").click(function(){
        if($(this).hasClass("aa")){
           openSM();
        } else {
            $(this).parent().stop().animate({height:"140px"},1000,"",function(){
                $(".flash-bt").addClass("aa");
                updateCookie();
            });
        }
    })
        
        
        $("ul.tab-list li a").attr({"href":"javascript:void(0)"});
    
        $(".tabs-part").each(function(index){
			var currId = $(this).attr('id');
			$("#"+currId+" .panel:gt(0)").hide();
			$("#"+currId+" ul li:eq(0)").addClass("selected");
			$("#"+currId+" ul li").each(function(index){
				var obj=$(this);
				obj.click(function(){
					obj.addClass("selected").siblings().removeClass("selected");
					$("#"+currId+" .panel:eq("+index+")").show().siblings().hide();	
					})
				})
		});
            
            
            
            
        //pop-up    
        //$(".link-02 a").attr({"href":"javascript:void(0)"});
        $(".pop-up").hide();        
        $(".link-02 a").click(function(){
			$(this).addClass("selected");
			var tPopup = $(this).parent().next();
			var tUrl = $(this).attr('href');
			//console.log(tUrl);
			$.ajax({
				type: "GET",
				url: tUrl,
				success: function(retVal){
					$(".mlAmount").text(retVal);
					tPopup.show().delay(3000).fadeOut();
				}
			});
			return false;
		});
        
        $(".pop-up .close").each(function(){
                $(this).click(function(){
                    $(this).parent().hide();
                    $(".link-02 a").removeClass("selected");
                })
            })
    
    
    
    //init accordion effect    
    $(".faqs-list dl dd").hide();
    $(".faqs-list .faq-item p").hide();
    $(".faqs-list dl dt.ac").addClass("active");
    $(".faqs-list .faq-item h1.ac").addClass("active");
    $(".faqs-list .faq-item p.open").show();

    //accordion effect for content elemens with border type accordion
    $(".faqs-list .faq-item:last").addClass("last");
    $(".faqs-list .faq-item h1").click(function(){
        $(".faqs-list .faq-item h1").not($(this)).removeClass("active").next().slideUp();
        $(this).toggleClass("active").next().slideToggle();
    });

    //accordion effect for FAQs
    $(".faqs-list dl dt").click(function(){
        $(".faqs-list dl dt").not($(this)).removeClass("active").next().slideUp();
        $(this).toggleClass("active").next().slideToggle();
    });

    //accordion effect for downloads
    //$(".download-list dl dt.ac").addClass("active");
    //$(".download-list dl dt").click(function(){
    //    $(this).toggleClass("active").next().slideToggle();
    //});

  	$('.download-list dl dt').click(function() {
  		$(this).next().slideToggle();
  		$(this).toggleClass("active")
  		return false;
  	}).next().hide();    
 
	$("select").change(function () {
		$(this).parent().submit();
	});
 
     //glossar-tab
        /*
        $("ul.glossar-tab-list li a").attr({"href":"javascript:void(0)"});
        $(".glossar-panel-part .glossar-panel:not(:eq(10))").hide();
        $(".glossar-tab ul.glossar-tab-list li:eq(10)").addClass("selected");
        $("ul.glossar-tab-list li").each(function(index){
            var obj=$(this);
            obj.click(function(){
                obj.addClass("selected").siblings().removeClass("selected");
                $(".glossar-panel:eq("+index+")").show().siblings().hide();    
                })
            })
        */

    //left-menu            
    function initMenu() {
            /*
    $('.left-menu ul').hide();
    
    $('.left-menu>li>a').click(function(){
        $('.left-menu>li').removeClass('selected');
        $('.left-menu>li>a').not($(this)).removeClass("checked").next().slideUp();
        $(this).addClass("checked").next().slideDown();
        $('.level-02>li>a').removeClass("checked");
        })
    */
    /*
    $('.level-02 .list').hide();
    
    $('.level-02>li>a').click(function(){
        $('.level-02>li>a').not($(this)).removeClass("checked").next().slideUp();
        $(this).addClass("checked").next().slideDown();
        $(".list dl dd a").removeClass("hover");
        $(".list dl dt").removeClass("hight");
        })
    
    
        $(".list dl dd a").click(function(){
        $(".list dl dt").removeClass("hight");
        $(".list dl dd a").removeClass("hover");
        $(this).addClass("hover").parents("dl").children("dt").addClass("hight");
        })
    */
    
   }
		
	initMenu();
    
	if(prodLiveSearch = $("#tx_4dproducts_pi1_searchtxt")) {
		var prdSrchUrl = prodLiveSearch.attr('title') + '?tx_4dproducts_pi1[act]=prdLvSrch';
		//console.log(prdSrchUrl);
		
		prodLiveSearch.indexedsearchAutocomplete( prdSrchUrl , { 
				minChars:3, 
				matchSubset:1, 
				matchContains:1, 
				cacheLength:20, 
				selectOnly:1
			}
		);
		
		
	}
	

    // download section
    // categories
    $('#categories').change(function(){
      var id_cat=$(this).children('option:selected').val();
      $('#productcategories').load('index.php?eID=cat&L='+globLang,{value: id_cat}); 
      var type = $('#downloadtypes').val();
      var source = $('#source').val();      
      $('#list-content').load('index.php?eID=cat_list&prcat=false&L='+globLang,{value: id_cat, type: type, source: source});
    	$('.download-list dl dt').next().hide();                    
    });  
    // productcategories 
    $('#productcategories').change(function(){
      var id_cat=$(this).children('option:selected').val();
      var type = $('#downloadtypes').val();
      var source = $('#source').val();      
      $('#list-content').load('index.php?eID=cat_list&L='+globLang,{value: id_cat, type: type, source: source});
    	$('.download-list dl dt').next().hide();      
    });      

    // downloadtypes 
    $('#downloadtypes').change(function(){
      var type=$(this).children('option:selected').val();
      var id_cat = $('#productcategories').val();
      var source = $('#source').val();      
      $('#list-content').load('index.php?eID=cat_list&L='+globLang,{value: id_cat, type: type, source: source});
    	$('.download-list dl dt').next().hide();      
    });      

    // source 
    $('#source').change(function(){
      var source=$(this).children('option:selected').val();
      var id_cat = $('#productcategories').val();
      var type = $('#downloadtypes').val();      
      $('#list-content').load('index.php?eID=cat_list&L='+globLang,{value: id_cat, type: type, source: source});
    	$('.download-list dl dt').next().hide();      
    });

    $('#reset').click(function(){
      $('#categories option:first').attr('selected',true);
      var id_cat=$('#categories').children('option:first').val();
      $('#productcategories').load('index.php?eID=cat&L='+globLang,{value: id_cat});
      $('#source option:first').attr('selected',true);
      $('#downloadtypes option:first').attr('selected',true);      
      var source = $('#source').val();
      var type = $('#downloadtypes').val();      
      $('#list-content').load('index.php?eID=cat_list&prcat=false&L='+globLang,{value: id_cat, type: type, source: source});
    	$('.download-list dl dt').next().hide();     
    });

    // delete all
    $('#delete_all').click(function(){    
      $('#stackcount').load('index.php?eID=deleteStack',{value: 1});
      $('#download-list dl').hide();
      $('.als-zip').attr('href', 'javascript: void(0)');
    });     

    // send
    $('.versenden').click(function(){    
      $('.mail').show();
    });


});


function updateCookie() {
	if (jQuery.cookie('hauff_solution_map') == 'opened') {
		jQuery.cookie('hauff_solution_map', 'closed', {path: '/'});
	} else {
		jQuery.cookie('hauff_solution_map', 'opened', {path: '/'});
	}
}


function openSM() {
   jQuery('.flash-part').stop().animate({height:"430px"},1000,"",function(){
		jQuery(".flash-part .flash-bt").removeClass("aa");
		updateCookie();
	});
}

// source outside the DOM ready function
function changeSource(source){
  var source = $('#source').val();
  var id_cat = $('#productcategories').val();
  var type = $('#downloadtypes').val();      
  $('#list-content').load('index.php?eID=cat_list&L='+globLang,{value: id_cat, type: type, source: source});
	$('.download-list dl dt').next().hide();     
}
function nextPage(page){
  var source = $('#source').val();
  var id_cat = $('#productcategories').val();
  var type = $('#downloadtypes').val();      
  $('#list-content').load('index.php?eID=cat_list&L='+globLang,{value: id_cat, type: type, source: source, page: page});
	$('.download-list dl dt').next().hide();     
}
function addToStack(file){   
  $('#stackcount').load('index.php?eID=addStack&L='+globLang,{file: file});     
}
function sendStack(file){   
  var mail = $('#mail').val();
  $('.mailtext').load('index.php?eID=sendStack&L='+globLang,{file: file, mail: mail}); 
  $('#mail').hide();
  $('.send').hide();    
}
