/* DESIGNATURE 2010 */
/* Barløseborg Golfklub */
/* This file contains all jquery function used on the sites. Whenever plugins are used, see plugin documentation for more information */

$(document).ready(function(){
						   
	/* DOCUMENT INITIALIZING */
	if($.browser.msie){
		if($.browser.version.substr(0,1) < "8"){
			$(".notice-top").slideDown('normal');
		}
	}
	// Initializing jQuery cycle
	if($("#content-ads1").length)
	{
		$("#content-ads1").cycle({
			fx: 'scrollVert', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			timeout: 8000,
			speed: 1000
		});
		$("#content-ads2").cycle({
			fx: 'scrollVert', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			timeout: 8000,
			speed: 1000,
			delay:2000
		});
		// Fixing jQuery cycle links
		$("#content-ads1 img").click(function(){
			if($(this).attr("class")=="target_blank")
			{
				window.open($(this).attr("rel"));
				return false;
			}
			else
				document.location.href = $(this).attr("rel");
		}).css({cursor:"pointer"});
		
		$("#content-ads2 img").click(function(){
			if($(this).attr("class")=="target_blank")
			{
				window.open($(this).attr("rel"));
				return false;
			}
			else
				document.location.href = $(this).attr("rel");
		}).css({cursor:"pointer"});
	}
	// Initalizing fancybox
	$("#db-content img").mouseover(function(){
		if($(this).attr("alt")!="done"){
			$(this).wrap("<a href=\""+$(this).attr("src").substr(0,($(this).attr("src").length-10))+"\" class=\"single_image\"></a>");
			$(this).attr({"alt":"done"});
			$("a.single_image").fancybox({"hideOnContentClick":true,centerOnScroll:true,overlayOpacity:0.6,titleShow:false,transitionIn:'fade',transitionOut:'fade',speedIn:500,speedOut:500});
		}
	});
	$("a.single_image").fancybox({"hideOnContentClick":true,centerOnScroll:true,overlayOpacity:0.6,titleShow:false,transitionIn:'fade',transitionOut:'fade',speedIn:500,speedOut:500});
	
	// Rounding various elements
	if($("#nav2").length != 0){
		if($("#nav3").length != 0){
			$("#nav3").corner("bottom 5px");
		}else{
			$("#nav2").corner("bottom 5px");
		}
	}
	$("#wrapper").corner("5px");
	$("#login-wrap").corner("5px");
	$("#login").corner("5px");
	$("#content-ads1").corner("5px");
	$("#content-ads2").corner("5px");
	$("#content").corner("5px");
	$(".sidebar").corner("5px");
	$(".nav1-list").corner("4px");
	$(".nav2-list").corner("3px");
	$(".nav3-list").corner("3px");
	$("#calendar").corner("5px");
	$("#calendar-large-topnav").corner("top 5px");
	$("#calendar-large-bg").corner("top 5px");
	$("#footer-wrap").corner("5px");
	$("#calendar-small-topnav").corner("top 4px");
	
	// Link fix. links with "target_blank" opens in "_blank"
	$(".target_blank").attr({"target":"_blank"});
	
	if($("#sponsor-rotator").length){
		$.ajax({
			url: "includes/sponsor_rotator.asp", // send the url through with parameters from the form.
			cache: false, // don't cache, in case same message sent more times
			success: function(html){ // When the form is sucessfully submitted, return function
				$("#sponsor-rotator").html(html);
				$("#sponsor-rotator").fadeIn('slow');
				$(".target_blank").attr({"target":"_blank"});
				sponsorTime = setTimeout(sponsorRotate,2000);
			}
		});
	}
	sponsorRotate = function(){
		$("#sponsor-rotator").fadeOut('slow',function(){
			$.ajax({
				url: "includes/sponsor_rotator.asp?last=" + $("#rotator-last").val(), // send the url through with parameters from the form.
				cache: false, // don't cache, in case same message sent more times
				success: function(html){ // When the form is sucessfully submitted, return function
					$("#sponsor-rotator").html(html);
					$("#sponsor-rotator").fadeIn('slow');
					$(".target_blank").attr({"target":"_blank"});
					sponsorTime = setTimeout(sponsorRotate,2000);
				}
			});
		});
	}
	
	// Top advertising rotater
	/*
	if($("#content-ads1").length && $("#advert1Block").val()!="1"){
		advert1Time = setTimeout("topLeftRotate()",8000);
	}
	
	topLeftRotate = function(){
		$("#content-ads1").fadeOut('slow',function(){
			$.ajax({
				url: "includes/ads_1.asp?adid=" + $("#advert1ID").val(),
				cache: false,
				success: function(html){
					$("#content-ads1").html(html);
					$("#content-ads1").fadeIn('slow');
					advert1Time = setTimeout("topLeftRotate()",8000);
				}
			});
		});
	}
	if($("#content-ads2").length && $("#advert2Block").val()!="1"){
		advert2Time = setTimeout("topRightRotate()",10000);
	}
	
	topRightRotate = function(){
		$("#content-ads2").fadeOut('slow',function(){
			$.ajax({
				url: "includes/ads_2.asp?adid=" + $("#advert2ID").val(),
				cache: false,
				success: function(html){
					$("#content-ads2").html(html);
					$("#content-ads2").fadeIn('slow');
					advert2Time = setTimeout("topRightRotate()",8000);
				}
			});
		});
	}
	*/
	/* ADMIN */
	
	$("#check-match").click(function(){
		if($(this).is(":checked")){
			$("#match-settings").stop(true,true).slideDown('slow');
		}else{
			$("#match-settings").stop(true,true).slideUp('slow');
		}
	});
	
	$("#check-allday").click(function(){
		if($(this).is(":checked")){
			$("#not-allday").stop(true,true).slideUp('normal');
		}else{
			$("#not-allday").stop(true,true).slideDown('normal');
		}
	});
	
	$(".radio-recur").click(function(){
		if($(this).attr("value") != "0"){
			$("#recur-for-div").stop(true,true).slideDown('normal');
			if($(this).val() == "1"){
				$("#recur-for").text("dag(e)");
			}else if($(this).val() == "2"){
				$("#recur-for").text("uge(r)");
			}else if($(this).val() == "3"){
				$("#recur-for").text("måned(er)");
			}
		}else{
			$("#recur-for-div").stop(true,true).slideUp('normal');
		}
	});
	
	if($("#date-pick-txt").length){
		$("#date-pick-txt").datePicker({createButton:false,renderCallback:function($td, thisDate, month, year){
			if (thisDate.isWeekend()){
				$td.addClass('weekend');
				$td.addClass('enabled');
			}
		}});
		$("#date-pick-txt").bind('click',function(){
			$(this).dpDisplay();
			$(this).blur();
			return false;
		});
		$("#date-pick-txt").bind('dateSelected',function(e, selectedDate, $td){
			//console.log('You selected ' + selectedDate);
		});
	}
	
	$("#article-add-image").click(function(){
		boxWidth = 500;
		boxHeight = 300;
		scrLeft = ($(window).width()/2)-(boxWidth/2);
		scrTop = ($(window).height()/2)-(boxHeight/2);
		window.open("includes/upload/article_image.asp","indsaet_billede","left="+scrLeft+",top="+scrTop+",width="+boxWidth+",height="+boxHeight+"");
	});
	
	$("#ad-add-image").click(function(){
		boxWidth = 500;
		boxHeight = 300;
		scrLeft = ($(window).width()/2)-(boxWidth/2);
		scrTop = ($(window).height()/2)-(boxHeight/2);
		window.open("includes/upload/ad_image.asp","indsaet_billede","left="+scrLeft+",top="+scrTop+",width="+boxWidth+",height="+boxHeight+"");
	});
	
	$("#sponsor-add-image").click(function(){
		boxWidth = 500;
		boxHeight = 300;
		scrLeft = ($(window).width()/2)-(boxWidth/2);
		scrTop = ($(window).height()/2)-(boxHeight/2);
		window.open("includes/upload/sponsor_image.asp","indsaet_billede","left="+scrLeft+",top="+scrTop+",width="+boxWidth+",height="+boxHeight+"");
	});
	
	$("#article-remove-image").click(function(){
		$("#insert-image").val("");
		$("#insert-org-image").val("");
		$("#article-image").html("");
		$("#article-add-image").html("<img src=\"../admin/images/admin_add.png\" />Tilf&oslash;j billede");
		$(this).css({display:"none"});
	});
	
	$("#ad-remove-image").click(function(){
		$("#insert-image").val("");
		$("#ad-image").html("");
		$("#ad-add-image").html("<img src=\"../admin/images/admin_add.png\" />Tilf&oslash;j billede");
		$(this).css({display:"none"});
	});
	
	$("#sponsor-remove-image").click(function(){
		$("#insert-image").val("");
		$("#sponsor-image").html("");
		$("#sponsor-add-image").html("<img src=\"../admin/images/admin_add.png\" />Tilf&oslash;j billede");
		$(this).css({display:"none"});
	});
	
	$("#submitUploadForm").click(function(){
		$("#upload-form").slideUp('normal',function(){
			$("#uploading").slideDown('normal');
			getUploadProgress();
		});
	});
	getUploadProgress = function(){
		$.ajax({
			url: "getuploadprogress.asp?progID=" + $("#progressID").val(), // send the url through with parameters from the form.
			cache: false, // don't cache, in case same message sent more times
			success: function(html){ // When the form is sucessfully submitted, return function
				if(html!="error"){
					$("#upload-progress").css({width:html});
					if(html!="100"){
						getProg = setTimeout("getUploadProgress",250);
					}
				}else{
					alert("progress error");
				}
			}
		});
	}
	$("#btn-recur").click(function(){
		$("#check-edit-all").slideUp('normal',function(){
			$("#edit-form").slideDown('normal');
		});
		return false;
	});
	
	$(".status-9").click(function(){
		if($(this).val() == "2"){
			$("#status-9-limits").slideDown('normal');
		}else{
			$("#status-9-limits").slideUp('normal');
		}
	});
	$(".status-18").click(function(){
		if($(this).val() == "2"){
			$("#status-18-limits").slideDown('normal');
		}else{
			$("#status-18-limits").slideUp('normal');
		}
	});
	
	if($("#txt-expire").length){
		$("#txt-expire").datePicker({createButton:false,renderCallback:function($td, thisDate, month, year){
			if (thisDate.isWeekend()){
				$td.addClass('weekend');
				$td.addClass('enabled');
			}
		}});
		$("#txt-expire").bind('click',function(){
			$(this).dpDisplay();
			$(this).blur();
			return false;
		});
		$("#txt-expire").bind('dateSelected',function(e, selectedDate, $td){
			//console.log('You selected ' + selectedDate);
		});
	}
	
	$(".adtype").click(function(){
		if($(this).val()=="1"){
			$("#ad-type-2").slideUp('normal');
			$("#ad-type-3").slideUp('normal');
			$("#ad-type-1").slideDown('normal');
		}else if($(this).val()=="2"){
			$("#ad-type-1").slideUp('normal');
			$("#ad-type-3").slideUp('normal');
			$("#ad-type-2").slideDown('normal');
		}else if($(this).val()=="3"){
			$("#ad-type-2").slideUp('normal');
			$("#ad-type-1").slideUp('normal');
			$("#ad-type-3").slideDown('normal');
		}
	});
	
	$(".admin-expand").click(function(){
		if($("#"+$(this).children(".hidden").val()).attr("title") != "ssub"){
			if($("#"+$(this).children(".hidden").val()).attr("class") != "active"){
				$(".active2").slideUp('fast',function(){
					$(this).removeClass("active2");
				});
				$(".active-img-down").attr("src","images/admin_plus.png");
				$(".active-img-down").removeClass("active-img-down");
				$(".active").slideUp('fast',function(){
					$(this).removeClass("active");
				});
				$(".active-img-top").attr("src","images/admin_plus.png");
				$(".active-img-top").removeClass("active-img-top");
				$("#"+$(this).children(".hidden").val()).stop(true,true).slideToggle('normal');
				$(this).children(".expand-img").attr("src","images/admin_minus.png");
				$(this).children(".expand-img").addClass("active-img-top");
				$("#"+$(this).children(".hidden").val()).addClass("active");
			}else{
				$(".active2").slideUp('fast',function(){
					$(this).removeClass("active2");
				});
				$("#"+$(this).children(".hidden").val()).stop(true,true).slideToggle('normal',function(){
					$(this).removeClass("active");
				});
				$(this).children(".expand-img").attr("src","images/admin_plus.png");
				$(".active-img-top").removeClass("active-img-top");
			}
		}else{
			if($("#"+$(this).children(".hidden").val()).attr("class") != "active2"){
				$(".active2").slideUp('fast',function(){
					$(this).removeClass("active2");
				});
				$(".active-img-down").attr("src","images/admin_plus.png");
				$(".active-img-down").removeClass("active-img-down");
				$("#"+$(this).children(".hidden").val()).stop(true,true).slideToggle('normal');
				$("#"+$(this).children(".hidden").val()).addClass("active2");
				$(this).children(".expand-img").attr("src","images/admin_minus.png");
				$(this).children(".expand-img").addClass("active-img-down");
			}else{
				$("#"+$(this).children(".hidden").val()).stop(true,true).slideToggle('normal',function(){
					$("#"+$(this).children(".hidden").val()).removeClass("active2");
				});
				$(this).children(".expand-img").attr("src","images/admin_plus.png");
				$(".active-img-down").removeClass("active-img-down");
			}
		}
	});
	
	$(".admin-sitemap").mouseover(function(){
		$(this).children(".admin-edit").css({"display":"block"});
	});
	$(".admin-sitemap").mouseout(function(){
		$(this).children(".admin-edit").css({"display":"none"});
	});
	
	$(".check-show").click(function(){
		$.thisCheck = $(this);
		if($.thisCheck.attr("id") == "1"){
			answer = confirm("Siden er i øjeblikket vist.\nEr du sikker på at du vil skjule den?");
		}else{
			answer = confirm("Siden er i øjeblikket skjult.\nEr du sikker på at du vil vise den?");
		}
		if(answer){
			$.ajax({
				url: "includes/set_checked.asp?id=" + $.thisCheck.attr("alt"), // send the url through with parameters from the form.
				cache: true, // don't cache, in case same message sent more times
				success: function(html){ // When the form is sucessfully submitted, return function
					if(html != "error"){
						if(html=="1"){
							alert("Siden vises nu på hjemmesiden");
							$.thisCheck.attr("id","1");
							$.thisCheck.parents(".admin-sitemap").removeClass("admin-phidden");
						}else{
							alert("Siden vises nu ikke på hjemmesiden");
							$.thisCheck.attr("id","0");
							$.thisCheck.parents(".admin-sitemap").addClass("admin-phidden");
						}
					}
				}
			});
		}
	});
	
	$("#select-page").change(function(){
		$.ajax({
			url: "includes/change_reference.asp?id=" + $(this).val(), // send the url through with parameters from the form.
			cache: true, // don't cache, in case same message sent more times
			success: function(html){ // When the form is sucessfully submitted, return function
				if(html != "error"){
					alert("Referencen er nu skiftet");
				}else{
					alert("Der forekom en uventet fejl.\nPrøv venligst igen");
				}
			}
		});
	});
	
	$(".user-right-check").click(function(){
		var isChecked;
		if($(this).is(":checked")){
			isChecked=1;
		}else{
			isChecked=0;
		}
		$.ajax({
			url: "includes/change_right.asp?id=" + $(this).val() + "&checked=" + isChecked, // send the url through with parameters from the form.
			cache: true, // don't cache, in case same message sent more times
			success: function(html){ // When the form is sucessfully submitted, return function
				if(html != "error"){
					alert("Ændringen er gemt");
				}else{
					alert("Der forekom en uventet fejl.\nPrøv venligst igen");
				}
			}
		});
	});
	$("#article-text").keyup(function(){
		if($(this).val().length >= 300){
			$("#article-manchet").slideDown('normal');
			$("#manchet-required").val("1");
		}else{
			$("#manchet-required").val("0");
			$("#article-manchet").slideUp('fast');
		}
	});
	$(".add-sub").click(function(){
		var theId = $(this).attr("id");
		var theTitle = prompt("Skriv en titel til siden:");
		if(theTitle.length > 3){
			$.ajax({
					url: "includes/create_sub.asp?id=" + theId + "&title=" + theTitle, // send the url through with parameters from the form.
					cache: true, // don't cache, in case same message sent more times
					success: function(html){ // When the form is sucessfully submitted, return function
						if(html != "error"){
							alert("Undersiden er tilføjet");
							window.location.reload();
						}else{
							alert("Der forekom en uventet fejl.\nPrøv venligst igen");
						}
					}
				});
		}else{
			alert("Du skal skrive en titel til menupunktet");
			$(this).click();
		}
	});
	
	/* CALCULATE SPH */
	
	$("#calculate").click(function(){
		var handi = parseFloat($("#handicap").val().replace(",","."));
		var slope = parseFloat($("#course option:selected").val());
		var rating = parseFloat($("#course option:selected").attr("id"));
		var par = parseFloat($("#course option:selected").attr("class"));
		if($("#handicap").val() != "" && ($("#handicap").val().replace(",",".")) == handi){
			var sph = Math.round(((handi*slope/113)+rating-par));
			$("#sph").html("<hr><br />Spillehandicap: "+sph);
		}else{
			alert("Du skal indtaste dit handicap");
		}
	});
	
	/* COURSE MAP */
	
	$(".showMap").mouseover(function(e){
		var showX,showY
		if($.browser.msie){
			showX = e.pageX-170;
			showY = e.pageY-330;
		}else{
			showX = e.pageX+5;
			showY = e.pageY+5;
		}
		$("#showInfo").html($(this).attr("id"));
		$("#showInfo").css({top:showY,left:showX,display:"block"});
	});
	
	if($("#showInfo").length != null){
		$(document).mousemove(function(e){
			var showX,showY
			if($.browser.msie){
				showX = e.pageX-170;
				showY = e.pageY-330;
			}else{
				showX = e.pageX+5;
				showY = e.pageY+5;
			}
			$("#showInfo").css({top:showY,left:showX});
		});
	}
	$(".showMap").mouseout(function(){
		$("#showInfo").css({display:"none"});
	});
	
	/* SIGNUP FORM */
	$("#signup-1").click(function(){
		var memberType = $(".selectMembership:checked").val();
		$("#membership").val(memberType);
		$("#start").slideUp('slow',function(){
			$("#formular").slideDown('slow');
		});
	});
});
