// Copyright (c) 2010 Marco Kellershoff <marco.kellershoff@googlemail.com>, http://stacksyard.com/


var gaia = new Object();
gaia.get = function(o){
	return document.getElementById(o);
};
gaia.decode = new Object();
/**
 * @author Marco Kellershoff <marco.kellershoff@googlemail.com>
 * @param JSON-String o
 * @return Object
 * Decodes a JSON-String and returns an Object
 */
gaia.decode.json = function(o) {
	return eval("(" + o + ")");
};
gaia.connection = new Object();
/**
 * @author Marco Kellershoff <marco.kellershoff@googlemail.com>
 * @param Object obj
 * @return void
 * XMLHttpRequest with callback
 */
gaia.connection.request = function(obj) {
    var xmlHttp = null;
    try {
        xmlHttp = new XMLHttpRequest();
    }catch(e) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(e) {
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            }catch(e) {
                xmlHttp = null;
            }
        }
    }if (xmlHttp) {
        xmlHttp.open(obj.method, obj.url, true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        if(obj.method == 'POST') {
            if(typeof(obj.params) != 'undefined') {
                xmlHttp.setRequestHeader("Content-length", obj.params.length);
            }
        }
        xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {
                var json = gaia.decode.json(xmlHttp.responseText);
                if(json.success) {
                    if(typeof(obj.success) == 'function'){obj.success(xmlHttp.responseText);}
                }
                else {
                    if(typeof(obj.failure) == 'function') {obj.failure(xmlHttp.responseText);}
                }
            }
        };
        if(obj.method == 'POST' && typeof(obj.params) != 'undefined') {
            xmlHttp.send(obj.params);
        }
        else {
            xmlHttp.send(null);
        }
    }
};
gaia.data = new Object();
gaia.data.addslashes = function(str) {
    str=str.replace(/\\/g,'\\\\');
    str=str.replace(/\'/g,'\\\'');
    str=str.replace(/\"/g,'\\"');
    str=str.replace(/\0/g,'\\0');
    return str;
};
gaia.data.stripslashes = function(str) {
    str=str.replace(/\\'/g,'\'');
    str=str.replace(/\\"/g,'"');
    str=str.replace(/\\0/g,'\0');
    str=str.replace(/\\\\/g,'\\');
    return str;
};
gaia.css = new Object();
gaia.css.setClass = function(O,C) {
    O.setAttribute('className',C);
};
gaia.fx = new Object();
gaia.fx.toggleVisibility = function(o) {
    if(o.style.display == 'block') {
        o.style.display = 'none';
    }
    else {
        o.style.display = 'block';
    }
};

gaia.fx.toggleClassName = function(o,a,b) {
    if(o.className == a) {
        o.setAttribute('class', b);
    }
    else {
        o.setAttribute('class', a);
    }
};

gaia.fx.image = new Object();
/**
 * @author Marco Kellershoff <marco.kellershoff@googlemail.com>
 * @param String URL, String TITLE, String BIGURL
 * @return void
 * Fancy Image Preview
 */
gaia.fx.image.preview = function(URL, TITLE, BIGURL) {
	if(URL != false){
		gaia.fx.modalmode(true);
		var div = document.createElement('div');
		div.setAttribute('id','gaiafx_imagePreview');
		var div2 = document.createElement('div');
		div2.setAttribute('class','outbox');
		var img = document.createElement('img');
		img.setAttribute('src',URL);
		img.setAttribute('class','image');
		//img.setAttribute('onload',"document.getElementById('gaiafx_imagePreview').setAttribute('style','left:'+(gaiaframe.getWindowDimensions('width')-120-document.getElementById('gaiafx_imagePreview').getElementsByClassName('image')[0].width)/2+'px');")
			if(TITLE) {var imgTitle = document.createElement('div');imgTitle.innerHTML = TITLE;}
			else{var imgTitle = document.createElement('span');}
		var closeLink = document.createElement('img');
		closeLink.setAttribute('id','gaiafx_imagePreviewCloseButton');
		closeLink.setAttribute('onclick','gaia.fx.image.preview(false);');
		closeLink.setAttribute('src','./core/images/48x48/button.close.png');
		div2.appendChild(closeLink);
		div2.appendChild(img);
		div2.appendChild(imgTitle);
		div.appendChild(div2);
		document.body.appendChild(div);
		window.onkeypress = function(event) {gaia.fx.image.previewCaptureKey(event);};
	}else {
		gaia.fx.modalmode(false);
		var child = document.getElementById('gaiafx_imagePreview');
		while(child.hasChildNodes()){
			child.removeChild(child.lastChild);
		}
		document.body.removeChild(child);
	}
};
gaia.fx.modalmode = function(TRIGGER){
	if(TRIGGER == true)
	{
		var div = document.createElement('div');
		div.setAttribute('id','gaiafx_modalBackground');
		document.body.appendChild(div);
	}else{
		var child = document.getElementById('gaiafx_modalBackground');
		while(child.hasChildNodes()){
			child.removeChild(child.lastChild);
		}
		document.body.removeChild(child); 
	}
};

gaia.fx.image.previewCaptureKey = function(e) {
	var kC  = (window.event) ?    // MSIE or Firefox?
	event.keyCode : e.keyCode;
	var Esc = (window.event) ?   
	27 : e.DOM_VK_ESCAPE // MSIE : Firefox
	if(kC==Esc) {
		gaia.fx.image.preview(false);
		window.onkeypress = null;
	}
};


function opacity(id, opacStart, opacEnd, millisec, MODE) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;
	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "','"+i+"','"+MODE+"')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++) {
			setTimeout("changeOpac(" + i + ",'" + id + "','"+i+"','"+MODE+"')",(timer * speed));
			timer++;
		}
	}
}
function portfolio_movePicture(id,dir,weburl) {
    gaia.connection.request({
        method: 'POST',
        url: weburl+'ajax.php?module=move&method=portfolio_picture&id='+id+'&direction='+dir,
        success: function() {
            var o_left,o_right,o_left_qDelete,o_left_qMLeft,o_left_qMRight,o_right_qDelete,o_right_qMLeft,o_right_qMRight = null;
            var c = document.getElementById('portfolio-outline-container');
            var i = 0;
            for(i=0;i<c.getElementsByClassName('surrounding_thumbnail').length;i++) {
                if(c.getElementsByClassName('surrounding_thumbnail')[i].id == 'surrounding_thumbnail_'+id) {
                    break;
                }
            }
            if(dir=='left') {
                o_left = c.getElementsByClassName('surrounding_thumbnail')[i-1];
                o_right = document.getElementById('surrounding_thumbnail_'+id+'');
            }else {
                o_left = document.getElementById('surrounding_thumbnail_'+id+'');
                o_right = c.getElementsByClassName('surrounding_thumbnail')[i+1];
            }
            o_left.parentNode.insertBefore(o_right,o_left);
        },
        failure: function() {
            alert('Konnte Photo nicht verschieben!');
        }
    });
}

