$(document).ready(function() {

    $("#masthead").cycle();
	
	$("ul.productlist li span.product-link a").bigTarget({
		hoverClass: 'over', // CSS class applied to the click zone onHover
		clickZone : 'li:eq(0)' // jQuery parent selector
	});
  
	$('#klantfoto').cycle({ 
	    fx:     'fade', 
	    speed:  'slow', 
	    timeout: 0, 
	    pager:  '#fotonav',
	 	pagerAnchorBuilder: function(idx, slide) { 
		        // return selector string for existing anchor 
		        return '#fotonav li:eq(' + idx + ') a'; 
		    }
	});
	
	
	// Execute functions
	convertLinks();
	initCategoryNav();
	
	
	$('ul#brandlist').masonry({ singleMode: true });
	
	
	$("#subForm input:submit").click(function() { 

      // First, disable the form from submitting
      $('form#subForm').submit(function() { return false; });

      // Grab form action
      var formAction = $("form#subForm").attr("action");

      // Hacking together id for email field
      // Replace the xxxxx below:
      // If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
      var id = "uljuz";
      var emailId = id + "-" + id;

      // Validate email address with regex
      if (!checkEmail(emailId)) {
        alert("Please enter a valid email address");
        return;
      }

      // Serialize form values to be submitted with POST
      var str = $("form#subForm").serialize();

      // Add form action to end of serialized data
      // CDATA is used to avoid validation errors
      //<![CDATA[
      var serialized = str + "&action=" + formAction;
      // ]]>

      // Submit the form via ajax
      $.ajax({
        url: "proxy.php",
        type: "POST",
        data: serialized,
        success: function(data){
          // Server-side validation
          if (data.search(/invalid/i) != -1) {
            alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
          }
          else
          {
            $("#theForm").hide(); // If successfully submitted hides the form
            $("#confirmation").slideDown("slow");  // Shows "Thanks for subscribing" div
            $("#confirmation").tabIndex = -1;
            $("#confirmation").focus(); // For screen reader accessibility
            // Fire off Google Analytics fake pageview
            //var pageTracker = _gat._getTracker("UA-XXXXX-X");
            //pageTracker._trackPageview("/newsletter_signup");
          }
        }
      });
    });

});

//Convert links to open in new window

var Domains = "www.jutenjuul.nl, jutenjuul.nl";
Domains = Domains.toLowerCase();
var DomainsArray = Domains.split(",");

function convertLinks() {
	var dom_count=0;
	for (var i = 0; i < document.links.length; i++)
	{
		if (typeof document.links[i].hostname=="undefined" || document.links[i].hostname.length < 1)
			continue;
		if (document.links[i].target.length > 0)
			continue;
		if (document.links[i].href.substr(0,11).toLowerCase()=="javascript:")
		{
			continue;
		}
		var h = document.links[i].hostname.toLowerCase();
		var makeNewWindow = true;
		for (var ii = 0; ii < DomainsArray.length; ii++)
		{
			if(DomainsArray[ii] != h)
				continue;
			makeNewWindow = false;
			break;
		}
		if (makeNewWindow == true) {
			document.links[i].target = '_blank';
			dom_count++;
		}
	}
}


function checkEmail(email) { 
    var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var emailVal = $("#" + email).val();
    return pattern.test(emailVal);
  }


function initCategoryNav() {
	$('#category_nav ul').hide();
	$('#category_nav .parent_active').next().show();
	$('#category_nav li a').click(
	function() {
		var checkElement = $(this).next();
		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
		return false;
		}
	if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
		$('#category_nav ul:visible').slideUp('normal');
		checkElement.slideDown('normal');
		return false;
		}
	});
}

