var ZeroClipboard = {dispatch:mys_flashEvents, moviePath:'ZeroClipboard.swf', total_buttons:1} 
var com_plexel_searchBox = {}


;(function() { 
var N = com_plexel_searchBox
//
// Public API
//
N.start = function () {
// Setup flash movie over the button.
var idNum, list
for(idNum=1; idNum<=ZeroClipboard.total_buttons; ++idNum) {
   mys_setMovie(idNum)
}
mys_but_copy_code_on() // Copy Code button 
// Quirks mode fix for IE 8+
if(document.documentMode==5 || document.compatMode=='BackCompat') { // Quirks mode, cross browser 
   if( /MSIE (.)/.test(navigator.userAgent) && Number(RegExp.$1)>=8 ) { // IE8+
      list = mys_getTagClassElements('div', 'mys_ib') // get inline blocks
      mys_setStyles(list, 'display', 'inline')
      // Need to set hover to an expression
   }
}
mys_buts.init()
mys_setEvents(mys_buts.list, 'click', mys_events) 
mys_engines.init() 
mys_engines.set()
mys_focus()
}
})()


mys_addEvent(top, 'load', com_plexel_searchBox.start) // After page loaded run start


var mys_o = {
create: function(o) {
// Same as returning {__proto__: o} but cross browser
// x = mys_o.create(Object.prototype) is the same as x = {}
// Linked
function F() {}
F.prototype = o
return new F()
},
swiss: function(o, parent) {
// Copy selected members of parent object to o, e.g. o = mys_o.swiss(o, a, 'listOb')
// Not linked, will not update if parent updates.
var i, name
for (i=2; i<arguments.length; ++i) {
   name = arguments[i]
   //o.constructor[name] = parent.constructor[name]
   o[name] = parent[name]
}
},
extend: function(o, parent) {
// Copy all members of parent object to o.
var i
for (i in parent) {
   o[i] = parent[i]
}
}
}


var aa = [1,2]

var a = {
p1:1,
listOb: aa
}

var b = {r:1}


mys_o.swiss(b, a, 'listOb')

aa[0] = 3

//top.listOb(b)


function mys_fade(elem) {
var level=1
function step() {
   var h = level.toString(16)
   elem.style.backgroundColor = '#FFFF' + h + h
   if(level < 15) {
      level +=1
      setTimeout(step, 100)
   }
}
setTimeout(step, 100)
}


function mys_but_copy_code_on() {
var style  = mys_el('mys_but_1').style
style.color = 'black'
return}


function mys_focus() {
if(!top.c1_G || document.title=='Master') { // Focus if Plexel not loaded or for Master page.
   //mys_getBox().select()
   mys_getBox().focus()
}
return}


function mys_flashEvents(idNum, eventName, args) {
var style
style = mys_el('mys_but_'+idNum).style
if(eventName=='complete') {  
   mys_click(idNum)
}
else if(eventName=='mouseDown') {  
   style.color = 'red'
}
else if(eventName=='mouseUp' || eventname=='mouseOut') {  
   style.color = 'black'
}
return}


function mys_click(idNum) {
// Only one button idNum=1. Get all the code text, set it into the movie, movie copies it to clipboard.
var txt, mov
txt = mys_getDivText('mys_copy')
mov = mys_el("ZeroClipboard_" + idNum)
mov.setText(txt)
setTimeout(function() {alert('The search box code should now be copied to your clipboard,\nand you can paste it into your webpage.\n\nFor the "CopyCode" option to work for you, please also\ncopy ZeroClipboard.swf and mys_search.js from this website to your website.')},500)
return}


function mys_addEvent(node, eventType, func) {
// E.g. mys_addEvent(top, 'load', mys_start)
// node['on' + type] = func  // CROCKFORD but not right since overwrites existing
if(window.addEventListener && !window.opera) { 
   // Firefox 1.5+, Gecko-1.0.1+, Mozilla, Konqueror, Safari, iCab
   node.addEventListener(eventType, func, false) // Use false for bubbling, compatible with IE
}
else if(window.attachEvent) {
   // Microsoft IE 5+, Opera 8.0+
   node.attachEvent('on' + eventType, func)
}
}


