

//======================================================================
//----------------Globals------------------------------------
//======================================================================


_fade_tip_divo_timeset = null;

//-------------------------------------------------------------------------------------
//==========================END GLOBALS=====================================
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------












//======================================================================
//----------------ROLLOVER FUNCTIONS------------------------------------
//======================================================================



function show_tip_divo(){

if (_fade_tip_divo_timeset) window.clearTimeout(_fade_tip_divo_timeset);

span_obj = document.getElementById("tip_span");
div_obj = document.getElementById("tip_divo");

x = GetX(span_obj);
y = GetY(span_obj);

w = GetWidth(div_obj);
h = GetHeight(div_obj);



div_obj.style.left = x+w-40 + 'px';
div_obj.style.top = y + 'px';

div_obj.style.visibility = 'visible';


}



function fade_tip_divo(){

//document.getElementById('tip_divo').style.visibility = 'hidden'

_fade_tip_divo_timeset = window.setTimeout("document.getElementById('tip_divo').style.visibility = 'hidden'",500);


}





function td_over(obj){

//obj = document.getElementById(id);

obj.style.background = '#736F64';

if (_fade_tip_divo_timeset) window.clearTimeout(_fade_tip_divo_timeset);


}



function td_out(obj){

//obj = document.getElementById(id);
obj.style.background = 'gray';

}


function set_indio(){

search_obj = document.getElementById('searchh');

indi_obj = document.getElementById('indi_divo');


x = GetX(search_obj);
y = GetY(search_obj);


w = GetWidth(search_obj);
h = GetHeight(search_obj);



indi_obj.style.left = x+w + 20 + 'px';
indi_obj.style.top = y + 'px';





}






//-------------------------------------------------------------------------------------
//==========================END ROLLOVER FUNCTIONS=====================================
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------










//-------------------------------------------------------------------------------------
//==========================CLICKING FUNCTIONS=====================================
//-------------------------------------------------------------------------------------




function show_comments(click_id,open_html,comments_id){

click_obj = document.getElementById(click_id);
div_obj = document.getElementById(comments_id);

x = GetX(click_obj);
y = GetY(click_obj);

w = GetWidth(click_obj);
h = GetHeight(click_obj);

div_obj.style.left = x+(0.5*w)-100 + 'px';
div_obj.style.top = y + 50 + 'px';

new Effect.BlindDown(comments_id, {duration: 0.5});

click_obj.innerHTML = open_html;

}



function close_comments(click_id, closed_html,comments_id){

new Effect.BlindUp(comments_id, {duration: 0.5});

$(click_id).innerHTML = closed_html;


}




function open_comments_preview(commentbox_id, previewbox_id){


commentbox_obj = document.getElementById(commentbox_id);
previewbox_obj = document.getElementById(previewbox_id);

x = GetX(commentbox_obj);
y = GetY(commentbox_obj);

w = GetWidth(commentbox_obj);
h = GetHeight(commentbox_obj);

previewbox_obj.style.left = x+100 + 'px';
previewbox_obj.style.top = y-100 + 'px';

new Effect.BlindDown(previewbox_id);


}



function close_comments_preview(previewbox_id){

new Effect.BlindUp(previewbox_id);

}



function setsearchindi(){


search_field_obj = document.getElementById('search_field');
indi_obj = document.getElementById('indi');

x = GetX(search_field_obj);
y = GetY(search_field_obj);

w = GetWidth(search_field_obj);
h = GetHeight(indi_obj);

indi_obj.style.left = x + w + 5 + 'px';
indi_obj.style.top = y + 'px';



}

function setindi(){

indi_obj = document.getElementById('indi');
search_field_obj = document.getElementById('mininav');

x = GetX(search_field_obj);
y = GetY(search_field_obj);

indi_obj.style.left = x + 150 + 'px';
indi_obj.style.top = y + 153 + 'px';


}




function setpreview(){


clicker_obj = document.getElementById('clicker');
preview_obj = document.getElementById('preview');

x = GetX(clicker_obj);
y = GetY(clicker_obj);

w = GetWidth(preview_obj);
h = GetHeight(preview_obj);

preview_obj.style.left = x + 2 + 'px';
preview_obj.style.top = y + 'px';



}



//-------------------------------------------------------------------------------------
//==========================END CLICKING FUNCTIONS=====================================
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------



function scrolldownmessages(){

  $("chat_messages").scrollTop = $("chat_messages").scrollHeight;

}



function chatformsubmit(code){

new Ajax.Updater('chat_messages', '/c/add_to_chat', {asynchronous:true, evalScripts:true, onComplete:function(request){scrolldownmessages();}, onLoading:function(request){$("chat_textarea").value = ""}, parameters:Form.serialize($('ff'))});

  if (typeof pe != 'object' || pe.timer == null){
    loadpe(code);
  }

return false;

}

function ispeactive(){

 if ((typeof pe == 'object') && pe.timer != null){
   return true;
 }else{
   return false;
 }
}

function ispeon(){

  if (ispeactive()){
    //$('peon').innerHTML = 'pe is on';
  }else{
    $('peon').innerHTML = 'Send message to activate chat';
  }

  //setTimeout("ispeon();",1000);
}




function submitenter(myfield,e,code)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
if (keycode == 13)
{
chatformsubmit(code);
}
else
return true;
}

