// === PRELOAD FUNCTIONS ===

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



// === TOGGLE ID ===

	var showMode = 'block';
	if (document.all) showMode = '';

	function toggleId(id, y_scroll){
	  if (document.all) {
		var obj = document.all[id];
	  } else if (document.getElementById) {
		 var obj = document.getElementById(id);
	  } else return false;
	  if (obj.style.display == 'none') {
	     obj.style.display = showMode;
	  	 if (y_scroll) scroll(0,y_scroll);
	  } else obj.style.display = 'none';
	  return false;
	}


// === POPUP OPEN ===

	function openWin(indirizzo,olw) {
  		myWindow=window.open(indirizzo,'','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,top=30,left=20,width=626,height=520');
		return myWindow;
	}
	mw=null;


// === SET LINKS TO "splinder.com" ===

	function parse_links() {
		   for(i=0; i< document.links.length; i++) {
		     re = /splinder.it/gi;
		     if ( document.links[i].href != document.links[i].href.replace(re,'splinder.com') ) {
		       document.links[i].href = document.links[i].href.replace(re,'splinder.com');
		       document.links[i].innerHTML = document.links[i].innerHTML.replace(re,'splinder.com');
			 }
		   }
	}

function isValidKeyCode(key) {
  switch (key) {
    case 16: // shift
    case 17: // ctrl
    case 18: // alt
    case 20: // caps lock
    case 33: // page up
    case 34: // page down
    case 35: // end
    case 36: // home
    case 37: // left arrow
    case 38: // up arrow
    case 39: // right arrow
    case 40: // down arrow
    case 9:  // tab
    case 13: // enter
    case 27: // esc
    case 112: // F1
    case 113: // F2
    case 114: // F3
    case 115: // F4
    case 116: // F5
    case 117: // F6
    case 118: // F7
    case 119: // F8
    case 120: // F9
    case 121: // F10
    case 122: // F11
    case 123: // F12
      return false;
  }
  return true;
}

function toggleCheckboxes(obj_name) {
	var d = document.getElementsByTagName('input');
        for(i=0;i<d.length;i++) if(d[i].type=='checkbox') {
                if (obj_name && d[i].name.indexOf(obj_name)==-1) continue;
                d[i].checked=!d[i].checked;
        }
	return false;
}

function myblogManagePostsComments(action, e) {
	var frm = document.forms[0];
	switch(action) {
		case 'filter':
			frm.action = submit_url;
			frm.submit();
			return;
		case 'selectall':
			return toggleCheckboxes('edit[selected]');
		case 'validate':
			var d = document.getElementsByTagName('input');
			var result = false;
			for(i=0;i<d.length;i++) if(d[i].type=='checkbox') {
				if (d[i].checked) {
					result = true;
					break;
				}
			}
			if (result) frm.submit();
				else alert(selectMessage);
			return result;
		case 'onUserKeyPress':
		case 'onUserKeyUp':
		case 'onUserBlur':
			var objStatus = document.getElementById('filter_status');
			var objUser = document.getElementById('filter_author');
			if (!objStatus) return false;
			if (action=='onUserBlur' && !objUser.value) {
				objStatus.disabled = false;
				return false;
			}
			objStatus.value = 'all';
			var user = objUser.value;
                        if (action=='onUserKeyUp' && !user) {
				objStatus.disabled = false;
	                        return false;
			}
			if (e.keyCode==8 && user.length==1) {
				objStatus.disabled = false;
				return false;
			}
			if (isValidKeyCode(e.keyCode)) {
				objStatus.disabled = true;
				return false;
			}
			return false;
	}
}

function showOverlibPopup(title, content) {
    overlib(content,
       FOLLOWSCROLL,
       STICKY,
       CAPTION, title,
       WIDTH, 300,
       CSSCLASS,
       TEXTFONTCLASS,'fontClassOL',
       FGCLASS,'fgClassOL',
       BGCLASS,'bgClassOL',
       CAPTIONFONTCLASS,'capfontClassOL',
       CLOSECLICK);
    return false;
}

function media_popup_style() {
	// keep alignment styles sync with function _media_post_format() in media.module
    var style = '';
    for(i=0; i<document.getElementsByName('edit[align]').length; i++) {
	  if (document.getElementsByName('edit[align]')[i].checked) {
		switch(i) {
			case 0:
				style = ' style=\'margin: 0pt 10px 10px 0pt; float:left;\' ';
				break;
			case 1:
				style = ' style=\'margin: 0px auto 10px; display: block; text-align: center;\' ';
				break;
			case 2:
				style = ' style=\'margin: 0pt 0pt 10px 10px; float: right;\' ';
				break;
		}
	  }
	}
	return style;
}

