$(document).ready(function(){
	
	$("a").focus(function() {
		$(this).blur();
	});
	
	
	$("#subscribe-namn").focus(function () {
		if ($(this).val() == "Ditt namn...") {$(this).val("")}
	});
	$("#subscribe-namn").blur(function () {
		if ($(this).val() == "") {$(this).val("Ditt namn...")}
	});
	$("#subscribe-epost").focus(function () {
		if ($(this).val() == "Din e-postadress...") {$(this).val("")}
	});
	$("#subscribe-epost").blur(function () {
		if ($(this).val() == "") {$(this).val("Din e-postadress...")}
	});
	$("#subscribe-postnr").focus(function () {
		if ($(this).val() == "Ditt postnummer...") {$(this).val("")}
	});
	$("#subscribe-postnr").blur(function () {
		if ($(this).val() == "") {$(this).val("Ditt postnummer...")}
	});
	
	
	$("#search-top-input,#search-input").focus(function () {
		if ($(this).val() == "Sök VVS-produkt här...") {$(this).val("")}
	});
	$("#search-top-input,#search-input").blur(function () {
		if ($(this).val() == "") {$(this).val("Sök VVS-produkt här...")}
	});
	
	$("#inkopslista").change(function(){
		
		if ($("#inkopslista option:selected").val() == '0') {
			$("#inkopslistanamn").css('display','table-row');
		} else {
			$("#inkopslistanamn").css('display','none');
		}
		
	});
	
	$("input[type=submit],input[type=button]").click(function() {
		
		if ($(this).css('opacity') == '0.5') {
			return false; }
		
		$(this).css('opacity','0.5');
		$(this).blur();
	});
	
	initCartBuy();
	
	initCartAjax();
	
	$("#btn-villkor").click(function () {
	
		$.fancybox({
			'autoDimensions': false,
			'padding'		: 10,
			'autoScale'		: true,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: '',
			'href'			: '/jquery/villkor.php',
			'type'			: 'ajax',
			'overlayOpacity': 0.3,
			'width'			: 600,
			'height'		: 500,
			'onComplete'	: function (data) {
				
				$(".price").click(function () { 
					$("#offert-price").val($(this).attr('name'));
					
					$.fancybox.close();
				});
			}
		});

		return false;
	});
	
	
	$(".btn-googlemaps").each(function () {
		
		$(this).click(function () {
		
			$.fancybox({
				'autoDimensions': false,
				'padding'		: 10,
				'autoScale'		: true,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: '',
				'href'			: '/jquery/map/'+ $(this).attr('title') +'.php',
				'type'			: 'ajax',
				'overlayOpacity': 0.3,
				'width'			: 800,
				'height'		: 604,
				'onComplete'	: function (data) {
					
					$(".price").click(function () { 
						$("#offert-price").val($(this).attr('name'));
						
						$.fancybox.close();
					});
				}
			});
	
			return false;
		});
	});
	
	//clearInputs();
	initNav();
	ieHover('.search .btn-search, .subscribe-form .submit','hover');
	
	$("#error-msg-close").click(function () {
		$(".msg.error").hide();
	});
	$("#success-msg-close").click(function () {
		$(".msg.success").hide();
	});
	if ($(".msg.success").is(':visible')) {
		
		$(".msg.success").delay(2000).fadeOut();
	}
});

