var input_name = '';
jQuery(document).ready(function(){
	jQuery('#top-links li').mouseover(function(){
		jQuery('#top-links li').css('background', '#efefef');
		jQuery(this).css('background', '#fff');
		jQuery('.boxes').hide();
		jQuery('#box'+jQuery(this).attr('rel')).show();
	});

	jQuery('#green-wrapper, #content-wrapper, #content-box, #header-wrapper, #man').mouseover(function() {
		jQuery('.boxes').hide();
		jQuery('#top-links li').css('background', '#efefef');
	});

	/* form fields */
	jQuery('input[type=text]').click(function(){
		input_name = jQuery(this).val();
		jQuery(this).val('');
	});

	jQuery('input[type=text]').blur(function(){
		if (jQuery(this).val() == '') {
			jQuery(this).val(input_name);
		}
	});
	
	/* credocomodel */
	jQuery('div.model a').mouseover(function(){
		var e = jQuery(this).children('img').get(0);
		e.src = e.src.replace('_n.','_o.');
	});
	jQuery('div.model a').mouseout(function(){
		var e = jQuery(this).children('img').get(0);
		e.src = e.src.replace('_o.','_n.');
	});
	
	
	
      
    // First, disable the form from submitting
    $('form.newsletterform').submit(function(e) { 
      e.preventDefault();
      var t = $(e.target);
      // Grab form action
      var formAction = t.attr("action");
      
      // Hacking together id for email field
      var id = "ckkutd";
      var emailId = id + "-" + id;
      
      // Validate email address with regex
      var f = t.children('.'+emailId).get(0);
      if (!checkEmail(f)) {
        alert("Indtast en gyldig e-mail adresse: ");
        return;
      }
      
      // Serialize form values to be submitted with POST
      var str = t.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: "/connectors/cm/proxy.php",
        type: "POST",
        data: serialized,
        success: function(data){
          // Server-side validation
          if (data.search(/invalid/i) != -1) {
            alert('Indtast en gyldig e-mail adresse');
          }
          else
          {
            $(".newsletterform").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 virtual pageview
            _gaq.push(['_trackPageview','/newsletter_signup']);
          }
        }
      });
      
     
    });
    
    
	// Load theme
	Galleria.loadTheme('/assets/js/themes/dots/galleria.dots.js?'+Math.floor(Math.random()*999999999999999));
	jQuery('#green-wrapper').galleria();
	jQuery('#images-wrapper').galleria();
	
	
})


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