thumbsNo = 0;
currentItem = 1;


function menuIE() {
	if(is_ie6()) {
		$("ul#nav li").mouseenter(function() {
			$(this).children("ul").show();
			$(this).children("ul").css("position", "absolute");
		});
		$("ul#nav li").mouseleave(function() {
			$(this).children("ul").hide();
		});
	}
}

function setCalendarSelectors() {
	$("#select-year").change(function() {
		window.location = "/tournaments/calendar/" + $(this).val();
	});
	$("#select-tense").change(function() {
		filterTenseStateRows($(this).val());
	});	
}
function setFaq() {
	$("div.answer").hide();
	$("p.question").click(function() {
		if($(this).next().css("display") == "block") {
			$(this).next().hide("slow");
			$(this).css("background-image", "url(/themes/site_themes/default/images/global/icon_faq_plus.gif)");
 		} else {
			$(this).next().show("slow");
			$(this).css("background-image", "url(/themes/site_themes/default/images/global/icon_faq_minus.gif)");
 		}
	});	
}
function setLogin() {
	$("#login ul li a").click(function() {
		ref = $(this);
		
		if( $("#login-form").css("display") == "none") {
			$("#login-form").css("display", "block");
			$("#login-form").fadeIn("slow");
			ref.html("");
			ref.parent().addClass("on");
		} else {
			$("#login-form").fadeOut("fast");
			ref.html("Member&nbsp;login");
			ref.parent().removeClass("on");
		}
	return false;
	});
	
	$("#login-form a#login-close").click(function() {
		$("#login ul").show();
		$("#login-form").hide();
		$("div#login ul li a").html("Member&nbsp;login");
		$(this).addClass("first");
		$("div#login ul li").removeClass("on");
		return false;
	});
	
	$("#login-form input#login-submit").click(function() {
		$("#login ul").show();
		$("#login-form").hide();
		$("div#login ul li a").html("Member login");
		$("div#login ul li").removeClass("first");
		$("#login-form").submit();
	});
}

function setVideo(theId) {
	vidhtml = '<div id="vid"><object width="540" height="405"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + theId + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=' + theId + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="540" height="405"></embed></object></div>';
}



function setGallery() {
	
	$("#g-thumbs a").each(function(i) {

		thumbsNo++;
		$(this).addClass("g-item-" + (i+1));
		
		$(this).click(function() {
			selectImage($(this), false);
			return false;
		});
	});
	
	// next button
	$("a.next").click(function() {
	
		if (isNaN(currentItem)) currentItem = 1;
		if (currentItem >= thumbsNo) currentItem = 0;
		currentItem++;
		nextImage = "g-item-" + currentItem;
		nextImageThumbRef = "#g-thumbs .g-item-" + currentItem;
		jNextImageThumbRef = $(nextImageThumbRef);
		jNextImageThumbRef.attr("rel") == "vid" ? isVideoFile = true : isVideoFile = false;
		//console.log( $(nextImageThumbRef).attr("rel") );
		selectImage(nextImage, true, isVideoFile);
		return false;
	});
	
	// previous button
	$("a.prev").click(function() {
		if (isNaN(currentItem)) currentItem = thumbsNo+1;
		if (currentItem == 1) currentItem = thumbsNo+1;
		currentItem--;
		nextImage = "g-item-" + currentItem;
		selectImage(nextImage, true);
		return false;
	});
	
	// set the total number of thumbs
	$(".g-total").html("<strong>" +  thumbsNo + "</strong>");

	// check if image needs to be loaded
	checkToLoadImage();	
	
}

