﻿
function checkSearch() {
	if ($("#headerWrap .quickSearch .txtWatermark").val() == "Type to Search") {
		alert("Please enter a search term.");
		return false;
	}
	if ($("#headerWrap .quickSearch .searchBox").val().length < 2) {
		alert("Please enter a longer search term.");
		return false;
	}
	else {
		return true;
	}
	return false;
}

function checkSearchBottom() {
	if ($("#superBarWrap .quickSearch .txtWatermark").val() == "Type to Search") {
		alert("Please enter a search term.");
		return false;
	}
	if ($("#superBarWrap .quickSearch .searchBox").val().length < 2) {
		alert("Please enter a longer search term.");
		return false;
	}
	else {
		return true;
	}
	return false;
}


$(document).ready(function() {

	//hide sub menus when document is ready
	$('.subMenu').hide();

	//show appropriate subMenu
	$(".rootNav + ul").prev().hover(function() {
		var currMenu = $(this).attr("id");
		$("#" + currMenu).parent().css("z-index", 101);
		$("#" + currMenu + "SubMenu").show();
	}, function() {
		var currMenu = $(this).attr("id");
		$("#" + currMenu).parent().css("z-index", 100);
		$("#" + currMenu + "SubMenu").hide();
	});

	//be sure subMenu remains shown and set active class of parent menu
	$('.subMenu').hover(function() {
		$(this).show();
		$(this).parent().css("z-index", 101);
		$(this).siblings().addClass("active");
	}, function() {
		$(this).hide();
		$(this).parent().css("z-index", 100);
		$(this).siblings().removeClass("active");
	});

	//SuperBar---------------------------------------------------------------
	function hideSuperDrop() {
		if ($(".superDrop").is(":visible")) {
			$(".superDrop:visible").animate({
				height: "0px",
				opacity: 0
			}, 200, "linear", function() { $(".superDrop:visible").hide(); })
		}
		$("body").unbind("click");
	}

	function resetFilter(id) {
		$("#" + id + "Item").html("Select a " + id.substr(0, 1).toUpperCase() + id.substr(1).replace("Filter", ""));
	}

	$(".superDrop").hide()
	.animate({ opacity: 0 });

	$(".barSection:has(div.superDrop)").click(function() {
		if ($(".superDrop").is(":visible")) {
			hideSuperDrop();
		}
		var currItem = $(this).attr("id");
		if ($("#" + currItem + "SubMenu").is(":hidden")) {
			$("#" + currItem + "SubMenu").css("height", "auto");
			var itemHeight = $("#" + currItem + "SubMenu").height();
			$("#" + currItem + "SubMenu").css("height", "0px");
			if ($("#" + currItem + "SubMenu").is(":hidden")) {
				$("#" + currItem + "SubMenu").show()
				.animate({
					height: itemHeight + "px",
					opacity: 1.0
				}, 200, "linear", function() {
					$("body").bind("click", hideSuperDrop);
					$("#" + currItem + "SubMenu").click(function(event) {
						event.stopPropagation();
					});
				});
			}
			$(".superDrop li").click(function() {
				var filter = $(this).parent().attr("id").replace("List", "");
				var text = $(this).text();
				if (text.length > 18) {
					text = text.substr(0, 18) + "...";
				}
				$("#" + filter + "Item").html(text + "<span><img src='/images/misc/16x16/cross.png' alt='' /></span>");
				$("#" + filter + "Item span").click(function(event) {
					event.stopPropagation();
					resetFilter(filter);
				});
				hideSuperDrop();
			});
		}
	}).css("cursor", "pointer");
	//END SuperBar-------------------------------------------------------------


	$("a[rel='ajaxBox']").colorbox({ transition: "none", height: '70%', overlayClose: false });
	$("a.helpLink").colorbox({ width: '509px', height: '70%', overlayClose: false });
	$(".iBox").colorbox({ iframe: true, transition: "none", width: 690, height: 550, overlayClose: false });

	$(".inBox").each(function() {
		$(this).colorbox({ inline: true, scrolling: false, href: $(this).attr('href'), overlayClose: false });
	});

	$("a.thickbox").colorbox({ transition: "none", overlayClose: false });

	editIconInit();

	qTipInit();

});

