$(document).ready(function(){
	initfocus();
	initpopup();
	inithover();
	initfade();
	initSlider();
	initAutocomplete(); 
	initGraph();
	initTabs();
	initList();
	initImgTitle();
	inittooltip();
//	tablesorted();
//	initselect();
//	initscroll();
	inittable();
	$("div.text-content p:first").addClass('first');
});
function initfocus(){	
	$("#hdSymbolLookup input.symbol_ac, .search input").each(function(){
   		var _val = $(this).val();
   		$(this).focus(function(){
     		$(this).addClass('focus');
			if($(this).val()==_val){ $(this).val(''); }
   		});
   		$(this).blur(function(){
    		$(this).removeClass('focus');
			if($(this).val()==''){ $(this).val(_val); }
   		});
  	});
}

function initpopup(){
	$("ul.menu li.ad").hover(
		function(){	
			var rand = Math.floor(Math.random()*99999999999);
			var $bannerContainer = $(this).find("div.drop-popup");

			if(!$bannerContainer.data("hasOpenX")) {
				$bannerContainer
					.data("hasOpenX", true)
					.find(".image-popup")
					.html("<a href='http://d1.openx.org/ck.php?n=a2a5da54&amp;cb="+rand+"' target='_blank'><img src='http://d1.openx.org/avw.php?zoneid=177114&amp;cb="+rand+"&amp;n=a2a5da54' border='0' alt='' /></a>")
			}
			$bannerContainer.animate({height: "320px"}, { duration: 200, queue: false });
		},
		function(){	$(this).find("div.drop-popup").animate({height: "0"}, { duration: 200, queue: false });}
	); 
	
	$("div.banner-block").mouseover(
		function(){ 
			var $bannerContainer = $(this).find("div.drop-banner");
			/*var rand = Math.floor(Math.random()*99999999999);
			
			if(!$bannerContainer.data("hasOpenX")) {
				$bannerContainer
					.html("<a href='http://d1.openx.org/ck.php?n=a8061dd1&amp;cb="+rand+"' target='_blank'><img src='http://d1.openx.org/avw.php?zoneid=177121&amp;cb="+rand+"&amp;n=a8061dd1' border='0' alt='' /></a>")
					.data("hasOpenX", true);
			}*/
			var objSelf = this;
			$bannerContainer.show().animate(
				{height: "200px"}, 
				{ duration: 200, queue: false }
			);			
		}/*,
		function(){
			//Hover out
			$(this).find("div.drop-banner").animate(
				{height: "0"}, 
				{duration: 200, queue: false, complete: function(){
					$(this).hide();
				} });			
		}*/
	);		
	$("div.banner-block div.drop-banner").mouseout(
		function(){
			//Hover out
			$(this).animate(
				{height: "0"}, 
				{duration: 200, queue: false, complete: function(){
					$(this).hide();
				} });			
		}
	);
}
function inithover(){
	$('#page .nav .bg-nav ul.menu li,#page .nav .bg-nav ul.menu-right li').hover(
		function(){ $(this).addClass('active');$(this).find("div.drop").animate({height: "350px"}, { duration: 200, queue: false }); },
		function(){ $(this).find("div.drop").animate({height: "0"}, { duration: 200, queue: false });$(this).removeClass('active');}	
	);
	$('#page .nav .bg-nav ul.menu li .drop').each(function(){
		if ($(this).find('li').size() > 9){
			$(this).find('ul').css('float','left');			
		}
		if ($(this).find('ul').size() > 1){
			$(this).find('ul,ul li').css('width',170);
			$(this).css('width',750);
			$(this).find('.drop-middle').css('width',750);
			$(this).find('.drop-middle-left').css('width',728);
		}
		else {
			$(this).css('width',269);
		}
	});
	
	$('div.ase').hover(
		function(){
		var src="img/images/statistic-1.png";	
		 $(this).parent().find("div.img-statistic img").attr("src", src); },
		function(){
		var src="img/images/statistic.png";
		 $(this).parent().find("div.img-statistic img").attr("src", src);}	
	);
}
function initfade(){
	/*$('ul.ticker li').each(function(){
		//$('div:last-child').addClass('last'); 
	});*/
	
	$('.ticker').cycle({
        fx:     'fade',
        speed:  '600',
        timeout: 5000,
        cleartype: 1
    });
}
function initSlider(){
	$('div.gallery').scrollable({
		loop: true,
		size: 4,
		interval: 4000,
		items: '.gallery-slide',
		next: '#next', 
   		prev: '#back' 
	});
}

function initAutocomplite(){
	var data = "Core Selectors Attributes Traversing Manipulation CSS complite Events Effects Ajax Utilities".split(" ");
	$(".symbol .form-text").autocomplete(data);
}

function initGraph(){
	$("div.market-data ul.tab-content li span").css('opacity','0').hide();
	$("div.market-data ul.tab-content li:first-child span").css('opacity','1').show();
	$("div.market-data ul.tab-content li").hover(
		function(){
			/*if ($(this).hasClass('up')){
				$("div.market-data ul.tab-content li.down span").stop().animate({opacity:0});
				$(this).find('span').stop().animate({opacity:1});
			}
			else{*/
				$activeSpan = $(this).find('span').stop().show().animate({opacity:1}, "slow");
				$("div.market-data ul.tab-content li span").not($activeSpan).stop().animate({opacity:0}, "slow", function(){ $(this).hide(); });
			/*}*/
		}
	);
}

