var searchTabArray = new Array();
function Search(tab, keyword, category)
{
	this.tab = tab;
	this.tabId = this.tab.id;
	this.keyword = keyword;
	this.categoryTitle = category;
	searchTabArray[searchTabArray.length] = this;
	this.vTabArray = new Array();
	this.counter = 0;

	this.paint = function (){
		var divObj = dojo.byId(this.tabId + "_div");

		this.backDivObj = dojo.byId(this.tabId + "_ad_div");
		dojo.addClass(this.backDivObj, 'backDivClass');
		// adding table for search
		this.searchTable = dojo.doc.createElement('table');
		this.searchTable.style.width = "100%";

		if (dojo.isIE)
		{
			this.searchTable.cellSpacing = "5";
			this.searchHead = dojo.doc.createElement('thead');
			this.searchTable.appendChild(this.searchHead);
		}
		else
		{
			this.searchTable.setAttribute("cellspacing", "5px");
			this.searchHead = this.searchTable;
		}

		this.backDivObj.appendChild(this.searchTable);

		// back link.
		this.tr1 = dojo.doc.createElement('tr');
		this.tr1.appendChild(dojo.doc.createElement('td'));
		this.bkToResDivSpan = dojo.doc.createElement('td');
		if (dojo.isIE)
		{
			this.bkToResDivSpan.nowrap = "true";

		}
		else
		{
			this.bkToResDivSpan.setAttribute("nowrap", "true");
		}

		this.tr1.appendChild(this.bkToResDivSpan);
		if (dojo.isFF)
		{
			this.tr1.appendChild(dojo.doc.createElement('td'));
			this.tr1.appendChild(dojo.doc.createElement('td'));
			this.tr1.appendChild(dojo.doc.createElement('td'));
		}

		this.searchHead.appendChild(this.tr1);
		if (dojo.isIE)
		{
			this.bkToResDivSpan.id = "bkToResDiv_" + this.tab.searchId;
		}
		else
		{
			this.bkToResDivSpan.setAttribute("id", "bkToResDiv_" + this.tab.searchId);
		}

		dojo.addClass(this.bkToResDivSpan, 'bkToResClass');
		this.bkToResDivSpan.style.display = 'none';
		this.bkToResDivSpan.innerHTML = '<<< Back to Search Results';


		// textbox.
		this.tr2 = dojo.doc.createElement('tr');
		this.searchHead.appendChild(this.tr2);

		this.tdHullabu = dojo.doc.createElement('td');
		this.tdHullabu.innerHTML = '<img src="/static/img/h.gif" height="25px">';
		if (dojo.isIE)
		{
			this.tdHullabu.width = "2%";
			this.tdHullabu.align = "center";
		}
		else
		{
			this.tdHullabu.setAttribute("width", "2%");
			this.tdHullabu.setAttribute("align", "center");
		}

		this.tr2.appendChild(this.tdHullabu);

		this.td2 = dojo.doc.createElement('td');
		if (dojo.isIE)
		{
			this.td2.width = "40%";
		}
		else
		{
			this.td2.setAttribute("width", "40%");
		}

		this.tr2.appendChild(this.td2);

		this.textboxDiv = dojo.doc.createElement('div');
		this.td2.appendChild(this.textboxDiv);
		
		var textBoxProperties = new Object();
		textBoxProperties.id = 'dtabKeyword_' + this.tab.searchId;
		textBoxProperties.style = "width:100%;font-size:15pt;vertical-align:middle;";
		this.textbox = new dijit.form.TextBox(textBoxProperties, this.textboxDiv);
		this.textbox.setValue(this.keyword);

		// category dropdown.
		this.td3 = dojo.doc.createElement('td');
		if (dojo.isIE)
		{
			this.td3.align = "right";
			this.td3.width = "13%";
		}
		else
		{
			this.td3.setAttribute("width", "13%");
			this.td3.setAttribute("align", "right");
		}

		this.tr2.appendChild(this.td3);
		this.categoryboxDiv = dojo.doc.createElement('div');
		this.td3.appendChild(this.categoryboxDiv);
		this.categoryBox = new dijit.form.ComboBox({
											id:"searchCategory_" + this.tab.searchId,
											name:"searchCategory_" + this.tab.searchId,
											autoComplete:true,
											store: categoryStore,
											searchAttr:"title",
											style:"width:100%;font-size:13pt;vertical-align:middle;"
									}, this.categoryboxDiv);
		this.categoryBox.setValue(this.categoryTitle);

		// search button.
		this.td4 = dojo.doc.createElement('td');
		this.tr2.appendChild(this.td4);

		this.searchButtonDiv = dojo.doc.createElement('div');
		this.td4.appendChild(this.searchButtonDiv);
		
		var buttonStyle = "vertical-align:middle;font-size:8pt;";
		if (dojo.isIE)
		{
			buttonStyle = "vertical-align:middle;font-size:4pt;padding-left:10px;";
		}

		this.searchButton = new dijit.form.Button({label:"Search", style:buttonStyle}, this.searchButtonDiv);

		// adding disclaimer here.
		this.td5 = dojo.doc.createElement('td');
		if (dojo.isIE)
		{
			this.td5.align = "right";
		}
		else
		{
			this.td5.setAttribute("align", "right");
		}

		dojo.addClass(this.td5, 'disclaimerClass');
		this.td5.innerHTML = "*Content beneath is not Hullabu's content. It is being shown using iframe."
		this.tr2.appendChild(this.td5);


		this.contentPane1Id = this.tabId + "_content";
		this.contentPane = new dojox.layout.ContentPane({id: this.contentPane1Id, preventCache: true}, divObj);
		this.contentPane.setHref('/static/dojoUtils/tabContainer.html');
		dojo.connect(this.contentPane, 'onLoad', this, 'paintVerticalTabs');
		dojo.connect(this.searchButton, 'onClick', this, 'redoSearch');
		dojo.connect(this.textbox.domNode, 'onkeypress', this, 'keyPressed');
	}

	this.keyPressed = function (evt){
		var key = evt.keyCode;
		if(key == dojo.keys.ENTER) 
		{
			this.redoSearch();
		}
	}

	this.paintVerticalTabs = function (){
		this.contentPane.setHref("");
		this.updateDS(this.keyword);
		var paneChildNodes = this.contentPane.containerNode.childNodes; 
		for (var i = 0; i < paneChildNodes.length; i++)
		{
			if ( paneChildNodes[i].className.indexOf("dijitTabContainer") != -1)
			{
				this.tabContainer = paneChildNodes[i];
				this.tabContainerObj = dijit.byId(this.tabContainer.id);
				break;
			}
		}

		if (this.tabContainer)
		{
			var categoryObj = getCategoryObjByName(this.categoryTitle);
			var uiParams = new Object();
			uiParams.dtabKeyword = this.keyword;
			for (var i = 0; i < categoryObj.tabs.length; i++)
			{
				// now we create search url here.
				var url = categoryObj.tabs[i].searchUrl;
				var hParams = categoryObj.tabs[i].hullabuParams;
				var sParams = categoryObj.tabs[i].searchParams;

				var searchUrl = getSearchUrl(hParams, sParams, url, uiParams);
				if (searchUrl == '')
				{
					continue;
				}

				var vTabObj = new Tab(this.tabId + "_" + categoryObj.tabs[i].id + "_" + this.counter, categoryObj.tabs[i].title, categoryObj.tabs[i].url, false, false, this.tabContainerObj, true);
				vTabObj.searchUrl = searchUrl;
				vTabObj.parentSearchObj = this;
				vTabObj.paint();
				if (i == 0)
				{
					vTabObj.display();
				}

				this.vTabArray[this.vTabArray.length] = vTabObj;
			}
		}
	}

	this.updateDS = function (keyword){
		return;
		var a = "add";
		var url = "/xhr?s=105&a=" + a + "&keyword=" + keyword;
		dojo.xhrGet({
			url: url,
			load: function(response, ioArgs) {
				return response
			},
			error: function(response, ioArgs) {
				// return silently
				return response;
			}
		});
	}

	this.redoSearch = function (){
		this.tab.destroy();
		this.keyword = this.textbox.getValue();
		this.categoryTitle = this.categoryBox.getValue();
		if (!this.categoryTitle || this.categoryTitle == '')
		{
			this.categoryTitle = "Search Engine";
		}

		dSearchNow(this.keyword, this.categoryTitle);
	}
}

