var tb_pathToImage = "/images/loading.gif";
var tb_url = "/ajaxfilial.asp";
var currentCity = getCookie("FilialCity");

$(document).ready(function(){
	tb_init('select#mySelect1');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
	//alert(currentCity);
	tb_show(currentCity);
});

//add thickbox to href & area elements that have a class of .thickbox
function tb_init(domChunk){
	$(domChunk).change(function(){
		var t = this.value || null;
		setCookie("FilialCity", t); 
		tb_show(t);
		this.blur();
		return false;
	}).change();
}


function tb_show(rid) {//function called when the user clicks on a thickbox link

	try {
		
		//$("div#TB_filinfo").css({display:"none"})
		$("div#TB_filinfo").remove();
		
		var url = tb_url + '?rid=' + rid + "&mode=" + mode;
		
		$("div#cityinfo").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#TB_load').show();//show loader
		
		$("div#cityinfo").append("<div id='TB_filinfo'></div>");

		$("div#TB_filinfo").load(url += "&random=" + (new Date().getTime()),function(){//to do a post change this load method
			$("#TB_load").remove();
			$("div#TB_filinfo").css({display:"block"});
		});

	} catch(e) {
		//nothing here
	}
}


function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function setCookie(name, value) {
	cswitch = getCookie("cswitch");
	if (!cswitch) {
		var today = new Date();d = new Date(today.getTime() + 7790000000); 
		var newCookie1 = name + "=" + escape(value) + ";expires=" + d.toGMTString() + ";path=/ ;";
		document.cookie = newCookie1;
	}
}