function selectImage(el, isClass, isVideo) {
			
		if ($(el).attr("rel") == "vid") {
			thisIsVideo = true;
		} else {
			thisIsVideo = false;
		}
		
		if (isVideo) thisIsVideo = true;
 	
		if(isClass) {
			imgTarget = el;
		} else {
			imgTarget = el.attr("class");
		}
		
		preItemSplit = imgTarget.split(" ");
		//console.log(preItemSplit[0]);
		currentItemSplit = preItemSplit[0].split("-");
		currentItem = currentItemSplit[2];
		
		//console.log(imgTarget);
		
		if(!thisIsVideo) {
			// show the right image
			$("#g-img img").each(function() {
				theImg = $(this);
				//console.log(preItemSplit[0]);
				$("#g-img div").hide();
				if (!theImg.hasClass(preItemSplit[0])) {
					theImg.hide();			
				} else {
					theImg.fadeIn("slow");
					//theImg.css("filter", "progid:DXImageTransform.Microsoft.Shadow(color='#969696', Direction=145, Strength=3);");
				}
				
			});
		} else {
			$("#g-img div").each(function() {
				theDiv = $(this);
				//console.log(preItemSplit[0]);
				$("#g-img img").hide();
				if (!theDiv.hasClass(preItemSplit[0])) {
					theDiv.hide();			
				} else {
					theDiv.fadeIn("slow");
				}
				
			});
		}
		
		// show the right caption
		$("#g-img p.caption").each(function() {
			theP = $(this);
			
			if (!theP.hasClass(preItemSplit[0])) {
				theP.hide();
			} else {
				theP.fadeIn("slow");	
			}
		
		});	
		
		// put class on
		$("#g-thumbs a").each(function() {
			theP = $(this);
			if (!theP.hasClass(preItemSplit[0])) {
				theP.removeClass("on");
			} else {
				theP.addClass("on");
			}
		});
		
		loadShareLink();
		$(".g-current").html("<strong>" +  currentItem + "</strong>");

}


function checkToLoadImage() {
	
	// check if image to be loaded from URL	
	var imgToLoad = getUrlVars()["item"];
	if(imgToLoad == undefined || imgToLoad == "") imgToLoad = "1";
	var imageToLoadOnOpen = ".g-item-1";
	var selectedThumb = "#g-thumbs a.g-item-" + imgToLoad;
	// if param found show that img, else show first
	if(imgToLoad > 0) {
		var imageToLoadOnOpen = "#g-img .g-item-" + imgToLoad;
		var pToLoadOnOpen = "p.g-item-" + imgToLoad;
		$(imageToLoadOnOpen).fadeIn("slow");
		//$(imageToLoadOnOpen).css("filter", "progid:DXImageTransform.Microsoft.Shadow(color='#969696', Direction=145, Strength=3);");
		$(pToLoadOnOpen).fadeIn("slow");
		//$(pToLoadOnOpen).css("filter", "progid:DXImageTransform.Microsoft.Shadow(color='#ffffff', Direction=145, Strength=0);");
	} 
	
	$(imageToLoadOnOpen).fadeIn("slow");
	
	// show on
	$(selectedThumb).addClass("on");
	currentItem = imgToLoad;
	$(".g-current").html("<strong>" +  currentItem + "</strong>");
	loadShareLink();
	
	
}

function loadShareLink() {
	var imgToLoad = getUrlVars()["item"];
	if(imgToLoad == undefined || imgToLoad == "") imgToLoad = currentItem;
	if(imgToLoad > 0) {
		itemToLoad = window.location.href.split("?");
		//$("#g-share input").val(itemToLoad[0] + "?item=" + currentItem);
		
		
		mailLink = '<a href="mailto:?subject=JNJG%20Gallery%20Image%20Link&body=%0DJNJG%20Image%20Gallery%20Link...%0D' + itemToLoad[0] + '?item=' + currentItem + '" title="Email image link">Email image link</a>';		
		$("#g-share").html(mailLink);
		
	}
	$("#g-share").show("fast");
}

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}



/* competition filters */

