function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;



jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 1100
	}, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}


$(function() {
	var offset = $("#scroll-slider").offset();
	var topPadding = 15;
	$(window).scroll(function() {
		if ($(window).scrollTop() > offset.top) {
			$("#scroll-slider").stop().animate({
				marginTop: $(window).scrollTop() - offset.top + topPadding
			});
		} else {
			$("#scroll-slider").stop().animate({
				marginTop: 0
			});
		};
	});
});


$(document).ready(function() {
	
	/*$('ul#innerfade').innerfade({
		speed: 1000,
		timeout: 10000,
		type: 'sequence',
		containerheight: '300px'
	});*/
	
	/*$(".box_lightblue form").hide();
	$(".box_content p, .box_content h4").click(function() {
		if($(".box_lightblue form").is(":visible")) {
			$(".box_lightblue form").slideUp(200);
		} else {
			$(".box_lightblue form").slideDown(200);
		}
	});*/
	
	$(".hab_planer_btn").hab_planer();
	
	var checked = $('#days_all').is(':checked');
	if(checked == true) {
	 	$('.one_day').attr('disabled', true);
	}
	$("a.anchorLink").anchorAnimate();
	$("#days_all").change(function() {					   
		var checked = $('#days_all').is(':checked');						   
		if(checked == true) {
			$('.one_day').attr('disabled', true);
		} else {
			$('.one_day').attr('disabled', false);
		}
	});
});


(function($) {

    $.fn.innerfade = function(options) {
        return this.each(function() {   
            $.innerfade(this, options);
        });
    };

    $.innerfade = function(container, options) {
        var settings = {
        		'animationtype':    'fade',
            'speed':            'normal',
            'type':             'sequence',
            'timeout':          2000,
            'containerheight':  'auto',
            'runningclass':     'innerfade',
            'children':         null
        };
        if (options)
            $.extend(settings, options);
        if (settings.children === null)
            var elements = $(container).children();
        else
            var elements = $(container).children(settings.children);
        if (elements.length > 1) {
            $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
            };
            if (settings.type == "sequence") {
                setTimeout(function() {
                    $.innerfade.next(elements, settings, 1, 0);
                }, settings.timeout);
                $(elements[0]).show();
            } else if (settings.type == "random") {
            		var last = Math.floor ( Math.random () * ( elements.length ) );
                setTimeout(function() {
                    do { 
												current = Math.floor ( Math.random ( ) * ( elements.length ) );
										} while (last == current );             
										$.innerfade.next(elements, settings, current, last);
                }, settings.timeout);
                $(elements[last]).show();
						} else if ( settings.type == 'random_start' ) {
								settings.type = 'sequence';
								var current = Math.floor ( Math.random () * ( elements.length ) );
								setTimeout(function(){
									$.innerfade.next(elements, settings, (current + 1) %  elements.length, current);
								}, settings.timeout);
								$(elements[current]).show();
						}	else {
							alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
						}
				}
    };

    $.innerfade.next = function(elements, settings, current, last) {
        if (settings.animationtype == 'slide') {
            $(elements[last]).slideUp(settings.speed);
            $(elements[current]).slideDown(settings.speed);
        } else if (settings.animationtype == 'fade') {
            $(elements[last]).fadeOut(settings.speed);
            $(elements[current]).fadeIn(settings.speed, function() {
							removeFilter($(this)[0]);
						});
        } else
            alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
        if (settings.type == "sequence") {
            if ((current + 1) < elements.length) {
                current = current + 1;
                last = current - 1;
            } else {
                current = 0;
                last = elements.length - 1;
            }
        } else if (settings.type == "random") {
            last = current;
            while (current == last)
                current = Math.floor(Math.random() * elements.length);
        } else
            alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
        setTimeout((function() {
            $.innerfade.next(elements, settings, current, last);
        }), settings.timeout);
    };

})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}


