	$(document).ready(function() 
	{
		var currentPage = '';
		
		//************ Track Clicks **************//
		
		$('body').click(function(event)
		{	
	
			if ($(event.target).is(".close"))
			{
				event.preventDefault();
			}
			
			if ($(event.target).is(".pagelink a"))
			{
				event.preventDefault();
				var pageId = $(event.target).attr('rel');
				if(pageId != currentPage)
				{
					$('.frame .content').fadeOut('fast', function() {
					    getPage(pageId, true);
					    currentPage = pageId;
					 });
				}
			}
			
			if ($(event.target).is("#confirmCart"))
			{
				$('#cartHolder').hide();
				$('#customerHolder').show();
			}
			
			if ($(event.target).is(".delItem"))
			{
				event.preventDefault();
				var itemId = $(event.target).attr('rel');
				
				$.post("/badrep/connectors/delItem.php", { id: itemId }, function(data)
				{
					if(data == 'success')
					{
					  var row = $('#row'+itemId);
						var price = $(row).find('.price input').val();
						var total = $('#total').val();
						$('#total').val(total - price);
						$('.total').text(total - price+' kr');
						$(row).remove();
						
						var gotItemsLeft = $('.itemrow').html();
						
						if(gotItemsLeft == null)
						{
							$('#cartHolder').hide();
							window.location.reload();
						}
						
						
					}else{
						alert('Något gick snett, vg. försök igen.');
					}
				});
			}
			
			if ($(event.target).is("#showMap, .closeMap"))
			{
				event.preventDefault();
				$('#mapHolder').toggle();
			}
			
      if ($(event.target).is("#signupBtn, #cancelBtn"))
      {
        event.preventDefault();
        $('#login').toggle();
        $('#signupForm').toggle();
        $('#signupForm').validator().reset();
        $('#loginForm').validator().reset();
      }
		});
		
		//************ END Track Clicks **************//
		
		//************ Validate Customer login & Signup **************//
		
		$("#loginForm, #signupForm, #editForm").validator();
		
		//************ Shop Menu Accordion **************//
		
		$("#accordion").tabs("#accordion div.pane", {tabs: 'h2.gotprods', effect: 'slide', initialIndex: null});

    var api = $('#accordion').data('tabs');
    
    api.onClick(function(event,ui)
    {
      var justClickedTabIndex;
      //var changeToPageForm=document.getElementById('changePageForm');
      justClickedTabIndex=ui; // This gets the index of the tab just clicked. Very useful to
      // know in a variety of contexts.
      var gotoPage='';
      //alert(this.getTabs().eq(justClickedTabIndex).attr('rel'));
      //if (justClickedTabIndex=0) { goToPage='somepage.html'; }
      //if (justClickedTabIndex=1) { goToPage='somepage2.html'; }
      // etc.
      //changeToPageForm.action=goToPage;
      //changeToPageForm.submit();

    })

    //************ Overlays for specific pages **************//
    
		$("#menu a[rel]").overlay({
	
			mask: {
				color: '#000000',
				loadSpeed: 200,
				opacity: 0.7,
				closeOnClick: false
			},
			target: '.frame',
			fixed: false,
			closeOnClick: false,
			close: '.close',
			onBeforeLoad: function() {
				$('#tempFrameHolder').html('');
				var id = this.getTrigger().attr('rel');
				currentPage = id;
				getPage(id, false);
				$("#menuClone").show();
			},
			effect: 'apple'
		});
		
		//************ Overlays for specific products **************//
		
		$("a.productLink[rel]").overlay({
	
			mask: {
				color: '#000000',
				loadSpeed: 200,
				opacity: 0.7
			},
			fixed: false,
			closeOnClick: true,
			onBeforeLoad: function() {
	
				var wrap = this.getOverlay().find(".detailsHolder");
				var details = this.getTrigger().next().html();
				
				wrap.html(details);
				$(".imageHover").hover(
				//$("#productFrame").hover(
				
				  function () {
				    $(".detailsHolder .longDescription").slideDown();
				  }, 
				  function () {
				   	$(".detailsHolder .longDescription").slideUp();
				  }
				);
			}
		});
		
		//************ Animation gifs **************//
		
		$("#clown").hover(
		
		  function () {
		    $(this).append($('<img src="img/clown.gif" />'));
		  }, 
		  function () {
		    $(this).find("img:last").remove();
		  }
		);
		
		//************ Product info on hover **************//
		
		$(".slidetabs").tabs("#productsHolder > ul", {
		
			// enable "cross-fading" effect
			effect: 'fade',
			fadeOutSpeed: "slow",
			// start from the beginning after the last tab
			rotate: true
			
			// use the slideshow plugin. It accepts its own configuration
		}).slideshow({clickable: false});
		 
	});
	
	//************ Get page contents for overlay **************//
	
	function getPage(id, fade)
	{
	  var language = 'swe';
	  var currLocation = window.location.toString();

	  if(currLocation.search("english") != '-1'){
	    var language = 'eng';
	  }

		$.post('/connectors/getPage.php', { id: id, lang: language },
		   function(data)
		   {
				$('.frame .content').html(data);
				//$("#kontaktForm").validator();
				$('#kontaktForm').validator().submit(function(e) {
				
					var form = $(this);
					
					// client-side validation passed
					if (!e.isDefaultPrevented()) {
				
						// submit the data to the server with AJAX
						$.getJSON(form.attr("action") + "?" + form.serialize(), function(json) {
				
							// everything is ok. (the server returned true)
							if (json === true)  {
								form.load("/incfiles/success.php");
								
							// server-side validation failed. use invalidate() to show errors
							} else {
								form.data("validator").invalidate(json);
							}
						});
				
						// prevent default form submission logic
						e.preventDefault();
					}
				})
				if(fade)
				{
					$('.frame .content').fadeIn('fast');
				}
		   });
	}
	
	//************ Validator() custom valid - confirm password **************//
	
    $.tools.validator.fn("[data-equals]", "Value not equal with the $1 field", function(input) {
      var name = input.attr("data-equals"),
         field = this.getInputs().filter("[name=" + name + "]"); 
      return input.val() == field.val() ? true : [name]; 
    });
    
  function toggleStatus() {
      if ($('#toggleWholesale').is(':checked')) {
          $('#wholesalePrice').removeAttr('disabled');
      } else {
          $('#wholesalePrice').attr('disabled', true);
      }
      if ($('#toggleWholesaleE').is(':checked')) {
          $('#wholesalePriceE').removeAttr('disabled');
      } else {
          $('#wholesalePriceE').attr('disabled', true);
      }          
  }
