/**
 * @author carndt
 */
openImage = function(uid, id, categorie, bildTitel){
    //popup('/gc/Gallery/Image.aspx?uid=' + uid + '&id=' + id + '&categorie=' + categorie + '&width=400&height=600','name_' + id,400,600);
    popup('/gc/Gallery/Image.aspx?uid=' + uid + '&id=' + id + '&categorie=' + categorie + '&width=400&height=600','name_image',400,600);
}
function trim(strIn){ 
	//entfernt überflüssige Leerstellen aus einem String
	return(strIn.replace(/(^\s+|\s+$)/g,'')); 
}
function getOpener(){
	var i = 0;
	var tempW = self;
	while (tempW.opener && !tempW.opener.closed && i < 10){
		tempW = tempW.opener;
		i++;
	}
	return tempW;
}

function popup(url,name,width,height) {
    
    
    var topWin = getOpener().top;
    
    if (typeof(topWin.singleImageWin) == 'undefined' || topWin.singleImageWin == null || topWin.singleImageWin.closed){
		var winX = width+17>screen.availWidth?screen.availWidth:width+17;
		var winY = height+133>screen.availHeight?screen.availHeight:height+133;
		topWin.Bids = self.Bids;
		topWin.imgsToShow = self.imgsToShow;	
		
		var left   = 50; //(screen.width  - width)/2;
        var top    = 50; //(screen.height - height)/2;
        var params = 'width='+width+', height='+height;
        params += ', top='+top+', left='+left;
        params += ', directories=no';
        params += ', location=no';
        params += ', menubar=no';
        params += ', resizable=yes';
        params += ', scrollbars=yes';
        params += ', status=yes';
        params += ', toolbar=no';
		
		topWin.singleImageWin = self.open(url,name,params);
		topWin.singleImageWin.focus();
	} else {		
		    topWin.singleImageWin=null;
		    popup(url,name,width,height);
	}

    return false;
}

/*************************************************************
* Umgebungsvariablen                                         *
*************************************************************/
var slideImg           = 0;
var Bids               = new Array();


openImageCB = function(result){
    //var div = dojo.byId("openImage");
    var img = new Image();
    var d = new Date();
    var w;
    var h;
    
    if (result != "imageNotFound") {
        var param = result.split(',');
        uid = param[0];
        id = param[1];
        categorie = param[2];
        w = param[3];
        h = param[4];
        
        var winH, winW;
        if (navigator.appName.indexOf("Microsoft") != -1) {
            winW = document.body.offsetWidth;
            winH = document.body.offsetHeight;
        }
        else {
            winW = window.outerWidth;
            winH = window.outerHeight;
        }
        img.width = w;
        img.height = h;
        if (img.width > winW) {
            img.width = winW - 150;
            var f = w / img.width;
            img.height = h / f;
        }
        if (img.height > winH) {
            img.height = winH - 150;
            var f = h / img.height;
            img.width = w / f;
        }

        //img.oncontextmenu = function(){return false;};
        //div.appendChild(img);

        //window.open('/gc/Gallery/Image.aspx?uid=' + uid + '&id=' + id + '&categorie=' + categorie + '&width=' + img.width + '&height=' + img.height,'name_' + id,'width=' + img.width + ',height=' + img.height + ',left=100,top=200');
        popup('/gc/Gallery/Image.aspx?uid=' + uid + '&id=' + id + '&categorie=' + categorie + '&width=' + img.width + '&height=' + img.height,'name_' + id,img.width,img.height);
        //$('#dlgImage').bind('click',function(){dijit.byId("dlgImage").hide();});
        //dijit.byId("dlgImage").show();        
    }
}

openImageDef = function(imagePath, bildTitel){
    var jsonObject = new dojo.rpc.JsonService('/Services/definition/info.smd');
    
    jsonObject.imageInfoDef(imagePath).addCallback(openImageDefCB);
    
    // BildTitel
    var divBildTitel = dojo.byId("divBildTitel");
    if (divBildTitel != null) {
        divBildTitel.innerHTML = "<strong>"+bildTitel+"</strong>";
    }
}
openImageDefCB = function(result){
    var div = dojo.byId("openImage");
    var img = new Image();
    var d = new Date();
    var w;
    var h;
    
    if (result != "imageNotFound") {
        var param = result.split(',');
        //uid = param[0];
        //id = param[1];
        path = param[0];
        w = param[1];
        h = param[2];
        
        while (div.hasChildNodes()) {
            div.removeChild(div.firstChild);
        }
        
        console.log(path);
        img.src = path;
        
        var winH, winW;
        if (navigator.appName.indexOf("Microsoft") != -1) {
            winW = document.body.offsetWidth;
            winH = document.body.offsetHeight;
        }
        else {
            winW = window.innerWidth;
            winH = window.innerHeight;
        }
        
        img.width = w;
        img.height = h;
  
        if (img.width > winW) {
            img.width = winW - 150;
            var f = w / img.width;
            img.height = h / f;
        }
        if (img.height > winH) {
            img.height = winH - 150;
            var f = h / img.height;
            img.width = w / f;
        }
	
        div.appendChild(img);
        dijit.byId("dlgImage").show();
        
        
    }
}
