// Show the submenu of the selected menu item
$(document).ready(function () {
    mainmenu();
    searchField();
    randomImage();
    checkMobile();
    subscriptionForm();
});

function mainmenu(){
    $('.main-menu > li > ul').hide();
    $('.main-menu li').each(function () {
        var node = this;
        var found = false;
        // search main menu item
        $(this).children("a").each(function() {
            if (this.href == window.location.href) {
                $(node).find("ul:hidden").css("background-color","transparent").slideToggle("slow");
                found = true;
                return false;
            }
            if (window.location.href.indexOf(this.pathname) >= 0) {
                // search submenu-items
                $(this).siblings("ul").find("a").each(function() {
                    if (this.href == window.location.href) {// main menu item
                        $(node).find("ul:hidden").css("background-color","transparent").show();
                        this.style.color = "#888";
                        found = true;
                        return false;
                    }
                });
            }
        });
        if (found) {
            return false;
        }
    });
}

function searchField() {
    var form = $(".search-form").get()[0];
    $(form).find("input[type=submit]").hide(); // hide ugly button
    $(".top-menu .search").show().click(function() {
        form.submit();
    }); 
}

function randomImage() {
    var n = rand(5);
    var lightpbpicture = "light"+n+".jpg";
    var object = $('#top_light');
    if(object.length > 0){
        object[0].style.backgroundImage="url(/images/"+lightpbpicture+")";
    }
    var n = rand(6);
    var lightpbpicture = "ondervlak"+n+".png";
    var object = $('.dark');
    if(object.length > 0){
        object[0].style.backgroundImage="url(/images/"+lightpbpicture+")";
    }
}

function subscriptionForm() {
  $('.subscriptions .subscribe-now').each(function(idx, el) {
  
   $('#new_subscription').dialog({
      autoOpen: false,
      modal: true,
      title: 'Direct aanmelden!',
      buttons: {
        'Aanvragen': function() {
          $(this).submit();
          $(this).dialog('close');
        },
        'Annuleren': function() {
          $(this).dialog("close");
        }
      }
    });
    
    $(el).click(function(e) {
      val = "Gratis";
      
      if($(el).hasClass('budget')) {
        val = "Budget";
      }
      
      if($(el).hasClass('gold')) {
        val = "Gold";
      }
      
      if($(el).hasClass('premium')) {
        val = "Premium";
      }
      
      $('#new_subscription #subscription_type').val(val)
      e.preventDefault();
      $('#new_subscription').dialog('open')
    });
  });
  
  $('.contact-now').each(function(idx, el) {
    $('#dialog').dialog({
      autoOpen: false,
      modal: true,
      title: 'Meer informatie aanvragen',
      buttons: {
        'Versturen': function() {
          $('form', this).submit();
          $(this).dialog('close');
        },
        'Annuleren': function() {
          $(this).dialog("close");
        }
      }
    });
    
    if($('#dialog .form-error').length > 0) {
      $('#dialog').dialog('open');
    }
    
    $(el).click(function(e) {
      e.preventDefault();
      $('#dialog').dialog('open');
    });
  });
}

function rand(n)
{
  return(Math.floor(Math.random()*n+1));
}

/* function dialog(){
	var COOKIE_NAME = 'lightwelkom'; 
	var COOKIE_SETTING = 'welkom';
	var options = { path: '/' };
	
	if($.cookie(COOKIE_NAME) != COOKIE_SETTING){
		$("#dialog").dialog({
   			close: function(event, ui) { 
				$("#dialog").dialog('destroy');
				$.cookie(COOKIE_NAME, COOKIE_SETTING, options); 
			}
		});
	}else{
		$("#dialog").hide();
	}
	
	$("#beurs").click(function(){
		$("#dialog").show();
		$.cookie(COOKIE_NAME, '', options);
		$("#dialog").dialog({
   			close: function(event, ui) { $("#dialog").dialog('destroy');}
		});
	});
	
	$("#dialog-dank").dialog({
		close: function(event, ui) { 
			$.cookie(COOKIE_NAME, COOKIE_SETTING, options); 
			window.location.href = ("/welkom"); 
		}
	});
	
}
 */

function checkMobile(){
    
    // controleer of er een cookie is die
    // controleer of er een post-variabele is
    // indien er een variabele is, onthoud deze met een cookie
}
