

$.fn.orderpop = function(c) {
	var d = new Date(), currentdate = '';
	currentdate += (d.getFullYear()) + '-';
	
	correct_month = (d.getMonth()<9) ? '0' + (d.getMonth()+1) : (d.getMonth()+1);
	correct_day = (d.getDate()<9) ? '0' + (d.getDate()) : (d.getDate());
	
	currentdate += correct_month + '-';
	currentdate += correct_day;

	c.id			= (c.id===undefined) ? 0 : c.id ;
	c.post			= (c.post===undefined) ? document.location.href : c.post ;
	c.from			= (c.from===undefined) ? 0 : c.from ;
	c.till			= (c.till===undefined) ? 1 : c.till ;
	c.winw 			= ($(document).width()>$(window).width()) ? $(document).width() : $(window).width() ;
	c.winh 			= ($(document).height()>$(window).height()) ? $(document).height() : $(window).height() ;
	
	$('body').append(
		$.create('div', {'id':'orderpop'}, [
			$.create('div', {'id':'orderpop_cnt'}, [
				$.create('form', {'method':'GET','action':c.post}, [
					$.create('input', {'type':'hidden','name':'products_id','value':c.id}, []),
					$.create('input', {'type':'hidden','name':'action','value':'add_product'}, []),
					$.create('label', {}, [ 'Pickup date' ]),
					$.create('input', {'type':'text','class':'text xxsmall','name':'pickupdate','id':'pickupdate','value':currentdate}, []),
					$.create('input', {'type':'submit','class':'btn_buy','value':''}, [])
				])
			]),
			$.create('div', {'id':'orderpop_overlay'}, [])
		])
	);
	
	$("#pickupdate").datepicker({
		altFormat: "yy-mm-dd",
		dateFormat: "yy-mm-dd",
		minDate: c.from+'d',
		maxDate: c.till+'d',
		duration: "fast",
		changeYear: true
	}).focus( function(){
		$('#ui-datepicker-div').css({ 'zIndex':999 });
	});

	$('#orderpop').css({
		'width':c.winw,
		'height':c.winh,
		'position':'absolute',
		'top':'0px',
		'left':'0px',
		'zIndex':995
	});

	$('#orderpop_overlay').css({
		'width':c.winw,
		'height':c.winh,
		'float':'left',
		'position':'absolute',
		'top':'0px',
		'left':'0px',
		'zIndex':996,
		'backgroundColor':'#000000',
		'opacity':0.7
	});

	$('#orderpop_cnt').css({
		'width':250,
		'float':'left',
		'position':'absolute',
		'top':'50%',
		'left':'50%',
		'marginTop':'-50px',
		'marginLeft':'-125px',
		'paddingBottom':'5px',
		'zIndex':997,
		'backgroundColor':'#FFFFFF',
		'border':'#000000 solid 2px'
	});

	$('#orderpop_cnt > form:first').css({
		'width':90,
		'float':'left',
		'marginTop':'5px',
		'marginLeft':'5px'
	}).submit( function(){
		
		var date_from = new Date((d.getMonth() + 1) + "/" + (parseInt(d.getDate()) + parseInt(c.from)) + "/" + (d.getFullYear()));
		var date_till = new Date((d.getMonth() + 1) + "/" + (parseInt(d.getDate()) + parseInt(c.till)) + "/" + (d.getFullYear()));
		var temp = $('#pickupdate').val().split('-');
		var pick_date = new Date( temp[1] + '/' + temp[2] + '/' + temp[0] ); 
		
		if (date_from<=pick_date && date_till>=pick_date) {
			return true;
		} else {
			alert('Incorrect date');
			return false;
		}
	});

};
