jQuery(document).ready(function($){
	var $hcolorspan=$('#hcolorspan'), $scolordiv=$('#scolorspan')
	var $palettedivs=$('div.colorpalette').find('div')
	$palettedivs.bind('mouseover', function(){
		var selectedhex=$(this).attr('title')
		$hcolorspan.html(selectedhex)
		$hcolorspan.css({borderColor: selectedhex})
	})
	$palettedivs.bind('click', function(){
		var selectedhex=$(this).attr('title')
		$scolordiv.html(selectedhex)
		$scolordiv.css({borderColor: selectedhex})
	})
	$('#keywords').bind('click', function(){
		if (this.value=="Search Site")
			this.value=""
	})
})

var infotooltip={
	isdocked: false,

	showbox:function($, $tooltip, e){
			$tooltip.fadeIn(200)
			$tooltip.css({left:e.pageX+20+'px', top:e.pageY-30+'px'})
	},

	hidebox:function($, $tooltip){
		if (!this.isdocked){
			$tooltip.stop(false, true).hide()
			$tooltip.css({borderColor:'black'}).find('#tipstatus').css({background:'black'}).html('Press "s" or right click to sticky this box')
		}
	},

	docktooltip:function($, $tooltip, e){
			this.isdocked=true
			$tooltip.css({borderColor:'darkred'}).find('#tipstatus').css({background:'darkred'}).html('Click outside this box to hide it')
	},

	init:function(){
		jQuery(document).ready(function($){
			var $targets=$('.galleryscreenshots')
			var $tooltip=$('#colorbox')
			if ($targets.length==0)
				return
			var $palettedivs=$tooltip.find('div.colorpalette')
			infotooltip.hidebox($, $tooltip)
			$targets.bind('mouseenter', function(e){
				$palettedivs.hide().filter('#'+$(this).attr('data-index')).show()
				infotooltip.showbox($, $tooltip, e)
			})
			$targets.bind('mouseleave', function(e){
				infotooltip.hidebox($, $tooltip)
			})
			$targets.bind('mousemove', function(e){
				if (!infotooltip.isdocked){
					$tooltip.css({left:e.pageX+20, top:e.pageY-30})
				}
			})
			$tooltip.bind("mouseenter", function(){
				infotooltip.hidebox($, $tooltip)
			})
			$tooltip.bind("click", function(e){
				e.stopPropagation()
			})
			$(this).bind("click", function(e){
				if (e.button==0){
					infotooltip.isdocked=false
					infotooltip.hidebox($, $tooltip)
				}
			})
			$(this).bind("contextmenu", function(e){
				if ($(e.target).parents().andSelf().filter('.galleryscreenshots').length==1){ //if oncontextmenu over a target element
					infotooltip.docktooltip($, $tooltip, e)
					return false
				}
			})
			$(this).bind('keypress', function(e){
				var keyunicode=e.charCode || e.keyCode
				if (keyunicode==115){
					infotooltip.docktooltip($, $tooltip, e)
				}
			})
		}) //end dom ready
	}
}

infotooltip.init()