function mys_getDivText(id) {
var div, txt
div = document.getElementById(id)
// Remove any text from the box
div.getElementsByTagName('input')[0].value=''
txt = div.innerHTML
// Set \n linefeeds
txt = txt.replace(/\r\n/g,'\n').replace(/\r/g,'\n')
// Add the div tag itself
txt = '<div id="' + id + '">' + txt + '</div>\n'
// Set \r\n linefeeds for Windows
if(navigator.platform.indexOf("Win")!=-1) {
   txt = txt.replace(/\n/g,'\r\n')
}
// Remove all anchor style= put in by browsers
txt = txt.replace(/<a(.*?) style=".*?"/gi,'<a$1') 
// Reset spans to just contain id info. THIS IS NOW OUT OF DATE
// txt = txt.replace(/<span id="mys_(.*?)">/gi,'<span id="mys_$1">') // Firefox
// txt = txt.replace(/<span id=mys_(.*?)>/gi,'<span id=mys_$1>') // IE
return txt}


function mys_setMovie(idNum) {
// Create hidden ZeroClipboard.swf movie over DOM element to be clicked when copying
var elemId, elem, Pos, div, style, zIndex, body
elemId = 'mys_but_' + idNum
Pos = mys_pos(elemId)
elem = mys_el(elemId)
div = document.createElement('div')
style = div.style
if( !/MSIE /.test(navigator.userAgent)) {
   style.overflow = 'auto'
}
style.position = 'absolute'
style.left = '' + Pos.left + 'px'
style.top = '' + Pos.top + 'px'
style.width = '' + Pos.width + 'px'
style.height = '' + Pos.height + 'px'
zIndex = 99
if (elem.style.zIndex) {
   zIndex = parseInt(elem.style.zIndex) + 1
}
style.zIndex = zIndex
//top.listObj(style)
// style.backgroundColor = '#f00'; // debug
body = document.getElementsByTagName('body')[0]
body.appendChild(div)
// Put the movie html in the div
div.innerHTML = mys_getHtml(Pos.width, Pos.height, idNum) 
return}


function mys_pos(elemId) {
var elem, Pos
elem = mys_el(elemId)
Pos = {left:0, top:0, width:elem.width ? elem.width : elem.offsetWidth, height:elem.height ? elem.height : elem.offsetHeight}
// Calculate absolute position
while (elem) {
   Pos.left += elem.offsetLeft
   Pos.top += elem.offsetTop
   elem = elem.offsetParent
}
// Below added for FF bug 
Pos.top -= 4
Pos.height += 4
return Pos}


function mys_getHtml(width, height, idNum) {
var flashvars, movieId, html, protocol
flashvars = "id=" +idNum+ "&width=" +width+ "&height=" +height
movieId = "ZeroClipboard_" + idNum
if (navigator.userAgent.match(/MSIE/)) {
   protocol = location.href.match(/^https/i) ? 'https://' : 'http://'
   html = '<object id="'+movieId+'" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+protocol+'download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+width+'" height="'+height+'" align="middle"><param name="movie" value="'+ZeroClipboard.moviePath+'" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="'+flashvars+'"/><param name="wmode" value="transparent"/></object>'
}
else { 
   html = '<embed src="'+ZeroClipboard.moviePath+'" id="'+movieId+'" name="'+movieId+'" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+width+'" height="'+height+'" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+flashvars+'" wmode="transparent" />';
}
return html}


