var badBrowser = (/MSIE ((5\.5)|6|7|8)/.test(navigator.userAgent) && navigator.platform == "Win32"); 
var badBrowser67 = (/MSIE ((5\.5)|6|7)/.test(navigator.userAgent) && navigator.platform == "Win32"); 
var $loading = $('#loading');

var curIndex = 0;

function ladeNaechstesBild() {
    if (curIndex == prodliste.length)
        return;
    var curImg = $('#'+prodliste[curIndex++]);
    //console.log(curImg);
    curImg.attr("src", curImg.attr("llsrc"));
    curImg.load(function () {
        ladeNaechstesBild();
    });
}

function ladeHS() {
    $('#searchribhs').load('ajalistehs.php?art=hs', function() {
        $('#my-dropdown5').change(function(){
            location.href = "liste.php?hs="+$(this).val();
        });
    });
    /*
    $('#dhtmlgoodies_menu_xx').load('ajalistehs.php?art=tabs', function() {
        if (typeof(menuzwei) == 'undefined') {
            $(".menu_tab_content").hide(); //Hide all content
            $("ul.menutabs li:first").addClass("active").show(); //Activate first tab
            $(".menu_tab_content:first").show(); //Show first tab content
        } else {
            $("#weiterid").addClass("active").show(); //Activate first tab
            $(".menu_tab_content").hide(); //Hide all content
            $("#menutab2").show();
        }
    });
    */
}

