function hideDiv(divid){
    $(divid).fadeOut("fast");
}
var galleryID = '#gallerydetail';
var galleryBasePath = '';
var simpleModalDialog = '';

function fillGalleryDetail(img, txt){
    var borderWidth = 22;
    var imgWidth = img.width;
    var imgHeight = img.height;
    var containerWidth = imgWidth + borderWidth + borderWidth;
    var containerHeight =  imgHeight + borderWidth + borderWidth;
    var detailContent = '<div style="width: '+containerWidth+'px; height: '+containerHeight+'px;" class="image-border image-border-left">';
    detailContent += '<div class="image-border-t-r" style="left: '+(imgWidth-102)+'px; top: -73px"/>';
    detailContent += '<div class="image-border-b-l"/>';
    detailContent += '<div class="gal-close" style="width: '+containerWidth+'px"><a href="javascript:$.modal.close();">fenster schliessen</a></div>';
    if(window.galPrvImgSrc && totalImages > 0){detailContent += '<div class="gal-prv" style="top: '+(containerHeight-90)/2+'px"><a href="javascript:checkModalContent(true)"><img src="'+window.galPrvImgSrc+'" alt="prv"/></a></div>';}
    if(window.galNxtImgSrc && totalImages > 0){detailContent += '<div class="gal-nxt" style="top: '+(containerHeight-90)/2+'px"><a href="javascript:checkModalContent()"><img src="'+window.galNxtImgSrc+'" alt="nxt"/></a></div>';}
    detailContent += '<div class="gal-txt">'+txt+'</div>';
    detailContent += '<div class="image-content" style="height: '+imgHeight+'px;">';
    detailContent += '<a href="javascript:checkModalContent();"><img width="'+imgWidth+'" src="'+img.src+'"/></a>';
    detailContent += '</div>';
    detailContent += '</div>';
    return detailContent;
}

function displayModalContent(me){
    window.currentImageNo = me;
    var tmpimg = new Image();
  $(tmpimg)
    // once the image has loaded, execute this code
    .load(function () {
      // set the image hidden by default
      $(this).hide();
      $(galleryID).html(fillGalleryDetail(this, bigImageTitles[me]));
      $("#gallerydetail").modal({overlayClose:true,overlay:90, overlayCss: {backgroundColor:"#000"},onOpen: function (dialog) {
          simpleModalDialog = dialog;
            dialog.overlay.fadeIn('fast', function () {
                dialog.data.hide();
                    dialog.container.fadeIn('fast', function () {
                        dialog.data.slideDown('slow');
                    });
                });
            }});
    })
    .attr('src', bigImageHandles[me]);
}



function checkModalContent(back){
    $(galleryID).fadeOut("fast", function () {
        if(back == true){
            currentImageNo--;
            if(currentImageNo<0){currentImageNo = totalImages;}
        }else{
            currentImageNo++;
            if(currentImageNo>totalImages){currentImageNo = 0;}
        }
        resizeModal(currentImageNo);
    });
}


function resizeModal(me){
    var tmpimg = new Image();
    tmpimg.onload = function(){
        var pic_real_width = this.width;
        var pic_real_height = this.height;
        simpleModalDialog.container.width(pic_real_width+44);
        simpleModalDialog.container.height(pic_real_height+44);
        $(galleryID).html(fillGalleryDetail(this, bigImageTitles[me]));
        $(window).trigger('resize.simplemodal');
        $(galleryID).fadeIn("fast");
    }
    tmpimg.src = bigImageHandles[me];
}

function removeDefault(formField, value){
    if(formField.value == value){
        formField.value = "";
    }
}
function resetDefault(formField, value){
    if(formField.value == ""){
        formField.value = value;
    }
}
function selectFormFieldElement(group, index) {
    var c = document.contactForm[group];
    if(c.length>0){
        c = c[index];
    }
    if(c.type == "radio"){
        c.checked = true;
    }else{
        c.checked = !c.checked;
    }
}


