/**
 * Galerie Lightbox
 * 
 * @author Christina Böhme <kontakt@christinaboehme.de>
 * @copyright Copyright (c) 2007-2008 Christina Böhme
*/
var showcaseTween;
var showcaseEndHeight;
var openList;
var showcaseNextPageLink;
var showcaseBackPageLink;
var showcaseNextPageLinkEl=false;
var showcaseBackPageLinkEl=false;
var showcasePreLinkLength;
var showcaseModuleId;
var showcasePixelPerInterval=20;

function initShowcaseGallery(wrapperId, moduleId, preLinkLength){
	showcaseModuleId=moduleId;
	showcasePreLinkLength=preLinkLength;
	var liList=document.getElementById(wrapperId).getElementsByTagName("LI");
	var l=liList.length;
	if (l>0){
		for (var i=0; i<l; i++){
			var aList=liList[i].getElementsByTagName("A");
			var l2=aList.length;
			if (l2>0){
				for (var j=0; j<l2; j++){
					if (aList[j].className&&aList[j].className=="pageLink"){
						var tmp=aList[j].href;
						aList[j].href="javascript:openShowcaseView('"+tmp+"', '"+moduleId+"', 'new');";						
					}
				}
			}
		}
	}
}

var ajaxFile;
var downloadFile;

function setAjaxFile(file){
	ajaxFile=file;
}

function createAjaxRequestObject() {
	if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
	}else if (window.ActiveXObject){
        return new ActiveXObject('Microsoft.XMLHTTP');
	}
    return false;
}

function openShowcaseView(params, moduleId, action){
    if (window.opera) showcasePixelPerInterval=20; //Opera
    else if( window.clipboardData && document.compatMode ) showcasePixelPerInterval=4; //IE
    else if(window.atob&&document.defaultCharset) showcasePixelPerInterval=4; //Sf

	var httpParams='module=showcase&action=getAlbumPageData&length='+showcasePreLinkLength+'&params='+params+'&moduleId='+moduleId;
	var showcaseHttp=createAjaxRequestObject();
	
	showcaseHttp.open('post', ajaxFile);
	showcaseHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	showcaseHttp.setRequestHeader('Content-length', httpParams.length);
	showcaseHttp.setRequestHeader('Connection', 'close');
    
    if (action=="new"){
        var coverEl=document.createElement("DIV");
		coverEl.id="showcaseCover";        
		document.body.appendChild(coverEl);
        
        var wrapperEl=document.createElement("DIV");
		wrapperEl.id="showcaseViewWrapper";        
        
        var contentEl=document.createElement("DIV");
		contentEl.id="showcaseViewContent";
        contentEl.className="fontStandard";
        
        wrapperEl.appendChild(contentEl);
        
        wrapperEl.style.width='350px';
        wrapperEl.style.height='250px';
        wrapperEl.style.visibility='hidden';
        
        document.body.appendChild(wrapperEl);
        
        wrapperEl.style.top=(document.documentElement.scrollTop+50)+'px';
        wrapperEl.style.left=(document.body.offsetWidth/2)-(wrapperEl.offsetWidth/2)+'px';
        wrapperEl.style.visibility='visible';
    }
    
    wrapperEl=document.getElementById("showcaseViewWrapper");
    contentEl=document.getElementById("showcaseViewContent");
    contentEl.innerHTML='';
    
    wrapperEl.className="progress";

	showcaseHttp.onreadystatechange=function() {
		if (showcaseHttp.readyState == 4) {
            wrapperEl.className="resize";
            contentEl.innerHTML=showcaseHttp.responseText;
            
            changeShowcaseViewLinks(contentEl);
			var imgEl=wrapperEl.getElementsByTagName("IMG")[0];
            
           	imgEl.onmouseover=function(event){startShowcaseMouseTrack(event, imgEl);};
			imgEl.onmouseout=function(event){endShowcaseMouseTrack(event, imgEl);};

			if (imgEl.complete){
				resizeShowcaseView();		
			}else{		
				imgEl.onload=new Function("resizeShowcaseView();");		
			}
        }
	}
	showcaseHttp.send(httpParams);
}

function resizeShowcaseView(){
	wrapperEl=document.getElementById("showcaseViewWrapper");
	var imgEl=wrapperEl.getElementsByTagName("IMG")[0];
    
    if (window.setInterval){
		if (showcaseTween) window.clearInterval(showcaseTween);
        if (imgEl.width>parseFloat(wrapperEl.style.width)){
            var direction='bigger';
        }else{
            var direction='smaller';
        }
		showcaseTween = window.setInterval("makeShowcaseResize(wrapperEl,'"+imgEl.width+"','"+direction+"','width')", 5);
	}else{
		wrapperEl.style.width=wrapperEl.offsetWidth+'px';
        wrapperEl.style.height=wrapperEl.offsetHeight+'px';
        wrapperEl.style.left=(document.body.offsetWidth/2)-(wrapperEl.offsetWidth/2)+'px';
    }
}