function iinit() {
    //ladeNaechstesBild();
    if ($('#tab_container_liste .tab_container').length) {
			  $('#tab_container_liste .tab_container').css("overflow", "visible").css("height", "auto");
    }

    if (badBrowser) {
        $("ul.sf-menu").supersubs({ 
            minWidth:    19,   // minimum width of sub-menus in em units 
            maxWidth:    42,   // maximum width of sub-menus in em units 
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
            // due to slight rounding differences and font-family 
        }).superfish({
            delay:       400,                            // one second delay on mouseout 
            speed:       1
        });  // call supersubs first, then superfish, so that subs are 
        // not display:none when measuring. Call before initialising 
        // containing tabs for same reason. 
    } else {
        $('ul#navileiste, ul#navileiste ul.sf-menu').superfish({
            delay:       400,                            // one second delay on mouseout 
            animation:   {opacity: 1 ,height: 'show'}, // fade-in and slide-down animation 
            speed:       1,                          // faster animation speed 
            dropShadows:   true
        });
    }

    $("#prodlist li ").hover(function() {
        var $here = $(this);
        $here.addClass("Suba");
    }, function() {
        var $here = $(this);
        $here.removeClass("Suba");
    });

    // Page 
    $(".curpage").click(function() {
        var $page = $(this).attr("href");
        $('#content').load("aja"+$page, function(){
            $('body').attr('id','liste');
            initcontent();
            return false;
        });

    });

    // WGs anclicken
    var wg_alle = $("#wg_alle").attr("checked");
    $("#wg_alle").click(function() {
        loadingimage();
        if (!wg_alle) {
            $("#wgliste input").checkCheckboxes("");
            $('#content').load("ajaliste.php?wgliste=1&wg_alle=true", function(){
                $('body').attr('id','liste');
                initcontent();
                return false;
            });
        } else {
            $("#wgliste input").unCheckCheckboxes("#wg_alle");
            $('#content').load("ajaliste.php?wgliste=1&wg_alle=false", function(){
                $('body').attr('id','liste');
                initcontent();
                return false;
            });
        }
        wg_alle = !wg_alle;
        return false;
    });

    $("#wgliste input").click(function() {
        var warengruppe = "";
        var $here = $(this);
        if ($here.val() == 'wg_alle')  return(false);
        $("#wgliste input").each(function() {
            warengruppe = warengruppe + "&" + $(this).val() + "=" + $(this).attr("checked");
        });
        loadingimage();
        if (warengruppe) {
            warengruppe =  "?wgliste=1&reload=1"+warengruppe ;
            $('#content').load("ajaliste.php" + warengruppe, function(){
                $('body').attr('id','liste');
                hslistefertig = false;
                initcontent();
                return false;
            });
        }
    });

    // HSs anclicken
    var hs_alle = $("#hs_alle").attr("checked");
    $("#hs_alle").click(function() {
        loadingimage();
        if (!hs_alle) {
            $("#hsliste input").checkCheckboxes("");
            $('#content').load("ajaliste.php?hsliste=1&hs_alle=true", function(){
                $('body').attr('id','liste');
                initcontent();
                return false;
            });
        } else {
            $("#hsliste input").unCheckCheckboxes("#hs_alle");
            $('#content').load("ajaliste.php?hsliste=1&hs_alle=false", function(){
                $('body').attr('id','liste');
                initcontent();
                return false;
            });
        }
        hs_alle = !hs_alle;
        return false;
    });

    $("#hsliste input").click(function() {
        var warengruppe = "";
        var $here = $(this);
        if ($here.val() == 'hs_alle')  return(false);
        $("#hsliste input").each(function() {
            warengruppe = warengruppe + "&" + $(this).val() + "=" + $(this).attr("checked");
        });
        loadingimage();
        if (warengruppe) {
            warengruppe =  "?hsliste=1&reload=1"+warengruppe ;
            $('#content').load("ajaliste.php" + warengruppe, function(){
                $('body').attr('id','liste');
                initcontent();
                return false;
            });
        }
    });

    $('#tabprojekt input[type=radio]').change(function(){
        location.href = "liste.php?projaktiv="+$(this).val();
        //cation.href = "liste.php?sort="+$(this).val();
    });
    $('#sort').change(function(){
        location.href = "liste.php?sort="+$(this).val();
    });
    $('#count').change(function(){
        location.href = "liste.php?count="+$(this).val();
    });

    // Für Listen
	  //Default Action
    if (typeof(hslistefertig) == "undefined" ) {
        hslistefertig = false;
    }
    if (typeof(verflistefertig) == "undefined" ) {
        verflistefertig = false;
    }

//   $(".tab_container_choose_login").hide();
//   $(".tabs_choose_login li:first").addClass("active").show();
//    $(".tab_content:first").show();
//  $(".tab_container_choose_login:first").show();

    $("#tabhs").show();

    //console.log(hslistefertig+  verflistefertig);
    if (hslistefertig) {
        $("ul.tabs li").removeClass("active");
        $(".tab_container div").hide();
        $("a[hash=#tabhs]").parent().addClass("active");
        $("#tabhs").show();
    }

    if (hslistefertig == false && verflistefertig == false) {
	      $(".tab_content").hide(); //Hide all content
        var $first = $("ul.tabs li:first");
	      $first.addClass("active").show(); //Activate first tab
        var activefirst = $first.find("a").attr("href");
        $(activefirst).show();
    }

	  //On Click Event

	  $("ul.tabs li").click(function() {
		    $("ul.tabs li").removeClass("active"); //Remove any "active" class
		    $(this).addClass("active"); //Add "active" class to selected tab
		    $(".tab_content").hide(); //Hide all tab content
		    var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		    $(activeTab).show(); //Fade in the active content
		    return false;
	  });

	  
	  //When page loads...für Login Tabs
	  $(".tab_content_choose_login").hide(); //Hide all content
	  $("ul.tabs_choose_login li:first").addClass("active").show(); //Activate first tab
	  $(".tab_content_choose_login:first").show(); //Show first tab content
	  
	  //On Click Event für Login Tabs
	  $("ul.tabs_choose_login li").click(function() {
		    $("ul.tabs_choose_login li").removeClass("active"); //Remove any "active" class
		    $(this).addClass("active"); //Add "active" class to selected tab
		    $(".tab_content_choose_login").hide(); //Hide all tab content
		    var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		    $(activeTab).show(); //Fade in the active content
		    return false;
	  });
		/*
    $("ul.tabs_choose_login").tabs("div.tab_container_choose_login div.tab_content_choose_login");
    */
    $("#details ul.tabs").tabs("#details div.tab_container div.tab_content");
    // Für Menü rechts

	  //grosses Menü links
/*
	  $("ul.menutabs li").click(function() {
		    $("ul.menutabs li").removeClass("active"); //Remove any "active" class
		    $(this).addClass("active"); //Add "active" class to selected tab
		    $(".menu_tab_content").hide(); //Hide all tab content
		    var activeMenuTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		    $(activeMenuTab).show(); //Fade in the active content
		    return false;
	  });
*/
    $('.projekt button[title]').tooltip();
    //$.fn.fancyzoom.defaultsOptions.imgDir='includes/fancyzoom/img/';
    //$.fn.fancyzoom.defaultsOptions.imagezindex = 2000;
    //$('#liste #prodlist .produktbild a, #picliste a').fancyzoom();

    $('a[rel="gally"]').colorbox({previous: 'zur&uuml;ck', next: 'weiter', close: 'schlie&szlig;en', current: 'Bild {current} von {total}'});
    $(".items img").hover(function() {
      var url    = $(this).attr("big");
      var wrap = $("#pd_images img[rel='magnify']").stop().fadeTo("medium", 0.5);

      var img = new Image();

      img.onload = function() {
        wrap.stop().fadeTo("fast", 1);
        wrap.attr("src", url); //.css("margin-top", margintop+"px");
      };
      img.src = url;
    });

    $("div.scrollable").scrollable({size: 3});

    $("div.scrollable .items ").click(function(e) {
      $url = $("img[rel='magnify']").attr("src");
      suche = $("#lightboxgallery").find(".cboxElement").filter("[href="+$url+"]");

      if (!suche.length) {
        suche = $("#lightboxgallery").find(".cboxElement:first");
      }
      if (!suche.length) {
         suche = $(this).parent().parent().parent().find(".cboxElement").filter("[href="+$url+"]");
      }
      suche.click();
      return(false);
    });

    $(".zurliste").click(function(event) {
        var artnr = $(this).attr("data-art");
        if ($('#add_more_'+artnr).length)
            var anzahl = $('#add_more_'+artnr).val();
        else
            var anzahl = 1;
        //var artnr = $(this).attr('artnr');
        //console.log("ll "+artnr);
        $.ajax({
            type: "GET",
            url: "ajawk.php?artnr="+artnr+"&stueck="+anzahl,
            success: function (msg) {
                wkerg = [];
                wkerg = msg ;
                $('#login_wk_artsumme').html(eval(wkerg));
            }
        });
        $('button[rel=#artinwk'+artnr+']').overlay().close();
        //alert("da");
        return false;
    });
    $(".zumwk").click(function(event) {
        var artnr = $(this).attr("data-art");
        if ($('#add_more_'+artnr).length)
            var anzahl = $('#add_more_'+artnr).val();
        else
            var anzahl = 1;
        //alert("anz "+anzahl+" art "+artnr);
        //var artnr = $(this).attr('artnr');
        //console.log("ll "+artnr);
        //$('button[rel=#artinwk'+artnr+']').overlay().close();
        location.href = "wk.php?art="+artnr+"&stueck="+anzahl;
        //alert("da");
        return false;
    });


}

   
function loadingimage() {
    return;
    //console.log("loadingimage ");
    var $window = $(window);
    var $halbehoehe =  Math.ceil($window.height() / 2);
    var $halbebreite =  Math.ceil(($window.width() - 115) / 2);
    var $scrolledto = $window.scrollTop();
    var $scrolledleft = $window.scrollLeft();

    //console.log("top "+$halbehoehe+" "+$scrolledto+"px left "+$halbebreite+" "+$scrolledleft+"px "+$loading.html());
    /*
    if (loading.length) {
        $loading.css('display', "block");
    }
    */
}
function initcontent() {
    iinit();
}
function ersetzeartliste() {
    if (artliste) {
        //$('#maincol_start').html(artliste).fadeTo('fast', 1);
        $('#content').html("");
        $('#content').html(artliste);
    }

    $('body').attr('id','liste');
    
    iinit();
    if ($loading.length) {
        $loading.hide();
    }

    //console.profileEnd();
    
}