var mys_srch = {
//
click: function(engine) { 
var wiki, url, text, what, domain, query, dir, unadjustedText
mys_controls.value = mys_getBox().value // crap way to do it. Better to fix mys_buts using .on() etc.
unadjustedText = mys_controls.value 
mys_type.value = ''
mys_buts.mod()
text = mys_controls.value
what = mys_type.value.toLowerCase()
if(engine=="Google") {
   domain = 'google'
   query  = '?q='
   dir = what
   if(what == 'video') {
      domain = 'youtube'
      query  = '?search_query='
      what = 'www'
      dir  = 'results'
   }
   else if(what == 'shopping') {
      dir = 'products'
   }
   else if(!what) {
      what = 'www'
      dir = 'search'
   }
   url = 'http://' + what + '.' + domain + '.com'
   if(text) {
      url += '/' + dir + query + text
   }
}
else if(engine=='Yahoo') {
   if(what=='Images') {
      url = 'http://images.search.yahoo.com/search/images?p=' + text + '&fr=yfp-t-501&toggle=1&cop=mss&ei=UTF-8'
   }
   else if(what=='News') {
      if(!text) {
         url = 'http://news.yahoo.com/i/721'
      }
      else {
         url = 'http://news.search.yahoo.com/search/news?p=' + text + '&c=&fr=sfp&ei=UTF-8&x=wrt'
      }
   }
   else if(what=='Video') {
      url = 'http://video.search.yahoo.com/search/?p=' + text + '&fr=yfp-t-501&ei=utf-8&fr2=tab-web&n=21&js=1&tnr=20'
   }
   else { // normal search  
      url = 'http://search.yahoo.com/search?p=' + text + '&fr=yfp-t-501&toggle=1&cop=mss&ei=UTF-8'
   }
}
else if(engine=='Bing') {
   url = 'http://www.bing.com/'
   if(what=='Images') {
      url += 'images/search?q=' + text 
   }
   else if(what=='News') {
      url += 'news/search?q=' + text 
   }
   else if(what=='Video') {
      url += 'video/search?q=' + text 
   }
   else { // normal search  
      url += 'search?q=' + text 
   }
}
else if(engine=='Ask') {
   url = 'http://www.ask.com/'
   if(what=='Images') {
      url += 'pictures?q=' + text + '&qsrc=2072&o=0&l=dir'
   }
   else if(what=='News') {
      url = 'http://news.ask.com/news?q=' + text + '&o=0&l=dir'
   }
   else if(what=='Video') {
      url += 'video?q=' + text + '&qsrc=179&o=0&l=dir'
   }
   else if(!text) { // Nothing entered
   }
   else { // normal search  
      url += 'web?q=' + text + '&search=search&qsrc=0&o=0&l=dir'
   }
}
else if(engine=='Wikipedia') {
   wiki = "http://en.wikipedia.org/wiki/Special:Search"
   url = wiki + '?search=' + text + '&go=Go'
   /*
   else if(what=="search") {
      url = wiki + '?search=' + text + '&fulltext=Search'
   }
   */
}
else if(engine=='Flickr') {
   url = 'http://flickr.com/search/?q=' + text
}
else if(engine=='CanUse') {
   url = 'http://flickr.com/search/?q=' + text + '&l=comm&ct=0'
}
else if(engine=='Mse360') {
   // normal images search
   url = 'http://mse360.com/new.php?search=' + text + '&set_country=ww'
}
else if(engine=='Twitter') {
   url = 'http://search.twitter.com/search?q='
   if(text.indexOf('inurl:') != -1) {
      url = 'http://twitter.com/' + unadjustedText.replace(/ /g,'')
   }
   else if(text.indexOf('intitle:') != -1) {
      url = url + '&tag=' + unadjustedText.replace(/ /g,'+')
   }
   else {
      url += text
   }
}
else if(engine=='TrueK') {
   url = 'http://www.trueknowledge.com/q/' + text
}
else if(engine=='Pow') {
   url = 'http://powerset.com/explore/go/' + text
}
else if(engine=='BBC') {
   if(!text) {
      url = 'http://news.bbc.co.uk'
   }
   else {
      url = 'http://search.bbc.co.uk/search?uri=%2F&scope=all&go=toolbar&q=' + text
   }
}
else if(engine=='Wolfram') {
   if(!text) {
      url = 'http://www.wolframalpha.com'
   }
   else {
      url = 'http://www.wolframalpha.com/input/?i=' + text
   }
}
else {
   alert('Unknown search engine "' + engine + '"')
   url = null
}
return url
}
}