function addLeaders() {
	var test;
	var leadersArray = new Array();
	$("#score-table tbody tr").each(function(i) {
	 	theTotal = $(this).children(".total").text();
		theTargetID = $(this).attr("id");
		leadersArray[i] = theTargetID + "_" + theTotal;
 	});
	//alert(leadersArray[0]);
	for(j=0; j<leadersArray.length; j++) {
		theTotalRef = leadersArray[j].split("_");
		ref = "#score-table tbody tr#" + theTotalRef[0];
		$(ref).children(".pos").text(theTotalRef[1]);
	}
	$("#score-table").tablesorter({sortList:[[0,0]], widgets: ['zebra']});
	
}

function cleanDates(tableCellClass) {
	$(tableCellClass).each(function(i) {
		// if same day
		if($(this).children('.d-1').text() == $(this).children('.d-2').text() && $(this).children('.m-1').text() == $(this).children('.m-2').text()) {
			$(this).text($(this).children('.d-1').text() + $(this).children('.m-1').text() + $(this).children('.y-2').text());
			//console.log($(this).children('.d-1').text() + $(this).children('.m-1').text() + $(this).children('.y-2').text());
		// if same month only
		} else if($(this).children('.d-1').text() !== $(this).children('.d-2').text() && $(this).children('.m-1').text() == $(this).children('.m-2').text()) {
			$(this).text($(this).children('.d-1').text() + "-" +$(this).children('.d-2').text() + $(this).children('.m-1').text() + $(this).children('.y-2').text());
			//console.log($(this).children('.d-1').text() + "-" +$(this).children('.d-2').text() + $(this).children('.m-1').text() + $(this).children('.y-2').text());
		} 
 	});
	$("#select-year").change(function() {
		window.location = "/tournaments/calendar/" + $(this).val();
	});	
}



function addPlus() {
	$("#score-table tbody tr td.par").each(function(i) {
		ref = $(this).text()*1+0;
		if(ref > 0 ) {
			$(this).text("+" + ref);	
		}	
 	});
	$("#score-table").tablesorter({sortList:[[0,0]], widgets: ['zebra']});
}



function setCompFilters() {
	//addLeaders();
	addPlus();
	
	$("#j-scratch").change(function() {
		testAgainst = $(this).val();
		sortNow(testAgainst);
		$("#score-table").tablesorter({sortList:[[0,0]], widgets: ['zebra']});
	});
	
	$("#j-gender").change(function() {
		testAgainst = $(this).val();
		sortNow(testAgainst);
		$("#score-table").tablesorter({sortList:[[0,0]], widgets: ['zebra']});
	});
	
	$("#j-tee").change(function() {
		testAgainst = $(this).val();
		sortNow(testAgainst);
		$("#score-table").tablesorter({sortList:[[0,0]], widgets: ['zebra']});
	});
	
	
	// set up intial division elements
	$("#j-division option").click(function() {
										  
		alert("Divisions competition filter needs to be re-evaluated after conclusions have been made on the draw process & a naming convention has been decided.");
		return false;
		
		fullDivName = $(this).text();
		
		//get the range
		if(fullDivName !== "All") {
			range = fullDivName.split(" ");
			rangeHiLo = range[1].split("-");
			
			  
			
			$("#score-table tbody tr").each(function(i) {
					if($(this).hasClass("gen-m")) {
							//alert($(this).children(".dvsn").text());
						//$(this).children(".dvsn").text("FFF");
						//$(this).children(".dvsn").text("FFF");
					} 
			});
		}
	$("#score-table").tablesorter({sortList:[[0,0]], widgets: ['zebra']});
	});
	
	$("#j-division").change(function() {
		testAgainst = $(this).text();
		//sortNow(testAgainst);
		$("#score-table").tablesorter({sortList:[[0,0]], widgets: ['zebra']});
	});
	
	
	function sortNow(testAgainst) {
		$("#score-table tbody tr").each(function(i) {
			if(testAgainst !== "all") {
				if($(this).hasClass(testAgainst)) {
					$(this).show();
				} else {
					$(this).hide();
				} 
			} else {
				$(this).show();
			}
			
		});
	}
}

