/* Open modal window with specified URL
	url - address of page to be shown in window
	width - width (in px)
	height - height (in px) 
*/
function draugiemWindowOpen(url,width,height){
	if(!width){
		width=400;
	}
	if(!height){
		height = 200;
	}
	draugiem_iframe.src = 'http://' + draugiem_domain+'/applications/external/external_actions.html?infobox='+encodeURIComponent(url)+'&width='+width+'&height='+height;
}

/* Close modal window,opened with draugiemWindowOpen */
function draugiemWindowClose(){
	draugiem_iframe.src = 'http://' + draugiem_domain + '/applications/external/external_actions.html?closeinfobox=1';
}

/* Resize application iframe to specified height
If no height is specified, iframe is resized to the size of element
with id that is specified by draugiem_container variable.
*/
function draugiemResizeIframe(height){
	if(!height){
		if(typeof(draugiem_container) !== 'undefined'){
			var container = document.getElementById(draugiem_container);
			height = Math.max( container.offsetHeight, container.scrollHeight ) + draugiem_container_offset;
		}
	}
	draugiem_iframe.src = 'http://' + draugiem_domain + '/applications/external/external_actions.html?setheight='+height;
}


/*Show send message dialog box */
function draugiemSendMessage(uid){
	draugiem_iframe.src = 'http://' + draugiem_domain + '/applications/external/external_actions.html?sendmessage='+uid;
}

/*Show send invitation
	text - default invitation text
	extra - extra data to attach to invitation, optional
 */
function draugiemSendInvite(text, extra){
	if(typeof(text) == 'undefined'){
		text = '';
	}
	if(typeof(extra) == 'undefined'){
		extra = '';
	}
	draugiem_iframe.src = 'http://' + draugiem_domain + '/applications/external/external_actions.html?invite=1&text='+encodeURIComponent(text)+'&extra='+encodeURIComponent(extra);
}


/* Post link to draugiem.lv Say
	title - link title
	url - link target url
	titlePrefix - prefix for title, optional
	text - default post text, optional
 */
function draugiemSay(title, url, titlePrefix, text ){
	draugiem_iframe.src = 'http://' + draugiem_domain + '/applications/external/external_actions.html?say=1'+
	'&title=' + encodeURIComponent( title ) +
	'&link=' + encodeURIComponent( url ) +
	(titlePrefix ? '&titlePrefix=' + encodeURIComponent( titlePrefix ) : '' ) +
	(text?'&text=' + encodeURIComponent( text ) : '' );
}

/* Post image(s) to draugiem.lv gallery
	title - gallery title
	url - image URL or an array of URLs (max 9)
 */
function draugiemGalleryAdd(title, url){
	if(typeof url == 'object'){
		url = url.join('|');
	}

	draugiem_iframe.src = 'http://' + draugiem_domain + '/applications/external/external_actions.html?gallery=1'+
	'&title=' + encodeURIComponent( title ) +
	'&url=' + encodeURIComponent( url );
}




/*Show  authorize popup */
function draugiemAuthorize(url){
	if(typeof(url) == 'undefined'){
		url = '';
	}
	draugiem_iframe.src = 'http://' + draugiem_domain + '/applications/external/external_actions.html?authorize=1&url='+encodeURIComponent(url);
}


function draugiemAddLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


var draugiem_iframe = document.createElement("iframe");
if(typeof(draugiem_container_offset) === 'undefined'){
	var draugiem_container_offset = 0;
}
if(!draugiem_domain){
	var draugiem_domain = 'www.draugiem.lv';
}
draugiem_iframe.style.display = "none";

draugiemAddLoadEvent(function() {
	document.body.appendChild(draugiem_iframe);
	if(typeof(draugiem_container) !== 'undefined'){
		draugiemResizeIframe();
	}	
});

