$(document).ready(function(){
	$("#sponsor-small, #sponsor-small-text").overlay({
		onBeforeLoad: function() {
			var $bannerContainer = this.getOverlay();
			
			if(!$bannerContainer.data("hasOpenX")) {
				var rand = Math.floor(Math.random()*99999999999);
				$bannerContainer
					.data("hasOpenX", true)
					.find(".contents")
					.html("<a href='http://d1.openx.org/ck.php?n=abdb97de&amp;cb="+rand+"' target='_blank'><img src='http://d1.openx.org/avw.php?zoneid=178285&amp;cb="+rand+"&amp;n=abdb97de' border='0' alt='' /></a>");
					$("img").load(function(){
						$bannerContainer.css("left", Math.max(($(window).width() - $bannerContainer.width()) / 2, 0) + "px");
					});
			}
		}
	}); // homepage gallery sponsor modal
	
	$("img.graph_tooltip").tooltip({
		onBeforeShow: function(e, position) {
			if(position.left < 0)
				this.getConf().offset = [0, -position.left]
			},
		onShow: function(e) {
				this.getConf().offset = [0, 0]
			}
	});
	
	$("#hdSymbolLookup select[name=destination]").change(update_header_symbol_lookup).change();
	$("#hdSymbolLookup input[type=submit]").click(function(){
		$("#hdSymbolLookup select[name=destination]").attr("disabled", "disabled");
		return true;
	});
});

function update_header_symbol_lookup() {
	var $form = $("#hdSymbolLookup");
	var action = $form.find("select[name=destination] option:selected").attr("rel"); 
	$form.find("input[name=a_name]").attr("value", action);
} 


function ac_split( val ) {
	return val.split( / \s*/ );
}
function ac_extractLast( term ) {
	return ac_split( term ).pop();
}

function initAutocomplete(){
	
	$(".symbol_ac").each(function() {
		$(this).autocomplete({
			minLength: 2,
			source: "/getSymbols.cfm",
			select: function( event, ui ) {
				if(!$(this).hasClass("multiSymbol_ac")) {
					$(this).val( ui.item.value_en );
				}
				return false;
			},
			focus: function(){ return false; }
		})
		.data( "autocomplete" )._renderItem = function( ul, item ) {
			
			var strVal = "(" + item.value_en + ") " + item.label_en; 
			if(item.value_gr != "" && item.label_gr != "") {
				strVal += "<br/>("+ item.value_gr + ") " + item.label_gr;
			}
			return $( "<li class="+item.market+"></li>" )
				.data( "item.autocomplete", item )
				.append( "<a>" + strVal +"</a>" )
				.appendTo( ul );
		};
		$(this).data("autocomplete")._move = function( direction, event ) {
			if ( !this.menu.element.is(":visible") ) {
				this.search( null, event );
				return;
			}
			if ( this.menu.first() && /^previous/.test(direction) ||
					this.menu.last() && /^next/.test(direction) ) {
				//this.element.val( this.term );
				//this.menu.deactivate();
				return;
			}
			this.menu[ direction ]( event );
		};
		
		if($(this).hasClass("multiSymbol_ac")){
			$(this).bind( "autocompleteselect", function(event, ui) {
				var terms = ac_split( this.value );
				terms.pop();
				terms.push( ui.item.value_en );
				terms.push( "" );
				this.value = terms.join(" ");
				return false;
			});
			$(this).autocomplete("option", "source", function( request, response ) {
				$.ajax({
					url: "/getSymbols.cfm",
					dataType: "json",
					data: {
						term: ac_extractLast( request.term )
					},
					success: function( data ) {
						response( eval(data) );
					}
				});
			});
		}
	});
	$.extend( $.ui.autocomplete, {
		filter: function(array, term) {
			var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
			return $.grep( array, function(value) {
				return matcher.test( value.forsearch );
			});
		}
	});
		
}

/* IE6 warning dialog */

window.arrIe6Selects = null;

function ie6WarnClose()
{
	$("#ie6modallayer").hide();
	$("#ie6warningpopup").hide();
	$("#ie6warningpopupIframe").hide();
	if (window.arrIe6Selects != null)
		window.arrIe6Selects.show()
	window.arrIe6Selects = null;
	
	var date = new Date();
	date.setMonth(date.getMonth()+1);

	if ($("#i6warn_remember:checked").length > 0)
	{
		document.cookie = "ie6warn_remember=1; expires=" + date.toUTCString();
	} else
	{
		document.cookie = "ie6warn_remember=0; expires=" + date.toUTCString();
	}
}

function ie6WarnShow()
{
	var bShowDialog = true;

	var allCookies = document.cookie.split("; ");

  for (var i=0; i < allCookies.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var arrParts = allCookies[i].split("=");
		if ("ie6warn_remember" == arrParts[0]) 
		{
			if (arrParts.length >1)
			{
				if (arrParts[1] == "1")
					bShowDialog = false;
			}
			break;
		}
	}

	if (bShowDialog)
	{
		$("#ie6modallayer").show();
		$("#ie6warningpopup").show();
		$("#ie6warningpopupIframe").show();
		window.arrIe6Selects = $("select:visible");
		window.arrIe6Selects.hide();
	}
}
