// JavaScript Document
var searchShowing = false;
var isLoading = false;

function updateDiv(div,url){
	var param = '';
	var updater = new Ajax.Updater(div,url,{method: 'get',parameter: param});
	return false;
}
//var animate;
function ajaxLoad(){
	isLoading = true;
	//animate = setInterval("animateLoad()",500);
	new Effect.Appear('load_on',{duration: 0.3, queue:'end'});
}
function ajaxUnload(){
	isLoading=false;
	//clearInterval(animate);
	//if(isOn){
		Effect.Fade('load_on',{duration: 0.3, queue:'end'});
		//isOn = false;
	//}
}
/*var isOn = false
function animateLoad(){
	isOn = isOn?false:true;
	if(isOn)new Effect.Appear('load_on',{duration: 0.7, queue:'end'});
	else new Effect.Fade('load_on',{duration: 0.7, queue:'end'});
}*/
Ajax.Responders.register({
    onCreate : ajaxLoad,
    onComplete : ajaxUnload
  });

function showSearch(){
	if(searchShowing)return;
	new Effect.SlideDown('search-result-container',{duration:0.4});
	searchShowing = true;	
	return false;
}	
function hideSearch(){
	if(!searchShowing)return;
	new Effect.SlideUp('search-result-container',{duration:0.4});
	searchShowing = false;
	return false;
}
function keyUpFilter(e){
	var unicode=e.keyCode? e.keyCode : e.charCode;
	if(!(unicode>=48&&unicode<=57)&&!(unicode>=65&&unicode<=90)&&unicode!=8&&unicode!=46&&unicode!=190&&unicode!=191&&unicode!=220&&unicode!=188&&unicode!=32)return false;
	hideSearch();
}
function startSearchName(current){
	if(searchShowing)hideSearch();
	current = (current==undefined)?1:current;
	var param = '';
	searchName = document.getElementById('searchName').value;
	while(searchName.indexOf(" ")>=0)searchName = searchName.replace(" ","_");
	var url = '../phones/searchName?searchName='+searchName+"&current="+current;
	var updater = new Ajax.Updater('search-result',url,{method: 'get',parameter: param, onComplete:function(){ showSearch();}});
	return false;	
}

//Anchor Manager
var anchorRegistry = {
	"phone":false,
	"main":false
}
function initAnchorCheck(registry){
	anchorRegistry[registry] = setInterval(registry+"_checkAnchor()",250);
}
var path;
var mainHash;
var subHash;
var param;
var isFirstCheck = true;
function parseURL(){
	var tmp = document.location.href.split('/');
	tmp2 = "";
	for(x=3;x<tmp.length;x++)tmp2 += tmp[x];
	tmp = tmp2.split("#");
	path = tmp[0];
	hash = document.location.hash.split("#");
	mainHash = (hash[1]==undefined)?"":hash[1];
	subHash = (hash[2]==undefined)?"":hash[2];
	
}
var titleList = {
	"newcomer":"มือถือเปิดตัวใหม่ - ",
	"toprates":"มือถือยอดนิยม - ",
	"homes":"มือถือ มือถือรุ่นใหม่ ราคามือถือ สเป็คมือถือ รุ่นใหม่ มาแรง - ",
	"":document.title
}
function updateURL(m,s){
	if(m!="n"){
		mainHash = m;
	}
	if(s!="n")subHash = s;
	document.location.hash = (subHash==""||subHash==undefined)?"#"+mainHash:"#"+mainHash+"#"+subHash;
	return false;
}
//Main Screen Manager
var currentMainScreen = 1;
var currentMainHash;
var mainRegistry = {
	"searchOption":false,
	"compareOption":false,
	"contact":false
};
function loadScreen(url){
	if(isLoading)return false;
	changePageTitle(titleList[mainHash]);
	var div = "screen"+(currentMainScreen*-1);
	currentMainHash = url;
	if(url.indexOf("?")<0)url += "?";
	url = "../"+url+"&ajax=1";
	//alert("updating "+div+" with url "+url);
	var updater = new Ajax.Updater(div,url,{method: 'get',parameter:'', onComplete:function(){ switchScreen();}});
	return false;
}
function switchScreen(){
	Effect.Fade("screen"+currentMainScreen, {duration: 0.3, queue:'end'});
	setInterval("document.getElementById('screen"+currentMainScreen+"').innterHTML = '';",300);
	currentMainScreen *= -1;
	Effect.Appear("screen"+currentMainScreen, {duration: 0.3, queue:'end'});
	
	return false;
}
function main_checkAnchor(){
	parseURL();
	if(isFirstCheck)currentMainHash = path;
	isFirstCheck = false;
	if(mainHash!=""){
		if(currentMainHash!=mainHash)loadScreen(mainHash);
	}
	else if(path!=""){
		if(currentMainHash!=path){
			//alert(currentMainHash+" VS "+path);
			loadScreen(path);
		}
	}
}

