
// selects a thumb by index
// sets/resets the marker above the thumb
// changes the preview image
// changes the alt-text for preview image
function selectThumb(index) {
    if (index == -1) {
        return false;
    }
    if (index >= 1) {
        if (index <= thumbCount) {
            //change preview picture
            if (data[index]['hasAnimation'] || data[index]['hasInteraction'] || data[index]['hasSound']) {
                alt = data[index]['name']+"\r\n\r\nKarte enthält\r\n-> Sound\r\n-> Animation\r\n-> Interaktion";
            }else{
                alt = data[index]['name'];
            }
            content  = '<table border="0" width="100%" cellspacing="0" cellpadding="0"><tr><td align="center" valign="top">';
            content += '<a href="#" onclick="viewPicture()"><img name="pictures" src="'+data[index]['image']+'" border="0" id="preview_image" alt="'+alt+'"></a>';
            content += '</td></tr></table>';
            
            alterContent(content);
            
            //set symbols for sound-animation-interaction
            showMediaMessage = false;
            if (data[index]['hasAnimation']) {
                showMediaMessage = true;
                has_animation = "images/has_animation.gif";
            }else{
                has_animation = "images/spacer.gif";
            }

            if (data[index]['hasInteraction']) {
                showMediaMessage = true;
                has_interaction = "images/has_interaction.gif";
            }else{
                has_interaction = "images/spacer.gif";
            }

            if (data[index]['hasSound']) {
                showMediaMessage = true;
                has_sound = "images/has_sound.gif";
            }else{
                has_sound = "images/spacer.gif";
            }
            icons  = '<table border="0" width="100%" cellspacing="0" cellpadding="0"><tr><td align="center" valign="top">';
            icons += '<img src="'+has_animation+'" width="16" height="16" id="has_animation" alt="Die eCard enth&auml;lt Animation">&nbsp;&nbsp;&nbsp;&nbsp;\n';
            icons += '<img src="'+has_sound+'" width="16" height="16" id="has_sound" alt="Die eCard enth&auml;lt Sound">&nbsp;&nbsp;&nbsp;&nbsp;\n';
            icons += '<img src="'+has_interaction+'" width="16" height="16" id="has_interaction" alt="Die eCard enth&auml;lt Interaktion">';
            icons += '</td></tr></table>';
            alterIcons(icons);

            if (showMediaMessage) {
                message  = '<table border="0" width="100%" cellspacing="0" cellpadding="0"><tr><td align="center" valign="top" class="cont">';
                message += mediamessage;
                message += '</td></tr></table>';
                alterMediaMessage(message);
            }else{
                alterMediaMessage('&nbsp;');
            }

            if (!bw.ns4) {
                //change thumb selection
                getLayerRef("marker_"+aktThumbIndex).background = "images/bg_thumbs1.gif";
                getLayerRef("marker_"+index).background = "images/bg_thumbs3.gif";
                //change class
                getLayerRef("marker_"+aktThumbIndex).className = "thumbs";
                getLayerRef("marker_"+index).className = "thumbs_selected";
            }
            aktThumbIndex = index;
        }else{
            if (thumbs[0]['next'] != '#') {
                locate(thumbs[0]['next']);
            }
        }
    }else{
        if (thumbs[0]["prev"] != "#") {
            locate(thumbs[0]["prev"]);
        }
    }
}

// select previous thumb
function selectPrevThumb() {
    selectThumb(aktThumbIndex - 1);
}

// select next thumb
function selectNextThumb() {
    selectThumb(aktThumbIndex + 1);
}

// shows the picture
function viewPicture() {
    locate("ecard_front.php?card_id="+data[aktThumbIndex]['id']);
}

// relocates the browser to a given url
var alreadyLocated = false;
function locate(url) {
    if (!alreadyLocated) {
        alreadyLocated = true;
        document.location.href = url;
    }
}

// shows a messagebox before relocating to url
var alreadyShown = false;
function showMessage(msg,url) {
    if (!alreadyShown) {
      alreadyShown = true;
      alert(msg);
      locate(url);
    }
}

// shows a confirmation before relocating to url
function showQuestion(msg,url) {
    if (confirm(msg)) {
        locate(url);
    }
}