function media_popup_post_photo(textarea_id, title, path_thumbnail, path_small, path_medium) {
    style = media_popup_style();
    var path = '';

	for(i=0; i<document.getElementsByName('edit[thumb_id]').length; i++) {
	  if (document.getElementsByName('edit[thumb_id]')[i].checked) {
		switch(i) {
			case 2:
				path = path_medium;
			    if (path) break;
			case 1:
				path = path_small;
			    if (path) break;
			case 0:
				path = path_thumbnail;
		}
	  }
	}
	if (!path) {
		alert('Path not valid');
		return false;
	}

	opener.InsertHTML(textarea_id, '<img ' + style + ' src=\'' + path + '\' alt=\'' + title + '\' />');
}

function media_popup_post_audio(textarea_id, placeholder) {
	style = media_popup_style();
        placeholder = placeholder.replace(/%style/gi, style);
	opener.InsertHTML(textarea_id, placeholder);	
}

function mysearch_submit(type) {
	var frm = document.topbar_search;
	var query = frm.elements[0].value;
	if (!query || query=='') return false;
	if (type == 'site') frm.elements[1].value = '';
	frm.submit();
}

function blognotifier_launch_tipicweb(uid, chat, notify) {
  var url = '/blognotifier/launchtipicweb';
  if (chat) url += '?chat_with=' + uid;
  window.open(url, 'TipicWEB', 'width=250, height=500, resizable');
}

function AjaxReport(nid, report_type) {
  var btn = new Array();
  var post_url = 'ajax_report/' + nid + '?' + report_type + '=1';
  AjaxSubmit(btn, post_url, null, AjaxReportResponse, 'content_control_throbber');
}
function AjaxReportResponse(xmlhttp, parameter) {
  var obj = document.getElementById('content_control');
  if (obj) obj.innerHTML = xmlhttp.responseText;
}

// === topframe animation ===

var visualization_order;
function makeSpans(textParam, destinationParam)
{
  textParam = textParam.replace(/\|/g, " ");
  mytext = textParam.split("</A>");
  if (mytext.length==1) // firefox puts everything in lowcase, IE not
     mytext = textParam.split("</a>");

  for(i =0; i< mytext.length; i++)
  {
     mytext[i]="<span style='visibility: hidden' id='keyword"+i+"' >" + mytext[i] + "</A></span>";
  }

  document.getElementById(destinationParam).innerHTML = "";
  for(i =0; i< mytext.length; i++) {
     document.getElementById(destinationParam).innerHTML += mytext[i];
     if (i< mytext.length-2)
        document.getElementById(destinationParam).innerHTML += "<span style='visibility: hidden' id='separator"+i+"_" + (i+1) + "' >&nbsp;&nbsp; | &nbsp;&nbsp;</span>";
  }

  visualization_order = new Array( mytext.length );  
  for(i=0; i< visualization_order.length; i++)
  {
     visualization_order [i] = i;
  }
}

function start_show()
{
   visualization_order = Shuffle(visualization_order);
   currentChar=0;
   visualization_order = Shuffle(visualization_order);
   for (i=0; i<visualization_order.length; i++) {
      document.getElementById("keyword"+i).style.visibility="hidden";
      sep1 = document.getElementById("separator"+(i-1)+"_"+i);
      if (sep1)
          sep1.style.visibility = "hidden";


   }

   setTimeout("show()", delay);
}

function show()
{
  if (document.getElementById)
  {
    var dest=document.getElementById(destination);
    if (dest)
    {
      toshow = visualization_order[currentChar];
      document.getElementById("keyword"+toshow).style.visibility="visible";
      preword   = document.getElementById("keyword"+(toshow+1));
      postword  = document.getElementById("keyword"+(toshow-1));
      sep1 = document.getElementById("separator"+toshow+"_"+(toshow+1));
      sep2 = document.getElementById("separator"+(toshow-1)+"_"+toshow);
      if (sep1)
          sep1.style.visibility = preword.style.visibility;
      if (sep2)
          sep2.style.visibility = postword.style.visibility;


      currentChar++;
      if (currentChar>=visualization_order.length)
      {
        setTimeout("start_show()", 6000);
      }
      else
      {
        setTimeout("show()", delay);
      }
    }
  } 
}

function Shuffle(Q) 
{
    var R, T, J;
    for (J = Q.length - 1; J > 0; J--) {
        R = Math.floor(Math.random() * (J+1));
        T = Q[J];
        Q[J] = Q[R];
        Q[R] = T;
    }
    return Q;
}

function startTyping2(textParam, delayParam, destinationParam)
{
  makeSpans( textParam, destinationParam);
  destination = destinationParam;
  delay = delayParam;
  start_show();
}

function startTopframeAnimation()
{
try {

startTyping2(
  document.getElementById("block-right").innerHTML,
  750,
  "block-right" );

}
catch(e) {};
}