function SearchBox(tabObj)
{
	this.tab = tabObj;
	this.tabId = this.tab.id;

	this.paint = function (){
		var divObj = dojo.byId(this.tabId + "_div");

		this.backDivObj = dojo.byId(this.tabId + "_ad_div");
		dojo.addClass(this.backDivObj, 'backDivClass');
		// adding table for search
		this.searchTable = dojo.doc.createElement('table');
		this.searchTable.style.width = "100%";

		if (dojo.isIE)
		{
			this.searchTable.cellSpacing = "5";
			this.searchHead = dojo.doc.createElement('thead');
			this.searchTable.appendChild(this.searchHead);
		}
		else
		{
			this.searchTable.setAttribute("cellspacing", "5px");
			this.searchHead = this.searchTable;
		}

		this.backDivObj.appendChild(this.searchTable);

		// textbox.
		this.tr2 = dojo.doc.createElement('tr');
		this.searchHead.appendChild(this.tr2);
		this.tdHullabu = dojo.doc.createElement('td');
		this.tdHullabu.innerHTML = '<img src="/static/img/h.gif" height="25px">';
		if (dojo.isIE)
		{
			this.tdHullabu.width = "2%";
			this.tdHullabu.align = "center";
		}
		else
		{
			this.tdHullabu.setAttribute("width", "2%");
			this.tdHullabu.setAttribute("align", "center");
		}

		this.tr2.appendChild(this.tdHullabu);

		this.td2 = dojo.doc.createElement('td');
		if (dojo.isIE)
		{
			this.td2.width = "40%";
		}
		else
		{
			this.td2.setAttribute("width", "40%");
		}

		this.tr2.appendChild(this.td2);

		this.textboxDiv = dojo.doc.createElement('div');
		this.td2.appendChild(this.textboxDiv);
		
		var textBoxProperties = new Object();
		textBoxProperties.id = 'dtabKeyword_' + this.tab.searchId;
		textBoxProperties.style = "width:100%;font-size:15pt;vertical-align:middle;";
		this.textbox = new dijit.form.TextBox(textBoxProperties, this.textboxDiv);

		// category dropdown.
		this.td3 = dojo.doc.createElement('td');
		if (dojo.isIE)
		{
			this.td3.align = "right";
			this.td3.width = "13%";
		}
		else
		{
			this.td3.setAttribute("width", "13%");
			this.td3.setAttribute("align", "right");
		}

		this.tr2.appendChild(this.td3);
		this.categoryboxDiv = dojo.doc.createElement('div');
		this.td3.appendChild(this.categoryboxDiv);
		this.categoryBox = new dijit.form.ComboBox({
											id:"searchCategory_" + this.tab.searchId,
											name:"searchCategory_" + this.tab.searchId,
											autoComplete:true,
											store: categoryStore,
											searchAttr:"title",
											style:"width:100%;font-size:13pt;vertical-align:middle;"
									}, this.categoryboxDiv);
		this.categoryBox.setValue("Search Engine");

		// search button.
		this.td4 = dojo.doc.createElement('td');
		this.tr2.appendChild(this.td4);

		this.searchButtonDiv = dojo.doc.createElement('div');
		this.td4.appendChild(this.searchButtonDiv);
		
		var buttonStyle = "vertical-align:middle;font-size:8pt;";
		if (dojo.isIE)
		{
			buttonStyle = "vertical-align:middle;font-size:4pt;padding-left:10px;";
		}

		this.searchButton = new dijit.form.Button({label:"Search", style:buttonStyle}, this.searchButtonDiv);
		
		// adding disclaimer here.
		this.td5 = dojo.doc.createElement('td');
		if (dojo.isIE)
		{
			this.td5.align = "right";
		}
		else
		{
			this.td5.setAttribute("align", "right");
		}

		dojo.addClass(this.td5, 'disclaimerClass');
		this.td5.innerHTML = "*Content beneath is not Hullabu's content. It is being shown using iframe."
		this.tr2.appendChild(this.td5);

		dojo.connect(this.searchButton, 'onClick', this, 'search');
		dojo.connect(this.textbox.domNode, 'onkeypress', this, 'keyPressed');
	}

	this.keyPressed = function (evt){
		var key = evt.keyCode;
		if(key == dojo.keys.ENTER) 
		{
			this.search();
		}
	}

	this.search = function (){
		this.keyword = this.textbox.getValue();
		this.categoryTitle = this.categoryBox.getValue();
		if (!this.categoryTitle || this.categoryTitle == '')
		{
			this.categoryTitle = "Search Engine";
		}

		dSearchNow(this.keyword, this.categoryTitle);
	}
}