function qTipInit() {

	$('a.jTip').each(function(i) {
		$(this).qtip({
			content: {
				text: '<img src="/images/misc/loader.gif" alt="Loading..." />',
				url: jQuery(this).attr('rel'), // Use the rel attribute of each element for the url to load
				title: {
					text: jQuery(this).attr('title')
				}
			},
			style: { name: 'dark', tip: true,
				width: { min: 200 }
			},
			hide: { when: 'mouseout', fixed: true },
			position: {
				corner: {
					target: 'topRight',
					tooltip: 'leftTop'
				}
			}
		});
		$(this).attr('title', '');
	});

	$('a.jTipTop').each(function(i) {
		$(this).qtip({
			content: {
				text: '<img src="/images/misc/loader.gif" alt="Loading..." />',
				url: jQuery(this).attr('rel'), // Use the rel attribute of each element for the url to load
				title: {
					text: jQuery(this).attr('title')
				}
			},
			style: { name: 'dark', tip: true,
				width: { min: 200 }
			},
			hide: { when: 'mouseout', fixed: true },
			position: {
				corner: {
					target: 'topMiddle',
					tooltip: 'bottomMiddle'
				}
			}
		});
		$(this).attr('title', '');
	});
}

function ptgrFav(id) {
	var messHolder = $("#ptgrFavWrap");
	if (messHolder != null) {
		$.get("/ui/ptgrFav.aspx?i=" + id, function(data) {
			if (data == "1") {
				$(messHolder).text("Favorite Added!");
			} else {

			}
		});
	} else {

	}
};

function photoFav(photo) {
	var messHolder = $("#photoMess");
	if (messHolder != null) {
		$.get("/ui/photoFav.aspx?i=" + photo, function(data) {
			if (data == "1") {
				showMessage("#photoMess", "Favorite Added!");
			} else {

			}
		});
	} else {

	}
};

function addTags(photoId) {
	var driver = $("#txtDriver").attr("value");
	var team = $("#txtTeam").attr("value");
	var misc = $("#txtMisc").attr("value");
	var shot = $(".lstShot option:selected").attr("value");

	var messHolder = $("#tagMess");
	if (messHolder != null && photoId != null && driver != null && team != null && misc != null && shot != null) {
		$.get("/ui/addTags.aspx?i=" + photoId + "&d=" + driver + "&te=" + team + "&t=" + misc + "&s=" + shot, function(data) {
			if (data == "1") {
				var curPicThumb = $("#" + photoId);
				var relArray = $(curPicThumb).attr("rel").split(",");
				var path = relArray[0];
				var loadExt = relArray[1];
				LoadTags(photoId, loadExt);
				//messHolder.html("Tags Added!");
				//$("#tagMess").fadeIn(500, function() { setTimeout("$('#tagMess').fadeOut(500)", 2000); });
				showMessage("#tagMess", "Tags Added!");

			} else if (data == "-1") {
				showMessage("#tagMess", "You session has expired. Please login again.");
				//messHolder.html("You session has expired. Please login again.");
				//$("#tagMess").fadeIn(500, function() { setTimeout("$('#tagMess').fadeOut(500)", 5000); });
			}
			else {
				showMessage("#tagMess", "We're working on this!");
				//messHolder.html("We're working on this!");
				//$("#tagMess").fadeIn(500, function() { setTimeout("$('#tagMess').fadeOut(500)", 2000); });
			}
		});
	}
}

function delTag(photoId, refId, refType) {
	if (photoId != null && refId != null && refType != null) {
		$.get("/ui/addTags.aspx?i=" + photoId + "&t=" + refId + "&r=" + refType + "&actn=del", function(data) {
			if (data == "1") {
				var curPicThumb = $("#" + photoId);
				var relArray = $(curPicThumb).attr("rel").split(",");
				var path = relArray[0];
				var loadExt = relArray[1];
				LoadTags(photoId, loadExt);
			}
			else {

			}
		});
	}
}


function showMessage(holderId, message) {
	var messHolder = $(holderId);
	if (messHolder != null) {
		messHolder.html(message);
		$(holderId).fadeIn(500, function() { setTimeout("$('" + holderId + "').fadeOut(500)", 2000); });
	}
}


function genColorBoxAjaxPop(el, page, pageEl, title, jumpTo) {
	//usage: onclick="genColorBoxAjaxPop('loadedTerms', '/terms/', '#termsWrap', 'Terms And Conditions'); return false;"
	//el = element id to load content into
	//page = page to load content from
	//pageEl = element on given page to load from
	//title = title of colorbox

	$("#tempPopup").remove();
	$("body").append("<div style='display:none;' id='tempPopup'><div style='color:black;' id='" + el + "'></div></div>");
	$("#" + el).load(page + " " + pageEl);
	$("<a></a>").colorbox({ open: true, width: '50%', height: '70%', href: "#" + el, inline: true, title: title });

	if (jumpTo != null) {
		window.location(jumpTo);
	}
}