var mys_buts = { 
// Button methods
//
list: '',
init: function() {
var buts, i, len, but
buts = mys_getIdTagElements('mys_b', 'button')
len = buts.length
for(i=0; i<len; i++) {
   but = buts[i]
   but.disabled = 'disabled' 
   but.setAttribute('type', 'button') // Disabled thing stops IE type submit.
   but.disabled = false 
   if(mys_cookie.get(but.innerHTML)) {
      this.on(but)
   }
}
this.list = buts
},
mod: function() {
var i, butsOn
butsOn = this.onList()
for(i=0; i<butsOn.length; i++) {
   eval(butsOn[i].aid.id + '.mod(butsOn[i])')  // EVAL SHOULD BE REMOVED
}
},
onList: function(elem) {
// list of elem descendant buttons turned on
var buts, i, butsOn=[]
if(elem) {
   buts = elem.getElementsByTagName('button')
}
else {
   buts = this.list
}
for(i=0; i<buts.length; i++) {
   if(this.isOn(buts[i])) { 
      buts[i].aid = mys_elem.aid(buts[i]) // Nearest ancestor having id
      butsOn[butsOn.length] = buts[i]
   }
}
return butsOn
},
isOn: function(elem) {
if(mys_getColor(elem)) {
   return true
}
return false
},
off: function(but) {
if( /MSIE (.)/.test(navigator.userAgent) && Number(RegExp.$1)<=7 ) {
   but.style.backgroundColor='tomato'; 
   but.style.filter="chroma(color='tomato')"; 
}
else {
   but.style.backgroundColor = 'transparent'
}
mys_cookie.set(but.innerHTML, '')
},
on: function(but) {
but.style.backgroundColor = '#eeee55'
mys_cookie.set(but.innerHTML, 'on', 10000)
},
allOff: function(elem) {
// Turn off all descendant buttons of elem
var i, buts, len
buts = elem.getElementsByTagName('button')
len = buts.length
for(i=0; i<len; i++) {
   but = buts[i]
   this.off(but) 
   mys_cookie.set(but.innerHTML, '')
}
}
}


function mys_buttonList() {
// button lists
//
}
mys_buttonList.prototype = {
value: '',
evt: function(elem, aid) { 
if(mys_buts.isOn(elem)) { // clicked a button that is on
   mys_buts.off(elem)
   this.value = ''
}
else { // clicked unset button with either another button set or none set
   mys_buts.allOff(aid)
   // Turn off all engine buttons
   // Turn on elem button
   mys_buts.on(elem)
   this.value = elem.innerHTML
   // turn on all engines that work with this button
}
mys_focus()
mys_engines.set()
}
}


var mys_controls = new mys_buttonList()

mys_controls.mod = function(button) { // called with Eval from mys_buts.mod()
var A, i, name, text
name = button.innerHTML.toLowerCase() // name e.g. InTitle
text = this.value
this.name = this.value
if(text.charAt(0)=='"') { // Assume text in quotes
   text = name + ':' + text
}
else { // Not in quotes
   A = text.split(' ')
   text = ""
   for(i=0; i<A.length; i++) {
      if(A[i]) {
         if(text) {
            text += ' ' // add space if there's something there
         }
         text += name.toLowerCase() + ':' + A[i]
      }
   }
}
text = encodeURIComponent(text)
text = text.replace(/%20/g,'+')
text = text.replace(/%3A/g,':')
this.value = text
}


var mys_type = {
// handles <span id="mys_type">
//
value: '',
evt: function(elem, aid) {
mys_controls.evt(elem, aid) // re-route it
},
mod: function(button) {
this.value = button.innerHTML 
}
}


var mys_modify = {
// handles <span id="mys_modify">
//
evt: function(elem) {
// Modify what's in the text box
var text, ix, Box, what, child
Box = mys_getBox()
text = Box.value
what = elem.innerHTML
// If quotes then add or remove quotes
if(what=='Clear') {
   Box.value = ""
}
else if(what=='Exact') {
   if(text.substring(0,1)=='"') { // Already has quotes
      text = text.substring(1)
      if(text.substring(text.length-1)=='"') {  // Remove trailing quote
         text = text.substring(0, text.length-1)
      }
   }
   else { // Add quotes
      text = '"' + text +'"'
   }
   Box.value = text
}
else if(what=='Site') {
   ix = text.toLowerCase().indexOf('.com') // Look for '.com'
   if(text.indexOf('site:')==0) { // Already is a site search
      text = text.substring(0,ix) + text.substring(ix+4) // Remove '.com'
      text = text.substring(5) // Remove existing 'site:'
   }
   else { // add 'site:' and '.com'
      if(ix == -1) { // no existing .com
         ix = text.indexOf(' ') // Look for any space
         if(ix==-1) {  // No space so tack on end
            text += '.com'
         }
         else { // insert '.com' before 1st space
            text = text.substring(0,ix) + '.com' + text.substring(ix)
         }
      }
      text = 'site:' + text
   }
   Box.value = text
}
else if(what=='Set') {
   //child = document.createElement('p')
   //child.data = "G"
   //elem.appendChild(child)
}
mys_focus()
mys_engines.set()
}
}


