Drupal.behaviors.lloydhotel_randomflower = function (context) {
	var pathname = document.location.pathname;
	var root = '/sites/all/themes/lloydhotel';

	var slugs = pathname.split('/');
	if (slugs[1] == 'en' || slugs[1] == 'nl') {
		var current = slugs[2];
	}
	else {
		var current = slugs[1];
	}

	// root
	if (!current) {
		current = '';
	}
	
	if(!(navigator.userAgent.match(/Blackberry/i))) {
		randomFlower(current, root);
	}
	
	$jq("select").change(function() {
	
			var date = jQuery("#frm_arrival_day").val() + '/' + jQuery("#frm_arrival_month").val() + '/' + jQuery("#frm_arrival_year").val();
		    var arr = Date.parse(date);
		
		    if (!arr) {
		      alert(date + ' is not a valid date!');
		      return false;
		    }
		    
		    var now = new Date();
		    now.add(-1).days();
    
		    if(arr < now) {
		    	
		    	if(arr.getMonth() == now.getMonth()) {
			    	arr.add(1).months();
				} else {
					arr.add(1).years();
				}
			    	    	
		    	var arrday = arr.toString('d');
		      	var arrmon = arr.toString('M');
		      	var arryear = arr.toString('yyyy');
			
		    	setSBValue("#frm_arrival_day", arrday);
			  	setSBValue("#frm_arrival_month", arrmon);
			  	setSBValue("#frm_arrival_year", arryear);
		    }
		
		    var date = jQuery("#frm_departure_day").val() + '/' + jQuery("#frm_departure_month").val() + '/' + jQuery("#frm_departure_year").val();
		    var dep = Date.parse(date);
		
		     if (arr >= dep) {
		      var dep = arr.add(1).days(); // 
		      
		      var depday = dep.toString('d');
		      var depmon = dep.toString('M');
		      var depyear = dep.toString('yyyy');
			
			  //console.log('dep: ', depday, depmon, depyear);
			
			  setSBValue("#frm_departure_day", depday);
			  setSBValue("#frm_departure_month", depmon);
			  setSBValue("#frm_departure_year", depyear);
			  
		    }
		
		update_hidden_values();

	});
	
}

function setSBValue(selectbox_id, value) {
	jQuery(selectbox_id).val(value);
	
	
	$jq(selectbox_id).selectBox('value', value);
	
}


$jq(window).load(function(){
	$jq('select').selectBox();	
	$jq('#slideshow #mask img').each(function() {
		$jq(this).css('background', 'none');
	});

});

/*******************************************************************************
 * Helpers
 */
function update_hidden_values() {
  
  var ad = jQuery('#frm_arrival_day').val();
  var am = jQuery('#frm_arrival_month').val();
  var ay = jQuery('#frm_arrival_year').val();

  var dd = jQuery('#frm_departure_day').val();
  var dm = jQuery('#frm_departure_month').val();
  var dy = jQuery('#frm_departure_year').val();

  jQuery('#FormArrivalDate').val(ad + '/' + am + '/' + ay);
  jQuery('#FormDepartureDate').val(dd + '/' + dm + '/' + dy);
}
/*
function setSBValue(selectbox_id, value) {
	$jq(selectbox_id).val(value);

	var li = $jq(selectbox_id).next().children('ul').children('li')[value-1];
	$jq(li).click();
	$jq('.easy-select-box ul').hide();
}

function maxSBLength(selectbox_id, value) {
	var esb = $jq(selectbox_id).next();
	var count = 1;
	esb.children('ul').children('li').each(function() {
		if(count <= value) {
			$jq(this).show();
		} else {
			$jq(this).hide();
		}

		count++;
	});
}*/

function daysInMonth(month,year) {
	var dd = new Date(year, month, 0);
	return dd.getDate();
}

Drupal.behaviors.lloydhotel_formimput = function (context) {
	jQuery('input#email, input#search_input').focus(function() {
			jQuery(this).val('');
	});

	jQuery('input').blur(function() {
		if(jQuery(this).val() == '') {
			var id = (jQuery(this).attr('id'));

			if(id == 'email') {
				jQuery(this).val('email@email.com');
			} else if(id == 'search_input') {
				jQuery(this).val('search');
			}
		}
	});
}


function randomFlower(current, root) {
	var positions = [];
	var max = Math.max($('#mainContent').height(), 400);

	var b = 'bloem';

	switch(current) {
		case '' :
		case 'home' :
			positions = ['top: 35em; left: 18em;'];
		break;
		case 'culture':
			b = 'blad';
			//positions = ['top: ' + Math.floor(10+Math.random()*((max/10)-20)) + 'em; left: 97em;' ];
			break;
		case 'contact-and-route':
			positions = ['top: ' + Math.floor(10+Math.random()*((max/10)-20)) + 'em; left: 97em;' ];
			break;
		default :
			positions = ['top: ' + Math.floor(10+Math.random()*((max/10)-20)) + 'em; left: 97em;' ];
	}

	//'top: ' + Math.floor(70+Math.random()*((max/10)-70)) + 'em; left: -7em;'
	//'top: ' + Math.floor(70+Math.random()*((max/10)-70)) + 'em; left: -7em;'
	//'top: ' + Math.floor(40+Math.random()*((max/10)-40)) + 'em; left: -7em;'

	var p = positions[0]; //Math.floor(Math.random() * positions.length)];

	var bloem = b + '00' + Math.ceil(Math.random()*10);
	if(bloem == b + '0010') bloem = b + '010';

	if(b != 'blad') {
		jQuery('#container').append('<img id="randomflower" src="'+root+'/assets/css/images/bloemen/'+bloem+'.png" style="position: absolute; '+p+' z-index: 20;"/>');
	
		jQuery("#randomflower").click(function () {
	      jQuery(this).fadeOut();
	    });
    }
}

