$(document).ready(function(){

	//PLAYER HOME PAGE
	//array of slideable elements. key = slideable element. value = element that triggers slideable element
	var elements = []
	elements["player-competition-submit-form"] = "player-upload-scores";
	elements["pro-shop-submit-form"] = "player-sell-item";
	elements["player-edit-details-form"] = "player-edit-details";
	elements["edit-item-proshop-form"] = "player-edit-item";
	
	for(x in elements){
		//hide all the slideable elements
		$("#"+x).hide();

		//bind slideable element trigger function to all links needed
		$("#"+elements[x]).click(function(){
			var trigger = $(this).attr("id")
			for(i in elements){
				if(elements[i] == trigger){
						if($(".slidedown").attr("id") == i){
							return
						}
						else {
							$(".slidedown")
								.slideUp("slow")
								.removeClass("slidedown");
						}
					$("#"+i)
						.slideDown("slow")
						.addClass("slidedown");
				}
			}
		})
	}
	
	//this will slide up all elements that are curretnly down via the hide this link in all slideable elements
	$(".player-option-form-hide a").click(function(){									   
		$(this).parent().parent().parent()
			.slideUp("slow")
			.removeClass("slidedown");
	})

	
	
	
	//CLUB HOME PAGE
	//array of slideable elements. key = slideable element. value = element that triggers slideable element
	var elements2 = []
	elements2["club-edit-details-form"] = "club-edit-details";
	elements2["monthly-medal-form"] = "club-upload-monthlymedal";
	elements2["edit-course-description-form"] = "club-edit-cousre-description";
	elements2["edit-logo-form"] = "club-edit-logo";
	elements2["edit-media-form"] = "club-edit-media";
	
	for(y in elements2){
		//hide all the slideable elements
		$("#"+y).hide();

		//bind slideable element trigger function to all links needed
		$("#"+elements2[y]).click(function(){
			var trigger2 = $(this).attr("id")
			for(j in elements2){
				if(elements2[j] == trigger2){
						if($(".slidedown").attr("id") == j){
							return
						}
						else {
							$(".slidedown")
								.slideUp("slow")
								.removeClass("slidedown");
						}
					$("#"+j)
						.slideDown("slow")
						.addClass("slidedown");
				}
			}
		})
	}
	
	//this will slide up all elements that are curretnly down via the hide this link in all slideable elements
	$(".club-option-form-hide a").click(function(){									   
		$(this).parent().parent().parent()
			.slideUp("slow")
			.removeClass("slidedown");
	})
	
	
	//hide or show inputs depending on input selection on #pro-shop-submit-form
	$("#pro-shop-item-playoffer").click(function(){
		$("#proshop-item-name").parent().slideUp("slow")
		$("#proshop-item-rrp").parent().slideUp("slow")
		$("#proshop-item-offer").prev().text("Play Offer:")
	})
	
	$("#pro-shop-item-club, #pro-shop-item-balls, #pro-shop-item-clothing, #pro-shop-item-accessories, #pro-shop-item-others ").click(function(){
		$("#proshop-item-name").parent().slideDown("slow")
		$("#proshop-item-rrp").parent().slideDown("slow")
		$("#proshop-item-offer").prev().text("Item Make:")
	})
	
	
	//hide or show player-partner input on #player-competition-submit-form
	$("#comp-partners-name-li").hide();
	$("#comp-type-pairs").click(function(){
		$("#comp-partners-name-li").slideDown("slow")	
	})
	$("#comp-type-individual").click(function(){
		$("#comp-partners-name-li").slideUp("slow")	
	})
	
	
	//remove an item from the pro-shop
	$(".edit-options-link-remove").click(function(){
		$(this).next().fadeIn("fast");
		$(".are-you-sure-link").click(function(){
			$(this).parent().fadeOut("fast");
		})
	});
	
	//limit item description
	function limitChars(textid, limit, infodiv){
		var text = $('#'+textid).val(); 
		var textlength = text.length;
		if(textlength > limit){
			$('#'+textid).val(text.substr(0,limit));
		return false;
		}
		else {
			$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
			return true;
		}
	}
	
	$(function(){
		$("#proshop-item-desc").keyup(function(){
			limitChars("proshop-item-desc", 100, "charlimitinfo");
		})
	});


	
	
	//BLOGS PAGE
	//show / hide leave comment
	$("#blog-post-leave-comment").hide();
	$("#leave-a-comment-link").click(function(){
		$("#blog-post-leave-comment").slideToggle("slow");
	})

	
	
	//LOGIN
	$("#login-box").hide();
	
	var loginBoxCaller;
	
	$("#player-login-link").click(function(){
		if(loginBoxCaller != 1){
			$("#login-box").slideUp("slow",function(){  $("#login-box h3").text("Player Login") });
			$("#login-box").slideDown("slow");
		}
		else {
			
			$("#login-box").slideToggle("slow");
		}
		loginBoxCaller = 1
	})
	

	$("#club-login-link").click(function(){
		if(loginBoxCaller != 2){
			$("#login-box").slideUp("slow",function(){  $("#login-box h3").text("Club Login") });
			$("#login-box").slideDown("slow");
		}
		else {
			$("#login-box").slideToggle("slow");
		}
		loginBoxCaller = 2
	})
	
	$("#pro-login-link").click(function(){
		if(loginBoxCaller != 3){
			$("#login-box").slideUp("slow",function(){  $("#login-box h3").text("Pro Login") });
			$("#login-box").slideDown("slow");
		}
		else {
			$("#login-box").slideToggle("slow");
		}
		loginBoxCaller = 3
	})
	
	
	
	//FEATURE GALLERY
	//get data via ajax
	function changeMainContent(){
		
	var datarequest = "featuregallery.asp?page=viewfromhere"
	var html = $.ajax({
		url: datarequest,
  		async: false
 	 }).responseText;
	
	//when fades out
	 $("#main-image-data").fadeOut("slow", function(){
		$("#main-image-data").html(html) 
		$("#main-image-info").hide() 
	 })
	 $("#main-image-info").slideUp("fast") 
	 $("#main-image-data").fadeIn("slow",function(){ 
		$("#main-image-info").slideDown("fast") 
	 },1500) 
	 
     setTimeout( function(){ changeMainContent() }, 20000 ) 
	 }

	
	//RIGHT-COLUMN MONTHLY MEDAL TOP TEN
	function monthlyMedalTopTen(division){
		if(division == 09){
			var datarequest = "monthlymedaltopten.asp?results=0-9"
		}
		if(division == 1028){
			var datarequest = "monthlymedaltopten.asp?results=10-28"
		}
		
		var html = $.ajax({
			url: datarequest,
			async: false
		 }).responseText;
		return html
	}
	
	$("#monthly-medal-topten-09").click(function(){					 
		$("#monthly-medal-topten-results").fadeOut("normal",function(){ $("#monthly-medal-topten-results").html(monthlyMedalTopTen(09)) })									
		$("#monthly-medal-topten-results").fadeIn("normal")	
	})
	
	$("#monthly-medal-topten-1028").click(function(){
		$("#monthly-medal-topten-results").fadeOut("normal",function(){ $("#monthly-medal-topten-results").html(monthlyMedalTopTen(1028)) })									
		$("#monthly-medal-topten-results").fadeIn("normal")
	})
	
	
	
	
	
	
	
	//PAGE ON-LOAD
	//feature gallery
	$("#main-image-data").fadeIn("slow")
	changeMainContent() 
	
	//monthly medal top ten
	$("#monthly-medal-topten-results").html(monthlyMedalTopTen(09))
	
	//club profile page images
	$(".side-image").fancybox(); 
	$(".club-image").fancybox(); 
	
	//if validation error on proshop submit form, when item is playoffer
	if( $("#pro-shop-item-playoffer").attr("checked") == true){
		$("#proshop-item-name").parent().hide()
		$("#proshop-item-rrp").parent().hide()
		$("#proshop-item-offer").prev().text("Play Offer:")
	}
	
	
});

		
		
		
		