function mys_getColor(elem) {
// Returns res: in hex e.g. '#f1e800'. Color words left alone. Transparent/tomato returned as ''.
var res = ""
if(!elem) {
   elem = mys_el('mys_b')
}
res = elem.style.backgroundColor
res = mys_rgbToHexColor(res)
if(res=='transparent' || res=='tomato') {
   res = ''
}
return res}


function mys_getProperty(elem, property) {
// Get the CURRENT element property, e.g. 'color'
// elem can be object or string
var res = ''
if(typeof elem == 'string') {
   elem = mys_el(elem)
}
if(window.getComputedStyle) {
   // res = document.defaultView().getComputedStyle(elem, '').getPropertyValue(property) // Crockford uses this
   res = window.getComputedStyle(elem, null)[property]
}
else if(elem.currentStyle) {
  res = elem.currentStyle[property]
}
return res}


function mys_rgbToHexColor(v) { 
// E.g. 'rgb(5,2,11)' converts to "#05020b". All other formats returned unchanged.
var i
v = v.split('(')
if(!v[1]) return v[0]
v = v[1].replace(/ /g, '').replace(/\)/, '')
v = v.split(',')
for(i=0; i<v.length; i++) {
   v[i] = Number(v[i]).toString(16)
   if(v[i].length==1) v[i] = '0' + v[i]
}
return '#'+v.join('')}


function mys_mOver() {
this.style.borderColor='darkgray'; 
this.style.cursor='pointer'; 
}


function mys_mOut() {
this.style.borderColor='tomato'; 
this.style.filter="chroma(color='tomato')"; 
}


function mys_getBox() {
var box
box = mys_el('mys_input')
return box}


var mys_key = {
//
pressFlag: false,
press: function(e) {
return this.pressed(e, 'press')
},
up: function(e) {
return this.pressed(e, 'up')
},
pressed: function(e, callr) {
var code
if(callr == 'press') { 
   this.pressFlag = true
}
else { // Up key
   mys_engines.set()
   if(!window.event || this.pressFlag) { // FF bugfix or press() occured before
      this.pressFlag = false
      return false
   }
}
e = e || window.event
code = e.keyCode || e.which
if(code == 13) {
   window.open(mys_srch.click('Yahoo'), '_blank')
   return false
}
return true
}
}


var mys_cookie = {
get: function(name) {
var start, end
if (document.cookie.length > 0) {
   start=document.cookie.indexOf(name + "=")
   if (start!=-1) {
      start=start + name.length+1
      end=document.cookie.indexOf(";",start)
      if (end==-1) {
         end=document.cookie.length
      }
      return unescape(document.cookie.substring(start,end))
  }
}
return ""
},
set: function(name, value, expireDays) {
// To delete cookie set expireDays falsey
var exdate=new Date(), cookie=name
exdate.setDate(exdate.getDate()+expireDays);
cookie += "=" + escape(value) + ( !expireDays ? ";expires=Thu, 01-Jan-70 00:00:01 GMT" : ";expires="+exdate.toGMTString() )
document.cookie = cookie
},
enabled: function() {
this.set('test', 'enabled', 1)
if(this.get('test')) {
   this.set('test', '', 0) // delete test cookie
   return true
}
return false
}
}



function mys_el(id) {
return document.getElementById(id)
}


function mys_getTagClassElements(tag, className) {
// Return an array of all tag elements with className
var res=[], list, i
list = document.getElementsByTagName(tag)
for(i=0; i<list.length; i++) {
   if(list[i].className==className) {
      res[res.length] = list[i]
   }
}
return res}