var detailHandles = new Array();
var minigalTitles = new Array();
var minigalActImg = 1;
function setDetailHandles(id,arr){
    detailHandles[id] = new Array();
    detailHandles[id] = arr;
}
function setMinigalTitles(id,arr){
    minigalTitles[id] = new Array();
    minigalTitles[id] = arr;
}
function showMinigal(no, id, ignore){
    //$(".minimailform").slideDown("slow");
    var isVisible = $('#'+id).is(':visible');
    if(ignore!=false && isVisible && minigalActImg == no*1){return hideMinigal(id);}
    minigalActImg = no*1;
    $("#"+id).slideDown("slow");
    //document.title = detailHandles[detailHandles]+" / "+detailHandles[0]+"| "+minigalActImg;
    $("#"+id+" .detail_img").css("background-image", "url('"+detailHandles[id][minigalActImg]+"')");
    $("#"+id+" .minigal_img_title").html(minigalTitles[id][minigalActImg]);

}
function hideMinigal(id){
    $("#"+id).slideUp("fast");
}
function showNext(id){
    minigalActImg++;
    if(minigalActImg>=detailHandles[id].length){minigalActImg=1;}
    showMinigal(minigalActImg, id, false);
}
function showPrevious(id){
    minigalActImg--;
    if(minigalActImg<1){minigalActImg=detailHandles[id].length-1;}
    showMinigal(minigalActImg, id, false);
}
var courselist_title = "";
var courselist_link = "";
var basePath  = "/";
var scriptPath = "/custom/course/handling.jsp";
var fullScriptPath = basePath+scriptPath;
function set_basePath(val){basePath = val; fullScriptPath = basePath+scriptPath;}
function set_courselist_title(val){courselist_title = val; }
function set_courselist_link(val){courselist_link = val; }

function registerCourseId(id,url){
    id = id.substring(3);
    $.get(fullScriptPath+"?cmd=ac&id="+id,function(data){
        var myObject = eval('(' + data + ')');//eval(data);
        showCourseList(myObject['courses']);
        window.location.href = url;
    });
}
function noteCourseId(id){
    id = id.substring(3);
    putCourseToSession(id);
}
function putCourseToSession(id){
    $.get(fullScriptPath+"?cmd=ac&id="+id,function(data){
        var myObject = eval('(' + data + ')');//eval(data);
        showCourseList(myObject['courses']);
    });
}

function showCourseList(details){
    if(details.length>0){
        details = courselist_title+details+courselist_link;
        $("#courselist").html(details);
        $("#courselist").fadeIn("fast");
    }
}

function displayCourseList(){
    $.get(fullScriptPath+"?cmd=gl",function(data){
        var myObject = eval('(' + data + ')');//eval(data);
        showCourseList(myObject['courses']);
    });
}
function createFormOutput(fieldName){
    $.get(fullScriptPath+"?cmd=gfl&fn="+fieldName,function(data){
        var myObject = eval('(' + data + ')');//eval(data);
        $("#form_courselist").html(myObject['courses']);
    });
}
function removeCourseFromList(id,fieldName){
    $.get(fullScriptPath+"?cmd=rm&id="+id+"&fn="+fieldName,function(data){
        var myObject = eval('(' + data + ')');//eval(data);
        $("#form_courselist").html(myObject['courses']);
    });
}

function showCourseDetail(id, scrollto){
    var speed = "slow";
    var courseDetail = $("#d"+id);
    if(!courseDetail.is(":animated")){
        if(!courseDetail.is(":visible")){
            courseDetail.slideDown(speed, function(){
                if(scrollto != false){
                    $.scrollTo( "#t"+id ,800);
                }
            });
        }else{
            if(scrollto != false){
                $.scrollTo( "#t"+id ,800);
            }else{
                courseDetail.slideUp("fast");
            }
        }
    }
}

$(document).ready(function(){
    $(".closedCourse").animate({"opacity": "0.5"}, 1);
    $(".activeimg").hover(
        function() {
            $(this).animate({"opacity": "0"}, "slow");
        },
        function() {
            $(this).animate({"opacity": "1"}, "slow");
        });

    /* show hide course list detail */
    $("div.courseListTitle").click( function() {
        var element = $(this);
        var courseDetail = $("div.courseDetail", element.parent());
        var courseID = courseDetail.attr("id").substring(1);
        showCourseDetail(courseID, false);
    });

});