function getSearchUrl(hParams, sParams, url, uiParams)
{
	var hParamList = '';
	var sParamList = '';
	if (hParams && hParams != '')
	{
		hParamList = hParams.split(',');
	}

	if (sParams && sParams != '')
	{
		sParamList = sParams.split(',');
	}

	if (hParamList == '' || sParamList == '')
	{
		return url;
	}

	var part2 = '';
	var doNotAddQuestion = false;
	for (var j = 0; j < hParamList.length; j++)
	{
		var param = hParamList[j];
		if (param && param != '')
		{
			var val = uiParams[param];
			var searchKey = sParamList[j];
			if (searchKey == 'hullBlank')
			{
				part2 += escape(val);
				doNotAddQuestion = true;
			}
			else if (searchKey != '')
			{
				part2 += '&' + searchKey + '=' + escape(val);
			}
		}
	}

	if (url.indexOf('?') == -1 && !doNotAddQuestion)
	{
		url += '?' + part2;
	}
	else
	{
		url += part2;
	}
	
	return url;
}

function initializeSearchCombo()
{
	dijit.byId('searchCategory').setDisplayedValue('Search Engine');
	dojo.addClass(dijit.byId('searchCategory'), 'selectMedium');
}

function dSearchNow(searchKeyword, searchCategory)
{
	var tabObj = addFrontTab("", "Search Results", true);
	if (!searchKeyword || searchKeyword == '')
	{
		searchKeyword = dojo.byId('dtabKeyword').value;
	}

	if (!searchCategory || searchCategory == '')
	{
		if (dojo.byId('searchCategory'))
		{
			searchCategory = dojo.byId('searchCategory').value;
		}
		else
		{
			searchCategory = "Search Engine";
		}
	}

	var searchTab = new Search(tabObj, searchKeyword, searchCategory);
	searchTab.paint();
	dijit.byId('dtabKeyword').setValue('');
	return false;
}

function paintVerticalTabs(tabId, key, category)
{
	var divObj = dojo.byId(tabId + "_div");
	var contentPane1Id = tabId + "_content";
	var contentPane1 = new dojox.layout.ContentPane({id: contentPane1Id}, divObj);
	contentPane1.setHref('/static/dojoUtils/tabContainer.html')
}

var isTravelCurrentForm = false;
function showTravelForm(arg)
{
	if (arg == "travel")
	{
		dojo.byId("searchFormDiv").style.display = "none";
		dojo.byId("travelFormDiv").style.display = "block";
		isTravelCurrentForm = true;
		dijit.byId('searchCategory2').setValue(arg);
	}
}

function showOtherForm(arg)
{
	if (arg != "travel")
	{
		dojo.byId("searchFormDiv").style.display = "block";
		dojo.byId("travelFormDiv").style.display = "none";
		isTravelCurrentForm = false;
		dijit.byId('searchCategory').setValue(arg);
	}
}