(function($){
$.fn.extend({
	hab_planer: function(options) {
		var settings = {
			type: 		"image",
			width:		false,
			height:		false,
			closeBtn:	"",
			onClose: function() {
			}, onComplete: function() {
			}
		}
		var options =  $.extend(settings, options);
		
		$("#plan-bg, .plan-close").live("click", function() {
			$("#plan-container, #plan-bg, .plan-close").remove();
			settings.onClose();
			return false;
		});
		
		function ajax_call(href, width, height) {
			
		}
		
		return this.live("click",function() {
			
			var href = $(this).attr("href");
			var rel = ($(this).attr("rel")) ? $(this).attr("rel") : "plan-main-content";
			
			$("body").append('<div id="plan-container"><div style="display: none;" class="plan-close">'+settings.closeBtn+'</div></div><div id="plan-bg"></div><div id="plan-loading"></div>');
			
			if(settings.type == "iframe") {
				settings.width = (settings.width) ? settings.width : 800;
				settings.height = (settings.height) ? settings.height : 600;
				$("#plan-container").append('<iframe width="'+settings.width+'" height="'+settings.height+'" scrolling="auto" frameborder="0" src="'+href+'" hspace="0" name="plan-iframe" id="plan-type"></iframe>');
			
			} else if(settings.type == "image") {
				$("#plan-container").append('<img id="plan-type" src="'+href+'" alt="planriting" />');
			}
			
			$("#plan-container").addClass(rel);
			$("#plan-type").hide();
			$("#plan-type").load(function() {
				$("#plan-type, .plan-close").show();
				$("#plan-loading").remove();
				
				var height = $(window).height();
				var height_img = $("#plan-type").height();
				if(height > height_img) {
					height = height_img;
				} else {
					height = height - 60;
				}
				
				if(!settings.type == "image") { $("#plan-type").css("height",  height+"px").css("width", "auto"); }
				var width = $("#plan-type").width();
				var marginTop = height / 2;
			
				$("#plan-container").css("width",  width+"px").css("marginLeft", "-" + (width/2) + "px");
				return false;
			});
			var height = settings.height / 2;
			$("#plan-container").css("width", settings.width+"px").css("height", settings.height+"px").css("marginLeft", "-" +(settings.width/2)+"px");
			if($(window).scrollTop() != 0) {
				$("#plan-container").css("top", ($(window).scrollTop() + 50)+"px").css("marginTop", "0");
			} else {
				$("#plan-container").css("top", "100px").css("marginTop", "0");
			}
			 if(settings.type == "ajax") {
				
				$.ajax({
					method: "get",url: href, data: "",
					beforeSend: function(){ },
					complete: function(){ $("#plan-loading").remove(); },
					success: function(html) {
					
						$("#plan-container").append('<div id="plan-type" style="width: '+settings.width+'px; height: '+settings.height+'px;">'+html+'</div>');
						settings.onComplete();
					}
				});				
				
				var scrolling = "";
				$("#plan-container").scroll(function(e) {
					scrolling = $(this).scrollTop();
					$(".plan-close").animate({ top: (scrolling + 15)+"px"}, 60);
				});
				return false;
			 }
			
			return false;
		});
	}
});
     
})(jQuery);