//Seaching Phones
searchOptionShowing = false;
function showSearchOption(){
	if(!mainRegistry['search_option']){
		mainRegistry['search_option']=true;
		var updater = new Ajax.Updater('searchOption',"../phones/getSearchOption",{method: 'get',parameter: '', onComplete:function(){ showSearchOption()}});
		return false;
	}
	Effect.Appear('searchOption', {duration:0.6});
	searchOptionShowing = true;
	return false;
}
function hideSearchOption(){
	Effect.Fade('searchOption', {duration:0.6});
	searchOptionShowing = false;
	return false;
}
var searchOptionRegistry = {
	"design":0,
	"screensize":0,
	"camera":0,
	"memory":0,
	"battery":0,
	"connects":"",
	"others":"",
	"os":0
}
function registerSearchOption(option,value, mode){
	switch(mode){
		case 0:
			searchOptionRegistry[option]+=value;
			break;
		case 1:
			if(searchOptionRegistry[option]!=0)Effect.Fade(option+searchOptionRegistry[option],{duration:0.1});
			searchOptionRegistry[option]=value;	
			break;
		case 2:
			searchOptionRegistry[option]+="_"+value;
			break;
			
	} 	
	Effect.Appear(option+value,{duration:0.1});
	
	href = "#screen#"+buildURL();
	document.getElementById('basicSearchButton').href = href;
	return false;
}
function unregisterSearchOption(option, value, mode){
	switch(mode){
		case 0:
			searchOptionRegistry[option]-=value;	
			break;
		case 1:
			searchOptionRegistry[option]=0;	
			break;
		case 2:
			tmp = searchOptionRegistry[option].split("_");
			for(x=0;x<tmp.length;x++){
				if(tmp[x]==value){
					tmp.splice(x,1);
					break;
				}
			}
			searchOptionRegistry[option] = tmp.join("_");
			break;
	}
	Effect.Fade(option+value,{duration:0.1});
	return false;
}
function startBasicSearch(){
	url = buildURL();
	updateURL(url,'n');
	hideSearchOption();
	return false;
}
function buildURL(){
	url = "phones/searchBasic/?";
	url += buildParam("design");
	url += buildParam("screensize");
	url += buildParam("camera");
	url += buildParam("memory");
	url += buildParam("battery");
	url += buildParam("connects");
	url += buildParam("others");
	url += buildParam("os");
	return url;
}
function buildParam(str){
	if(searchOptionRegistry[str]) return str+"="+searchOptionRegistry[str]+"&";
	return "";
}

//Compare Phones
compareOptionShowing = false;
function showCompareOption(){
	if(!mainRegistry['compareOption']){
		mainRegistry['compareOption']=true;
		var updater = new Ajax.Updater('compareOption',"../phones/getCompareOption",{method: 'get',parameter: '', onComplete:function(){ showCompareOption()}});
		return false;
	}
	Effect.Appear('compareOption', {duration:0.6});
	compareOptionShowing = true;
	return false;
}
function hideCompareOption(){
	Effect.Fade('compareOption', {duration:0.6});
	compareOptionShowing = false;
	return false;
}
function updatePhoneList(e, num){
	var unicode=e.keyCode? e.keyCode : e.charCode;
	if(!(unicode>=96&&unicode<=111)&&!(unicode>=48&&unicode<=57)&&!(unicode>=65&&unicode<=90)&&unicode!=8&&unicode!=46&&unicode!=190&&unicode!=191&&unicode!=220&&unicode!=188&&unicode!=undefined)return false;
	div = "compareList"+num;
	brand = document.getElementById("brand"+num).options[document.getElementById('brand'+num).options.selectedIndex].value;
	name = document.getElementById("compareQuery"+num).value; 
	while(name.indexOf(" ")>=0)name = name.replace(" ","%");
	url = "../phones/getComparePhone?num="+num+"&";
	if(brand!=0)url += "brand="+brand+"&";
	if(name.length>0)url+= "name="+name;
	var updater = new Ajax.Updater(div,url,{method: 'get',parameter: ''});
}
var compareRegistry = {
	0:false,
	1:false
	};