function mys_getIdTagElements(id, tagName) {
// Return a list of all descendant tag elements of id
var list, elem
elem = mys_el(id)
list = elem.getElementsByTagName(tagName)
return list}


function mys_setStyles(list, property, value) {
// E.g. mys_setStyles(elementsList, 'display', 'inline')
var i
for(i=0; i<list.length; i++) {
   list[i].style[property]=value
}
}


function mys_setEvents(list, eventName, eventHandler) {
// E.g. mys_setEvents(mys_buts.list, 'click', mys_events) 
var i
for(i=0; i<list.length; i++) {
   mys_addEvent(list[i], eventName, eventHandler)
}
}


function mys_setProperties(id, tagName, property, value) {
var list, i
list = mys_getIdTagElements(id, tagName)
for(i=0; i<list.length; i++) {
   list[i][property]=value
}
}


var mys_engines = {
//
list: '',
init: function(){
this.list = mys_getIdTagElements('mys_e', 'a')
},
set: function(){
var i, list=this.list, parent, eng
for(i=0; i<list.length; i++) {
   parent = list[i].parentNode
   eng = list[i].cloneNode(true) 
   eng.setAttribute('href', mys_srch.click(list[i].innerHTML)) 
   mys_addEvent(eng, 'mouseover', mys_events) // Add event since FF events not cloned
   parent.replaceChild(eng, list[i]) // Forces any visited color to appear for IE
}
}
}


var mys_elem = {
// Additional element methods
//
aid: function(elem) {
// Returns first ancestor with id or null
var ancestor
ancestor = elem.parentNode
while(ancestor) {
   if(ancestor.id) {
      return ancestor
   }
   ancestor = ancestor.parentNode
}
return {} // no id for any ancestor
}
}


function mys_events(e) {
// All button click events come here
// Always remove all event handlers from nodes before removing the node. Applies to innerHTML,
// removeChild, replaceChild, IE6 CROCKFORD
var elem, aid
e = e || event
elem = e.target || e.srcElement
if (elem.nodeType == 3) { // defeat Safari bug
   elem = elem.parentNode
}
// Stop event propogating higher.
e.cancelBubble = true
if (e.stopPropagation) {
   e.stopPropagation()
}
e.returnValue = false
if(e.preventDefault) {
   e.preventDefault()
}
// Handled by first ancestor with an id
aid = mys_elem.aid(elem)
if(aid) {
   eval( aid.id + '.evt(elem, aid)' )   // EVAL SHOULD BE REMOVED
}
return false
}


var mys_e = {
//
evt: function(elem) {
// All mouse over anchors come here
var url
url=mys_srch.click(elem.innerHTML)
elem.href = url
elem.target = '_blank'
}
}


function mys_disableLinks() {
var list
//list = mys_getIdTagElements('mys_e', 'a')
list = mys_getIdTagElements('mys_modify', 'button')
for(i=0; i<list.length; i++) {
   list[i].disabled = 'disabled'
   //list[i].removeAttribute('href')
   //list[i].style.color = 'lightgray'
}
}


function purgeEventHandlers(elem) {
// Use this before deleting elements due to IE6 problems.
walkTheDOM(elem, function(e) {
   for(var n in e) {
      if(typeof e[n] === 'function') {
         e[n]=null
      }
   }
})
}


function walkTheDOM(node, func) {
func(node)
node = node.firstChild
while(node) {
   walkTheDOM(node, func)
   node = node.nextSibling
}
}


function getElementsByClassName(className, startNode) {
var results = []
startNode = startNode || document.body
walkTheDOM(startNode, function(node) {
   var a, c=node.className, i
   if(c) {
      a = c.split(' ')
      for(i=0; i<a.length; ++i) {
         if(a[i] === className) {
            results.push(node)
            break
         }
      }
   }
})
}


Object.prototype.later = function(msec, method) {
// Usage: myObject.later(1000, 'doit', true)
var that=this, args=Array.prototype.slice.apply(arguments, [2])
if(typeof method === 'string') {
   method = that[method]
}
setTimeout( function() {method.apply(that, args)}, msec)
return that
}


//mys_el('mys_test').setAttribute('disabled', 'disabled')