$(document).ready(function() {
				 
	$(".hab-planer").hab_planer({ 
		type: "ajax",
		width: 480,
		height: 860,
		onComplete: function() {

			$(".plan-input-submit input").click(function() {
				
				$("#ajax-send-load").show().css("opacity", 0.6);
				
				var dataString = "ajaxpost=1&type=mail";
				$(".myPlanInputs input").each(function() {
					dataString +="&"+$(this).attr("name")+"="+$(this).val();
				});
				
				$.post("process.php", dataString, function(theResponse) {
					
					try {
						var json = jQuery.parseJSON( theResponse );
						
					} catch(e) {
						$("#hab-planer-container").fadeOut(300, function() {
							$("#hab-planer-send").fadeIn(300);
						});
						return false;
					}
					
					if(json.errorMsg) {	
						switch(parseInt(json.errorMsg)) {
							case 2:
								$(".plan-box h1 span").html("Din e-post &auml;r fel!");
							break;
							case 3:
								$(".plan-box h1 span").html("Bara nummer i telefonnumret!");
							break;
						}
						$(".plan-box h1 span").show();
					}
					
					$("#ajax-send-load").hide();
				});
				
				return false;
			});
		}
	});
	
	
	$('.auto-submit-star').rating({
		readOnly: false,
		required: true,
		callback: function(value, link) {
			var parent = $(this).parent();
			var id = parent.attr("id").substring(5);
			
			var dataString = 'submit=1&type=rate&rate='+value+'&parent='+id+'';
			$.post("process.php", dataString, function(theResponse) {
				parent.find("input").rating('disable');
			});
		}
	});
	
	$(".pagein a").click(function() {
		var thisIs = $(this);
		$(".pagein a").removeClass("active");
		var parent = $(this).attr("rel");
		$(".parentIs-"+parent).animate({ opacity: 0.6 }, 100);
		$.post("annonsarkiv.php", $(this).attr("href"), function(theResponse) {
			thisIs.addClass("active");
			$(".parentIs-"+parent).animate({ opacity: 1 }, 100).delay(200).html(theResponse);
		});
		return false;
	});
	
	$(".comment-form .plan-input-submit input").click(function() {
		var parent = $(this).parent().parent();
		var commentID = parent.find(".myparent").val();
		var message = parent.parent().find(".form-message");
		var loading = parent.parent().find(".form-loading");
		var dataString = "content="+encodeURIComponent(parent.find("textarea").val());
		var error = 0;
		
		
		parent.find("input").each(function() {
			if($(this).val() == $(this).attr("title")) {
				error = 1;
			}
			dataString += "&"+$(this).attr("name")+"="+$(this).val();
		});
		
		if(error == 1 || parent.find("textarea").val() == parent.find("textarea").attr("title")) {
			message.html("<span>Du måste fylla i alla fält korrekt!</span>").slideDown(100).delay(3000).slideUp(100);
			return false;
		}
		
		loading.css("opacity", 0.6).show();
		$.post("process.php", dataString, function(theResponse) {				  
			$(".parentIs-"+commentID).prepend(theResponse);
			var count = 0;
			$(".parentIs-"+commentID+" .comment").each(function() {
				count++;
			});
			if(count == 6) {
				$(".parentIs-"+commentID).find(".comment:last-child").remove()
			}
			message.html("Din kommentar har blivit publicerad!").slideDown(100).delay(3000).slideUp(100);
			loading.hide();
			$("input.getvalue, textarea.getvalue").val("").blur();
			
		});
		
		return false;
	});
	
	$(".accord a.header").click(function() {
		var content = $(this).parent().next(".accord-content");
		$(".accord-content").slideUp(300);
		
		if(!content.is(":visible")) {
			content.slideDown(300);
			// AJAX IN HÄR
		}
		return false;
	});
	
	$("input, textarea").focus(function() {
		if($(this).val() == $(this).attr("title")) {
			$(this).val("");
		}
	});
	
	
	$("input, textarea").blur(function() {
		if($(this).val() == "") {
			$(this).val($(this).attr("title"));
		}
	});
	
	
	var animates = "fade";
	var speed = 400;
	var timer = 2000;
	var ClickStopInterval = false;
	var totWidth = 0;
	var positions = new Array();
	$('#animate-content .animate').each(function(i){
		positions[i]= totWidth;
		totWidth += $(this).width();
		if(!$(this).width()) {
			alert("Please, fill in width & height for all your images!");
			return false;
		}
	});
	
	$('#animate-content').width(totWidth);
	$('#menu-animate ul li a').click(function(e,keepScroll) {
		$('li.menuItem').removeClass('act').addClass('inact');
		$(this).parent().addClass('act');
		
		var pos = $(this).parent().prevAll('.menuItem').length;
		if(animates == "fade") {
			$(".animate").fadeOut(speed, function() {
				$('#animate-content').stop().animate({marginLeft:-positions[pos]+'px'},0);
				$(".animate").fadeIn(speed);
			});
		} else if(animates == "slide") {
			$('#animate-content').stop().animate({marginLeft:-positions[pos]+'px'}, speed);
		}
		
		e.preventDefault();
		// Stopping the auto-advance if an icon has been clicked:
		if(!keepScroll && ClickStopInterval == true) { 
			clearInterval(itvl);
		
		}
	});
	
	$('#menu-animate ul li.menuItem:first').addClass('act').siblings().addClass('inact');
	
});
