// JavaScript Document
var indexs = -1;
var default_box=1;
var feed_array = Array('Commodities','Currencies','Currencies&amp;Indices');
var timer;
var timer_flag=0;
var on_menu_flag=0;
var cur_open_flag=0;
var old_open_flag=0;
var IE7_flag=false;

$(document).ready(function() {
	//load the background image	
	/*$('#content').hide();*/
	//after 5 sec. hide content
	/*
	setTimeout(function(){
		$('#content').slideUp('slow',function(){
			$(this).hide();
		});	
		//$('#content').remove();
	},10000);
	*/
	
	$('div.nav_container ul li.menuitem').hover(function(){
		
		//$('div.drop_down_container').show();		
		//show_drop_down();
		setTimeout("show_drop_down();",100);
	},function(){
		setTimeout("close_drop_down();",100);
		//$('div.drop_down_container').hide();	
	});
	
	$('div.drop_down_container').mouseover(function(){
		on_menu_flag=1;	
	});
	
	$('div.nav_container').mouseout(function(){
		on_menu_flag=0;
		setTimeout("close_drop_down();",100);
		//alert(2);
	});
	
	$('ul.sub_menuitem li').click(function(){
		var x=$(this).find('a').attr('href');
		window.location = x;
		//alert(x);
	});
	
	/*
	$('li.menuitem').mouseover(function(){
		//if(cur_open_flag!=$(this).index())
		//{
		old_open_flag =	cur_open_flag; 
		if(timer_flag==0 && on_menu_flag==0 && cur_open_flag==0)//initial 
		{
			timer=setTimeout("show_drop_down("+cur_open_flag+");",350);	
			timer_flag=1;
		}else if(timer_flag==1)
		{
			clearTimeout(timer);
			timer_flag=0;
		}
		cur_open_flag=$(this).index();
		//alert("On_menu_flag:"+on_menu_flag+"---Cur_open_flag:"+cur_open_flag);
		//}
	});
	
	$('div.drop_down_container').mouseover(function(){
		on_menu_flag=1;
		
	});
	$('div.drop_down_container').mouseout(function(){
		on_menu_flag=0;
		
	});
	$('div.nav_container').mouseout(function(){
		if(timer_flag==1)
		{
			clearTimeout(timer);
			timer_flag=0;
		}else if(cur_open_flag==0 || on_menu_flag==0 )
		{
			//alert("On_menu_flag:"+on_menu_flag+"---Cur_open_flag:"+cur_open_flag); 
			timer=setTimeout("close_drop_down();",100);
			timer_flag=1;
		}
		
	})
	*/
	if ($.browser.msie  && parseInt($.browser.version) == 7) {
  		//alert('IE7'); 
		//IE7_flag=true;
	} 
	var menu=$('div.nav_container');
	$(window).scroll(function(){
		if(IE7_flag==false)
		{
			if($(this).scrollTop()>130)
			{
				$('div.nav_container').addClass('stayontop');
				//$('div.select_content').html($(this).scrollTop());
			}else if($(this).scrollTop()<130 && menu.hasClass('stayontop'))
			{
				$('div.nav_container').removeClass('stayontop');
			}
		}
	});
		
	$('a.rss-item').each(function(){
		
		var temp_news=$(this).text();
		
		var temp_length=temp_news.length;
		var final_news=temp_news.substring(0,20);
		$(this).text(final_news+'...');
	});
	
	/***Pirce Feed Arrow***/
	$('div.cur_feed_select #arrow_left').click(function(){
		if(default_box==1)
		{
			return false;
		}else
		{
			$('div.feed_container #feed_'+default_box.toString()).hide();
			default_box--;
			$('div.feed_container #feed_'+default_box.toString()).show();
			$("div.cur_feed_select .select_content").html(feed_array[default_box-1]);
		}
	});
	
	
	$('div.cur_feed_select #arrow_right').click(function(){
		if(default_box==3)
		{
			return false;
		}else
		{
			$('div.feed_container #feed_'+default_box.toString()).hide();
			default_box++;
			$('div.feed_container #feed_'+default_box.toString()).show();
			$("div.cur_feed_select .select_content").html(feed_array[default_box-1]);
		}
	});
	/***Pirce Feed Arrow***/
	$('div#item:first a').hover(function(){stopblink();},function(){startblink();});	
});

function call_econ(){
	$.ajax({
			method: "GET",
			url: "return_economic_calendar.php",
			//data: "ca_from_date="+vary,
			datatype: "html",
			//data: "page="+content_show,
			//beforeSend: function(){	$("div.ajax img").show();}, //show loading just when link is clicked
			error: function(){},
			complete: function(){ $("div.ajax img").hide();}, //stop showing loading when the process is complete
			success: function(res){ //so, if data is retrieved, store it in html
				$('div.calendar_body img').remove();
				$('div.calendar_body').html(res);
				//return false;
				//$(".content").text(html); //show the html inside .content div
				decode();
			}
	}); 
}


function show_drop_down()
{
	//var content=$("div.nav_bar ul.nav_menu li.menuitem:eq("+(x-1)+")").find('div.menuitem_content').html();
	$('div.drop_down_container').stop(true,true).show();	
	//$('div.drop_down_container').addClass('drop_down_show');
	//$('div.drop_down_content').html(content);
	timer_flag=0;
}
function close_drop_down()
{
	if(on_menu_flag==0)
	{
		timer_flag=0;
		on_menu_flag=0;
		cur_open_flag=0
		$('div.drop_down_container').stop(true,true).hide();
		//$('div.drop_down_container').removeClass('drop_down_show');
		//$('div.drop_down_content').html('');
	}
	
}

function query_cgse()
{
	
	var x=$('input#refnum').attr('value');
	if(x.length==10)
	{
		url="http://www.cgse.com.hk/cn/product_01.php?code="+x+"&is_check_code=Y";
		params  = 'width='+screen.width;
		 params += ', height='+screen.height;
		 params += ', top=0, left=0'
		 params += ', fullscreen=yes';
		
		 newwin=window.open(url,'cgse_query', params);
		 if (window.focus) {newwin.focus()}
		 $('input#refnum').attr('value','');
		 return false;
	}else
	{
		 alert('你所輸入的編號錯誤,請重新輸入一次');
		 $('input#refnum').attr('value','');
	}
	
}

var blinking_flag=false;
var blinking_timer;
function startblink()
{
	
	blinking_timer = setInterval(function(){
    	if(blinking_flag){
    	   $('div#item:first a').css('color','#ff0000');
    	    blinking_flag=false;
    	 }
    	else{
    		$('div#item:first a').css('color','#003300');
    		 blinking_flag = true;
    	}
    },800);
}

function stopblink()
{
	 if(blinking_timer) clearInterval(blinking_timer);
	 $('div#item:first a').css('color','#ff0000');
	 blinking_flag=false;
}