function tableTabs() {
	$("ul.tabs li").each(function(i){
		if(i==0) {
			$(this).children(0).addClass("on");
		}
		$(this).show();							
	});
	
$("ul.tournaments li a").click(function(i){
		//alert($(this).attr("id"));
		//return false;
		$("ul.tournaments li a").each(function(i){
			$(this).removeClass("on");							
		});
		$(this).addClass("on");
		
		//currentGroupFilterState = $(this).attr("id");
		filterTableRows($(this).attr("id"));
		$("#select-tense").val("tour-schedule-all");
		
		return false;
	});	
}

function setTabstoStart() {
	$("ul.tournaments li a").each(function(i){
		$(this).removeClass("on");							
	});	
	$("ul.tournaments li a#tour-schedule-all").addClass("on");
}

var currentTenseFilterState = 'all';
var currentGroupFilterState = 'all';

function filterTenseStateRows(tense) {
	//alert(tense);
	
	$("table#comp-table tr").each(function(i){
		if (tense !== "tour-schedule-all") {
			setTabstoStart();
			$(this).show();
			if(!$(this).hasClass(tense) && $(this).attr("class") !== "header") {
				$(this).hide();
			} else {
				$(this).show();
			}
		} else {
			$(this).show();
		}
 		
	});
	
}


function filterTableRows(which) {
	$("table#comp-table tr").each(function(i){
 		if (which !== "tour-schedule-all") {
			if(!$(this).hasClass(which) && $(this).attr("class") !== "header") {
				$(this).hide();
			} else {
				$(this).show();
			}
		} else {
			$(this).show();
		}
	});
}

//CHECK FOR IE6
function is_ie6(){
	 return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined));
}
 
function tableHomeTabs(callType) {	
		
			
 			
			
			
			$("ul.tabs li").each(function(){
				$(this).show();							
			});
			
			if(callType == "row-jnjg-tour") {
				filterHomeTableRows(callType);
				$("ul.tabs li a#" + callType).addClass("on");	
			}
			
			/*
			if(is_ie6()) {
				alert("isie6");
			}
			*/
			
		
			$("ul.tabs li a").click(function(i){
				
				$("ul.tabs li a").each(function(i){
					$(this).removeClass("on");
				});
				
				$(this).addClass("on");
				filterHomeTableRows($(this).attr("id"));
				return false;
				
			});	
			j = 0;
	
			$("table#home-table tr").each(function(i){

				j++;
				
				if(j%2 == 1) {
					//alert(i%2);
					if(!$(this).hasClass("header")) {
						$(this).addClass("alt");
					};
				} else {
					$(this).removeClass("alt");
					//alert(i%2);
				}
				if(j>6) {
					//$(this).hide();
				}
			});
			
 			
			cleanDates("table#home-table tr td.date");
			
			
  }

function filterHomeTableRows(which) {
 	j = 0;
	
	$("table#home-table tr").each(function(i){
		
		theTR = $(this);
		theTR.show();	
		
		
		
 			if(!theTR.hasClass(which) && $(this).attr("class") !== "header") {
					
				
				
					theTR.hide();
				 
					 
				} else {
					theTR.fadeIn("slow");
					j++;
			}
			//is_ie6() ? alert(j) : boo = boo;
			
			if(j%2 == 1) {
				if(!theTR.hasClass("header")) {
					theTR.addClass("alt");
				};
			} else {
				theTR.removeClass("alt");
			}
			
				if(j>6) {
					theTR.hide();
				}
			

		
	});
 }



function setLinksOnSite() {
	$("span.slug").each(function(i){		
		//console.log($(this).attr("id"));
		slugId = $(this).attr("id");
		slugSplit = slugId.split("-");
		targetID = slugSplit[1];
		targetNavItem = "#nav-sub-" + targetID;
		targetNavItemLink = $(targetNavItem).children().attr("href");
		//console.log(targetNavItem);
		$(this).prev().prev().children('.search-site-link').attr("href",targetNavItemLink);
		
	});
}
