setupModalLinks(document);
jQuery(document).ready(function() {
	scrollToSelected(document);
	jQuery("#adv_search_link").removeClass("ui-tabs-hide");
	jQuery("#basic_search_link").removeClass("ui-tabs-hide");

	if (location.search.indexOf('preview=1') >= 0) {
		jQuery("#search-box .search-tabs li a, #result-box .results-tabs li a").each(function() {
			var hashNdx = jQuery(this).attr("href").indexOf('#');
			if (hashNdx && jQuery(this).attr("href").substring(0, hashNdx) == location.search)
				jQuery(this).attr("href", jQuery(this).attr("href").substring(hashNdx));
		});
	}
	jQuery("#search-box").tabs();
	jQuery("#result-box").tabs({
		select: function(event, ui) {
			//updateHash(ui.panel.id);
			return true;
		}
	});
//	document.cookie = 'js_enabled=true';
});


function updateHash(newHash) {
	if (jQuery('#' + newHash).size() > 0) {
		var element = jQuery('#' + newHash).get(0);

		jQuery(element).removeAttr('id', '');
		window.location.hash = '#' + newHash;
		jQuery(element).attr('id', newHash);
	}
	else
		window.location.hash = '#' + newHash;
}

function setupModalLinks(selector) {
	jQuery(selector).ready(function() {
		makeLinkModal('.receive-updates a', 'm=true', '', 'height=275&width=600');
		makeLinkModal('.save-search a', 'm=true');
		makeLinkModal('.search-edit a.edit-dialog', 'm=true', '', 'height=478&width=600');
		makeLinkModal('.search-edit a:not(.edit-dialog)', 'm=true');
	});	
}

function setupModalPage() {
	if (self.parent && self.parent.tb_remove) {
		jQuery(".m-close-button").click(modalCloseClick);
		jQuery(".m-open-button").click(function() {
			modalCloseClick();
			self.parent.location = this.href;
			return false;
		});
	}
}

function limitSelectionCount(sel, max) {
	var num = 0;
	var cancel = false;
	for (var ndx = 0; ndx < sel.options.length; ndx++) {
		if (sel.options[ndx].selected && ++num > max) {
			if (!cancel && sel.selectedIndex >= 0 && sel.selectedIndex <= ndx) {
				sel.options[sel.selectedIndex].selected = false;
				num--;
			} 
			else
				sel.options[ndx].selected = false;
			
			cancel = true;
		}
	}

	if (cancel)
		alert("You can't select more than " + max + " items in this list.");
	
	return !cancel;
}

function scrollToSelected(parent) {
	jQuery('select', parent).each(function() {
		var options = this.options;

		if (options.length > 0) {
			for (var ndx = 0; ndx < options.length; ndx++) { 
				if (options[ndx].selected) {
					options[ndx].selected = false;
					options[ndx].selected = true;

					if (options[ndx].focus)
						options[ndx].focus();
				} 
			} 
		}
		
		
	});

	/*
	function() {
		var sel = this;
		sel.options[sel.selectedIndex].selected = true;
		jQuery(this).children('option:selected:first').each(function() {
			if (this.focus) this.focus();
		});
	});
	*/
}

function modalCloseClick() {
	if (self.parent && self.parent.tb_remove) {
		 self.parent.tb_remove(); 
	}	
}

function makeLinkModal(jQuerySel, appendQs, title, dim) {
	if (!title)
		title = '';

	appendQs = (appendQs ? (appendQs + "&") : '')
		+ 'TB_iframe=true&'
		+ (!dim ? 'height=225&width=600' : dim);
	
	jQuery(jQuerySel).each(function(ndx) {
		if (this.className && this.className.indexOf('thickbox') != -1)
			return;

		if (this.href)
			this.href += (this.href.lastIndexOf('?') == -1 ? '?' : '&') + appendQs;
		
		if (!this.name && !this.title)
			this.title = title;
	});
	
	tb_init(jQuerySel);
}

function updatePortfolioSection(section) {
	var sel = '#' + section;
	var loc = document.location + (document.location.href.indexOf('?') >= 0 ? '&' : '?') + 'r=' + Math.random();
	jQuery(sel).load(loc + ' ' + sel, null, function() { setupModalLinks(sel); });
}