function updateCompareShowcase(num){
	div = "compareShowcase"+num;
	id = document.getElementById("compareResult"+num).options[document.getElementById('compareResult'+num).options.selectedIndex].value;
	url = "../phones/getCompareShowcase?num="+num+"&id="+id;
	compareRegistry[num]=true;
	if(compareRegistry[0]&&compareRegistry[1]){
		id1= (document.getElementById("compareResult1").options[document.getElementById('compareResult1').options.selectedIndex]==undefined)?0:document.getElementById("compareResult1").options[document.getElementById('compareResult1').options.selectedIndex].value;
		id2= (document.getElementById("compareResult2").options[document.getElementById('compareResult2').options.selectedIndex]==undefined)?0:document.getElementById("compareResult2").options[document.getElementById('compareResult2').options.selectedIndex].value;
		document.getElementById("compare_button").href = "../phones/compareBase?id1="+id1+"&id2="+id2;
	}
	var updater = new Ajax.Updater(div,url,{method: 'get',parameter: ''});
}
function startCompare(){
	if(document.getElementById("compareResult1").options[document.getElementById('compareResult1').options.selectedIndex].value>0&&document.getElementById("compareResult2").options[document.getElementById('compareResult2').options.selectedIndex].value>0){
		id1= document.getElementById("compareResult1").options[document.getElementById('compareResult1').options.selectedIndex].value;
		id2 = document.getElementById("compareResult2").options[document.getElementById('compareResult2').options.selectedIndex].value;
		url = "../phones/compareBase?id1="+id1+"&id2="+id2+"&ajax=1";
		updateURL(url,'n');
		hideCompareOption();
	}
	return false;
}
function changePageTitle(title){
	title = (title==undefined)?"":title;
	document.title = title+"PhoneAxis.com - ราคา ข้อมูล รูปภาพ คะแนน รีวิว มุมมอง 360 องศา มือถือ - price infomations score review 360 degrees view - PhoneAxis.com - มือถือ โทรศัพท์มือถืออัพเดทรุ่นใหม่ ข้อมูลมือถือ เปรียบเทียบมือถือ ราคามือถือ โนเกีย ซัมซุง โซนี่ Nokia Samsung - Mobile phone update price compare specs";
}

//Contact
contactShowing = false;
function showContact(){
	if(!mainRegistry['contact']){
		mainRegistry['contact']=true;
		var updater = new Ajax.Updater('contact',"../queries/contact",{method: 'get',parameter: '', onComplete:function(){ showContact()}});
		return false;
	}
	Effect.Appear('contact', {duration:0.6});
	contactShowing = true;
	return false;
}
function hideContact(){
	Effect.Fade('contact', {duration:0.6});
	contactShowing = false;
	return false;
}

function submitContact(){
	hideContact();
	name = document.getElementById('contact_name').value;
	email = document.getElementById('contact_email').value;
	topic = document.getElementById('contact_topic').value;
	content = document.getElementById('contact_content').value;
	url = "../queries/submitContact?name="+name+"&email="+email+"&subject="+topic+"&content="+content;
	while(url.indexOf("\n")>=0)url = url.replace("\n","<BR>");
	while(url.indexOf(" ")>=0)url = url.replace(" ","%20");
	var updater = new Ajax.Updater('ajaxLoad',url,{method: 'get',parameter: '', onComplete:function(){ alert('ทางเว็บไซท์ได้ทำการบันทึกข้อความของท่านไว้เรียบร้อยแล้ว ทาง PhoneAxis.com จะติดต่อกลับไปโดยเร็วที่สุด ขอบคุณครับ')}});
}