
var actImageId;
var galleryParamString;
var galleryInfo;
var nowloading = false;
var images = new Array();
var isfirst, islast = false;
var itemsperpage = 25;


function getPageInfos () {		
	data = "";	
    dojo.xhrGet ({
        url: 'json/picture/'+actImageId+"?"+galleryParamString,
        handleAs: 'json', 
        load: function (data) {
            console.debug('Success: Foto '+actImageId+' Daten geladen.');
			setPageInfos(data);
        },
        error: function (error) {
            console.error('Error: ', error);
        }
    });
}

//picture gallery
var initKlicks = function(){
    console.log("Initializing Ajax Nav");	
	actImageId = document.startImage;
	galleryParamString = document.startParams;
	getPageInfos();
};

function setAjaxLinks(){
    console.log("Changing Links to Ajax");	
	
	if(dojo.byId('prevlink')){
		dojo.byId('prevlink').href="javascript:linkprev(); void(0);";
	}
	if(dojo.byId('imagoPreviousImageLink')){
		dojo.byId('imagoPreviousImageLink').href="javascript:linkprev(); void(0);";
	}
	if(dojo.byId('nextlink')){
		dojo.byId('nextlink').href="javascript:linknext(); void(0);";
	}
	if(dojo.byId('imagoNextImageLink')){
		dojo.byId('imagoNextImageLink').href="javascript:linknext(); void(0);";
	}
	if(dojo.byId('overviewlink')){
		dojo.byId('overviewlink').href="javascript:linkup(); void(0);";
	}
	//setslideshow
	dojo.byId('slideshow').style.visibility = "visible";
}

function setPageInfos(data){
	galleryInfo = "";
	galleryInfo = data.galleryInfo;	
	var pics = galleryInfo.pictures;
	var pic = pics[galleryInfo.subsetIndex];
	
	setAjaxLinks();
	nowloading = false;
	ajaxprepared = true;
		
	galleryParamString = "";	
	galleryParamString = galleryInfo.galleryParamString;

	
	if((galleryInfo.galleryIndex*1)==0) isfirst = true;
	else isfirst = false;
	
	if(!galleryInfo.pictures[(galleryInfo.subsetIndex*1+1)] || galleryInfo.pictures[(galleryInfo.subsetIndex*1+1)] == "undefined")
		 islast = true;
	else
		 islast = false;
	
	if(isfirst){
		dojo.byId('prevlink').style.visibility = "hidden";
		dojo.byId('imagoPreviousImageLink').style.visibility = "hidden";
	}
	else{
		dojo.byId('prevlink').style.visibility = "visible";
		dojo.byId('imagoPreviousImageLink').style.visibility = "visible";
	}
	
	if(islast){
		dojo.byId('nextlink').style.visibility = "hidden";
		dojo.byId('imagoNextImageLink').style.visibility = "hidden";
	}
	else{
		dojo.byId('nextlink').style.visibility = "visible";
		dojo.byId('imagoNextImageLink').style.visibility = "visible";
	}
	
	dojo.byId('picture_index').innerHTML = (galleryInfo.galleryIndex*1+1);
	
	//preload pics
	for(var r=pics.length-1; r>=0; r--){
		preloadImage(pics[r]);
	}		
}


function preloadImage(imagedata){
	if(!images[imagedata.id]){
		var pictemp = new Image(imagedata.width,imagedata.height);
		pictemp.src = document.baseUrlPics + imagedata.filename;
		pictemp.loaded = false;
		pictemp.trys = 0;
		
        console.debug('Created: Foto '+imagedata.id+' wurde angelegt.');
		images[imagedata.id] = pictemp;
		checkImageLoaded(imagedata.id);
	}
	else console.debug('not Created: Foto '+imagedata.id+' wurde bereits angelegt.');
}

function checkImageLoaded(imageid){
	if(images[imageid].complete == true){
		images[imageid].loaded = true;
        console.debug('Loaded: Foto '+imageid+' wurde komplett geladen.');
	}
	else if(dojo.isSafari){ // doesen't support .complete
		images[imageid].trys++;
		var tempfunc = "images["+imageid+"].loaded = true";
		setTimeout(tempfunc, 1000);
	}
	else if(images[imageid].trys<6){
		images[imageid].trys++;
		var tempfunc = "checkImageLoaded("+imageid+")";
		setTimeout(tempfunc, 1000*images[imageid].trys);
		console.debug('Still Loading: '+imageid+' After '+images[imageid].trys+' nothing happened.');
	}
	else console.debug('Error Loading: '+imageid+' After '+images[imageid].trys+' nothing happened.');
}