function portfolio_deletePictureConfirm(id,weburl) {
    o = document.getElementById('portfolioThumb_'+id);
    o.style.border='2px solid #f00';
    if(window.confirm("Dieses Bild wirklich löschen?")) {
        portfolio_deletePicture(id,weburl);
    }else {
        o.style.border='none';
    }
}

function portfolio_deletePicture(id,weburl) {
    gaia.connection.request({
        method: 'POST',
        url: weburl+'ajax.php?module=delete&method=portfolio_picture&id='+id,
        success: function() {
            var d = document.getElementById('portfolio-outline-container');
            d.removeChild(document.getElementById('surrounding_thumbnail_'+id+''));
        },
        failure: function() {
            alert('Konnte Photo nicht löschen!');
        }
    });
}

function portfolio_addPage(id,title,weburl) {
    if(window.confirm("Seite zu:\n\n\t"+title+"\n\nhinzufügen?"))
    {
        gaia.connection.request({
            method: 'POST',
            url: weburl+'ajax.php?module=add&method=portfolio_page&id='+id,
            success: function() {
                window.location.reload();
            },
            failure: function() {
                alert('Konnte Seite nicht erstellen!');
            }
        });
    }
}

function portfolio_editPortfolio(id,weburl, mode) {
    var d = document;
    if(!mode)
    {
        if(d.getElementById('form_edit_portfolio').style.display != 'block')
        {
            gaia.connection.request({
                method: 'POST',
                url: weburl+'ajax.php?module=get&method=portfolio&id='+id,
                success: function(o) {
                    var json = gaia.decode.json(o);
                    d.getElementById('form_edit_portfolio').style.display = 'block';
                    d.getElementById('formfield_edit_portfolio_name').value = json.name;
                    d.getElementById('formfield_edit_portfolio_description').value = json.description;
                    d.getElementById('formfield_edit_portfolio_keywords').value = json.keywords;
                },
                failure: function() {
                    alert('Konnte Daten von Portfolio nicht ermitteln!');
                }
            });
        }
        else {
            d.getElementById('form_edit_portfolio').style.display = 'none';
        }
    }
    else
    {
        var name = d.getElementById('formfield_edit_portfolio_name').value;
        var description = d.getElementById('formfield_edit_portfolio_description').value;
        var keywords = d.getElementById('formfield_edit_portfolio_keywords').value;

        gaia.connection.request({
            method: 'POST',
            url: weburl+'ajax.php?module=edit&method=portfolio&id='+id,
            params: 'name='+name+'&description='+description+'&keywords='+keywords,
            success: function() {
                alert('Erfolgreich editiert!');
                window.location.reload();
            },
            failure: function() {
                alert('Konnte Daten von Portfolio nicht speichern!');
            }
        });
    }
}

