﻿function addToQs(name, val) {
	if (val != "") {
		return name + "=" + encodeURIComponent(val) + "&";
	}
	return "";
}

function AddToBasket(clickObj, nodeId) {
	var url = "/BasketHttpHandler.ashx?method=AddToBasket&nodeId=" + nodeId + "&CultureCodeId=" + umbCultureCodeId;
	$.ajax({
		type: "GET",
		url: url,
		dataType: "json",
		success: function(data) {
			if (data.ActionPerformed) {
				$("#addeditemcontainer").text(data.Message);
			}
			else {
				$("#addeditemcontainer").text(data.Message);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(textStatus);
			alert(errorThrown);
		},
		beforeSend: function(XMLHttpRequest) {
			$("#addeditemcontainer").show();
			$(clickObj).effect("transfer", { to: "#addeditemcontainer" }, 1200);
			$("#addeditemcontainer").effect("pulsate", { times: 1 }, 600).text(umbPleaseWait);
		}

	});
}

function AddToCard() {
	$.ajax({
		type: "GET",
		url: "/BasketHttpHandler.ashx?method=AddToBasket&nodeId=" + umbNodeId,
		dataType: "json",
		success: function(data) {
			if (data.ActionPerformed) {
				$("#addeditemcontainer").text(umbDicHasBeenAdded);
			}
			else {
				var allReadyAdded = false;
				$.each(data.Container.Items, function(i, item) {
					if (umbNodeId == item.NodeId) {
						allReadyAdded = true;
					}
				});
				if (allReadyAdded) {
					$("#addeditemcontainer").text(umbDicCouldNotBeAddedExist);
				}
				else {
					$("#addeditemcontainer").text(umbDicCouldNotBeAdded);
				}
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(textStatus);
			alert(errorThrown);
		},
		beforeSend: function(XMLHttpRequest) {
			$("#addeditemcontainer").show();
			$("#addbutton").effect("transfer", { to: "#addeditemcontainer" }, 1200);
			$("#addeditemcontainer").effect("pulsate", { times: 1 }, 600).text(umbDicAddingToBasket);
		}

	});


	//$("#addeditemcontainer").show().text("Tilføjer: " + umbNodeName);
	//$("#content").effect("transfer", { to: "#addeditemcontainer" }, 800);
	//$("#addeditemcontainer").effect("pulsate", { times: 3 }, 1200);
}

$(function() {
	$.fn.nyroModal.settings.padding = 25;

});

$.fn.removeDefaultInputValue = function() {
	$(this).bind("focus", function(event) {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	}),
		$(this).bind("blur", function(event) {
			if ($(this).val() == '') {
				$(this).val($(this).attr('title'));
			}
		});
	var obj = $(this);
	$(this).parents('form').bind("submit", function(event) {
		if (obj.val() == obj.attr('title')) {
			obj.val('');
		}
	});
	return this;
};

function GalleryFullscreen() {
	var currentImageIndex = $('#counterCurrentIndex').text();
	$("#gallery" + currentImageIndex).click();
}


function equalHeightSnippet(group) {
	tallest = 0;
	$(window).load(function() {
	group.each(function() {
		thisHeight = $(this).height();
		if (thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
	});
}



$(document).ready(function() {

	$(".removeDefaultInputValue").removeDefaultInputValue();

	/* START global */
	$("img").pngfix();


	/*  Gallery   */
	$('#galleryforwardlink').click(function() {
		if ($('#imglist li:last').is(':visible')) {
			$('#imglist li:last').hide();
			$('#imglist li:first').show();
			$('#counterCurrentIndex').text('1');
		}
		else {
			$('#imglist li:visible').hide().next('li').show();
			$('#counterCurrentIndex').text(parseInt($('#counterCurrentIndex').text()) + 1);
		};
		return false;
	});

	$('#gallerybacklink').click(function() {
		if ($('#imglist li:first').is(':visible')) {
			$('#imglist li:first').hide();
			$('#imglist li:last').show();
			$('#counterCurrentIndex').text($('#imglist li').size());
		}
		else {
			$('#imglist li:visible').hide().prev('li').show();
			$('#counterCurrentIndex').text(parseInt($('#counterCurrentIndex').text()) - 1);
		};
		return false;
	});

	$('#button_download').click(function() {
		$('.reference').hide();
		$('#download').show();
	});

	$('#button_globe').click(function() {
		$('.reference').hide();
		$('#global').show();
	});

	$('#button_contact').click(function() {
		$('.reference').hide();
		$('#contact').show();
	});

	$('#button_add').click(function() {
		/*$('.reference').hide();*/
		AddToCard();
	});

	if (typeof (umbNodeTypeAlias) != "undefined") {
		var containers = $('.contentcontainer');
		if (containers.size() > 0) {
			if (umbNodeTypeAlias == "Project" || umbNodeTypeAlias == "Textpage") {
				$('#showall').show();
			}
			else {
				$('#hideall').show();
			}
		}
	}

	$('#openclosebottom').css('display', 'none');
	$('#closeopenbutton').css('display', 'block');


	$('#quote p').hide();
	$('#quote .opacity1').show();
	$('#quote').css('background', 'url(/Resources/Images/icon_citatClosed.gif) no-repeat 12px 6px');
	$('#quote').css('width', '60px');
	$('#quote').css('height', '36px');
	$('#quote').css('padding', '0');


	$('#quote').click(function(event) {
		if (event.ctrlKey) {
			$("#quote").draggable({
				cursor: "move",
				stop: function() {
					var msg = "top: " + $("#quote").css("top");
					msg += "\n" + "left: " + $("#quote").css("left");
					$("#quote").draggable("destroy");
					alert(msg);

				}
			});
			return;
		}
		if ($('#quote p').is(':visible')) {
			$('#quote p').hide();
			$('#quote .opacity1').show();
			$('#quote').css('background', 'url(/Resources/Images/icon_citatClosed.gif) no-repeat 12px 6px');
			$('#quote').css('width', '60px');
			$('#quote').css('height', '36px');
			$('#quote').css('padding', '0');
		}
		else {
			$('#quote .opacity1').hide();
			$('#quote p').show();
			$('#quote').css('background', 'url(/Resources/Images/icon_citatopen.gif) no-repeat 12px 6px');
			$('#quote').css('width', '174px');
			$('#quote').css('height', 'auto');
			$('#quote').css('padding', '34px 0 0 0');
		}
	})


	$('#hidebottom').click(function() {
		$('#openclosebottom').toggle();
		$('#closeopenbottom').toggle();
		return false;
	});

	$('#showbottom').click(function() {
		$('#closeopenbottom').toggle();
		$('#openclosebottom').toggle();
		return false;
	});

	$('#searchInputFooter').focus(function() {
		$('#searchInputFooter').css('width', '190px');
		$('#searchInputFooter').addClass("searchwordjq")
		$('#searchsubmit').css('width', '24px');
		$('#searchsubmit').addClass("searchsubmitjq")
		$('#searchfield').addClass("shearchfieldborder");
	});

	//$(document).pngFix();

	$('#showall').click(function() {
		$('.closedcontentcontainer').hide(); ;
		$('.contentcontainer').show();
		$('#showall').hide();
		$('#hideall').show();
	});

	$('#hideall').click(function() {
		$('.contentcontainer').hide();
		$('.closedcontentcontainer').show();
		$('#showall').show();
		$('#hideall').hide();
	});

	$('.closedcontentcontainer').click(function() {
		$(this).next('.contentcontainer').show();
		$(this).hide();
		if ($('#showall').is(':visible')) {
			$('#hideall').show();
			$('#showall').hide();
		}
	});

	$('.ie6box h3').click(function() {
		$(this).parent().parent().parent('.contentcontainer').hide();
		$(this).parent().parent().parent('.contentcontainer').prev('.closedcontentcontainer').show();
		if ($('#hideall').is(':visible')) {
			$('#hideall').hide();
			$('#showall').show();
		}
	});


	$('.close').click(function() {
		$(this).parent().parent('.contentcontainer').hide();
		$(this).parent().parent('.contentcontainer').prev('.closedcontentcontainer').show();
	});

	/*SLUT global*/
});