function makeShowcaseResize(wrapperEl, endValue, direction, type){
    if (type=="width"){
        var currWidth=parseFloat(wrapperEl.style.width);
        if (direction=="bigger"){
            if(endValue>(currWidth+showcasePixelPerInterval)){
                wrapperEl.style.width=(currWidth+showcasePixelPerInterval)+'px';
                wrapperEl.style.left=(document.body.offsetWidth/2)-(wrapperEl.offsetWidth/2)+'px';
            }else{
                wrapperEl.style.width=endValue+'px';
                wrapperEl.style.left=(document.body.offsetWidth/2)-(wrapperEl.offsetWidth/2)+'px';
                
                window.clearInterval(showcaseTween);
                
                contentEl=document.getElementById("showcaseViewContent");
                
                if (contentEl.offsetHeight>parseFloat(wrapperEl.style.height)){
                    var direction2='bigger';
                }else{
                    var direction2='smaller';
                } 
               
                showcaseTween = window.setInterval("makeShowcaseResize(wrapperEl,'"+contentEl.offsetHeight+"','"+direction2+"','height')", 5);
            }
        }else{
            if(endValue<(currWidth-showcasePixelPerInterval)){
                wrapperEl.style.width=(currWidth-showcasePixelPerInterval)+'px';
                wrapperEl.style.left=(document.body.offsetWidth/2)-(wrapperEl.offsetWidth/2)+'px';
            }else{
                wrapperEl.style.width=endValue+'px';
                wrapperEl.style.left=(document.body.offsetWidth/2)-(wrapperEl.offsetWidth/2)+'px';
                
                window.clearInterval(showcaseTween);
                
                contentEl=document.getElementById("showcaseViewContent");

                if (contentEl.offsetHeight>parseFloat(wrapperEl.style.height)){
                    var direction2='bigger';
                }else{
                    var direction2='smaller';
                } 
                showcaseTween = window.setInterval("makeShowcaseResize(wrapperEl,'"+contentEl.offsetHeight+"','"+direction2+"','height')", 5);
            }
        }       
    }else{
        var currHeight=parseFloat(wrapperEl.style.height);
        if (direction=="bigger"){
            if(endValue>(currHeight+showcasePixelPerInterval)){
                wrapperEl.style.height=(currHeight+showcasePixelPerInterval)+'px';
            }else{
                wrapperEl.style.height=endValue+'px';
                window.clearInterval(showcaseTween);
                
                contentEl=document.getElementById("showcaseViewContent");
                contentEl.style.cssText='opacity:0; -moz-opacity:0; filter:Alpha(opacity=0);';
                wrapperEl.className="";
                showcaseTween = window.setInterval("makeShowcaseBlend(contentEl)", 5);
            }
        }else{
            if(endValue<(currHeight-showcasePixelPerInterval)){
                wrapperEl.style.height=(currHeight-showcasePixelPerInterval)+'px';
            }else{
                wrapperEl.style.height=endValue+'px';
                window.clearInterval(showcaseTween);
                
                contentEl=document.getElementById("showcaseViewContent");
                contentEl.style.cssText='opacity:0; -moz-opacity:0; filter:Alpha(opacity=0);';
                wrapperEl.className="";
                showcaseTween = window.setInterval("makeShowcaseBlend(contentEl)", 5);
            }
        }
    }
}



function makeShowcaseBlend(contentEl){
    if (contentEl.style.opacity<1){
        var newVal=parseFloat(contentEl.style.opacity)+0.1;
        contentEl.style.cssText='opacity:'+newVal+'; -moz-opacity:'+newVal+'; filter:Alpha(opacity='+(newVal*100)+');';
    }else{
        window.clearInterval(showcaseTween);
        contentEl.style.cssText='';        
    }    
}

function startShowcaseMouseTrack(event, el){
	if (showcaseNextPageLinkEl){
		showcaseNextPageLinkEl.style.display="block";
	}
	if (showcaseBackPageLinkEl){
		showcaseBackPageLinkEl.style.display="block";
	}
}

function endShowcaseMouseTrack(event, el){
	if (showcaseNextPageLinkEl) showcaseNextPageLinkEl.style.display="none";
	if (showcaseBackPageLinkEl) showcaseBackPageLinkEl.style.display="none";
}

function changeShowcaseViewLinks(wrapperEl){
	var aList=wrapperEl.getElementsByTagName("A");
		if (aList.length>0){
			var l=aList.length;
			showcaseNextPageLinkEl=false;
			showcaseBackPageLinkEl=false;
			for (var i=0; i<l; i++){
				if (aList[i].className&&aList[i].className=="viewNavNext"){
					showcaseNextPageLinkEl=aList[i];
					showcaseNextPageLink=aList[i].href;
					aList[i].href="javascript:void(0);";
					aList[i].onclick=function(event){changeShowcasePage(event, showcaseNextPageLink);};
					aList[i].onmouseover=function(event){startShowcaseMouseTrack(event, aList[i]);};
					aList[i].onmouseout=function(event){endShowcaseMouseTrack(event, aList[i]);};
					
				}
				if (aList[i].className&&aList[i].className=="viewNavBack"){
					showcaseBackPageLinkEl=aList[i];
					showcaseBackPageLink=aList[i].href;
					aList[i].href="javascript:void(0);";
					aList[i].onclick=function(event){changeShowcasePage(event, showcaseBackPageLink);};
					aList[i].onmouseover=function(event){startShowcaseMouseTrack(event, aList[i]);};
					aList[i].onmouseout=function(event){endShowcaseMouseTrack(event, aList[i]);};
				}
			}
		}
}

function changeShowcasePage(event, oldLink){
	openShowcaseView(oldLink, showcaseModuleId, 'change');
}

function closeShowcaseView(){
	var coverEl=document.getElementById("showcaseCover");
	document.body.removeChild(coverEl);
    
    var wrapperEl=document.getElementById("showcaseViewWrapper");
	document.body.removeChild(wrapperEl);
}