function portfolio_deletePage(parent_id,sub_id,title,weburl,wikiUrl) {
    if(window.confirm("Seite "+sub_id+" von:\n\n\t"+title+"\n\ninklusive aller Bilder entfernen?"))
    {
        gaia.connection.request({
            method: 'POST',
            url: weburl+'ajax.php?module=delete&method=portfolio_page&sub_id='+sub_id+'&parent_id='+parent_id+'&wikiUrl='+wikiUrl,
            success: function() {
                window.location.href = weburl+'portfolio/'+wikiUrl+'/1.html';
            },
            failure: function() {
                alert('Konnte Seite nicht löschen / Evtl. gibt es nur noch diese eine Seite im Portfolio!');
            }
        });
    }
}

function portfolio_deletePortfolio(wikiUrl, title, weburl) {
    if(window.confirm("Möchten Sie wirklich das gesamte Portfolio:\n\n\t"+title+"\n\ninklusive aller Bilder entfernen?"))
    {
        gaia.connection.request({
            method: 'POST',
            url: weburl+'ajax.php?module=delete&method=portfolio&wikiUrl='+wikiUrl,
            success: function() {
                window.location.href = weburl+'portfolio.html';
            },
            failure: function() {
                alert('Konnte Portfolio nicht löschen!');
            }
        });
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id, LAST, MODE) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
    if(LAST == 0 && MODE == 'out') {
    	document.getElementById(id).style.display = 'none';
    }
    if(LAST == 100 && MODE == 'in') {
    	document.getElementById(id).style.display = 'block';
    }
}

function gaia_fxFade(VARMODE, VARID) {
	if(VARMODE == 'out') {
		opacity(VARID, 100, 0, 500, VARMODE);
	}
	else if(VARMODE == 'in') {
		opacity(VARID, 0, 100, 500, VARMODE);
	}
}

function gaia_fx_openFullscreen(url) {
	params  = 'width='+screen.width;
	params += ', height='+screen.height;
	params += ', top=0, left=0';
	params += ', fullscreen=yes';
	newwin = window.open(url,'windowname4', params);
	if (window.focus) {
		newwin.focus();
	}
	return false;
}