$(document).ready(function(){
    ladeHS();
    iinit();
    if (!badBrowser67) {
    }
    $.tools.validator.localize("de", {
              "*"               : "Eingabefeld korrigieren",
              ":email"          : "E-Mail Adresse eingeben",
              ":password"       : "Bitte Passwort eingeben",
              ":number"         : "Bitte eine Zahl eingeben",
              ":url"            : "Bitte eine g&uuml;ltige URL eingeben",
              "[max]"           : "Bitte eine Zahl kleiner oder gleich als $1 eingeben",
              "[min]"           : "Bitte eine Zahl gr&ouml;&szlig;er oder gleich $1 eingeben",
              "[minlength]"             : "Eingabe mu&szlig; mindestens  $1 Zeichen lang sein",
              "[data-equals]" : "Eingabe mu&szlig; mit Eingabe $1 &uuml;bereinstiummen",
              "[required]"      : "Bitte Feld ausf&uuml;llen"
    });
    
    $.tools.dateinput.localize("de",  {
        months:        'Januar,Februar,M&auml;rz,April,Mai,Juni,Juli,August,' +
            'September,Oktober,November,Dezember',
        shortMonths:   'Jan,Feb,M&auml;r,Apr,Mai,Jun,Jul,Aug,Sep,Okt,Nov,Dez',
        days:          'Sonntag,Montag,Dienstag,Mittwoch,Donnerstag,Freitag,Samstag',
        shortDays:     'So,Mo,Di,Mi,Do,Fr,Sa'
    });

    $('.weiterarbeiten').click( function() {
        var artnr = $(this).attr("data-art");
        //console.log(" "+artnr);
        $.ajax({
            type: "GET",
            url: "ajaprojektliste.php?artnr="+artnr,
            success: function (msg) {
                wkerg = [];
                wkerg = msg ;
                //console.log("OK "+wkerg);
            }
        });
        $('button[rel=#apiprojekte'+artnr+']').overlay().close();
        return false;
    });
    $("#projektliste #content form, #projektlisteupload #content form").validator();

});
