/**
 * @author christians
 */

var images = new Array();
var preloadedImages = new Array();
var target = null;
var targetnew = null;
var targetFlash = null;
var targetNav = null;
var targetCaption = null;
var countImg = 0;
var curIdx = -1;
var curImg = -1;
var curFlash = -1;
var matchswf = /.swf$/;

function preloading() {
	target = $("img");
        targetnew = jQuery("#switchImage"); //übergangslösung
	targetFlash = jQuery("#switchFlashContainer object");
	targetNav = $("imagenav");
	targetCaption = jQuery("#imagecaption");
	countImg = images.length;
	for(var i = 0; i < countImg; i++) { 
            if(matchswf.match(images[i][0]) == false){
		preloadedImages[i] = new Image();
		preloadedImages[i].src = images[i][0];
		preloadedImages[i].alt = 'Bild '+images[i][1];
		preloadedImages[i].caption = images[i][2];
            }
	}
	buildNav();
	show(0);
}

function buildNav() {
        if (!targetNav  || targetNav == "undefined") {
            return false;
        }

	targetNav.innerHTML = ""; 
	for(var i = 0; i < countImg; i++) {		
		var anchor = document.createElement("a");
		anchor.setAttribute("href", "javascript:show(" + i + ");");
		anchor.setAttribute("title", images[i][1]);	
		if(i==curIdx){
			anchor.setAttribute("class", "active");
		}	
		anchor.innerHTML = images[i][1] + "&nbsp;";

		targetNav.appendChild(anchor);
	}
}

function show(idx) {
        if(matchswf.match(images[idx][0]) == false){
            //image
            if (!preloadedImages[idx] || preloadedImages[idx] == "undefined") {
                return false;
            }
            if($('fadingCover')){
                jQuery('#fadingCover').fadeIn(600, function(){
                    if(idx != curImg){
                        targetnew.attr('src',preloadedImages[idx].src);
	                targetnew.attr('alt',preloadedImages[idx].alt);
	                targetCaption.html(preloadedImages[idx].caption);
                    }
                    if($("switchImgContainer")){
                        jQuery("#switchImgContainer").css('display','inline');
                        jQuery("#switchFlashContainer").css('display','none');
                    }
	            curIdx = idx;
                    curImg = idx;
	            buildNav();
                    targetnew.ready(function(){jQuery("#fadingCover").fadeOut(600);});
                });
            }
            else {
                if(idx != curImg){
                    target.src = preloadedImages[idx].src;
	            target.alt = preloadedImages[idx].alt;
	            targetCaption.html(preloadedImages[idx].caption);
                }
            }

	    curIdx = idx;
            curimg = idx;
	    buildNav();
        }
        else {
            //flash-swf
            jQuery('#fadingCover').fadeIn(600, function(){
                if(idx != curFlash){
                    targetFlash.html('<param name="movie" value="'+images[idx][0]+'" id="flashSourceParam" ><param name="wmode" value="opaque" ><embed src="'+images[idx][0]+'" id="switchFlash" quality="high" scale="exactfit" menu="false" bgcolor="#000000" width="504" height="359" swLiveConnect="false" type="application/x-shockwave-flash" wmode="opaque" pluginspage="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash"></embed>');
	                targetCaption.html(images[idx][2]);
                }
                if($("switchImgContainer")){
                    jQuery("#switchFlashContainer").css('display','inline');
                    jQuery("#switchImgContainer").css('display','none');
                }
	        curIdx = idx;
                curFlash = idx;
	        buildNav();
                targetFlash.ready(function(){jQuery("#fadingCover").fadeOut(600);});
                
            });
        }
}