function initCartBuy() {
	
	$(".form-buy-article").submit(function(event){
		var form = this;
		var data = $(this).serialize();
		var arr = $(this).serializeArray();
		var arid = 0;
		var agid = 0;
		var antal = 0;
		
		$.each(arr,function(k,v){
			if (v.name == 'arid') {
				arid = v.value;
			} else if (v.name == 'agid') {
				agid = v.value;
			} else if (v.name == 'antal') {
				antal = v.value;
			}
		});
		
		var btn = $('input[name=btn-buy-article-' + arid + ']');
		
		$.post(
			"/jquery/cart-change.php?do=addtocart",
			data,
			function(result){
				
				var obj = JSON.parse(result);
				
				$("#topcart").html(obj.smallcart);
				$('.content-articlegrp-' + agid).html(obj.artgrp);
				
				$(btn).css('opacity','1');
				
				var msgbox = $(".msg.success");
				var errorbox = $(".msg.error");
				
				if (obj.error.length > 0) {
					$('.info',errorbox).html(obj.error);
					$(errorbox).show().delay(1500).fadeOut();
				} else if (antal > 0) {
					btn.removeClass();
					btn.addClass("btn-upd-small");
					$('.info',msgbox).html('Artikeln finns nu i din varukorg');
					$(msgbox).show().delay(1000).fadeOut();
				} else {
					btn.removeClass();
					btn.addClass("btn-buy-small");
					$('.info',msgbox).html('Artikeln är nu borttagen från din varukorg');
					$(msgbox).show().delay(1000).fadeOut();
				}
				
				initCartBuy();
			}
		);
		
		return false;
	});
}

function initCartAjax() {
	
	$("#ajax-varukorg #frm-varukorg").submit(function() {
		
		$.post("/jquery/cart-upd.php?do=updatecart",
			$("#ajax-varukorg form").serialize(),
			function(result) {
				
				var obj = JSON.parse(result);
				
				$("#topcart").html(obj.smallcart);
				$("#ajax-varukorg").html(obj.cart);
				
				$(this).css('opacity','1');
				
				initCartAjax();
			}
		);
		
		return false;
	});
	
	$("#ajax-varukorg #btn-varukorg-del").click(function() {
		
		if (confirm($(this).data('onclick'))) {
		
			$.post("/jquery/cart-upd.php?do=deletecart&"+ $(this).attr("name"),
				"",
				function(result) {
					
					var obj = JSON.parse(result);
					
					if (obj.count <= 0) { window.location = "/varukorg/"; 
					
					} else {
						
						$("#topcart").html(obj.smallcart);
						$("#ajax-varukorg").html(obj.cart);
						
						$(this).css('opacity','1');
						
						initCartAjax();
					}
				}
			);
		}
		
		return false;
	});
}
// IE6 hover
function ieHover(h_list, h_class){
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		if(!h_class) var h_class = 'hover';
		jQuery(h_list).mouseenter(function(){
			jQuery(this).addClass(h_class);
		}).mouseleave(function(){
			jQuery(this).removeClass(h_class);
		});
	};
};
// init navigation
function initNav(){
	
	var h = 34;
	var activeClass = 'active';
	var header = $('#header div.container');
	var nav = $("#nav");
	var links = nav.find('li > a > span').parent();
	var drops = links.parent().find('>div.drop');
	
	links.each(function(){
		
		var hold = $(this);
		var drop = hold.parent().find('>div.drop');

		//alert(hold.parent().hasClass(activeClass));
		if (hold.parent().hasClass(activeClass)) {
			if (drop.find('ul > li').length) {

				drops.hide();
				drop.show();
				header.css({paddingBottom:h});
			}
		}
		
		links.click(function() {
			
			return true;
			
			links.parent().removeClass(activeClass);
			
			var hold = $(this);
			var drop = hold.parent().find('>div.drop');
			
			if (drop.find('ul > li').length) {
			
				hold.parent().addClass(activeClass);

				drops.hide();
				drop.show();
				header.css({paddingBottom:h});
				
				return true;
			} else {
				return true; }
		});
	});
}
function initNav2(){
	var h = 34;
	var dur = 250; //ms
	var open = false;
	var activeClass = 'active';
	var header = $('#header div.container');
	var links = header.find('#nav > li > a > span').parent();
	var drops = links.parent().find('>div.drop');
	links.each(function(){
		
		var hold = $(this);
		var drop = hold.parent().find('>div.drop');
		
		if(drop.find('ul > li').length){
			if(hold.parent().hasClass(activeClass)){
				drop.show();
				header.css({paddingBottom:h});
				open = true;
			} else { // FIX! så inte alla visas från början
				hold.parent().removeClass(activeClass);
				drop.hide();
				open = false;
			}
			hold.click(function(){
				
				if(!hold.parent().hasClass(activeClass)){
					if(open){
						links.parent().removeClass(activeClass);
						header.animate({paddingBottom:0},{duration:dur,queue:false,complete:function(){
							hold.parent().addClass(activeClass);
							drops.hide();
							drop.show();
							header.animate({paddingBottom:h},{duration:dur,queue:false,complete:function(){
								open = true;
							}})
						}})
					} else {
						hold.parent().addClass(activeClass);
						drop.show();
						header.stop().animate({paddingBottom:h},{duration:dur,queue:false,complete:function(){
							open = true;
						}})
					}
				} else {
					hold.parent().removeClass(activeClass);
					header.stop().animate({paddingBottom:0},{duration:dur,queue:false,complete:function(){
						drop.hide();
						open = false;
					}})
				}
				return false;
			});
		}
	});
}
// clear inputs
function clearInputs(){
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
}
function clearFormFields(o){
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filter) o.filter = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass)) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}