// relocates the browser depending
// on the ecard(action,interaction,sound)
function buttonClick(id) {
    if (typeof aktThumbIndex != 'undefined') {
        if (aktThumbIndex) {
            switch(id) {
            case 1:
                break;
            case 2:
                if (data[aktThumbIndex]['hasAnimation'] || data[aktThumbIndex]['hasInteraction'] || data[aktThumbIndex]['hasSound']) {
                    locate("ecard_front.php?card_id="+data[aktThumbIndex]['id']+"&showbig=1");
                }else{
                    locate("ecard_back.php?card_id="+data[aktThumbIndex]['id']);
                }
                break;
            case 3:
                break;
            }
        }
    }else{
        locate("ecard_back.php");
    }
}

function getLayerRef (id, document) {
  if (!document)
    document = window.document;
  if (document.layers) {
    for (var l = 0; l < document.layers.length; l++)
      if (document.layers[l].id == id)
        return document.layers[l];
    for (var l = 0; l < document.layers.length; l++) {
      var result = getLayerRef(id, document.layers[l].document);
      if (result)
        return result;
    }
    return null;
  }
  else if (document.all) {
    return document.all[id];
  }
  else if (document.getElementById) {
    return document.getElementById(id);
  } else {
    return null;
  }
}

function lib_bwcheck(){ //Browsercheck (needed)
	this.ver=navigator.appVersion;
	this.agent=navigator.userAgent;
	this.dom=document.getElementById?1:0;
	this.opera5=this.agent.indexOf("Opera 5")>-1;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6;
	this.mac=this.agent.indexOf("Mac")>-1;
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5);
	return this;
}
var bw = new lib_bwcheck();

function alterContent(newcontent) {
    //if IE 4+
    if (document.all) {
        divPreview.innerHTML = newcontent;
    }else{
        if (document.layers) {
            //else if NS 4
            document.ns4Preview.document.ns4Preview2.document.write(newcontent);
            document.ns4Preview.document.ns4Preview2.document.close();
        }else{ 
            if (document.getElementById) {
                //else if NS 6 (supports new DOM)
                rng = document.createRange();
                el = document.getElementById('divPreview');
                rng.setStartBefore(el);
                htmlFrag = rng.createContextualFragment(newcontent);
                while (el.hasChildNodes()) {
                    el.removeChild(el.lastChild);
                }
                el.appendChild(htmlFrag);
            }
        }
    }
}

function alterIcons(newcontent) {
    //if IE 4+
    if (document.all) {
        divIcons.innerHTML = newcontent;
    }else{
        if (document.layers) {
            //else if NS 4
            document.ns4Icons.document.ns4Icons2.document.write(newcontent);
            document.ns4Icons.document.ns4Icons2.document.close();
        }else{ 
            if (document.getElementById) {
                //else if NS 6 (supports new DOM)
                rng = document.createRange();
                el = document.getElementById('divIcons');
                rng.setStartBefore(el);
                htmlFrag = rng.createContextualFragment(newcontent);
                while (el.hasChildNodes()) {
                    el.removeChild(el.lastChild);
                }
                el.appendChild(htmlFrag);
            }
        }
    }
}

function alterMediaMessage(newcontent) {
    //if IE 4+
    if (document.all) {
        divMediaMessage.innerHTML = newcontent;
    }else{
        if (document.layers) {
            //else if NS 4
            document.ns4MediaMessage.document.ns4MediaMessage2.document.write(newcontent);
            document.ns4MediaMessage.document.ns4MediaMessage2.document.close();
        }else{ 
            if (document.getElementById) {
                //else if NS 6 (supports new DOM)
                rng = document.createRange();
                el = document.getElementById('divMediaMessage');
                rng.setStartBefore(el);
                htmlFrag = rng.createContextualFragment(newcontent);
                while (el.hasChildNodes()) {
                    el.removeChild(el.lastChild);
                }
                el.appendChild(htmlFrag);
            }
        }
    }
}

function alterErrorMessage(newcontent) {
    //if IE 4+
    if (document.all) {
        divErrorMessage.innerHTML = newcontent;
    }else{
        if (document.layers) {
            //else if NS 4
            document.ns4ErrorMessage.document.ns4ErrorMessage2.document.write(newcontent);
            document.ns4ErrorMessage.document.ns4ErrorMessage2.document.close();
        }else{ 
            if (document.getElementById) {
                //else if NS 6 (supports new DOM)
                rng = document.createRange();
                el = document.getElementById('divErrorMessage');
                rng.setStartBefore(el);
                htmlFrag = rng.createContextualFragment(newcontent);
                while (el.hasChildNodes()) {
                    el.removeChild(el.lastChild);
                }
                el.appendChild(htmlFrag);
            }
        }
    }
}

var alreadySubmitted = false;
function eCard_submit() {
    if (!alreadySubmitted) {
        alreadySubmitted = true;
        document.form1.submit();
    }
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