function togglechatwindow(){

  new Effect.toggle('sales_chat_window','blind', {duration: 0.3, afterFinish: function() { document.ff.content.focus(); scrolldownmessages(); } });
  

}

function deactivate_chat(code){

  if (ispeactive() == true){
    pe.stop()
    new Ajax.Updater('nothing_of_significance', '/c/disactivate_chat?code=' + code, {asynchronous:true, evalScripts:true}); return false;
  }else{
    new Ajax.Updater('nothing_of_significance', '/c/stop_bugging', {asynchronous:true, evalScripts:true}); return false;
  }
}


function topone(){

  if ($('sales_chat_window').style.display == 'none'){
    togglechatwindow();
  }

}

function chatwindowload(){

  setTimeout('topone()',20000);

}


function loadpe(code){

 pe = new PeriodicalExecuter(function() {new Ajax.Updater('chat_messages', '/c/chat_messages?code=' + code, {asynchronous:true, evalScripts:true})}, 5);

}


//-------------------------------------------------------------------------------------
//===========================HELPER FUNCTIONS==========================================
//-------------------------------------------------------------------------------------




//=========FORMAT TEXT OF PREVIEWS===============

function jtextformat(s){


 //newlines, spaces, tags

 temp = s.replace(/</gi,'&lt;').replace(/\r?\n/gi,'<br>').replace(/\s\s/gi,' &nbsp;');



 //normal tags <b> <u> <i> <small>

 temp = temp.replace(/\[b\](.*?)\[\/b\]/gi, '<b>$1</b>');
 temp = temp.replace(/\[u\](.*?)\[\/u\]/gi, '<u>$1</u>');
 temp = temp.replace(/\[i\](.*?)\[\/i\]/gi, '<i>$1</i>');
 temp = temp.replace(/\[small\](.*?)\[\/small\]/gi, '<small>$1</small>');

 // <hr>

 temp = temp.replace(/\[hr\](<br>)?/gi, '<hr/>');

 //links

 temp = temp.replace(/\[url=(.*?)\](.*?)\[\/url]/gi, "<a class='bluelink' rel='nofollow' target='_blank' href='$1'>$2</a>");
 //temp = temp.replace(/[^"](http.*?)([\s|<])/gi, "<a class='bluelink' rel='nofollow' target='_blank' href='$1'>$1</a>$2");


 //list stuff


  temp = temp.replace(/\[ul\](<br>)*(.*?)\[\/ul\](<br>)*/gi, '<ul>$2</ul>');
  temp = temp.replace(/\[ol\](<br>)*(.*?)\[\/ol\](<br>)*/gi, '<ol>$2</ol>') ;
    
  temp = temp.replace(/(&nbsp;)*?\[li\]/gi, '<li>') ;

 //span

 temp = temp.replace(/\[span.*?id="(.*?)".*?\](.*?)\[\/span\]/gi, "<span id='$1'\>$2</span>");



 //code

 temp = temp.replace(/\[code\](<br>)*(.*?)\[\/code\](<br>)*/gi, "<code><pre>$2</pre></code>");





 //delete character

 temp = temp.replace(/%\^/gi, '') 



 return temp;

}









//==========Get Width ofHTML element===============
function GetWidth (obj){

  var width = obj.offsetWidth;
  return width;
	
}



//==========Get Height of HTML element=============
function GetHeight (obj){

  var height = obj.offsetHeight;
  return height;

}


//==========Get Coords of HTML element=============

function GetX (obj){
  var x = 0;
  do{
    x += obj.offsetLeft;
    obj = obj.offsetParent;
  }
  while (obj);
  return x;
}

function GetXat (obj, elm){

  var x = 0;
  while (obj){
    x += obj.offsetLeft;
    obj = obj.offsetParent;
    }
    if (obj == elm)
    return x;
  return x - cmGetX (elm);

   
}


function GetY (obj){
  var y = 0;
  do{
    y += obj.offsetTop;
    obj = obj.offsetParent;
  }
  while (obj);
  return y;
}

//----------End Get Coords of HTML element---------



//-------------------------------------------------------------------------------------
//==========================END HELPER FUNCTIONS=====================================
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------














//-------------------------------------------------------------------------------------
//==========================UPDATING FUNCTIONS=====================================
//-------------------------------------------------------------------------------------




//-------------------------------------------------------------------------------------
//==========================END UPDATING FUNCTIONS=====================================
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------









//========LEFTOVERS===============






  //--set tip_div location
  /*  
	var body = document.body;
	var browserLeft;
	var browserRight;
	var browserTop;
	var browserBottom;


	if (window.innerWidth)
	{
		// DOM window attributes
		browserLeft = window.pageXOffset;
		browserRight = window.innerWidth + browserLeft;
		browserTop = window.pageYOffset;
		browserBottom = window.innerHeight + browserTop;

	}
	else if (body.clientWidth)
	{
		// IE attributes
		browserLeft = body.clientLeft;
		browserRight = body.clientWidth + browserLeft;
		browserTop = body.clientTop;
		browserBottom = body.clientHeight + browserTop;

	}
  */


function pause(millis) 
{
date = new Date();
var curDate = null;

do { var curDate = new Date(); } 
while(curDate-date < millis);
} 