function initTabs(){	
	if ( $('div.tabs').length == 0 ) return false;
	
	$('div.tabs > ul.fin-news > li > a').click(function(){
		var href = $(this).attr('href');
		href = href.replace(/[^#]*#/,'');
		$('div.tabs > ul.fin-news > li').removeClass('active');
		$('div.fin-news > ul.menu-box').removeClass('active-tab');
		$('#'+href).addClass('active-tab');
		$(this).parent().addClass('active');
		return false;
	});

	$('div.tabs > ul.graph-tabs > li > a').click(function(){
		$("div.market-data ul.tab-content li.down span").css('opacity','1');
		var href = $(this).attr('href');
		href = href.replace(/[^#]*#/,'');
		$('div.tabs > ul.graph-tabs > li').removeClass('active');
		$('div.market-data ul.tab-content').removeClass('active-tab');
		$('#'+href).addClass('active-tab');
		$(this).parent().addClass('active');
		
		/* ANNA */
		var linkedTo = $(this).parent().attr("linkedTo");
		if(linkedTo != undefined){
			$(linkedTo).show();
			$(".market-data li.right").not(linkedTo).hide();
		}
		/* ANNA END */
		initGraph();
		return false;
	});

}

function initList(){
	$('div.datagrid table tr.trref').each(function(i, $el){
		if(($('div.datagrid table tr').index($(this)) % 2)==0){
			$(this).after("<tr></tr>");
		}
	});
	
	$('#main ul.gainers li:even,div.datagrid table tr:not(.trnotes):even').addClass('even');
	$('div.banks table tr:not(.trnotes):even').removeClass('even');
	$('div.banks table tr:not(.trnotes):odd').addClass('odd');
	$('div.datagrid table tr').find('td:first').addClass('first');

	$('div.datagrid table tr.trnotes, div.banks table tr.trnotes').each(function(){
		if($(this).prev().hasClass('even')) {
			$(this).addClass('even');
		} else if($(this).prev().hasClass('odd')) {
			$(this).addClass('odd');
		}
	});
}

function initImgTitle(){
	title = '';
	title = $('#main div.img-with-title img').attr('title');
	if (title != ''){
		$('#main div.img-with-title').append('<p>' + title +'</p>');
	}
}

function inittooltip(){
	$('div.table-block table td a').tooltip({ 
	    track: true, 
	    delay: 0, 
	    showURL: false 
	});
	$('div.table-economic table td a').tooltip({ 
	    track: true, 
	    delay: 0, 
	    showURL: false 
	});
	/*$('ul.fin-news a').tooltip({ 
	    track: true, 
	    delay: 0, 
	    showURL: false
	});*/
	
}

window.currentTableSort = null;

function tablesorted(){
	   $.tablesorter.defaults.widgets = ['zebra']; 
    // extend the default setting to always sort on the first column 
    $.tablesorter.defaults.sortList = [[0,0]]; 
    // call the tablesorter plugin 
    $("#myTable").tablesorter(
		{
			
			// pass the headers argument and assing a object 
        headers: { 
			1: {sorter: false},
        	2: {sorter: false}, 
			3: {sorter: false}, 
			4: {sorter: false}, 
			5: {sorter: false} 
        }}).bind("sortEnd", function(sorter) 
				{
			   	window.currentTableSort = sorter.target.config.sortList;
				});

		$(".table-economic table").tablesorter(
	{ 
        // pass the headers argument and assing a object 
		headers: { 
			1: {sorter: false}, 
            3: {sorter: false}, 
			4: {sorter: false}, 
			5: {sorter: false},
			6: {sorter: false}
		 }}); 
}



function inittable(){
	
	var NoOfRows = 10;
	
$('.table-economic a.more-events').click(function(){
		
		NoOfRows += 10;
		
		if(NoOfRows == 30) {
			$("a.more-events").hide();
		}
		$("a.view-less").show();
		
		changeEconomicCalendar(NoOfRows);
		
		return false;
	});

	$('.table-economic a.view-less').click(function(){
		
		NoOfRows -= 10;
		
		if(NoOfRows == 10) {
			$("a.view-less").hide();
		}
		$("a.more-events").show();
		
		changeEconomicCalendar(NoOfRows);
		
		return false;
	});
}


function changeEconomicCalendar(NoOfRows) {
	if(NoOfRows == 20) {
		$(".table-economic span.border").show();
	} else {
		$(".table-economic span.border").hide();
	}
	var url =('/include/xx_forex_events_table.cfm');
	$.ajax({
		url : url,
		data: { noofrows: NoOfRows },
		success : function(msg){
			$('.table-economic tbody').html(msg);
			inittooltip();
		},
		complete:function(){
			$(".table-economic table").trigger('update');
		}
	});
}
