var v1TopNav = {
	defaultSearchFieldValue: "Search sportsnet.ca",
	
	onSearchFocus: function() {
		if ($.trim($("#v1topnavSearchField").val()) == v1TopNav.defaultSearchFieldValue) {
			$("#v1topnavSearchField").val("");
		}
	},
	
	onSearchBlur: function() {
		if ($.trim($("#v1topnavSearchField").val()) == "") {
			$("#v1topnavSearchField").val(v1TopNav.defaultSearchFieldValue);
		}
	},
	
	sponsorExpand: function(_height) {
		var status = "success";
		try {
			$("div#expandoAd").height(_height);
			if ($("div#expandoAd").height() != _height) {
				status = "failure (height: " + $("div#expandoAd").height() + ")";
			}
		} catch(e) {
			status = "failure - height not set";
		}
		return status;
	},
	
	sponsorCollapse: function() {
		var status = "success";
		try {
			$("div#expandoAd").height(47);
			if ($("div#expandoAd").height() != 47) {
				status = "failure (height: " + $("div#expandoAd").height() + ")";
			}
		} catch(e) {
			status = "failure - height not set";
		}
		return status;
	},
	
	categoryExpand: function(_height) {
		var status = "success";
		try {
			$("div.catgory_bar_ad").height(_height);
			if ($("div.catgory_bar_ad").height() != _height) {
				status = "failure (height: " + $("div.catgory_bar_ad").height() + ")";
			}
		} catch(e) {
			status = "failure - height not set";
		}
		return status;
	},
	
	categoryCollapse: function() {
		var status = "success";
		try {
			$("div.catgory_bar_ad").height(47);
			if ($("div.catgory_bar_ad").height() != 47) {
				status = "failure (height: " + $("div.catgory_bar_ad").height() + ")";
			}
		} catch(e) {
			status = "failure - height not set";
		}
		return status;
	},
	
	sliverExpand: function(_height) {
		var status = "success";
		try {
			$("div.hidden_sliver_ad").height(_height);
			if ($("div.hidden_sliver_ad").height() != _height) {
				status = "failure (height: " + $("div.hidden_sliver_ad").height() + ")";
			}
		} catch(e) {
			status = "failure - height not set";
		}
		return status;
	},
	
	sliverCollapse: function() {
		var status = "success";
		try {
			$("div.hidden_sliver_ad").height(47);
			if ($("div.hidden_sliver_ad").height() != 47) {
				status = "failure (height: " + $("div.hidden_sliver_ad").height() + ")";
			}
		} catch(e) {
			status = "failure - height not set";
		}
		return status;
	}
};

var v1Tabs = {
	oldHtml: '', //container for the current content - reverts to this on error
	loadingHtml: '<div class="v1-ajax-loading" style="height:{height}px;"></div>',
	errorMessage: 'Sorry...\nWe\'re experiencing technical difficulties. Please try again later.',
	
	loadContent: function(contentUrl) {
		//valid contentId's are the property names of contentUrls
		v1Tabs.showLoading();
		$.ajax({
			url: contentUrl,
			cache: false,
			success: v1Tabs.onLoad,
			error: v1Tabs.onLoadError
		});
	},
	
	showLoading: function(){
		var curContentDiv = $("div.v1TabsWidget div.contentHolder");
		v1Tabs.oldHtml = curContentDiv.html();
		
		var contentHeight = curContentDiv.height();
		curContentDiv.attr("style", "height:"+contentHeight+"px");
		var curLoadingHtml = v1Tabs.loadingHtml.replace("{height}", contentHeight);
		curContentDiv.html(curLoadingHtml);
	},
	
	onLoad: function(responseHtml){
		v1Tabs.curThumb = -1;
		
		var curContentDiv = $("div.v1TabsWidget div.contentHolder");
		curContentDiv.html(responseHtml);
		curContentDiv.attr("style", "");
	},
	
	onLoadError: function(xhr, status, exception){
		//alert("error; status: " + status + "; exception: " + exception);
		alert(v1Tabs.errorMessage);
		$("div.v1TabsWidget div.contentHolder").html(v1Tabs.oldHtml);
	}
};

var v1TabsBox = function (div) {
	this.$div = div;
	this.$curMenuLists = $(this.$div).find("li.v1TabsMenu");
	
	this.$curMenuLists.css("cursor", "pointer");
	$(this.$curMenuLists).click(function(evt) {
									var curContentDiv = $(this).closest(".v1TabsWidget").find("div.v1TabsHolder");
									var index = $(this).parent().children().index($(this));
									var contentLists = $(this).closest(".v1TabsWidget").find("div.v1TabsContent");
									
									var content = $(contentLists[index]).html();
									$(curContentDiv[0]).html(content.replace("<!-- //", "").replace("// -->", ""));
								});
	
};

var v1ToggleBox = function (div) {
	this.$div = div;
	this.$curMenuLists = $(this.$div).find("li.v1TabsMenu");
	
	this.$curMenuLists.css("cursor", "pointer");
	$(this.$curMenuLists).click(function(evt) {
									var curContentDiv = $(this).closest(".v1TabsWidget").find("div.v1TabsHolder");
									var index = $(this).parent().children().index($(this));
									var contentLists = $(this).closest(".v1TabsWidget").find("div.v1TabsContent");
									
									$(contentLists).each( function(index) { this.style.display = 'none'; } );
									$(contentLists[index]).css('display', 'inline');
								});
	
};

