function initGallery() {
    var gallery = jQuery("div#gallery");
    var galleryPager = "";
    if (gallery && window.name != "printwindow") {
        var images = jQuery("div#gallery img");
        if (images.length > 1) {
            images.each(function(i){
                this.id = "galleryPic" + (i+1);
                this.style.display = "none";
                galleryPager += "<li><a id=\"galleryLink" + (i+1) +"\" href=\"" + this.src + "\" onclick=\"switchCurrentGalleryPicture(this.id, '" + this.id + "');return false;\">" + (i+1) + "</a></li>";
            });
        }
        if (galleryPager != "") {
            gallery.append("<div id=\"gallerynav\"><ul>" + galleryPager + "</ul></div>");
            switchCurrentGalleryPicture("galleryLink1", "galleryPic1");
        }
    }
}
var currentPic;
var currentLink;
function switchCurrentGalleryPicture(linkid, picid) {
    if (currentPic) { jQuery("#" + currentPic).css({ display: "none"}); }
    if (currentLink) { jQuery("#" + currentLink).removeClass("current"); }
    jQuery("#" + picid).css({ display: "block" });
    jQuery("#" + linkid).addClass("current");
    currentPic = picid;
    currentLink = linkid;
}

function prepareRegistrationForm() {
    var count = jQuery("select#count");
    var eurocost = jQuery("span#eurocost");
    if (count && eurocost) {
        count.change(calculateTotalCost);
        count.after("<div id=\"eventcosts\">Gesamtpreis <span id=\"totalcost\"></span> &euro;</div>");
        calculateTotalCost();
    }
}
function calculateTotalCost() {
    var count = jQuery("#count");
    var eurocost = jQuery("#eurocost");
    var totalcost = jQuery("#totalcost");
    if (count.get(0) && eurocost.get(0) && totalcost.get(0)) {
        totalcost.empty();
        var c = 0;
        var st = eurocost.get(0).innerHTML;
        var split = ((st.indexOf(".") != -1) ? "." : ((st.indexOf(",") != -1) ? "," : ""));
        if (split != "") {
            var t = st.split(split);
            c = t[0] + "" + t[1];
        } else {
            c = st;
            split = "";
        }
        total = (c * count.get(0).options[count.get(0).selectedIndex].value) + "";
        if (split == "") {
            totalcost.append(total);
        } else {
            totalcost.append(total.substring(0,total.length-2) + split + total.substring(total.length-2));
        }
    }
}
function prepareWrapperMinHeight() {
    var mainTop = jQuery("#main").get(0).offsetTop;
    var mainHeight = jQuery("#main").height();
    var mainSize = mainTop + mainHeight;
    var additionalSize = 0;

    if (jQuery("#additionalinfo").get(0)) {
        var additionalTop = jQuery("#additionalinfo").get(0).offsetTop;
        var additionalHeight = jQuery("#additionalinfo").height();
        additionalSize = additionalTop + mainTop + additionalHeight;
    }
    var marginalSize = 0;
    if (jQuery("#marginal").get(0)) {
        var marginalTop = jQuery("#marginal").get(0).offsetTop;
        var marginallHeight = jQuery("#marginal").height();
        marginalSize = marginalTop + marginallHeight;
   }

    var result = mainSize;
    if (additionalSize > mainSize) {
        result = additionalSize;
    }
    if (marginalSize > mainSize && marginalSize > additionalSize) {
        result = marginalSize;
    }
    jQuery("#wrap-inner").css({ minHeight: ((result+60) + "px") });
    jQuery("#footer").css({ display: "block" });
}
function checkWrapConditions(wrap) {
    var result = "100%";
	
    var minHeight = wrap.style.minHeight;
    if (minHeight != "" && !document.getElementById('preview-mod')) {
        var minHeightInt = minHeight.substring(0, minHeight.length-2);
        //window.status = "minHeightInt: " + minHeightInt + "----document.body.offsetHeight: " + document.body.offsetHeight;
        if (document.body.offsetHeight <= minHeightInt) {
            result = minHeight;
        }
    }
    return result;
}


function initPage() {
    initGallery();
    prepareRegistrationForm();
    jQuery(".popupwindow").popupwindow();
    if (window.name == "printwindow") {
        jQuery("#printstyle").attr("media", "screen,print"); /* for IE */
        jQuery("head").append("<link href=\"" + printstylepath + "\" type=\"text/css\" rel=\"stylesheet\" media=\"screen.print\" />"); /* for FF */
        window.print();
    }
    jQuery("option[@disabled]").each(function(i) {
       jQuery(this).css("color", "graytext");
    });
	jQuery('a.popup').click(function() { 
			/* 
			 * STRUCTURE OF THE LINK 
			 * 
			 * http://www.somehostname.com?param1=value1&param2=value2&param3=value3&popupattrs=width%3D300%2Cheight%3D400 
			 * 
			 */ 
			var q = jQuery.location.strToObj(this.search, {decode: true}); 
			
			if(q.popupattrs){
				window.open(this.href, 'PopUp_Window', q.popupattrs); 
				return false; 
			}
		});
}
jQuery(document).ready(initPage);

/* Webkit fix: Postpone calculation of page height until after the images are loaded */
jQuery(window).load( function() {
    if (window.name != "printwindow") {
        prepareWrapperMinHeight();
    }
});