(function( $ ){
	
	$.fn.ImageShuffle = function( method ) {
		
		var root = $(this);
		var items = $(this).find(".small .item");
		
		$(this).find(".image_content").hide(); //Hide all content
		$(this).find(".image_content:first").show(); //Hide all content
		$(this).find(".small .item:first").addClass("active");
		$(this).find(".small .item").show(); //Hide all content
		
		root.find("ul li.item").click(function() {
		
			if ($(this).hasClass("active")) {
				return false; }
			
			root.find("ul li.item").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			root.find(".image_content").hide(); //Hide all tab content
			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			$(activeTab).fadeIn(); //Fade in the active ID content
			return false;
		});
	};
	
	$.fn.Tabs = function( method ) {
		
		var root = $(this);
		
		root.find(".tab_content").hide(); //Hide all content
		root.find("ul.tab_header li:first").addClass("active").show(); //Activate first tab
		root.find(".tab_content:first").show(); //Show first tab content
		
		//On Click Event
		root.find("ul.tab_header li").click(function() {
			
			if ($(this).hasClass("active")) {
				return false; }
			
			root.find("ul.tab_header li").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			root.find(".tab_content").hide(); //Hide all tab content
			
			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			$(activeTab).fadeIn(); //Fade in the active ID content
			return false;
		});
	};
	
})( jQuery );


// accordion function
function initAccordion() {
	$('ul.accordion').multiAccordion();
	$('ul.accordion > li > div.slide > ul').multiAccordion({
		activeClass:'opened',
		opener:'> a',
		slider:'div.slide'
	});
}

// multilevel accordion plugin
jQuery.fn.multiAccordion = function(_options){
	// default options
	var _options = jQuery.extend({
		activeClass:'active',
		opener:'.opener',
		slider:'>div.slide',
		slideSpeed: 300,
		collapsible:true,
		event:'click'
	},_options);

	return this.each(function(){
							  
							  return true;
		// options
		var _event = _options.event;
		var _accordion = jQuery(this);
		var _items = _accordion.find(':has('+_options.slider+')');
		_items.each(function(){
			var _holder = $(this);
			var _opener = _holder.find(_options.opener);
			var _slider = _holder.find(_options.slider);
			_opener.bind(_event, function(){
				if(!_slider.is(':animated')) {
					if(_holder.hasClass(_options.activeClass)) {
						if(_options.collapsible) {
							_slider.slideUp(_options.slideSpeed, function(){
								_holder.removeClass(_options.activeClass);
							});
						}
					} else {
						var _levelItems = _holder.siblings('.'+_options.activeClass);
						_holder.addClass(_options.activeClass);
						_slider.slideDown(_options.slideSpeed);

						// collapse others
						_levelItems.find(_options.slider).slideUp(_options.slideSpeed, function(){
							_levelItems.removeClass(_options.activeClass);
						})
					}
				}
				return false;
			});
			if(_holder.hasClass(_options.activeClass)) _slider.show(); else _slider.hide();
		});
	});
}

