$(function(){
/* ADMIN-PANEL */
$("#adminToolBoxToggler").toggle(function () {
	$("#adminToolBox").slideDown("slow");
},function () {
	$("#adminToolBox").slideUp("slow");
}); 
$("img.resizeAjax").each(function(){
	var theImage	= $(this);
	var originalSrc = theImage.attr("src");
	theImage.attr({"src" : "http://www.smode-resource.se/gfx/icons/ajax-loader.gif"});
	var newWidth 	= theImage.attr("width");
	var theHeight 	= theImage.height();
	$.ajax({
	  type: "GET",
	  url: "/core/coreSrc/ajax/resize-image.asp",
	  data: "width="+newWidth+"&path="+originalSrc+"&height="+theHeight,
	  contentType: "text/html; charset=iso-8859-1",
	  success : function (data) {
			theImage.attr({"src" : data});
		  }
	})
});
$(".colBoxContainer > .top").click(function () {
	var theContentBox 	= $(this).parent().find(".content");
	var theIcon 		= $(this).find(".icon");
	
	if(theContentBox.is(":visible")){
		theContentBox.slideUp("fast");
		if(theIcon.is(".visible")){
			theIcon.removeClass("visible");
		}else{
			theIcon.addClass("visible");
		}
	}else{
		theContentBox.slideDown("fast");
		if(theIcon.is(".visible")){
			theIcon.removeClass("visible");
		}else{
			theIcon.addClass("visible");
		}
	}
});
$(".moduleBoxContainer > .top").toggle(function () {
	$(this).parent().find(".content").slideUp("slow");
},function () {
	$(this).parent().find(".content").slideDown("slow");
}); 
	captchaField 		= $("#captchaValue");

if(captchaField.is("*")){
	captchaForm			= captchaField.parents("form");
	captchaFormTrigger 	= captchaForm.find(".formSubmitTrigger");
	captchaValidField 	= captchaForm.find("#captcha-is-validated");
	captchaValidField.val(0);
	
	if(editionLCID==1053){
	  captchaFormMessage	= $("<div id='captchaMessage'>Vänligen ange korrekt skräppostkod ovan innan du fortsätter.<br>Klicka på de gröna pilarna för att verifiera koden.</b>");
		}else{
	  captchaFormMessage	= $("<div id='captchaMessage'>Please enter the captcha-code above to continue.<br>To verify your code, press the green arrows.</b>");
	}
	
	captchaForm.submit(function(){return false;});
	captchaField.change(function(){validateCaptchaInput();});
	captchaHideSubmit();
}
$("input#userNewPassword").val("")
$(".autoEmpty").find("input").focus(function () {$(this).val("");});
$(".ajaxFormLoader").removeClass("ajaxFormLoader");
if($("div.imageSwitcher").is("*")){
var switchImages = $("div.imageSwitcher img");   
switchImages.each(function(index){
	var currentImage = $(switchImages[index]);
	currentImage.hide();
	currentImage.click(function(){
		switchImages.hide();
		if(index==(switchImages.size()-1)){
			$(switchImages[0]).show();
		}else{
			$(switchImages[index+1]).show();					
		}
	});
});
$(switchImages[0]).show();
}
if($(".lightboxGallery").is("*")){
	var thumbs 			= $(".lightboxGallery > img");
	var overlay 		= $("<div class='lightboxOverlay'></div>");
	var container 		= $("<div class='lightboxContainer'></div>");
	var viewPort		= $("<div class='lightboxViewport'></div>");
	var captionBox		= $("<div class='lightboxCaption'></div>");
	var navRow			= $("<div class='lightboxNavigation'></div>");
	var closeBtn		= $("<span class='closeBtn btn'>×</span>");
	var prevBtn			= $("<span class='prevBtn btn'>«</span>");
	var nextBtn			= $("<span class='nextBtn btn'>»</span>");
	var winH			= $(window).height();
	var winW			= $(window).width();
	var currentImage	= 0;
	
	$("body").append(overlay).append(container);
	container.append(navRow).append(viewPort).append(closeBtn).append(captionBox);
	navRow.append(prevBtn).append(nextBtn);
	container.show().css({"top": Math.round((winH-container.outerHeight())/2) + "px","left": Math.round((winW-container.outerWidth())/2) + "px"}).hide();

	thumbs.each(function(index){
	 $(this).click(function(){overlay.css({"opacity" : 0.8, "height" : $(document).height()+"px"}); overlay.add(container).fadeIn("slow"); loadImage(index);});
	 $(this).attr({"image-index": index});
	});

	function loadImage(imageIndex) {
	  container.addClass("loadingImage");
	  
		var newImage = thumbs.eq(imageIndex);	
		var img = new Image();
			img.onload = function() {
			img.style.display = "none";
		
			var maxWidth = winW-20-100;
			var maxHeight = winH-20-200; 
			if(img.width > maxWidth || img.height > maxHeight){
				var ratio = img.width / img.height;
				if(img.height >= maxHeight) {
					img.height = maxHeight;
					img.width = maxHeight*ratio;
				}else{
					img.width = maxWidth;
					img.height = maxWidth*ratio;
				}
			}
			navRow.css({"width" : img.width+"px", "top" : img.height/3 +"px","opacity" : 0.7});
			container.animate({"width": img.width,"height": img.height,"top": Math.round((winH - img.height - 20)/2) + "px","left": Math.round((winW - img.width - 20)/2) + "px"},
								"normal", function(){
									viewPort.append(img);
									$(img).fadeIn("normal",function(){
										captionBox.html((currentImage+1)+" / "+thumbs.size()+"<br/>"+newImage.attr("alt"));
										container.removeClass("loadingImage");
										container.animate({"height": container.height() + captionBox.height() + 30},"fast", function(){
											captionBox.fadeIn("fast",function(){closeBtn.fadeIn("fast",function(){navRow.fadeIn("fast");});});
										});
									});
			});
			
			
		} 
		img.src = newImage.attr("src");
		thumbs.filter(".selected").removeClass("selected");
		newImage.addClass("selected");
		currentImage = parseInt(thumbs.filter(".selected").attr("image-index"));
		$.scrollTo("0px",800);
	}

	closeBtn.add(overlay).click(function(){
		viewPort.children().fadeOut("normal",function(){viewPort.children().remove();});
		overlay.add(container).add(closeBtn).add(captionBox).fadeOut("normal");
	});

	nextBtn.add(prevBtn).click(function(){
		navRow.hide();
		closeBtn.fadeOut("fast",function(){captionBox.fadeOut("fast");});
		viewPort.children().fadeOut("normal",function(){viewPort.children().remove();});
		if($(this).is(".prevBtn")){
			loadImage(currentImage == 0 ? thumbs.size()-1 : currentImage-1);
		}else{
			loadImage(currentImage == (thumbs.size()-1) ? 0 : currentImage+1);
		}
	});
}
$("form#loginForm").keydown(function (event) {
	if(event.keyCode == 13){
		$(this).parent().find("form")[0].submit();
		$(this).parent().html("Loading...").css({height : "60px", padding : "30px"}).addClass("AJAXloading").css("background-color","#fff");
	}
});
	   
$("#swlLoginA").click(function () {
		$(this).parent().parent().parent().find("form")[0].submit();
		$(this).parent().parent().parent().html("Loading...").css({height : "60px", padding : "30px"}).addClass("AJAXloading").css("background-color","#fff");
});

$("input#remind").click(function(){
	var pswBox = $(this).parent().parent().find("div.loginPassword");
	
	if(editionLCID==1053){
		var remindBtnTxt 	= "Begär lösenord";
		var loginBtnTxt		= "Logga in";
	}else{
		var remindBtnTxt 	= "Request password";
		var loginBtnTxt		= "Login";
	}
	
	if($(this).is(":checked")){
		pswBox.slideUp("fast")
		$("#swlLoginA").html(remindBtnTxt);
	}else{
		pswBox.slideDown("fast")
		$("#swlLoginA").html(loginBtnTxt);
	}
});
ajaxRenewSession();
if($("#mainHoverMenu").is('*')){
	$(".menu.level1").superfish({
		hoverClass:    "hover-menu",
		delay:         500,
		animation:     {height:"show"},
		speed:         "fast",
		autoArrows:    true,
		dropShadows:   true,
		disableHI:     false
	});
}
var systemMessage = $(".system-message-container");
if(systemMessage.is('*')){
	var	messageDelay = Math.floor(systemMessage.find(".delay").text())*1000;
    systemMessage.fadeIn("slow");
	setTimeout('clockSystemMessage2010()',1000);
}
$(".system-message-box > .close").click(function(){systemMessage.fadeOut("slow");});
$(".system-message-box > .pause").click(function(){systemMessage.append("<span class='pause-delay'></span>");});
if($("div.systemMessageBox").is('*')){
   $("div.systemMessageBox").fadeIn("slow");
   $("div.systemMessageBox").animate({opacity: 0.7}, 2500);
}
setTimeout('hideSystemMessage()', 3000);
if($("table#mainDataTable").is('*')){
  if($("table#mainDataTable").tablesorter!=undefined){	
	$("table#mainDataTable").tablesorter({
		 widgets: ['zebra']	
	});
   }
}
$("a.toolActivator").toggle(function () {
	$("div.toolTip",this).show("slow");
},function () {
	$("div.toolTip",this).hide("slow");
}); 
$(".helpIcon").toggle(function(){
		$(this).parent().find(".helpBox").slideDown("fast");
	},function(){
		$(this).parent().find(".helpBox").slideUp("slow");			
});
$(".swlUserPicker > input.inputUserAutoComplete").focus(function () {
	if ($(this).val()=="Sök användare här"){
		$(this).val("");
		$(this).parent().find("input.destinationField").val("0");
	}
});
$(".swlUserPicker > input.inputUserAutoComplete").keyup(function () {
  userInputStr 	= this.value;
  userField 	= $(this);
  destField		= userField.parent().find("input.destinationField").attr("id");
  valueField	= userField.attr("id");	  
  
	if(userInputStr.length>2){
		$.ajax({
		url : "/core/coreSrc/ajax/user-picker.asp",
		data : "userSearch="+userInputStr+"&desinationField="+destField+"&valueField="+valueField,
		error : function(data,string) {alert(string);},
		success : function (data) {
			userField.parent().find("div.autoCompleteContainer").slideDown("fast").animate({opacity: "0.8"}, "fast").html(data);
			}		
		});
	}
});
$(".swlUserPicker > input.inputUserAutoComplete").change(function () {
	if ($(this).val().length==0){
		$(this).val("Ingen användare vald");
		$(this).parent().find("input.destinationField").val("0");
	}
	$("div#autoComplete"+$(this).parent().find("input.destinationField").attr("id")).slideUp("slow");		
});

});
