var loading_img = "_media/loading.gif";
var loaded_img = "_media/loaded.gif";
var play_img = "_media/play.gif";
var stop_img = "_media/stop.gif";
var mod='home'; 
var keywords=[];
var timer_all = true;
var timer_sti = true;
var timer_world = true;
var max_counter = 20;
var tm_sti = true;
var tm_chartlist = true;
var dft_sec_price = 25;
var sec_price = dft_sec_price;
var dft_sec_chart = 60;
var sec_chart = dft_sec_chart;
var last_price = '20081118000900';
var tm_forum = [];
$(document).ready(function() {
	init_start();
	init_width();
	if(timer_all) {
		setTimeout('doUpdateTimer()',1000);
	}
	setTimeout('doUpdatePrice()',1000);
});
function init_start() {
	timer_all = checkCookie('timer_all',timer_all);
	timer_sti = checkCookie('timer_sti',timer_sti);
	timer_world = checkCookie('timer_world',timer_world);
	tm_sti = checkCookie('tm_sti',tm_sti);
	tm_chartlist = checkCookie('tm_chartlist',tm_chartlist);
	//alert(timer_all + " : " + timer_sti + " : " +  timer_world);
}
function checkCookie(cname,orgvalue) {
	var value = getCookie(cname);
	if(value == "true") {
		return true;
	} else if(value == "false") {
		return false;
	}
	return orgvalue;
}
function init_width() {
	var width = $(window).width();
	if(width<990) {
		width = 990;
	} else {
		width = width - 20 ;
	}
	width = 978;
	$("#div_top").width(width);
	$("#div_sti").width(width);
	$("#div_world").width(width);
}

function init_tablesort(item) {
	$(item)
		.tablesorter({widthFixed: true, widgets: ['zebra'], sortList: [[1,0]]})
		.tablesorterPager({container: $("#pager"),positionFixed:false});
}

function dosearch(keyword,url,target) {
	if(!keyword || !target || !url) {
		return false;
	}
	keyword = escape(keyword);
	showLoading(target);
	download(url,'keyword='+keyword,target,true);
	return false;
}
function doUpdateTimer() {
	if(!timer_all) {
		return false;
	}
	if(timer_sti) {
		if(tm_sti) {
			// manage price timer
			$('#sti_price').html(""+sec_price);
			if(sec_price<=0) {
				$("img#sti_loading").attr("src",loading_img);
				doUpdatePrice();
				sec_price = dft_sec_price;
			} else {
				sec_price--;
			}
		}
		// manage chart timer
		if(tm_chartlist) {
			if(sec_chart == dft_sec_chart) {
				//$("img#sti_chartlist_loading").attr("src",loaded_img);
			}
			$('#sti_chart').html(""+sec_chart);
			if(sec_chart<=0) {
				//$("img#sti_chartlist_loading").attr("src",loading_img);
				$("img.chart").each(
					function(){ 
						if(this.src.length>90) {
							this.src = this.src.substr(0,80); 
						}
						this.src = this.src+Math.round(Math.random()*10); 
					}
				);
				sec_chart = dft_sec_chart;
			} else {
				sec_chart--;
			}
		}
	}
	if(timer_world) {
	}
	setTimeout('doUpdateTimer()',1000);
}
function swapTimer(type) {
	switch(type) {
		case "all":
			if(timer_all) {
				timer_all = false;
				setCookie("timer_all",timer_all);
				$('img#'+type+'_global_streaming').attr("src",stop_img);
			} else {
				timer_all = true;
				setCookie("timer_all",timer_all);
				$('img#'+type+'_global_streaming').attr("src",play_img);
				setTimeout('doUpdateTimer()',1000);
			}
			break;
		case "sti":
			if(timer_sti) {
				timer_sti = false;
				setCookie("timer_sti",timer_sti);
				$('img#'+type+'_global_streaming').attr("src",stop_img);
			} else {
				timer_sti = true;
				setCookie("timer_sti",timer_sti);
				$('img#'+type+'_global_streaming').attr("src",play_img);
			}
			break;
		case "world":
			if(timer_world) {
				timer_world = false;
				setCookie("timer_world",timer_world);
				$('img#'+type+'_global_streaming').attr("src",stop_img);
			} else {
				timer_world = true;
				setCookie("timer_world",timer_world);
				$('img#'+type+'_global_streaming').attr("src",play_img);
			}
			break;
	}
}
function swapSTI() {
	// check img src
	if(tm_sti) {
		$('img#sti_streaming').attr("src",stop_img);
	} else {
		$('img#sti_streaming').attr("src",play_img);
	}
	tm_sti = !tm_sti;
	setCookie("tm_sti",tm_sti);
}
function swapChart() {
	// check img src
	if(tm_chartlist) {
		$('img#sti_chartlist_streaming').attr("src",stop_img);
	} else {
		$('img#sti_chartlist_streaming').attr("src",play_img);
	}
	tm_chartlist = !tm_chartlist;
	setCookie("tm_chartlist",tm_chartlist);
}
function swapStar(id) {
	if($('#sti_'+id+' > div.star').children().attr("src")!=loading_img) {
		$('#sti_'+id+' > div.star').children().attr("src",loading_img);
		dosubmit('ajax/'+mod+'/star/'+id);
	}
}
function doUpdatePrice() {
	dosubmit('ajax/'+mod+'/updateprice/?alp='+alp,'');
}
function replacePrice(cid,newclass,price,classlist) {
	$('#sti_'+cid).removeClass('up');
	$('#sti_'+cid).removeClass('down');
	$('#sti_'+cid).addClass(newclass);
	$('#sti_'+cid+' div.price').children().each(
		function (i,e) {
			switch(i) {
				case 0:
				case 1:
				case 6:
				case 7:
				case 8:
				case 9:
					if(price[i]=="0.000" || price[i]=="0" || price[i]=="0.0") {
						$(this).text('-');
					} else {
						$(this).text(price[i]);
					}
					break;
				case 2:
				case 3:
				case 4:
				case 5:
					if(price[i]=="0.000" || price[i]=="0" || price[i]=="0.0") {
						$(this).text('-');
					} else {
						$(this).text(price[i]);
					}
					$(this).removeClass('red');
					$(this).removeClass('green');
					$(this).addClass(classlist[i]);
					break;
			}
		}
	);
}

function show_search_result() {
	$('#table_stock').removeClass("hide");
	$('#pager').removeClass("hide");
}