function loadImage(index){
		var fadefront = "mainpcic";
		nowloading = true;
		//bild bereits vorausgeladen
		if(images[actImageId].loaded){
			
			dojo.fadeOut({
				node: fadefront, 
				duration: 150,
				onEnd: function(){ 
				
					dojo.byId('imagoLoadingDiv').style.height = images[actImageId].height;
					dojo.byId(fadefront).width = images[actImageId].width;
					dojo.byId(fadefront).height = images[actImageId].height;
					dojo.byId(fadefront).src = images[actImageId].src;
					dojo.byId('imagoLoadingDiv').style.display = "none";
					
					dojo.fadeIn({
					node: fadefront, 
					duration: 300
					}).play();
				}
			}).play();	
				
				var pic = galleryInfo.pictures[index];
							
				if(galleryInfo.galleryEvent){
					if(galleryInfo.galleryEvent.name && galleryInfo.galleryEvent.id){
						dojo.byId('content_event').innerHTML = 
							"Event: <a href='gallery?e="+galleryInfo.galleryEvent.id+"&"+galleryParamString+"'>"+galleryInfo.galleryEvent.name+"</a> ";
					}
					if(galleryInfo.galleryEvent.location){
						dojo.byId('content_location').innerHTML = 
							"Location: <a href='location?l="+galleryInfo.galleryEvent.location.id+"&"+galleryParamString+"'>"+galleryInfo.galleryEvent.location.name+"</a> ";
					}
				} else{
					if(pic.eventInfo){
						if(pic.eventInfo.name && pic.eventInfo.id){
							dojo.byId('content_event').innerHTML = 
								"Event: <a href='gallery?e="+pic.eventInfo.id+"&"+galleryParamString+"'>"+pic.eventInfo.name+"</a> ";
						}
						if(pic.eventInfo.location){
							dojo.byId('content_location').innerHTML = 
								"Location: <a href='location?l="+pic.eventInfo.location.id+"&"+galleryParamString+"'>"+pic.eventInfo.location.name+"</a> ";
						}
					}
				}
				
				if(pic.persons.length>0){
					var persstring = "Personen: ";
					for(var f=0; f<pic.persons.length; f++){
						if(f>0) persstring += ", "
						if(pic.persons[f].name) persstring += "<a href='gallery?p="+pic.persons[f].id+"&"+galleryParamString+"'>"+pic.persons[f].name+"</a>";			
					}
					dojo.byId('content_personen').innerHTML = persstring;		
					dojo.fadeIn({
						node: 'eventperson', 
						duration: 500,
						onPlay: function(){ 				
							dojo.byId('eventperson').style.display = "block";
						}
					}).play();	
				}
				else {
					dojo.byId('content_personen').innerHTML = "";			
					dojo.fadeOut({
						node: 'eventperson', 
						duration: 200,
						onEnd: function(){ 				
							dojo.byId('eventperson').style.display = "none";
						}
					}).play();	
				}
				if(pic.info){
					dojo.byId('content_picinfo').innerHTML = pic.info;		
					dojo.fadeIn({
						node: 'eventinfo', 
						duration: 500,
						onPlay: function(){ 		
							dojo.byId('eventinfo').style.display = "block";
						}
					}).play();	
				}
				else{
					dojo.byId('content_picinfo').innerHTML = "";			
					dojo.fadeOut({
						node: 'eventinfo', 
						duration: 200,
						onEnd: function(){ 				
							dojo.byId('eventinfo').style.display = "none";
						}
					}).play();	
				}
					
	       dojo.byId('picture').style.height = images[actImageId].height + "px"; 
			
		   getPageInfos();
				
		}
		else{
			// ++ loading div
			dojo.byId('imagoLoadingDiv').style.display = "inline";
			var tempfunc = "loadImage("+index+")";
			setTimeout(tempfunc, 400);
		}
	}

function goright(){
	if(!nowloading && !islast){
		if(galleryInfo.pictures[(galleryInfo.subsetIndex*1+1)]) actImageId = galleryInfo.pictures[galleryInfo.subsetIndex*1+1].id;
		loadImage(galleryInfo.subsetIndex*1+1);
	}
}
function goleft(){
	if(!nowloading && !isfirst){
		if(galleryInfo.pictures[galleryInfo.subsetIndex*1-1]) actImageId = galleryInfo.pictures[galleryInfo.subsetIndex*1-1].id;
		loadImage(galleryInfo.subsetIndex*1-1);
	}
}

//**************
// slideshow
var slide_clock;
var slide_setting = 4;
var slide_duration;

function slide_klick(){
	slide_duration = slide_setting;
	
	if(slide_clock){
		//stop
		clearInterval(slide_clock);
		slide_clock = null;
		dojo.byId('slide_link').innerHTML = "start>>";
	}
	else {
		slide_clock = setInterval('slide_count()', 1000);
		dojo.byId('slide_link').innerHTML = "stop||";
	}
}
function slide_count(){
	if(!islast){
		if(slide_duration>1){
			slide_duration--;
			dojo.byId('slide_sek').innerHTML = slide_duration;
		}
		else{
			goright();
			slide_duration = slide_setting;
			dojo.byId('slide_sek').innerHTML = slide_duration;
		}
	}
	else slide_klick();
}

function slide_up(){
	slide_setting++;
	slide_duration = slide_setting;
	dojo.byId('slide_sek').innerHTML = slide_duration;
}
function slide_down(){
	if(slide_setting>1){
		slide_setting--;
		slide_duration = slide_setting;
		dojo.byId('slide_sek').innerHTML = slide_duration;
	}
}




//**************
// reload werbung

function reloadAds(){
	dijit.byId("bannerbottom").setHref("http://www.conny.at/banner_jscript.jsp");
	//reloadWerbung("adscale:MTA0Yzgw", "http://www.conny.at/banner_jscript.jsp");
	//reloadWerbung("bannerright", "http://www.conny.at/banner_jscript.jsp");
	//reloadWerbung("bannerbottom", "http://www.conny.at/banner_jscript.jsp");
}

 function reloadWerbung(divid, url) { // (2)
      dojo.xhrGet( { // (3)
        // The following URL must match that used to test the server.
        url: url, 
        handleAs: "javascript",

        // The LOAD function will be called on a successful response.
        load: function(response, ioArgs) { // (4)
			alert("setting: "+response);
          dojo.byId(divid).setContent(response);  // KEY!
          return response; // (6)
        },

        // The ERROR function will be called in an error case.
        error: function(response, ioArgs) { // (4)
			alert("error: "+response);
          console.error("HTTP status code: ", ioArgs.xhr.status); // (7)
          return response; // (6)
          }
     });
}
